]> mj.ucw.cz Git - libucw.git/commitdiff
forgotten commit from yesterday
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Fri, 13 Apr 2007 07:29:06 +0000 (09:29 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Fri, 13 Apr 2007 07:29:06 +0000 (09:29 +0200)
lib/getopt.c [new file with mode: 0644]
lib/getopt.t [new file with mode: 0644]

diff --git a/lib/getopt.c b/lib/getopt.c
new file mode 100644 (file)
index 0000000..1a728c8
--- /dev/null
@@ -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 (file)
index 0000000..6a821cd
--- /dev/null
@@ -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