bug-glob1.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /*
  28. fname = mktemp (fname);
  29. */
  30. close(mkstemp(fname));
  31. unlink(fname);
  32. if (fname == NULL || *fname == '\0')
  33. error (EXIT_FAILURE, errno, "cannot create temp file name");
  34. if (symlink ("bug-glob1-does-not-exist", fname) != 0)
  35. {
  36. if (errno == EEXIST)
  37. goto again;
  38. error (EXIT_FAILURE, errno, "cannot create symlink");
  39. }
  40. add_temp_file (fname);
  41. }
  42. static int
  43. do_test (void)
  44. {
  45. glob_t gl;
  46. int retval = 0;
  47. int e;
  48. e = glob (fname, 0, NULL, &gl);
  49. if (e == 0)
  50. {
  51. printf ("glob(\"%s\") succeeded when it should not have\n", fname);
  52. retval = 1;
  53. }
  54. globfree (&gl);
  55. size_t fnamelen = strlen (fname);
  56. char buf[fnamelen + 2];
  57. strcpy (buf, fname);
  58. buf[fnamelen - 1] = '?';
  59. e = glob (buf, 0, NULL, &gl);
  60. if (e == 0)
  61. {
  62. printf ("glob(\"%s\") succeeded when it should not have\n", buf);
  63. retval = 1;
  64. }
  65. globfree (&gl);
  66. strcpy (buf, fname);
  67. buf[fnamelen] = '*';
  68. buf[fnamelen + 1] = '\0';
  69. e = glob (buf, 0, NULL, &gl);
  70. if (e == 0)
  71. {
  72. printf ("glob(\"%s\") succeeded when it should not have\n", buf);
  73. retval = 1;
  74. }
  75. globfree (&gl);
  76. return retval;
  77. }