1 /* realpath.c - Return the canonicalized absolute pathname */
3 /* Written 2000 by Werner Almesberger */
14 /* FIXME: buffer overrun possible, loops forever on cyclic symlinks */
18 * Canonical name: never ends with a slash
21 static int resolve_path(char *path,char *result,char *pos)
34 slash = *path ? strchr(path,'/') : NULL;
35 if (slash) *slash = 0;
36 if (!path[0] || (path[0] == '.' &&
37 (!path[1] || (path[1] == '.' && !path[2])))) {
39 if (pos != result && path[0] && path[1])
40 while (*--pos != '/');
44 if (lstat(result,&st) < 0) return -1;
45 if (S_ISLNK(st.st_mode)) {
48 if (readlink(result,buf,sizeof(buf)) < 0) return -1;
55 if (*path == '/') result[1] = 0;
56 pos = strchr(result,0);
59 pos = strchr(result,0);
72 char *realpath(const char *path,char *resolved_path)
79 errno = ENOENT; /* SUSv2 */
82 if (!getcwd(cwd,sizeof(cwd))) return NULL;
83 strcpy(resolved_path,"/");
84 if (resolve_path(cwd,resolved_path,resolved_path)) return NULL;
85 strcat(resolved_path,"/");
86 path_copy = strdup(path);
87 if (!path_copy) return NULL;
88 res = resolve_path(path_copy,resolved_path,strchr(resolved_path,0));