Refactor some code in libm-test-driver.c.
[glibc.git] / posix / bug-getopt1.c
bloba47dc7e229c03666a32713ac740578fc46780bcf
1 /* BZ 11039 */
2 #include <unistd.h>
3 #include <stdio.h>
5 static int
6 one_test (const char *fmt, int argc, char *argv[], int expected[argc - 1])
8 optind = 1;
10 int res = 0;
11 for (int i = 0; i < argc - 1; ++i)
13 rewind (stderr);
14 if (ftruncate (fileno (stderr), 0) != 0)
16 puts ("cannot truncate file");
17 return 1;
20 int c = getopt (argc, argv, fmt);
21 if (c != expected[i])
23 printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
24 fmt, i, expected[i], c);
25 res = 1;
27 if (ftell (stderr) != 0)
29 printf ("format '%s' test %d failed: printed to stderr\n",
30 fmt, i);
31 res = 1;
35 return res;
39 static int
40 do_test (void)
42 char *fname = tmpnam (NULL);
43 if (fname == NULL)
45 puts ("cannot generate name for temporary file");
46 return 1;
49 if (freopen (fname, "w+", stderr) == NULL)
51 puts ("cannot redirect stderr");
52 return 1;
55 remove (fname);
57 int ret = one_test ("+:a:b", 2,
58 (char *[2]) { (char *) "bug-getopt1", (char *) "-a" },
59 (int [1]) { ':' });
61 ret |= one_test ("+:a:b", 3,
62 (char *[3]) { (char *) "bug-getopt1", (char *) "-b",
63 (char *) "-a" },
64 (int [2]) { 'b', ':' });
66 if (ret == 0)
67 puts ("all OK");
69 return ret;
72 #define TEST_FUNCTION do_test ()
73 #include "../test-skeleton.c"