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