]>
Commit | Line | Data |
---|---|---|
e3267684 UD |
1 | /* BZ 11039 */ |
2 | #include <unistd.h> | |
3 | #include <stdio.h> | |
4 | ||
5 | static int | |
6 | one_test (const char *fmt, int argc, char *argv[], int expected[argc - 1]) | |
7 | { | |
8 | optind = 1; | |
9 | ||
10 | int res = 0; | |
11 | for (int i = 0; i < argc - 1; ++i) | |
12 | { | |
13 | rewind (stderr); | |
14 | if (ftruncate (fileno (stderr), 0) != 0) | |
15 | { | |
16 | puts ("cannot truncate file"); | |
17 | return 1; | |
18 | } | |
19 | ||
20 | int c = getopt (argc, argv, fmt); | |
21 | if (c != expected[i]) | |
22 | { | |
23 | printf ("format '%s' test %d failed: expected '%c', got '%c'\n", | |
24 | fmt, i, expected[i], c); | |
25 | res = 1; | |
26 | } | |
27 | if (ftell (stderr) != 0) | |
28 | { | |
29 | printf ("format '%s' test %d failed: printed to stderr\n", | |
30 | fmt, i); | |
31 | res = 1; | |
32 | } | |
33 | } | |
34 | ||
35 | return res; | |
36 | } | |
37 | ||
38 | ||
39 | static int | |
40 | do_test (void) | |
41 | { | |
42 | char *fname = tmpnam (NULL); | |
43 | if (fname == NULL) | |
44 | { | |
45 | puts ("cannot generate name for temporary file"); | |
46 | return 1; | |
47 | } | |
48 | ||
49 | if (freopen (fname, "w+", stderr) == NULL) | |
50 | { | |
51 | puts ("cannot redirect stderr"); | |
52 | return 1; | |
53 | } | |
54 | ||
55 | remove (fname); | |
56 | ||
57 | int ret = one_test ("+:a:b", 2, | |
58 | (char *[2]) { (char *) "bug-getopt1", (char *) "-a" }, | |
59 | (int [1]) { ':' }); | |
60 | ||
61 | ret |= one_test ("+:a:b", 3, | |
62 | (char *[3]) { (char *) "bug-getopt1", (char *) "-b", | |
63 | (char *) "-a" }, | |
64 | (int [2]) { 'b', ':' }); | |
65 | ||
66 | if (ret == 0) | |
67 | puts ("all OK"); | |
68 | ||
69 | return ret; | |
70 | } | |
71 | ||
72 | #define TEST_FUNCTION do_test () | |
73 | #include "../test-skeleton.c" |