bug-glob1.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. static void prepare (int argc, char *argv[]);
  10. #define PREPARE prepare
  11. static int do_test (void);
  12. #define TEST_FUNCTION do_test ()
  13. #include "../test-skeleton.c"
  14. static char *fname;
  15. static void
  16. prepare (int argc, char *argv[])
  17. {
  18. if (argc < 2)
  19. error (EXIT_FAILURE, 0, "missing argument");
  20. size_t len = strlen (argv[1]);
  21. static const char ext[] = "globXXXXXX";
  22. fname = malloc (len + sizeof (ext));
  23. if (fname == NULL)
  24. error (EXIT_FAILURE, errno, "cannot create temp file");
  25. again:
  26. strcpy (stpcpy (fname, argv[1]), ext);
  27. /* fname = mktemp (fname); */
  28. close(mkstemp(fname));
  29. unlink(fname);
  30. if (fname == NULL || *fname == '\0')
  31. error (EXIT_FAILURE, errno, "cannot create temp file name");
  32. if (symlink ("bug-glob1-does-not-exist", fname) != 0)
  33. {
  34. if (errno == EEXIST)
  35. goto again;
  36. error (EXIT_FAILURE, errno, "cannot create symlink");
  37. }
  38. add_temp_file (fname);
  39. }
  40. static int
  41. do_test (void)
  42. {
  43. glob_t gl;
  44. int retval = 0;
  45. int e;
  46. e = glob (fname, 0, NULL, &gl);
  47. if (e == 0)
  48. {
  49. printf ("glob(\"%s\") succeeded when it should not have\n", fname);
  50. retval = 1;
  51. }
  52. globfree (&gl);
  53. size_t fnamelen = strlen (fname);
  54. char buf[fnamelen + 2];
  55. strcpy (buf, fname);
  56. buf[fnamelen - 1] = '?';
  57. e = glob (buf, 0, NULL, &gl);
  58. if (e == 0)
  59. {
  60. printf ("glob(\"%s\") succeeded when it should not have\n", buf);
  61. retval = 1;
  62. }
  63. globfree (&gl);
  64. strcpy (buf, fname);
  65. buf[fnamelen] = '*';
  66. buf[fnamelen + 1] = '\0';
  67. e = glob (buf, 0, NULL, &gl);
  68. if (e == 0)
  69. {
  70. printf ("glob(\"%s\") succeeded when it should not have\n", buf);
  71. retval = 1;
  72. }
  73. globfree (&gl);
  74. return retval;
  75. }