io: Re-flow and sort multiline Makefile definitions
[glibc.git] / posix / bug-getopt2.c
blob8f92f0c6ee08507d38d0fa2a97333fb406eacf04
1 /* BZ 11039 */
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 static int
7 one_test (const char *fmt, int argc, char *argv[], int expected[argc - 1])
9 int res = 0;
10 for (int i = 0; i < argc - 1; ++i)
12 rewind (stderr);
13 if (ftruncate (fileno (stderr), 0) != 0)
15 puts ("cannot truncate file");
16 return 1;
19 int c = getopt (argc, argv, fmt);
20 if (c != expected[i])
22 printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
23 fmt, i, expected[i], c);
24 res = 1;
26 if (ftell (stderr) == 0)
28 printf ("format '%s' test %d failed: not printed to stderr\n",
29 fmt, i);
30 res = 1;
34 return res;
38 static int
39 do_test (void)
41 char fname[] = "/tmp/bug-getopt2.XXXXXX";
42 int fd = mkstemp (fname);
43 if (fd == -1)
45 printf ("mkstemp failed: %m\n");
46 return 1;
48 close (fd);
50 if (freopen (fname, "w+", stderr) == NULL)
52 puts ("cannot redirect stderr");
53 return 1;
56 remove (fname);
58 optind = 0;
59 int ret = one_test ("+a", 2,
60 (char *[2]) { (char *) "bug-getopt2", (char *) "-+" },
61 (int [1]) { '?' });
63 optind = 1;
64 ret |= one_test ("+a", 2,
65 (char *[2]) { (char *) "bug-getopt2", (char *) "-+" },
66 (int [1]) { '?' });
68 if (ret == 0)
69 puts ("all OK");
71 return ret;
74 #define TEST_FUNCTION do_test ()
75 #include "../test-skeleton.c"