getcwd.c revision 29476
1/* 2 * Copyright (c) 1989, 1991, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#if defined(LIBC_SCCS) && !defined(lint) 35static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; 36#endif /* LIBC_SCCS and not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.h> 40 41#include <dirent.h> 42#include <errno.h> 43#include <fcntl.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <unistd.h> 48 49#define ISDOT(dp) \ 50 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ 51 (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) 52 53char * 54getcwd(pt, size) 55 char *pt; 56 size_t size; 57{ 58 register struct dirent *dp; 59 register DIR *dir = NULL; 60 register dev_t dev; 61 register ino_t ino; 62 register int first; 63 register char *bpt, *bup; 64 struct stat s; 65 dev_t root_dev; 66 ino_t root_ino; 67 size_t ptsize, upsize; 68 int save_errno; 69 char *ept, *eup, *up, c; 70 71 /* 72 * If no buffer specified by the user, allocate one as necessary. 73 * If a buffer is specified, the size has to be non-zero. The path 74 * is built from the end of the buffer backwards. 75 */ 76 if (pt) { 77 ptsize = 0; 78 if (!size) { 79 errno = EINVAL; 80 return (NULL); 81 } 82 if (size == 1) { 83 errno = ERANGE; 84 return (NULL); 85 } 86 ept = pt + size; 87 } else { 88 if ((pt = malloc(ptsize = 1024 - 4)) == NULL) 89 return (NULL); 90 ept = pt + ptsize; 91 } 92 if (!__getcwd(pt, ept - pt)) { 93 if (*pt != '/') { 94 bpt = pt; 95 ept = pt + strlen(pt) - 1; 96 while (bpt < ept) { 97 c = *bpt; 98 *bpt++ = *ept; 99 *ept-- = c; 100 } 101 } 102 return (pt); 103 } 104 bpt = ept - 1; 105 *bpt = '\0'; 106 107 /* 108 * Allocate bytes (1024 - malloc space) for the string of "../"'s. 109 * Should always be enough (it's 340 levels). If it's not, allocate 110 * as necessary. Special case the first stat, it's ".", not "..". 111 */ 112 if ((up = malloc(upsize = 1024 - 4)) == NULL) 113 goto err; 114 eup = up + MAXPATHLEN; 115 bup = up; 116 up[0] = '.'; 117 up[1] = '\0'; 118 119 /* Save root values, so know when to stop. */ 120 if (stat("/", &s)) 121 goto err; 122 root_dev = s.st_dev; 123 root_ino = s.st_ino; 124 125 errno = 0; /* XXX readdir has no error return. */ 126 127 for (first = 1;; first = 0) { 128 /* Stat the current level. */ 129 if (lstat(up, &s)) 130 goto err; 131 132 /* Save current node values. */ 133 ino = s.st_ino; 134 dev = s.st_dev; 135 136 /* Check for reaching root. */ 137 if (root_dev == dev && root_ino == ino) { 138 *--bpt = '/'; 139 /* 140 * It's unclear that it's a requirement to copy the 141 * path to the beginning of the buffer, but it's always 142 * been that way and stuff would probably break. 143 */ 144 bcopy(bpt, pt, ept - bpt); 145 free(up); 146 return (pt); 147 } 148 149 /* 150 * Build pointer to the parent directory, allocating memory 151 * as necessary. Max length is 3 for "../", the largest 152 * possible component name, plus a trailing NULL. 153 */ 154 if (bup + 3 + MAXNAMLEN + 1 >= eup) { 155 if ((up = realloc(up, upsize *= 2)) == NULL) 156 goto err; 157 bup = up; 158 eup = up + upsize; 159 } 160 *bup++ = '.'; 161 *bup++ = '.'; 162 *bup = '\0'; 163 164 /* Open and stat parent directory. */ 165 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s)) 166 goto err; 167 168 /* Add trailing slash for next directory. */ 169 *bup++ = '/'; 170 *bup = '\0'; 171 172 /* 173 * If it's a mount point, have to stat each element because 174 * the inode number in the directory is for the entry in the 175 * parent directory, not the inode number of the mounted file. 176 */ 177 save_errno = 0; 178 if (s.st_dev == dev) { 179 for (;;) { 180 if (!(dp = readdir(dir))) 181 goto notfound; 182 if (dp->d_fileno == ino) 183 break; 184 } 185 } else 186 for (;;) { 187 if (!(dp = readdir(dir))) 188 goto notfound; 189 if (ISDOT(dp)) 190 continue; 191 bcopy(dp->d_name, bup, dp->d_namlen + 1); 192 193 /* Save the first error for later. */ 194 if (lstat(up, &s)) { 195 if (!save_errno) 196 save_errno = errno; 197 errno = 0; 198 continue; 199 } 200 if (s.st_dev == dev && s.st_ino == ino) 201 break; 202 } 203 204 /* 205 * Check for length of the current name, preceding slash, 206 * leading slash. 207 */ 208 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { 209 size_t len, off; 210 211 if (!ptsize) { 212 errno = ERANGE; 213 goto err; 214 } 215 off = bpt - pt; 216 len = ept - bpt; 217 if ((pt = realloc(pt, ptsize *= 2)) == NULL) 218 goto err; 219 bpt = pt + off; 220 ept = pt + ptsize; 221 bcopy(bpt, ept - len, len); 222 bpt = ept - len; 223 } 224 if (!first) 225 *--bpt = '/'; 226 bpt -= dp->d_namlen; 227 bcopy(dp->d_name, bpt, dp->d_namlen); 228 (void) closedir(dir); 229 dir = NULL; 230 231 /* Truncate any file name. */ 232 *bup = '\0'; 233 } 234 235notfound: 236 /* 237 * If readdir set errno, use it, not any saved error; otherwise, 238 * didn't find the current directory in its parent directory, set 239 * errno to ENOENT. 240 */ 241 if (!errno) 242 errno = save_errno ? save_errno : ENOENT; 243 /* FALLTHROUGH */ 244err: 245 if (ptsize) 246 free(pt); 247 if (dir) 248 (void) closedir(dir); 249 free(up); 250 return (NULL); 251} 252