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". */
29 #include <sys/param.h>
31 static char cwd
[PATH_MAX
];
32 static size_t cwd_len
;
38 {"SYMLINK_LOOP", "SYMLINK_LOOP"},
40 {"SYMLINK_2", "//////./../../etc"},
41 {"SYMLINK_3", "SYMLINK_1"},
42 {"SYMLINK_4", "SYMLINK_2"},
43 {"SYMLINK_5", "doesNotExist"},
47 const char * in
, * out
, * resolved
;
52 {"/////////////////////////////////", "/"},
53 {"/.././.././.././..///", "/"},
55 {"/etc/../etc", "/etc"},
57 {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT
},
58 {"./././././././././.", "."},
59 {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT
},
60 {"./doesExist", "./doesExist"},
61 {"./doesExist/", "./doesExist"},
63 {"./doesExist/../doesExist", "./doesExist"},
64 {"foobar", 0, "./foobar", ENOENT
},
66 {"./foobar", 0, "./foobar", ENOENT
},
67 {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP
},
69 {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP
},
71 {"SYMLINK_1/foobar", 0, "./foobar", ENOENT
},
72 {"SYMLINK_2", "/etc"},
75 {"SYMLINK_4", "/etc"},
76 {"../stdlib/SYMLINK_1", "."},
77 {"../stdlib/SYMLINK_2", "/etc"},
78 {"../stdlib/SYMLINK_3", "."},
79 {"../stdlib/SYMLINK_4", "/etc"},
81 {"./SYMLINK_5", 0, "./doesNotExist", ENOENT
},
82 {"SYMLINK_5", 0, "./doesNotExist", ENOENT
},
83 {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT
},
84 {"doesExist/../../stdlib/doesExist", "./doesExist"},
85 {"doesExist/.././../stdlib/.", "."}
90 check_path (const char * result
, const char * expected
)
95 return (expected
== NULL
);
100 if (expected
[0] == '.' && (expected
[1] == '/' || expected
[1] == '\0'))
101 good
= (strncmp (result
, cwd
, cwd_len
) == 0
102 && strcmp (result
+ cwd_len
, expected
+ 1) == 0);
104 good
= (strcmp (expected
, result
) == 0);
111 main (int argc
, char ** argv
)
114 int fd
, i
, errors
= 0;
117 getcwd (cwd
, sizeof(buf
));
118 cwd_len
= strlen (cwd
);
121 if (realpath (NULL
, buf
) != NULL
|| errno
!= EINVAL
)
123 printf ("%s: expected return value NULL and errno set to EINVAL"
124 " for realpath(NULL,...)\n", argv
[0]);
129 if (realpath ("/", NULL
) != NULL
|| errno
!= EINVAL
)
131 printf ("%s: expected return value NULL and errno set to EINVAL"
132 " for realpath(...,NULL)\n", argv
[0]);
137 if (realpath ("", buf
) != NULL
|| errno
!= ENOENT
)
139 printf ("%s: expected return value NULL and set errno to ENOENT"
140 " for realpath(\"\",...)\n", argv
[0]);
144 for (i
= 0; i
< (int) (sizeof (symlinks
) / sizeof (symlinks
[0])); ++i
)
145 symlink (symlinks
[i
].value
, symlinks
[i
].name
);
147 fd
= open("doesExist", O_CREAT
| O_EXCL
, 0777);
149 for (i
= 0; i
< (int) (sizeof (tests
) / sizeof (tests
[0])); ++i
)
152 result
= realpath (tests
[i
].in
, buf
);
154 if (!check_path (result
, tests
[i
].out
))
156 printf ("%s: flunked test %d (expected `%s', got `%s')\n",
157 argv
[0], i
, tests
[i
].out
? tests
[i
].out
: "NULL",
158 result
? result
: "NULL");
163 if (!check_path (buf
, tests
[i
].out
? tests
[i
].out
: tests
[i
].resolved
))
165 printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
166 argv
[0], i
, tests
[i
].out
? tests
[i
].out
: tests
[i
].resolved
,
172 if (!tests
[i
].out
&& errno
!= tests
[i
].error
)
174 printf ("%s: flunked test %d (expected errno %d, got %d)\n",
175 argv
[0], i
, tests
[i
].error
, errno
);
181 getcwd (buf
, sizeof(buf
));
182 if (strcmp (buf
, cwd
))
184 printf ("%s: current working directory changed from %s to %s\n",
192 for (i
= 0; i
< (int) (sizeof (symlinks
) / sizeof (symlinks
[0])); ++i
)
193 unlink (symlinks
[i
].name
);
197 printf ("%d errors.\n", errors
);