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