# HG changeset patch # User Binki # Date 2009-05-21 23:16:55 # Node ID 2b98f7dd04fcaf6d5ee8e6b9b32ea5bc7479b9be # Parent 0a8f68f3d17fd1806c4841ab410df993b9518455 added getopt to distren client diff --git a/src/client/distren.c b/src/client/distren.c --- a/src/client/distren.c +++ b/src/client/distren.c @@ -31,6 +31,7 @@ #include /* sprintf, printf */ #include /* malloc, free */ +#include /* getopt */ #include /* Happy fun XML time */ #include #include @@ -39,6 +40,11 @@ int main(int argc, char *argv[]) { + char *input; + char *output; + + char curopt; + struct options_common *options; cfg_t *cfg; @@ -52,16 +58,65 @@ int main(int argc, char *argv[]) CFG_END() }; - int i; + input = NULL; + output = NULL; - i = 1; - + while(-1 != (curopt = getopt(argc, argv, "i:o:h"))) + { + if(curopt == ':') + { + fprintf(stderr, "-%c: is missing an argument\n", optopt); + return 1; + } + else if(curopt == '?') + { + fprintf(stderr, "-%c: invalid option specified\n", optopt); + return 1; + } + else if(curopt == 'h') + { + fprintf(stderr, "Usage: %s -i -o \n", argv[0]); + fprintf(stderr, "\t-i\tSpecifies the input file\n\ +\t-o\tSpecifies the output file\n\ +\t-h\tShows this help message\n"); + /* + don't return here because options_init will have an overall + options help page + */ + } + else if(curopt == 'i') + /* + TODO: is strdup good or bad? what about + the idea that all libs should allow plugging different + malloc/free implementations in? + */ + input = strdup(optarg); + else if(curopt == 'o') + output = strdup(optarg); + } + /* + parse the config file after the arguments so we can intercept -h for help + */ if(options_init(argc, argv, &cfg, cfg_opts, "client", &options)) { fprintf(stderr, "error getting configuration\n"); + //return 1; + } + + /* + give this error after the general arguments parsing so that + the general help from options_init can have effect + */ + if(!input + || !output) + { + fprintf(stderr, "Input and output files must be specified\n"); return 1; } + fprintf(stderr, "reading from %s\nwriting to %s\n", input, output); + + // Please find a better way of doing this :( you can't strcat multiple strings.. meh // use something like sprintf