From 8239ce8980337f454228bf97e689a56b34e003ac Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Fri, 13 Apr 2007 09:29:06 +0200 Subject: [PATCH] forgotten commit from yesterday --- lib/getopt.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/getopt.t | 21 ++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 lib/getopt.c create mode 100644 lib/getopt.t diff --git a/lib/getopt.c b/lib/getopt.c new file mode 100644 index 00000000..1a728c8a --- /dev/null +++ b/lib/getopt.c @@ -0,0 +1,55 @@ +#include "lib/lib.h" +#include "lib/getopt.h" + +void +reset_getopt(void) +{ + // Should work on GNU libc + optind = 0; +} + +#ifdef TEST +static void +parse(int argc, char **argv) +{ + static struct option longopts[] = { + { "longa", 0, 0, 'a' }, + { "longb", 0, 0, 'b' }, + { "longc", 1, 0, 'c' }, + { "longd", 1, 0, 'd' }, + { 0, 0, 0, 0 } + }; + int opt; + while ((opt = getopt_long(argc, argv, "abc:d:", longopts, NULL)) >= 0) + switch (opt) + { + case 'a': + case 'b': + printf("option %c\n", opt); + break; + case 'c': + case 'd': + printf("option %c with value `%s'\n", opt, optarg); + break; + case '?': + printf("unknown option\n"); + break; + default: + printf("getopt returned unexpected char 0x%02x\n", opt); + break; + } + if (optind != argc) + printf("%d nonoption arguments\n", argc - optind); +} + +int +main(int argc, char **argv) +{ + opterr = 0; + parse(argc, argv); + printf("reset\n"); + reset_getopt(); + parse(argc, argv); + return 0; +} +#endif diff --git a/lib/getopt.t b/lib/getopt.t new file mode 100644 index 00000000..6a821cdb --- /dev/null +++ b/lib/getopt.t @@ -0,0 +1,21 @@ +# Tests for getopt + +Run: obj/lib/getopt-t -a -b --longc 2819 -d -a 1 2 3 +Out: option a + option b + option c with value `2819' + option d with value `-a' + 3 nonoption arguments + reset + option a + option b + option c with value `2819' + option d with value `-a' + 3 nonoption arguments + +Run: obj/lib/getopt-t -a -x +Out: option a + unknown option + reset + option a + unknown option -- 2.39.2