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