2 * realpath.c -- canonicalize pathname by removing symlinks
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program 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
13 * GNU Library Public License for more details.
16 #define readlink __readlink
17 #define getcwd __getcwd
23 #include <sys/types.h>
28 #include <limits.h> /* for PATH_MAX */
29 #include <sys/param.h> /* for MAXPATHLEN */
32 #include <sys/stat.h> /* for S_IFLNK */
36 #define PATH_MAX _POSIX_PATH_MAX
39 #define PATH_MAX MAXPATHLEN
46 #define MAX_READLINKS 32
49 char *realpath(const char *path, char resolved_path[])
51 char *realpath(path, resolved_path)
56 char copy_path[PATH_MAX];
57 char link_path[PATH_MAX];
58 char got_path[PATH_MAX];
59 char *new_path = got_path;
64 /* Make a copy of the source path since we may need to modify it. */
65 if (__strlen(path) >= PATH_MAX - 2) {
66 __set_errno(ENAMETOOLONG);
69 __strcpy(copy_path, path);
71 max_path = copy_path + PATH_MAX - 2;
72 /* If it's a relative pathname use getwd for starters. */
77 getcwd(new_path, PATH_MAX - 1);
81 new_path += __strlen(new_path);
82 if (new_path[-1] != '/')
88 /* Expand each slash-separated pathname component. */
89 while (*path != '\0') {
90 /* Ignore stray "/". */
97 if (path[1] == '\0' || path[1] == '/') {
101 if (path[1] == '.') {
102 if (path[2] == '\0' || path[2] == '/') {
104 /* Ignore ".." at root. */
105 if (new_path == got_path + 1)
107 /* Handle ".." by backing up. */
108 while ((--new_path)[-1] != '/');
113 /* Safely copy the next pathname component. */
114 while (*path != '\0' && *path != '/') {
115 if (path > max_path) {
116 __set_errno(ENAMETOOLONG);
119 *new_path++ = *path++;
122 /* Protect against infinite loops. */
123 if (readlinks++ > MAX_READLINKS) {
127 /* See if latest pathname component is a symlink. */
129 n = readlink(got_path, link_path, PATH_MAX - 1);
131 /* EINVAL means the file exists but isn't a symlink. */
132 if (errno != EINVAL) {
133 /* Make sure it's null terminated. */
135 __strcpy(resolved_path, got_path);
139 /* Note: readlink doesn't add the null byte. */
141 if (*link_path == '/')
142 /* Start over for an absolute symlink. */
145 /* Otherwise back up over this component. */
146 while (*(--new_path) != '/');
147 /* Safe sex check. */
148 if (__strlen(path) + n >= PATH_MAX - 2) {
149 __set_errno(ENAMETOOLONG);
152 /* Insert symlink contents into path. */
153 __strcat(link_path, path);
154 __strcpy(copy_path, link_path);
160 /* Delete trailing slash but don't whomp a lone slash. */
161 if (new_path != got_path + 1 && new_path[-1] == '/')
163 /* Make sure it's null terminated. */
165 __strcpy(resolved_path, got_path);
166 return resolved_path;