From f056d09c2e0e5cd2e4e3028e49dbfc9f3e1a2f30 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 9 Jul 2021 22:30:07 +0100 Subject: [PATCH] Added `--help` option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for naka's current commit: Small curse − 小凶 --- include/map.h | 1 + include/project.h | 14 ++++++++++++++ src/main.c | 16 ++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/map.h b/include/map.h index a795fa9..76c7938 100644 --- a/include/map.h +++ b/include/map.h @@ -38,6 +38,7 @@ typedef union memory_map { } map_t; map_result_t map_fd(int fd, bool write, usize size, off_t offset, map_t *pOUT map); +/// Specify size as 0 to use the file's full size for the map. map_result_t map_file(const char* file, bool write, usize size, off_t offset, map_t *pOUT map); map_result_t map_anon(void* ptr, usize len, map_t *pOUT map); map_result_t map_free(map_t map); diff --git a/include/project.h b/include/project.h index 294af40..e6e48c1 100644 --- a/include/project.h +++ b/include/project.h @@ -24,4 +24,18 @@ /// Compiled time (UTC unix timestamp) #define PROG_COMPILED_TIMESTAMP AS(_PROJECT_COMPILED, u64) + +// Returns from the program + +// Invalid arguments to program (print usage to stderr) +#define PROG_RET_ARGS 1 +// Mapping of argv[1] failed +#define PROG_RET_MAP_NEEDLE_FAILED 2 +// `h` is the number in argv[] of the haystack file +#define PROG_RET_MAP_HAYSTACK_N_FAILED(h) ((h) << 2) +// `h` is the number in argv[] of the haystack file +#define PROG_RET_UNMAP_HAYSTACK_N_FAILED(h) (((h) << 2) | 1) +// Unmapping of argv[1] failed +#define PROG_RET_UNMAP_NEEDLE_FAILED 3 + #endif /* _PROJECT_H */ diff --git a/src/main.c b/src/main.c index 40d4d8f..dc36a00 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ void usage(FILE* out, int argc, char** argv) prog_info(out); fprintf(out, "\nUsage: %s \n", argv[0] ?: PROG_NAME); + fprintf(out, "\nUsage: %s --help\n", argv[0] ?: PROG_NAME); } // err: 0 for normal exit and print to stdout. @@ -43,20 +44,27 @@ noreturn void usage_then_exit(int err, int argc, char** argv) exit(err); } + int main(int argc, char** argv) { TRACE("main start with %d (pn: %s, a1: %s)", argc, argv[0], argv[1] ?: ""); - if(!argv[1] || !argv[2]) usage_then_exit(1, argc, argv); + if(!argv[1]) +inv_args: + usage_then_exit(PROG_RET_ARGS, argc, argv); + else if(strcmp(argv[1], "--help")==0) + usage_then_exit(0, argc, argv); + else if(!argv[2]) + goto inv_args; map_t needle; INFO("Mapping needle file `%s'", argv[1]); - if(!map_handle_err(map_file(argv[1], false, 0, 0, &needle))) return 2; + if(!map_handle_err(map_file(argv[1], false, 0, 0, &needle))) return PROG_RET_MAP_NEEDLE_FAILED; - if(!map_handle_err(map_free(needle))) return 3; + + if(!map_handle_err(map_free(needle))) return PROG_RET_UNMAP_NEEDLE_FAILED; TRACE("main end"); - return 0; }