ChangeLog update from my last commit
[glibc.git] / posix / bug-getopt1.c
bloba5a37116d2e7fc4e97d6159b86dfb128bf0af25b
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 optind = 1;
11 int res = 0;
12 for (int i = 0; i < argc - 1; ++i)
14 rewind (stderr);
15 if (ftruncate (fileno (stderr), 0) != 0)
17 puts ("cannot truncate file");
18 return 1;
21 int c = getopt (argc, argv, fmt);
22 if (c != expected[i])
24 printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
25 fmt, i, expected[i], c);
26 res = 1;
28 if (ftell (stderr) != 0)
30 printf ("format '%s' test %d failed: printed to stderr\n",
31 fmt, i);
32 res = 1;
36 return res;
40 static int
41 do_test (void)
43 char fname[] = "/tmp/bug-getopt1.XXXXXX";
44 int fd = mkstemp (fname);
45 if (fd == -1)
47 printf ("mkstemp failed: %m\n");
48 return 1;
50 close (fd);
52 if (freopen (fname, "w+", stderr) == NULL)
54 puts ("cannot redirect stderr");
55 return 1;
58 remove (fname);
60 int ret = one_test ("+:a:b", 2,
61 (char *[2]) { (char *) "bug-getopt1", (char *) "-a" },
62 (int [1]) { ':' });
64 ret |= one_test ("+:a:b", 3,
65 (char *[3]) { (char *) "bug-getopt1", (char *) "-b",
66 (char *) "-a" },
67 (int [2]) { 'b', ':' });
69 if (ret == 0)
70 puts ("all OK");
72 return ret;
75 #define TEST_FUNCTION do_test ()
76 #include "../test-skeleton.c"