diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-30 13:56:13 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-30 13:56:13 +0530 |
commit | 13f18ecfb05fb572b6f30364b570023661977ebb (patch) | |
tree | 153309290bca5ac6b731879c505f98bf8a9d437b /src | |
parent | 01ef152082e4e396b4b5510aab642cf6235bc59d (diff) |
main: use getopt() to parse arguments
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 36 | ||||
-rw-r--r-- | src/msg.c | 12 |
2 files changed, 37 insertions, 11 deletions
@@ -19,9 +19,43 @@ #define _GNU_SOURCE #include <msg.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +msg_t *msg; + +void +usage(char *program) +{ + printf("Usage: %s [-o <output>] [-h] <directory>\n", program); + printf("\t-o <output>: Output directory\n"); + printf("\t-h : Help\n"); + printf("\t<directory>: Working directory\n"); +} int main(int argc, char **argv) { - return run(argc, argv); + int opt; + msg = malloc(sizeof(msg_t)); + + while ((opt = getopt(argc, argv, "o:h")) != -1) { + switch (opt) { + case 'o': + /* msg.output_directory = optarg; */ + break; + case 'h': + default: + usage(argv[0]); + return EXIT_SUCCESS; + } + } + + msg->base_directory = "compromyse.xyz"; + + int r = run(); + + free(msg); + return r; } @@ -36,8 +36,8 @@ #include <template.h> #include <util.h> +extern msg_t *msg; template_t *base_template; -msg_t *msg; void handle_file(const char *path) @@ -108,15 +108,8 @@ handle_file(const char *path) } int -run(int argc, char **argv) +run(void) { - if (argc < 2) { - printf("Usage: %s [directory]\n", argv[0]); - return EXIT_FAILURE; - } - msg = malloc(sizeof(msg_t)); - msg->base_directory = argv[1]; - struct stat sb; if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) { printf("%s does not exist.\n", msg->base_directory); @@ -156,6 +149,5 @@ run(int argc, char **argv) template_delete(base_template); config_delete(config); - free(msg); return EXIT_SUCCESS; } |