]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/bug-getopt5.c
Use libsupport for tst-spawn.c
[thirdparty/glibc.git] / posix / bug-getopt5.c
CommitLineData
e3267684
UD
1/* BZ 11041 */
2#include <getopt.h>
3#include <unistd.h>
4#include <stdio.h>
5c112f1b 5#include <stdlib.h>
e3267684
UD
6
7static const struct option opts[] =
8 {
9 { "a1", no_argument, NULL, 'a' },
10 { "a2", no_argument, NULL, 'a' },
11 { NULL, 0, NULL, 0 }
12 };
13
14static int
15one_test (const char *fmt, int argc, char *argv[], int n, int expected[n])
16{
17 optind = 1;
18
19 int res = 0;
20 for (int i = 0; i < n; ++i)
21 {
22 rewind (stderr);
23 if (ftruncate (fileno (stderr), 0) != 0)
24 {
25 puts ("cannot truncate file");
26 return 1;
27 }
28
29 int c = getopt_long (argc, argv, fmt, opts, NULL);
30 if (c != expected[i])
31 {
32 printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
33 fmt, i, expected[i], c);
34 res = 1;
35 }
36 if (ftell (stderr) != 0)
37 {
38 printf ("format '%s' test %d failed: printed to stderr\n",
39 fmt, i);
40 res = 1;
41 }
42 }
43
44 return res;
45}
46
47
48static int
49do_test (void)
50{
5c112f1b
JM
51 char fname[] = "/tmp/bug-getopt5.XXXXXX";
52 int fd = mkstemp (fname);
53 if (fd == -1)
e3267684 54 {
5c112f1b 55 printf ("mkstemp failed: %m\n");
e3267684
UD
56 return 1;
57 }
5c112f1b 58 close (fd);
e3267684
UD
59
60 if (freopen (fname, "w+", stderr) == NULL)
61 {
62 puts ("cannot redirect stderr");
63 return 1;
64 }
65
66 remove (fname);
67
68 int ret = one_test (":W;", 2,
69 (char *[2]) { (char *) "bug-getopt5", (char *) "--a" },
70 1, (int [1]) { 'a' });
71
72 ret |= one_test (":W;", 3,
73 (char *[3]) { (char *) "bug-getopt5", (char *) "-W",
74 (char *) "a" },
75 1, (int [1]) { 'a' });
76
77 if (ret == 0)
78 puts ("all OK");
79
80 return ret;
81}
82
83#define TEST_FUNCTION do_test ()
84#include "../test-skeleton.c"