summaryrefslogtreecommitdiff
path: root/cph.c
diff options
context:
space:
mode:
authorOlivier Gayot <duskcoder@gmail.com>2015-02-08 17:56:45 +0000
committerOlivier Gayot <duskcoder@gmail.com>2015-02-08 17:56:45 +0000
commit21b0e4322a67e579cb43d52258df10014f3dc324 (patch)
treeda9a0a01e1796cb33857f96b8c2333870fea99a8 /cph.c
parent56d92f01a90c37fab54d3960ca97af80f70fbfd7 (diff)
cph: allow to overrride yes and no answersHEADmaster
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 <duskcoder@gmail.com>
Diffstat (limited to 'cph.c')
-rw-r--r--cph.c57
1 files 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 <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <getopt.h>
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) {