Update.
[glibc.git] / stdlib / test-canon.c
blobc8be8d2d962172263844d3b6975b45363c858f2e
1 /* Test program for returning the canonical absolute name of a given file.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by David Mosberger <davidm@azstarnet.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This file must be run from within a directory called "stdlib". */
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/param.h>
31 #ifndef PATH_MAX
32 # define PATH_MAX 4096
33 #endif
34 static char cwd[PATH_MAX];
35 static size_t cwd_len;
37 struct {
38 const char * name;
39 const char * value;
40 } symlinks[] = {
41 {"SYMLINK_LOOP", "SYMLINK_LOOP"},
42 {"SYMLINK_1", "."},
43 {"SYMLINK_2", "//////./../../etc"},
44 {"SYMLINK_3", "SYMLINK_1"},
45 {"SYMLINK_4", "SYMLINK_2"},
46 {"SYMLINK_5", "doesNotExist"},
49 struct {
50 const char * in, * out, * resolved;
51 int error;
52 } tests[] = {
53 /* 0 */
54 {"/", "/"},
55 {"/////////////////////////////////", "/"},
56 {"/.././.././.././..///", "/"},
57 {"/etc", "/etc"},
58 {"/etc/../etc", "/etc"},
59 /* 5 */
60 {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT},
61 {"./././././././././.", "."},
62 {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT},
63 {"./doesExist", "./doesExist"},
64 {"./doesExist/", "./doesExist"},
65 /* 10 */
66 {"./doesExist/../doesExist", "./doesExist"},
67 {"foobar", 0, "./foobar", ENOENT},
68 {".", "."},
69 {"./foobar", 0, "./foobar", ENOENT},
70 {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
71 /* 15 */
72 {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP},
73 {"SYMLINK_1", "."},
74 {"SYMLINK_1/foobar", 0, "./foobar", ENOENT},
75 {"SYMLINK_2", "/etc"},
76 {"SYMLINK_3", "."},
77 /* 20 */
78 {"SYMLINK_4", "/etc"},
79 {"../stdlib/SYMLINK_1", "."},
80 {"../stdlib/SYMLINK_2", "/etc"},
81 {"../stdlib/SYMLINK_3", "."},
82 {"../stdlib/SYMLINK_4", "/etc"},
83 /* 25 */
84 {"./SYMLINK_5", 0, "./doesNotExist", ENOENT},
85 {"SYMLINK_5", 0, "./doesNotExist", ENOENT},
86 {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT},
87 {"doesExist/../../stdlib/doesExist", "./doesExist"},
88 {"doesExist/.././../stdlib/.", "."}
92 int
93 check_path (const char * result, const char * expected)
95 int good;
97 if (!result)
98 return (expected == NULL);
100 if (!expected)
101 return 0;
103 if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
104 good = (strncmp (result, cwd, cwd_len) == 0
105 && strcmp (result + cwd_len, expected + 1) == 0);
106 else
107 good = (strcmp (expected, result) == 0);
109 return good;
114 main (int argc, char ** argv)
116 char * result;
117 int fd, i, errors = 0;
118 char buf[PATH_MAX];
120 getcwd (cwd, sizeof(buf));
121 cwd_len = strlen (cwd);
123 errno = 0;
124 if (realpath (NULL, buf) != NULL || errno != EINVAL)
126 printf ("%s: expected return value NULL and errno set to EINVAL"
127 " for realpath(NULL,...)\n", argv[0]);
128 ++errors;
131 errno = 0;
132 if (realpath ("/", NULL) != NULL || errno != EINVAL)
134 printf ("%s: expected return value NULL and errno set to EINVAL"
135 " for realpath(...,NULL)\n", argv[0]);
136 ++errors;
139 errno = 0;
140 if (realpath ("", buf) != NULL || errno != ENOENT)
142 printf ("%s: expected return value NULL and set errno to ENOENT"
143 " for realpath(\"\",...)\n", argv[0]);
144 ++errors;
147 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
148 symlink (symlinks[i].value, symlinks[i].name);
150 fd = open("doesExist", O_CREAT | O_EXCL, 0777);
152 for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
154 buf[0] = '\0';
155 result = realpath (tests[i].in, buf);
157 if (!check_path (result, tests[i].out))
159 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
160 argv[0], i, tests[i].out ? tests[i].out : "NULL",
161 result ? result : "NULL");
162 ++errors;
163 continue;
166 if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
168 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
169 argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
170 buf);
171 ++errors;
172 continue;
175 if (!tests[i].out && errno != tests[i].error)
177 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
178 argv[0], i, tests[i].error, errno);
179 ++errors;
180 continue;
184 getcwd (buf, sizeof(buf));
185 if (strcmp (buf, cwd))
187 printf ("%s: current working directory changed from %s to %s\n",
188 argv[0], cwd, buf);
189 ++errors;
192 if (fd >= 0)
193 unlink("doesExist");
195 for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
196 unlink (symlinks[i].name);
198 if (errors != 0)
200 printf ("%d errors.\n", errors);
201 exit (EXIT_FAILURE);
204 puts ("No errors.");
205 return EXIT_SUCCESS;