]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/misc/bug-glob1.c
Inital import
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / misc / bug-glob1.c
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 /*
36   fname = mktemp (fname);
37 */
38   close(mkstemp(fname));
39   unlink(fname);
40
41   if (fname == NULL || *fname == '\0')
42     error (EXIT_FAILURE, errno, "cannot create temp file name");
43   if (symlink ("bug-glob1-does-not-exist", fname) != 0)
44     {
45       if (errno == EEXIST)
46         goto again;
47
48       error (EXIT_FAILURE, errno, "cannot create symlink");
49     }
50   add_temp_file (fname);
51 }
52
53
54 static int
55 do_test (void)
56 {
57   glob_t gl;
58   int retval = 0;
59   int e;
60
61   e = glob (fname, 0, NULL, &gl);
62   if (e == 0)
63     {
64       printf ("glob(\"%s\") succeeded when it should not have\n", fname);
65       retval = 1;
66     }
67   globfree (&gl);
68
69   size_t fnamelen = strlen (fname);
70   char buf[fnamelen + 2];
71
72   strcpy (buf, fname);
73   buf[fnamelen - 1] = '?';
74   e = glob (buf, 0, NULL, &gl);
75   if (e == 0)
76     {
77       printf ("glob(\"%s\") succeeded when it should not have\n", buf);
78       retval = 1;
79     }
80   globfree (&gl);
81
82   strcpy (buf, fname);
83   buf[fnamelen] = '*';
84   buf[fnamelen + 1] = '\0';
85   e = glob (buf, 0, NULL, &gl);
86   if (e == 0)
87     {
88       printf ("glob(\"%s\") succeeded when it should not have\n", buf);
89       retval = 1;
90     }
91   globfree (&gl);
92
93   return retval;
94 }