Annotation of /trunk/mkinitrd-magellan/klibc/usr/klibc/tests/getoptlong.c
Parent Directory | Revision Log
Revision 1297 -
(hide annotations)
(download)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 1197 byte(s)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 1197 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
1 | niro | 532 | /* |
2 | * getoptlong.c | ||
3 | * | ||
4 | * Simple test for getopt_long, set the environment variable GETOPTTEST | ||
5 | * to give the argument string to getopt() | ||
6 | */ | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <unistd.h> | ||
10 | #include <stdio.h> | ||
11 | #include <getopt.h> | ||
12 | |||
13 | static int foo = 0; | ||
14 | niro | 1297 | |
15 | niro | 532 | static const struct option long_options[] = { |
16 | { "first", 1, NULL, 'f' }, | ||
17 | { "second", 0, NULL, 's' }, | ||
18 | { "third", 2, NULL, '3' }, | ||
19 | { "fourth", 0, NULL, 4 }, | ||
20 | { "set-foo", 0, &foo, 1 }, | ||
21 | { NULL, 0, NULL, 0 } | ||
22 | }; | ||
23 | |||
24 | int main(int argc, char *const *argv) | ||
25 | { | ||
26 | const char *parser; | ||
27 | const char *showchar; | ||
28 | char one_char[] = "\'?\'"; | ||
29 | char num_buf[16]; | ||
30 | int c; | ||
31 | int longindex; | ||
32 | |||
33 | parser = getenv("GETOPTTEST"); | ||
34 | if (!parser) | ||
35 | parser = "abzf:o:"; | ||
36 | |||
37 | do { | ||
38 | c = getopt_long(argc, argv, parser, long_options, &longindex); | ||
39 | |||
40 | if (c == EOF) { | ||
41 | showchar = "EOF"; | ||
42 | } else if (c >= 32 && c <= 126) { | ||
43 | one_char[1] = c; | ||
44 | showchar = one_char; | ||
45 | } else { | ||
46 | snprintf(num_buf, sizeof num_buf, "%d", c); | ||
47 | showchar = num_buf; | ||
48 | } | ||
49 | |||
50 | printf("c = %s, optind = %d (\"%s\"), optarg = \"%s\", " | ||
51 | "optopt = \'%c\', foo = %d, longindex = %d\n", | ||
52 | showchar, optind, argv[optind], | ||
53 | optarg, optopt, foo, longindex); | ||
54 | } while (c != -1); | ||
55 | |||
56 | return 0; | ||
57 | } |