Back to index

glibc  2.9
bug-glob1.c
Go to the documentation of this file.
00001 /* Test case for globbing dangling symlink.  By Ulrich Drepper.  */
00002 #include <errno.h>
00003 #include <error.h>
00004 #include <glob.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <unistd.h>
00009 
00010 
00011 static void prepare (int argc, char *argv[]);
00012 #define PREPARE prepare
00013 static int do_test (void);
00014 #define TEST_FUNCTION do_test ()
00015 
00016 #include "../test-skeleton.c"
00017 
00018 
00019 static char *fname;
00020 
00021 static void
00022 prepare (int argc, char *argv[])
00023 {
00024   if (argc < 2)
00025     error (EXIT_FAILURE, 0, "missing argument");
00026 
00027   size_t len = strlen (argv[1]);
00028   static const char ext[] = "globXXXXXX";
00029   fname = malloc (len + sizeof (ext));
00030   if (fname == NULL)
00031     error (EXIT_FAILURE, errno, "cannot create temp file");
00032  again:
00033   strcpy (stpcpy (fname, argv[1]), ext);
00034   fname = mktemp (fname);
00035   if (fname == NULL || *fname == '\0')
00036     error (EXIT_FAILURE, errno, "cannot create temp file name");
00037   if (symlink ("bug-glob1-does-not-exist", fname) != 0)
00038     {
00039       if (errno == EEXIST)
00040        goto again;
00041 
00042       error (EXIT_FAILURE, errno, "cannot create symlink");
00043     }
00044   add_temp_file (fname);
00045 }
00046 
00047 
00048 static int
00049 do_test (void)
00050 {
00051   glob_t gl;
00052   int retval = 0;
00053   int e;
00054 
00055   e = glob (fname, 0, NULL, &gl);
00056   if (e == 0)
00057     {
00058       printf ("glob(\"%s\") succeeded\n", fname);
00059       retval = 1;
00060     }
00061   globfree (&gl);
00062 
00063   size_t fnamelen = strlen (fname);
00064   char buf[fnamelen + 2];
00065 
00066   strcpy (buf, fname);
00067   buf[fnamelen - 1] = '?';
00068   e = glob (buf, 0, NULL, &gl);
00069   if (e == 0)
00070     {
00071       printf ("glob(\"%s\") succeeded\n", buf);
00072       retval = 1;
00073     }
00074   globfree (&gl);
00075 
00076   strcpy (buf, fname);
00077   buf[fnamelen] = '*';
00078   buf[fnamelen + 1] = '\0';
00079   e = glob (buf, 0, NULL, &gl);
00080   if (e == 0)
00081     {
00082       printf ("glob(\"%s\") succeeded\n", buf);
00083       retval = 1;
00084     }
00085   globfree (&gl);
00086 
00087   return retval;
00088 }