7 static const struct option opts
[] =
9 { "alpha", optional_argument
, NULL
, 'a' },
14 one_test (const char *fmt
, int argc
, char *argv
[], int n
, int expected
[n
])
19 for (int i
= 0; i
< n
; ++i
)
22 if (ftruncate (fileno (stderr
), 0) != 0)
24 puts ("cannot truncate file");
28 int c
= getopt_long (argc
, argv
, fmt
, opts
, NULL
);
31 printf ("%s: format '%s' test %d failed: expected '%c', got '%c'\n",
32 argv
[0], fmt
, i
, expected
[i
], c
);
35 else if (optarg
!= NULL
)
37 printf ("%s: format '%s' test %d failed: optarg is \"%s\", not NULL\n",
38 argv
[0], fmt
, i
, optarg
);
41 if (ftell (stderr
) != 0)
43 printf ("%s: format '%s' test %d failed: printed to stderr\n",
56 char fname
[] = "/tmp/bug-getopt4.XXXXXX";
57 int fd
= mkstemp (fname
);
60 printf ("mkstemp failed: %m\n");
65 if (freopen (fname
, "w+", stderr
) == NULL
)
67 puts ("cannot redirect stderr");
73 int ret
= one_test ("W;", 2,
74 (char *[2]) { (char *) "bug-getopt4a", (char *) "--a" },
75 1, (int [1]) { 'a' });
77 ret
|= one_test ("W;", 3,
78 (char *[3]) { (char *) "bug-getopt4b", (char *) "-W",
80 1, (int [1]) { 'a' });
88 #define TEST_FUNCTION do_test ()
89 #include "../test-skeleton.c"