diff --git a/src/common/options.c b/src/common/options.c --- a/src/common/options.c +++ b/src/common/options.c @@ -28,6 +28,7 @@ #include #include #include +#include struct options_common_data { @@ -37,10 +38,46 @@ struct options_common_data int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts) { + char *configfileprefix; char *configfile; size_t i; + char *optstring = "hc:"; + char curopt; + + configfileprefix = NULL; + while((curopt = getopt(argc, argv, optstring)) != -1) + switch(curopt) + { + case 'h': + fprintf(stderr, "libdistren common options\n\ +\n\ +\t-h\tShow this help.\n\ +\t-c \tBasename for configuration files. For instance, if distrencommon.conf is located at /etc/distren/distrencommon.conf, set this option /etc/distren/distren . Default value: %s\n\ +\n", SYSCONFDIR "/distren"); + return 2; + break; + case 'c': + configfileprefix = strdup(optarg); + if(!configfileprefix) + /* return is unnecessary here */ + fprintf(stderr, "OOM\n"); + break; + case ':': + fprintf(stderr, "Option -%c requires an argument\n", optopt); + return 1; + } + /* restore optind for other people who use getopt */ + optind = 1; + + if(!configfileprefix) + configfileprefix = strdup(SYSCONFDIR "/distren"); + if(!configfileprefix) + { + fprintf(stderr, "OOM\n"); + return 1; + } /* For those of you who don't know, the following is an example of concatenation of constant strings by the C compiler. Now, that doesn't mean you can do run-time string concatenation ;-) strdup is because someday configfile will be customizable via argv[] */