]> mj.ucw.cz Git - libucw.git/blob - lib/getopt.c
forgotten commit from yesterday
[libucw.git] / lib / getopt.c
1 #include "lib/lib.h"
2 #include "lib/getopt.h"
3
4 void
5 reset_getopt(void)
6 {
7   // Should work on GNU libc
8   optind = 0;
9 }
10
11 #ifdef TEST
12 static void
13 parse(int argc, char **argv)
14 {
15   static struct option longopts[] = {
16     { "longa", 0, 0, 'a' },
17     { "longb", 0, 0, 'b' },
18     { "longc", 1, 0, 'c' },
19     { "longd", 1, 0, 'd' },
20     { 0, 0, 0, 0 }
21   };
22   int opt;
23   while ((opt = getopt_long(argc, argv, "abc:d:", longopts, NULL)) >= 0)
24     switch (opt)
25       {
26         case 'a':
27         case 'b':
28           printf("option %c\n", opt);
29           break;
30         case 'c':
31         case 'd':
32           printf("option %c with value `%s'\n", opt, optarg);
33           break;
34         case '?':
35           printf("unknown option\n");
36           break;
37         default:
38           printf("getopt returned unexpected char 0x%02x\n", opt);
39           break;
40       }
41   if (optind != argc)
42     printf("%d nonoption arguments\n", argc - optind);
43 }
44
45 int
46 main(int argc, char **argv)
47 {
48   opterr = 0;
49   parse(argc, argv);
50   printf("reset\n");
51   reset_getopt();
52   parse(argc, argv);
53   return 0;
54 }
55 #endif