lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | static void do_prepare (void); |
| 2 | #define PREPARE(argc, argv) do_prepare () |
| 3 | static int do_test (void); |
| 4 | #define TEST_FUNCTION do_test () |
| 5 | #include "../test-skeleton.c" |
| 6 | |
| 7 | |
| 8 | static char *fname; |
| 9 | |
| 10 | |
| 11 | static void |
| 12 | do_prepare (void) |
| 13 | { |
| 14 | if (create_temp_file ("tst-getopt_long1", &fname) < 0) |
| 15 | { |
| 16 | printf ("cannot create temp file: %m\n"); |
| 17 | exit (1); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | |
| 22 | static const struct option opts[] = |
| 23 | { |
| 24 | { "one", no_argument, NULL, '1' }, |
| 25 | { "two", no_argument, NULL, '2' }, |
| 26 | { "one-one", no_argument, NULL, '3' }, |
| 27 | { "four", no_argument, NULL, '4' }, |
| 28 | { "onto", no_argument, NULL, '5' }, |
| 29 | { NULL, 0, NULL, 0 } |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | static int |
| 34 | do_test (void) |
| 35 | { |
| 36 | if (freopen (fname, "w+", stderr) == NULL) |
| 37 | { |
| 38 | printf ("freopen failed: %m\n"); |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | char *argv[] = { (char *) "program", (char *) "--on" }; |
| 43 | int argc = 2; |
| 44 | |
| 45 | int c = getopt_long (argc, argv, "12345", opts, NULL); |
| 46 | printf ("return value: %c\n", c); |
| 47 | |
| 48 | rewind (stderr); |
| 49 | char *line = NULL; |
| 50 | size_t len = 0; |
| 51 | if (getline (&line, &len, stderr) < 0) |
| 52 | { |
| 53 | printf ("cannot read stderr redirect: %m\n"); |
| 54 | return 1; |
| 55 | } |
| 56 | printf ("message = \"%s\"\n", line); |
| 57 | |
| 58 | static const char expected[] = "\ |
| 59 | program: option '--on' is ambiguous; possibilities: '--one' '--onto' '--one-one'\n"; |
| 60 | |
| 61 | return c != '?' || strcmp (line, expected) != 0; |
| 62 | } |