diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-31 16:13:03 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-31 18:45:23 (EDT) |
commit | 06a0b4d1ec8ee10736565eda0c4a5e3c885c73c6 (patch) | |
tree | af5c221c9297449afddb6a5cec4f4a36ec388335 /src | |
parent | 4fd3d0565ed204ea40e361d82fab9eb2bd292940 (diff) | |
download | atsign-06a0b4d1ec8ee10736565eda0c4a5e3c885c73c6.zip atsign-06a0b4d1ec8ee10736565eda0c4a5e3c885c73c6.tar.gz atsign-06a0b4d1ec8ee10736565eda0c4a5e3c885c73c6.tar.bz2 |
main: Handle options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 93 |
1 files changed, 90 insertions, 3 deletions
@@ -24,10 +24,67 @@ #include <string.h> #include <time.h> #include <unistd.h> +#include "config.h" #include "datetime.h" +#if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG +#include <getopt.h> +#endif + +extern const char *PACKAGE_VERSION_GIT; + +#if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG +struct option LONGOPTS_[] = { + { + .name = "help", + .has_arg = 0, + .flag = NULL, + .val = 'h', + }, + { + .name = "version", + .has_arg = 0, + .flag = NULL, + .val = 'V', + }, +}; +#endif + +static void +_print_usage(FILE *stream, const char *program_name) +{ + fprintf(stream, "Usage: %s [date]time\n", program_name); +} + +static void +_print_help(const char *program_name) +{ + _print_usage(stdout, program_name); + puts("Options:"); +#if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG + puts("\t-h, --help Show this help information"); + puts("\t-V, --version Show version information"); +#else + puts("\t-h Show this help information"); + puts("\t-V Show version information"); +#endif +} + +static void +_print_version(void) +{ + printf("@ (atsign) %s%s\n", PACKAGE_VERSION, PACKAGE_VERSION_GIT); + puts("Copyright (C) 2021 P. J. McDermott"); + puts("License GPLv3+: GNU GPL version 3 or later " + "<http://gnu.org/licenses/gpl.html>."); + puts("This is free software: you are free to change and redistribute " + "it."); + puts("There is NO WARRANTY, to the extent permitted by law.\n"); + printf("Please report bugs to <%s>.\n", PACKAGE_BUGREPORT); +} + static char * -_concat_args(int argc, const char *argv[]) +_concat_args(int argc, char * const argv[]) { int buf_l; int i; @@ -60,14 +117,44 @@ _concat_args(int argc, const char *argv[]) } int -main(int argc, const char *argv[]) +main(int argc, char * const argv[]) { + int opt; char *buf; time_t arg; time_t now; time_t dif; - buf = _concat_args(argc - 1, argv + 1); + optind = 1; + opterr = 0; +#if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG + while ((opt = getopt_long(argc, argv, "hV", LONGOPTS_, NULL)) > 0) { +#else + while ((opt = getopt(argc, argv, "hV")) > 0) { +#endif + switch (opt) { + case 'h': + _print_help(argv[0]); + return EXIT_SUCCESS; + case 'V': + _print_version(); + return EXIT_SUCCESS; + default: + _print_usage(stderr, argv[0]); +#if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG + fprintf(stderr, "Try '%s --help' for more " + "information.\n", argv[0]); +#else + fprintf(stderr, "Try '%s -h' for more " + "information.\n", argv[0]); +#endif + return EXIT_FAILURE; + } + } + argc -= optind; + argv += optind; + + buf = _concat_args(argc, argv); if (buf == NULL) { return EXIT_FAILURE; } |