io: Re-flow and sort multiline Makefile definitions
[glibc.git] / posix / bug-getopt3.c
blob45a8d3ec3dddb23d39bed78d44a13f96a5b8671b
1 /* BZ 11040 */
2 #include <getopt.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
7 static const struct option opts[] =
9 { "alpha", no_argument, NULL, 'a' },
10 { "beta", required_argument, NULL, 'b' },
11 { NULL, 0, NULL, 0 }
14 static int
15 one_test (const char *fmt, int argc, char *argv[], int n, int expected[n],
16 int out[n])
18 optind = 1;
20 int res = 0;
21 for (int i = 0; i < n; ++i)
23 rewind (stderr);
24 if (ftruncate (fileno (stderr), 0) != 0)
26 puts ("cannot truncate file");
27 return 1;
30 int c = getopt_long (argc, argv, fmt, opts, NULL);
31 if (c != expected[i])
33 printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
34 fmt, i, expected[i], c);
35 res = 1;
37 if ((ftell (stderr) != 0) != out[i])
39 printf ("format '%s' test %d failed: %sprinted to stderr\n",
40 fmt, i, out[i] ? "not " : "");
41 res = 1;
45 return res;
49 static int
50 do_test (void)
52 char fname[] = "/tmp/bug-getopt3.XXXXXX";
53 int fd = mkstemp (fname);
54 if (fd == -1)
56 printf ("mkstemp failed: %m\n");
57 return 1;
59 close (fd);
61 if (freopen (fname, "w+", stderr) == NULL)
63 puts ("cannot redirect stderr");
64 return 1;
67 remove (fname);
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 });
73 ret |= one_test ("ab:W;", 2,
74 (char *[2]) { (char *) "bug-getopt3", (char *) "-a:" }, 2,
75 (int [2]) { 'a', '?' }, (int [2]) { 0, 1 });
77 if (ret == 0)
78 puts ("all OK");
80 return ret;
83 #define TEST_FUNCTION do_test ()
84 #include "../test-skeleton.c"