Created
December 5, 2014 18:31
-
-
Save jaypeche/13663121cc7bdaa4b508 to your computer and use it in GitHub Desktop.
getopt POSIX for clamav-realtime.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <getopt.h> | |
| /* Define version */ | |
| static const char version[] = "0.0.3"; | |
| /* Flag set by ‘--verbose’. */ | |
| static int verbose_flag; | |
| /* | |
| * Show brief command usage help: | |
| */ | |
| static void print_usage() { | |
| fputs("Usage: [hvVCdlp] [-h | --help] [-v | --verbose] [-V | --version] [-C | --config CONFIG_PATH] [-d | --directory DIRECTORY_TARGET] [-l | --log LOGFILE] [-p | --pid PIDFILE]\n",stderr); | |
| fputs("\t-h (or --help)\t\t\tGives this help display\n",stderr); | |
| fputs("\t-v (or --verbose)\t\tBe verbose\n",stderr); | |
| fputs("\t-V (or --version)\t\tDisplays program version.\n",stderr); | |
| fputs("\t-C (or --config=CONFIGFILE)\tSetup configuration file path\n",stderr); | |
| fputs("\t-d (or --directory=DIRECTORY_TARGET)\tSetup target directory\n",stderr); | |
| fputs("\t-l (or --log=LOGFILE)\t\tSetup log file path\n",stderr); | |
| fputs("\t-p (or --pid=PIDFILE)\t\tSetup PID path\n",stderr); | |
| } | |
| int | |
| main (int argc, char **argv) | |
| { | |
| int c; | |
| while (1) | |
| { | |
| static struct option long_options[] = | |
| { | |
| /* These options set a flag. */ | |
| {"verbose", no_argument, &verbose_flag, 1}, /* verbose flag set */ | |
| {"help", no_argument, 0, 'h'}, | |
| {"version", no_argument, 0, 'V'}, | |
| /* These options don’t set a flag. | |
| We distinguish them by their indices. */ | |
| {"config", required_argument, 0, 'C'}, | |
| {"directory", required_argument, 0, 'd'}, | |
| {"log", required_argument, 0, 'l'}, | |
| {"pid", required_argument, 0, 'p'}, | |
| {0, 0, 0, 0} | |
| }; | |
| /* getopt_long stores the option index here. */ | |
| int option_index = 0; | |
| c = getopt_long (argc, argv, "C:d:l:p:hvV", | |
| long_options, &option_index); | |
| /* Detect the end of the options. */ | |
| if (c == -1) | |
| break; | |
| switch (c) | |
| { | |
| case 0: | |
| /* If this option set a flag, do nothing else now. */ | |
| if (long_options[option_index].flag != 0) | |
| break; | |
| printf ("option %s", long_options[option_index].name); | |
| if (optarg) | |
| printf (" with arg %s", optarg); | |
| printf ("\n"); | |
| break; | |
| case 'C': | |
| printf ("set config from `%s'\n", optarg); | |
| break; | |
| case 'd': | |
| printf ("set directory target to `%s'\n", optarg); | |
| break; | |
| case 'h': | |
| print_usage(); | |
| break; | |
| case 'l': | |
| printf ("set log path to `%s'\n", optarg); | |
| break; | |
| case 'p': | |
| printf ("set pid path to `%s'\n", optarg); | |
| break; | |
| case 'v': | |
| printf ("Be verbose \n"); | |
| int verbose_flag = 1; | |
| if (verbose_flag) | |
| puts ("verbose flag is set"); | |
| break; | |
| case 'V': | |
| fprintf (stderr,"This is clamav-realtime version %s\n",version); | |
| break; | |
| case '?': | |
| /* getopt_long already printed an error message. */ | |
| break; | |
| default: | |
| print_usage(); | |
| abort (); | |
| } | |
| } | |
| /* Instead of reporting ‘--verbose’ | |
| and ‘--brief’ as they are encountered, | |
| we report the final status resulting from them. */ | |
| // if (verbose_flag) | |
| // puts ("verbose flag is set"); | |
| /* Print any remaining command line arguments (not options). */ | |
| if (optind < argc) | |
| { | |
| printf ("non-option ARGV-elements: "); | |
| while (optind < argc) | |
| printf ("%s ", argv[optind++]); | |
| putchar ('\n'); | |
| } | |
| exit (0); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment