realpath.c (333fc21e3cd79bca0c94d7722c5a56cb5ad078d1) | realpath.c (de216a83c249f840ed7ab77170e2f235aed2d937) |
---|---|
1/* 2 * Copyright (c) 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Jan-Simon Pendry. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 38 unchanged lines hidden (view full) --- 47#include <errno.h> 48#include <fcntl.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> 52#include "un-namespace.h" 53 54/* | 1/* 2 * Copyright (c) 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Jan-Simon Pendry. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 38 unchanged lines hidden (view full) --- 47#include <errno.h> 48#include <fcntl.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> 52#include "un-namespace.h" 53 54/* |
55 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | 55 * char *realpath(const char *path, char resolved_path[PATH_MAX]); |
56 * 57 * Find the real name of path, by removing all ".", ".." and symlink 58 * components. Returns (resolved) on success, or (NULL) on failure, 59 * in which case the path which caused trouble is left in (resolved). 60 */ 61char * 62realpath(path, resolved) 63 const char *path; 64 char *resolved; 65{ 66 struct stat sb; 67 int fd, n, rootd, serrno; | 56 * 57 * Find the real name of path, by removing all ".", ".." and symlink 58 * components. Returns (resolved) on success, or (NULL) on failure, 59 * in which case the path which caused trouble is left in (resolved). 60 */ 61char * 62realpath(path, resolved) 63 const char *path; 64 char *resolved; 65{ 66 struct stat sb; 67 int fd, n, rootd, serrno; |
68 char *p, *q, wbuf[MAXPATHLEN]; | 68 char *p, *q, wbuf[PATH_MAX]; |
69 int symlinks = 0; 70 71 /* Save the starting point. */ 72 if ((fd = _open(".", O_RDONLY)) < 0) { 73 (void)strcpy(resolved, "."); 74 return (NULL); 75 } 76 77 /* 78 * Find the dirname and basename from the path to be resolved. 79 * Change directory to the dirname component. 80 * lstat the basename part. 81 * if it is a symlink, read in the value and loop. 82 * if it is a directory, then change to that directory. 83 * get the current directory name and append the basename. 84 */ | 69 int symlinks = 0; 70 71 /* Save the starting point. */ 72 if ((fd = _open(".", O_RDONLY)) < 0) { 73 (void)strcpy(resolved, "."); 74 return (NULL); 75 } 76 77 /* 78 * Find the dirname and basename from the path to be resolved. 79 * Change directory to the dirname component. 80 * lstat the basename part. 81 * if it is a symlink, read in the value and loop. 82 * if it is a directory, then change to that directory. 83 * get the current directory name and append the basename. 84 */ |
85 (void)strncpy(resolved, path, MAXPATHLEN - 1); 86 resolved[MAXPATHLEN - 1] = '\0'; | 85 (void)strncpy(resolved, path, PATH_MAX - 1); 86 resolved[PATH_MAX - 1] = '\0'; |
87loop: 88 q = strrchr(resolved, '/'); 89 if (q != NULL) { 90 p = q + 1; 91 if (q == resolved) 92 q = "/"; 93 else { 94 do { --- 9 unchanged lines hidden (view full) --- 104 105 /* Deal with the last component. */ 106 if (*p != '\0' && lstat(p, &sb) == 0) { 107 if (S_ISLNK(sb.st_mode)) { 108 if (++symlinks > MAXSYMLINKS) { 109 errno = ELOOP; 110 goto err1; 111 } | 87loop: 88 q = strrchr(resolved, '/'); 89 if (q != NULL) { 90 p = q + 1; 91 if (q == resolved) 92 q = "/"; 93 else { 94 do { --- 9 unchanged lines hidden (view full) --- 104 105 /* Deal with the last component. */ 106 if (*p != '\0' && lstat(p, &sb) == 0) { 107 if (S_ISLNK(sb.st_mode)) { 108 if (++symlinks > MAXSYMLINKS) { 109 errno = ELOOP; 110 goto err1; 111 } |
112 n = readlink(p, resolved, MAXPATHLEN - 1); | 112 n = readlink(p, resolved, PATH_MAX - 1); |
113 if (n < 0) 114 goto err1; 115 resolved[n] = '\0'; 116 goto loop; 117 } 118 if (S_ISDIR(sb.st_mode)) { 119 if (chdir(p) < 0) 120 goto err1; 121 p = ""; 122 } 123 } 124 125 /* 126 * Save the last component name and get the full pathname of 127 * the current directory. 128 */ 129 (void)strcpy(wbuf, p); | 113 if (n < 0) 114 goto err1; 115 resolved[n] = '\0'; 116 goto loop; 117 } 118 if (S_ISDIR(sb.st_mode)) { 119 if (chdir(p) < 0) 120 goto err1; 121 p = ""; 122 } 123 } 124 125 /* 126 * Save the last component name and get the full pathname of 127 * the current directory. 128 */ 129 (void)strcpy(wbuf, p); |
130 if (getcwd(resolved, MAXPATHLEN) == 0) | 130 if (getcwd(resolved, PATH_MAX) == 0) |
131 goto err1; 132 133 /* 134 * Join the two strings together, ensuring that the right thing 135 * happens if the last component is empty, or the dirname is root. 136 */ 137 if (resolved[0] == '/' && resolved[1] == '\0') 138 rootd = 1; 139 else 140 rootd = 0; 141 142 if (*wbuf) { | 131 goto err1; 132 133 /* 134 * Join the two strings together, ensuring that the right thing 135 * happens if the last component is empty, or the dirname is root. 136 */ 137 if (resolved[0] == '/' && resolved[1] == '\0') 138 rootd = 1; 139 else 140 rootd = 0; 141 142 if (*wbuf) { |
143 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { | 143 if (strlen(resolved) + strlen(wbuf) + rootd + 1 > PATH_MAX) { |
144 errno = ENAMETOOLONG; 145 goto err1; 146 } 147 if (rootd == 0) 148 (void)strcat(resolved, "/"); 149 (void)strcat(resolved, wbuf); 150 } 151 --- 16 unchanged lines hidden --- | 144 errno = ENAMETOOLONG; 145 goto err1; 146 } 147 if (rootd == 0) 148 (void)strcat(resolved, "/"); 149 (void)strcat(resolved, wbuf); 150 } 151 --- 16 unchanged lines hidden --- |