lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Test case for globbing dangling symlink. By Ulrich Drepper. */ |
| 2 | #include <errno.h> |
| 3 | #include <error.h> |
| 4 | #include <glob.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | |
| 11 | static void prepare (int argc, char *argv[]); |
| 12 | #define PREPARE prepare |
| 13 | static int do_test (void); |
| 14 | #define TEST_FUNCTION do_test () |
| 15 | |
| 16 | #include "../test-skeleton.c" |
| 17 | |
| 18 | |
| 19 | static char *fname; |
| 20 | |
| 21 | static void |
| 22 | prepare (int argc, char *argv[]) |
| 23 | { |
| 24 | if (argc < 2) |
| 25 | error (EXIT_FAILURE, 0, "missing argument"); |
| 26 | |
| 27 | size_t len = strlen (argv[1]); |
| 28 | static const char ext[] = "globXXXXXX"; |
| 29 | fname = malloc (len + sizeof (ext)); |
| 30 | if (fname == NULL) |
| 31 | error (EXIT_FAILURE, errno, "cannot create temp file"); |
| 32 | again: |
| 33 | strcpy (stpcpy (fname, argv[1]), ext); |
| 34 | |
| 35 | /* fname = mktemp (fname); */ |
| 36 | close(mkstemp(fname)); |
| 37 | unlink(fname); |
| 38 | |
| 39 | if (fname == NULL || *fname == '\0') |
| 40 | error (EXIT_FAILURE, errno, "cannot create temp file name"); |
| 41 | if (symlink ("bug-glob1-does-not-exist", fname) != 0) |
| 42 | { |
| 43 | if (errno == EEXIST) |
| 44 | goto again; |
| 45 | |
| 46 | error (EXIT_FAILURE, errno, "cannot create symlink"); |
| 47 | } |
| 48 | add_temp_file (fname); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static int |
| 53 | do_test (void) |
| 54 | { |
| 55 | glob_t gl; |
| 56 | int retval = 0; |
| 57 | int e; |
| 58 | |
| 59 | e = glob (fname, 0, NULL, &gl); |
| 60 | if (e == 0) |
| 61 | { |
| 62 | printf ("glob(\"%s\") succeeded when it should not have\n", fname); |
| 63 | retval = 1; |
| 64 | } |
| 65 | globfree (&gl); |
| 66 | |
| 67 | size_t fnamelen = strlen (fname); |
| 68 | char buf[fnamelen + 2]; |
| 69 | |
| 70 | strcpy (buf, fname); |
| 71 | buf[fnamelen - 1] = '?'; |
| 72 | e = glob (buf, 0, NULL, &gl); |
| 73 | if (e == 0) |
| 74 | { |
| 75 | printf ("glob(\"%s\") succeeded when it should not have\n", buf); |
| 76 | retval = 1; |
| 77 | } |
| 78 | globfree (&gl); |
| 79 | |
| 80 | strcpy (buf, fname); |
| 81 | buf[fnamelen] = '*'; |
| 82 | buf[fnamelen + 1] = '\0'; |
| 83 | e = glob (buf, 0, NULL, &gl); |
| 84 | if (e == 0) |
| 85 | { |
| 86 | printf ("glob(\"%s\") succeeded when it should not have\n", buf); |
| 87 | retval = 1; |
| 88 | } |
| 89 | globfree (&gl); |
| 90 | |
| 91 | return retval; |
| 92 | } |