2010-04-08 13:59:40 +08:00
|
|
|
/* BZ 11041 */
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2018-07-19 05:04:12 +08:00
|
|
|
#include <stdlib.h>
|
2010-04-08 13:59:40 +08:00
|
|
|
|
|
|
|
static const struct option opts[] =
|
|
|
|
{
|
|
|
|
{ "alpha", optional_argument, NULL, 'a' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
one_test (const char *fmt, int argc, char *argv[], int n, int expected[n])
|
|
|
|
{
|
|
|
|
optind = 1;
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
rewind (stderr);
|
|
|
|
if (ftruncate (fileno (stderr), 0) != 0)
|
|
|
|
{
|
|
|
|
puts ("cannot truncate file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int c = getopt_long (argc, argv, fmt, opts, NULL);
|
|
|
|
if (c != expected[i])
|
|
|
|
{
|
2017-04-02 04:19:50 +08:00
|
|
|
printf ("%s: format '%s' test %d failed: expected '%c', got '%c'\n",
|
|
|
|
argv[0], fmt, i, expected[i], c);
|
2010-04-08 13:59:40 +08:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
else if (optarg != NULL)
|
|
|
|
{
|
2017-04-02 04:19:50 +08:00
|
|
|
printf ("%s: format '%s' test %d failed: optarg is \"%s\", not NULL\n",
|
|
|
|
argv[0], fmt, i, optarg);
|
2010-04-08 13:59:40 +08:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
if (ftell (stderr) != 0)
|
|
|
|
{
|
2017-04-02 04:19:50 +08:00
|
|
|
printf ("%s: format '%s' test %d failed: printed to stderr\n",
|
|
|
|
argv[0], fmt, i);
|
2010-04-08 13:59:40 +08:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
2018-07-19 05:04:12 +08:00
|
|
|
char fname[] = "/tmp/bug-getopt4.XXXXXX";
|
|
|
|
int fd = mkstemp (fname);
|
|
|
|
if (fd == -1)
|
2010-04-08 13:59:40 +08:00
|
|
|
{
|
2018-07-19 05:04:12 +08:00
|
|
|
printf ("mkstemp failed: %m\n");
|
2010-04-08 13:59:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2018-07-19 05:04:12 +08:00
|
|
|
close (fd);
|
2010-04-08 13:59:40 +08:00
|
|
|
|
|
|
|
if (freopen (fname, "w+", stderr) == NULL)
|
|
|
|
{
|
|
|
|
puts ("cannot redirect stderr");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
remove (fname);
|
|
|
|
|
|
|
|
int ret = one_test ("W;", 2,
|
2017-04-02 04:19:50 +08:00
|
|
|
(char *[2]) { (char *) "bug-getopt4a", (char *) "--a" },
|
2010-04-08 13:59:40 +08:00
|
|
|
1, (int [1]) { 'a' });
|
|
|
|
|
|
|
|
ret |= one_test ("W;", 3,
|
2017-04-02 04:19:50 +08:00
|
|
|
(char *[3]) { (char *) "bug-getopt4b", (char *) "-W",
|
2010-04-08 13:59:40 +08:00
|
|
|
(char *) "a" },
|
|
|
|
1, (int [1]) { 'a' });
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
puts ("all OK");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|