sparc: refactor sparc64 signbit{f} selector to C
[glibc.git] / posix / tst-glob_symlinks.c
blob5c4b4ecf4a4035136698dbe7d4d5a59f299a2537
1 /* Test glob danglin symlink match (BZ #866).
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <stddef.h>
26 #include <glob.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
30 #include <support/check.h>
31 #include <support/temp_file.h>
33 static void do_prepare (int argc, char *argv[]);
34 #define PREPARE do_prepare
35 static int do_test (void);
36 #include <support/test-driver.c>
38 /* Maximum number of symlink calls for create_link function. */
39 #define MAX_CREATE_LINK_TRIES 10
41 static void
42 create_link (const char *base, const char *fname, char *linkname,
43 size_t linknamesize)
45 int ntries = 0;
46 while (1)
48 snprintf (linkname, linknamesize, "%s/%s%02d", test_dir, base,
49 ntries);
50 if (symlink (fname, linkname) == 0)
51 break;
52 if (errno != EEXIST)
53 FAIL_EXIT1 ("symlink failed: %m");
54 if (ntries++ == MAX_CREATE_LINK_TRIES)
55 FAIL_EXIT1 ("symlink failed with EEXIST too many times");
57 add_temp_file (linkname);
60 static char valid_link[PATH_MAX];
61 static char dangling_link[PATH_MAX];
62 static char dangling_dir[PATH_MAX];
64 static void
65 do_prepare (int argc, char *argv[])
67 char *fname;
69 create_temp_file ("tst-glob_symlinks.", &fname);
71 /* Create an existing symlink. */
72 create_link ("valid-symlink-tst-glob_symlinks", fname, valid_link,
73 sizeof valid_link);
75 /* Create a dangling symlink to a file. */
76 int fd = create_temp_file ("dangling-tst-glob_file", &fname);
77 TEST_VERIFY_EXIT (close (fd) == 0);
78 /* It throws a warning at process end due 'add_temp_file' trying to
79 unlink it again. */
80 TEST_VERIFY_EXIT (unlink (fname) == 0);
81 create_link ("dangling-symlink-file-tst-glob", fname, dangling_link,
82 sizeof dangling_link);
84 /* Create a dangling symlink to a directory. */
85 char tmpdir[PATH_MAX];
86 snprintf (tmpdir, sizeof tmpdir, "%s/dangling-tst-glob_folder.XXXXXX",
87 test_dir);
88 TEST_VERIFY_EXIT (mkdtemp (tmpdir) != NULL);
89 create_link ("dangling-symlink-dir-tst-glob", tmpdir, dangling_dir,
90 sizeof dangling_dir);
91 TEST_VERIFY_EXIT (rmdir (tmpdir) == 0);
94 static int
95 do_test (void)
97 char buf[PATH_MAX];
98 glob_t gl;
100 TEST_VERIFY_EXIT (glob (valid_link, 0, NULL, &gl) == 0);
101 TEST_VERIFY_EXIT (gl.gl_pathc == 1);
102 TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], valid_link) == 0);
103 globfree (&gl);
105 TEST_VERIFY_EXIT (glob (dangling_link, 0, NULL, &gl) == 0);
106 TEST_VERIFY_EXIT (gl.gl_pathc == 1);
107 TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0);
108 globfree (&gl);
110 TEST_VERIFY_EXIT (glob (dangling_dir, 0, NULL, &gl) == 0);
111 TEST_VERIFY_EXIT (gl.gl_pathc == 1);
112 TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_dir) == 0);
113 globfree (&gl);
115 snprintf (buf, sizeof buf, "%s", dangling_link);
116 buf[strlen(buf) - 1] = '?';
117 TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) == 0);
118 TEST_VERIFY_EXIT (gl.gl_pathc == 1);
119 TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0);
120 globfree (&gl);
122 /* glob should handle dangling symbol as normal file, so <file>? should
123 return an empty string. */
124 snprintf (buf, sizeof buf, "%s?", dangling_link);
125 TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) != 0);
126 globfree (&gl);
128 snprintf (buf, sizeof buf, "%s*", dangling_link);
129 TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) == 0);
130 TEST_VERIFY_EXIT (gl.gl_pathc == 1);
131 TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0);
132 globfree (&gl);
134 return 0;