]>
Commit | Line | Data |
---|---|---|
e3267684 UD |
1 | /* BZ 11040 */ |
2 | #include <getopt.h> | |
3 | #include <unistd.h> | |
4 | #include <stdio.h> | |
5c112f1b | 5 | #include <stdlib.h> |
e3267684 UD |
6 | |
7 | static const struct option opts[] = | |
8 | { | |
9 | { "alpha", no_argument, NULL, 'a' }, | |
10 | { "beta", required_argument, NULL, 'b' }, | |
11 | { NULL, 0, NULL, 0 } | |
12 | }; | |
13 | ||
14 | static int | |
15 | one_test (const char *fmt, int argc, char *argv[], int n, int expected[n], | |
16 | int out[n]) | |
17 | { | |
18 | optind = 1; | |
19 | ||
20 | int res = 0; | |
21 | for (int i = 0; i < n; ++i) | |
22 | { | |
23 | rewind (stderr); | |
24 | if (ftruncate (fileno (stderr), 0) != 0) | |
25 | { | |
26 | puts ("cannot truncate file"); | |
27 | return 1; | |
28 | } | |
29 | ||
30 | int c = getopt_long (argc, argv, fmt, opts, NULL); | |
31 | if (c != expected[i]) | |
32 | { | |
33 | printf ("format '%s' test %d failed: expected '%c', got '%c'\n", | |
34 | fmt, i, expected[i], c); | |
35 | res = 1; | |
36 | } | |
37 | if ((ftell (stderr) != 0) != out[i]) | |
38 | { | |
39 | printf ("format '%s' test %d failed: %sprinted to stderr\n", | |
40 | fmt, i, out[i] ? "not " : ""); | |
41 | res = 1; | |
42 | } | |
43 | } | |
44 | ||
45 | return res; | |
46 | } | |
47 | ||
48 | ||
49 | static int | |
50 | do_test (void) | |
51 | { | |
5c112f1b JM |
52 | char fname[] = "/tmp/bug-getopt3.XXXXXX"; |
53 | int fd = mkstemp (fname); | |
54 | if (fd == -1) | |
e3267684 | 55 | { |
5c112f1b | 56 | printf ("mkstemp failed: %m\n"); |
e3267684 UD |
57 | return 1; |
58 | } | |
5c112f1b | 59 | close (fd); |
e3267684 UD |
60 | |
61 | if (freopen (fname, "w+", stderr) == NULL) | |
62 | { | |
63 | puts ("cannot redirect stderr"); | |
64 | return 1; | |
65 | } | |
66 | ||
67 | remove (fname); | |
68 | ||
69 | int ret = one_test ("ab:W;", 2, | |
70 | (char *[2]) { (char *) "bug-getopt3", (char *) "-a;" }, | |
71 | 2, (int [2]) { 'a', '?' }, (int [2]) { 0, 1 }); | |
72 | ||
73 | ret |= one_test ("ab:W;", 2, | |
74 | (char *[2]) { (char *) "bug-getopt3", (char *) "-a:" }, 2, | |
75 | (int [2]) { 'a', '?' }, (int [2]) { 0, 1 }); | |
76 | ||
77 | if (ret == 0) | |
78 | puts ("all OK"); | |
79 | ||
80 | return ret; | |
81 | } | |
82 | ||
83 | #define TEST_FUNCTION do_test () | |
84 | #include "../test-skeleton.c" |