debug: Add fortify dprintf tests
[glibc.git] / posix / bug-getopt5.c
blob4f67d9b2ece68573298d4898f0f5cd9f662bdb0b
1 /* BZ 11041 */
2 #include <getopt.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
7 static const struct option opts[] =
9 { "a1", no_argument, NULL, 'a' },
10 { "a2", no_argument, NULL, 'a' },
11 { NULL, 0, NULL, 0 }
14 static int
15 one_test (const char *fmt, int argc, char *argv[], int n, int expected[n])
17 optind = 1;
19 int res = 0;
20 for (int i = 0; i < n; ++i)
22 rewind (stderr);
23 if (ftruncate (fileno (stderr), 0) != 0)
25 puts ("cannot truncate file");
26 return 1;
29 int c = getopt_long (argc, argv, fmt, opts, NULL);
30 if (c != expected[i])
32 printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
33 fmt, i, expected[i], c);
34 res = 1;
36 if (ftell (stderr) != 0)
38 printf ("format '%s' test %d failed: printed to stderr\n",
39 fmt, i);
40 res = 1;
44 return res;
48 static int
49 do_test (void)
51 char fname[] = "/tmp/bug-getopt5.XXXXXX";
52 int fd = mkstemp (fname);
53 if (fd == -1)
55 printf ("mkstemp failed: %m\n");
56 return 1;
58 close (fd);
60 if (freopen (fname, "w+", stderr) == NULL)
62 puts ("cannot redirect stderr");
63 return 1;
66 remove (fname);
68 int ret = one_test (":W;", 2,
69 (char *[2]) { (char *) "bug-getopt5", (char *) "--a" },
70 1, (int [1]) { 'a' });
72 ret |= one_test (":W;", 3,
73 (char *[3]) { (char *) "bug-getopt5", (char *) "-W",
74 (char *) "a" },
75 1, (int [1]) { 'a' });
77 if (ret == 0)
78 puts ("all OK");
80 return ret;
83 #define TEST_FUNCTION do_test ()
84 #include "../test-skeleton.c"