1 /* 2 * Copyright (c) 1989, 1991, 1993 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) 35 static char sccsid[] = "@(#)getcwd.c 8.1 (Berkeley) 6/4/93"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 41 #include <errno.h> 42 #include <dirent.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #define ISDOT(dp) \ 49 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ 50 dp->d_name[1] == '.' && dp->d_name[2] == '\0')) 51 52 char * 53 getcwd(pt, size) 54 char *pt; 55 size_t size; 56 { 57 register struct dirent *dp; 58 register DIR *dir; 59 register dev_t dev; 60 register ino_t ino; 61 register int first; 62 register char *bpt, *bup; 63 struct stat s; 64 dev_t root_dev; 65 ino_t root_ino; 66 size_t ptsize, upsize; 67 int save_errno; 68 char *ept, *eup, *up; 69 70 /* 71 * If no buffer specified by the user, allocate one as necessary. 72 * If a buffer is specified, the size has to be non-zero. The path 73 * is built from the end of the buffer backwards. 74 */ 75 if (pt) { 76 ptsize = 0; 77 if (!size) { 78 errno = EINVAL; 79 return (NULL); 80 } 81 if (size == 1) { 82 errno = ERANGE; 83 return (NULL); 84 } 85 ept = pt + size; 86 } else { 87 if ((pt = malloc(ptsize = 1024 - 4)) == NULL) 88 return (NULL); 89 ept = pt + ptsize; 90 } 91 bpt = ept - 1; 92 *bpt = '\0'; 93 94 /* 95 * Allocate bytes (1024 - malloc space) for the string of "../"'s. 96 * Should always be enough (it's 340 levels). If it's not, allocate 97 * as necessary. Special * case the first stat, it's ".", not "..". 98 */ 99 if ((up = malloc(upsize = 1024 - 4)) == NULL) 100 goto err; 101 eup = up + MAXPATHLEN; 102 bup = up; 103 up[0] = '.'; 104 up[1] = '\0'; 105 106 /* Save root values, so know when to stop. */ 107 if (stat("/", &s)) 108 goto err; 109 root_dev = s.st_dev; 110 root_ino = s.st_ino; 111 112 errno = 0; /* XXX readdir has no error return. */ 113 114 for (first = 1;; first = 0) { 115 /* Stat the current level. */ 116 if (lstat(up, &s)) 117 goto err; 118 119 /* Save current node values. */ 120 ino = s.st_ino; 121 dev = s.st_dev; 122 123 /* Check for reaching root. */ 124 if (root_dev == dev && root_ino == ino) { 125 *--bpt = '/'; 126 /* 127 * It's unclear that it's a requirement to copy the 128 * path to the beginning of the buffer, but it's always 129 * been that way and stuff would probably break. 130 */ 131 bcopy(bpt, pt, ept - bpt); 132 free(up); 133 return (pt); 134 } 135 136 /* 137 * Build pointer to the parent directory, allocating memory 138 * as necessary. Max length is 3 for "../", the largest 139 * possible component name, plus a trailing NULL. 140 */ 141 if (bup + 3 + MAXNAMLEN + 1 >= eup) { 142 if ((up = realloc(up, upsize *= 2)) == NULL) 143 goto err; 144 bup = up; 145 eup = up + upsize; 146 } 147 *bup++ = '.'; 148 *bup++ = '.'; 149 *bup = '\0'; 150 151 /* Open and stat parent directory. */ 152 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s)) 153 goto err; 154 155 /* Add trailing slash for next directory. */ 156 *bup++ = '/'; 157 158 /* 159 * If it's a mount point, have to stat each element because 160 * the inode number in the directory is for the entry in the 161 * parent directory, not the inode number of the mounted file. 162 */ 163 save_errno = 0; 164 if (s.st_dev == dev) { 165 for (;;) { 166 if (!(dp = readdir(dir))) 167 goto notfound; 168 if (dp->d_fileno == ino) 169 break; 170 } 171 } else 172 for (;;) { 173 if (!(dp = readdir(dir))) 174 goto notfound; 175 if (ISDOT(dp)) 176 continue; 177 bcopy(dp->d_name, bup, dp->d_namlen + 1); 178 179 /* Save the first error for later. */ 180 if (lstat(up, &s)) { 181 if (!save_errno) 182 save_errno = errno; 183 errno = 0; 184 continue; 185 } 186 if (s.st_dev == dev && s.st_ino == ino) 187 break; 188 } 189 190 /* 191 * Check for length of the current name, preceding slash, 192 * leading slash. 193 */ 194 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { 195 size_t len, off; 196 197 if (!ptsize) { 198 errno = ERANGE; 199 goto err; 200 } 201 off = bpt - pt; 202 len = ept - bpt; 203 if ((pt = realloc(pt, ptsize *= 2)) == NULL) 204 goto err; 205 bpt = pt + off; 206 ept = pt + ptsize; 207 bcopy(bpt, ept - len, len); 208 bpt = ept - len; 209 } 210 if (!first) 211 *--bpt = '/'; 212 bpt -= dp->d_namlen; 213 bcopy(dp->d_name, bpt, dp->d_namlen); 214 (void)closedir(dir); 215 216 /* Truncate any file name. */ 217 *bup = '\0'; 218 } 219 220 notfound: 221 /* 222 * If readdir set errno, use it, not any saved error; otherwise, 223 * didn't find the current directory in its parent directory, set 224 * errno to ENOENT. 225 */ 226 if (!errno) 227 errno = save_errno ? save_errno : ENOENT; 228 /* FALLTHROUGH */ 229 err: 230 if (ptsize) 231 free(pt); 232 free(up); 233 return (NULL); 234 } 235