From 21b0e4322a67e579cb43d52258df10014f3dc324 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sun, 8 Feb 2015 17:56:45 +0000 Subject: cph: allow to overrride yes and no answers using --yes and --no with an argument, we can now replace the default value printed on stdout respectively for a yes or a no. Signed-off-by: Olivier Gayot --- cph.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/cph.c b/cph.c index f9c00c6..b3e3407 100644 --- a/cph.c +++ b/cph.c @@ -21,13 +21,66 @@ #include #include #include +#include static const char *answer_yes = "y"; static const char *answer_no = "n"; -int main(void) +static unsigned int seed_g; + +static struct option options_g[] = { + { + .name = "yes", + .has_arg = required_argument, + .flag = NULL, + .val = 'y', + }, { + .name = "no", + .has_arg = required_argument, + .flag = NULL, + .val = 'n', + }, { + .name = 0, + .has_arg = 0, + .flag = NULL, + .val = 0, + } +}; + +static int parse_arguments(int *argcp, char *argv[]) +{ + int opt; + int ret = 0; + + while ((opt = getopt_long(*argcp, argv, "y:n:s:", options_g, NULL)) >= 0) + { + switch (opt) { + case 'y': + answer_yes = optarg; + break; + case 'n': + answer_no = optarg; + break; + case '?': + ret = -1; + break; + default: + break; + } + } + + return ret; +} + +int main(int argc, char *argv[]) { - srand(time(NULL)); + seed_g = time(NULL); + if (parse_arguments(&argc, argv) < 0) { + fprintf(stderr, "usage: %s [OPTIONS]\n", argv[0]); + return -1; + } + + srand(seed_g); for (;;) { switch (rand() % 2) { -- cgit v1.2.3