diff options
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/getcwd.c | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/openbsd-compat/getcwd.c b/openbsd-compat/getcwd.c index d58c03e0e..9354f7ae4 100644 --- a/openbsd-compat/getcwd.c +++ b/openbsd-compat/getcwd.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1989, 1991, 1993 | 3 | * Copyright (c) 1989, 1991, 1993 |
3 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -33,10 +34,6 @@ | |||
33 | 34 | ||
34 | #if !defined(HAVE_GETCWD) | 35 | #if !defined(HAVE_GETCWD) |
35 | 36 | ||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $"; | ||
38 | #endif /* LIBC_SCCS and not lint */ | ||
39 | |||
40 | #include <sys/param.h> | 37 | #include <sys/param.h> |
41 | #include <sys/stat.h> | 38 | #include <sys/stat.h> |
42 | #include <errno.h> | 39 | #include <errno.h> |
@@ -54,12 +51,12 @@ static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp | |||
54 | char * | 51 | char * |
55 | getcwd(char *pt, size_t size) | 52 | getcwd(char *pt, size_t size) |
56 | { | 53 | { |
57 | register struct dirent *dp; | 54 | struct dirent *dp; |
58 | register DIR *dir = NULL; | 55 | DIR *dir = NULL; |
59 | register dev_t dev; | 56 | dev_t dev; |
60 | register ino_t ino; | 57 | ino_t ino; |
61 | register int first; | 58 | int first; |
62 | register char *bpt, *bup; | 59 | char *bpt, *bup; |
63 | struct stat s; | 60 | struct stat s; |
64 | dev_t root_dev; | 61 | dev_t root_dev; |
65 | ino_t root_ino; | 62 | ino_t root_ino; |
@@ -80,7 +77,7 @@ getcwd(char *pt, size_t size) | |||
80 | } | 77 | } |
81 | ept = pt + size; | 78 | ept = pt + size; |
82 | } else { | 79 | } else { |
83 | if ((pt = malloc(ptsize = 1024 - 4)) == NULL) | 80 | if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL) |
84 | return (NULL); | 81 | return (NULL); |
85 | ept = pt + ptsize; | 82 | ept = pt + ptsize; |
86 | } | 83 | } |
@@ -88,13 +85,13 @@ getcwd(char *pt, size_t size) | |||
88 | *bpt = '\0'; | 85 | *bpt = '\0'; |
89 | 86 | ||
90 | /* | 87 | /* |
91 | * Allocate bytes (1024 - malloc space) for the string of "../"'s. | 88 | * Allocate bytes for the string of "../"'s. |
92 | * Should always be enough (it's 340 levels). If it's not, allocate | 89 | * Should always be enough (it's 340 levels). If it's not, allocate |
93 | * as necessary. Special * case the first stat, it's ".", not "..". | 90 | * as necessary. Special * case the first stat, it's ".", not "..". |
94 | */ | 91 | */ |
95 | if ((up = malloc(upsize = 1024 - 4)) == NULL) | 92 | if ((up = malloc(upsize = MAXPATHLEN)) == NULL) |
96 | goto err; | 93 | goto err; |
97 | eup = up + MAXPATHLEN; | 94 | eup = up + upsize; |
98 | bup = up; | 95 | bup = up; |
99 | up[0] = '.'; | 96 | up[0] = '.'; |
100 | up[1] = '\0'; | 97 | up[1] = '\0'; |
@@ -139,8 +136,8 @@ getcwd(char *pt, size_t size) | |||
139 | 136 | ||
140 | if ((nup = realloc(up, upsize *= 2)) == NULL) | 137 | if ((nup = realloc(up, upsize *= 2)) == NULL) |
141 | goto err; | 138 | goto err; |
139 | bup = nup + (bup - up); | ||
142 | up = nup; | 140 | up = nup; |
143 | bup = up; | ||
144 | eup = up + upsize; | 141 | eup = up + upsize; |
145 | } | 142 | } |
146 | *bup++ = '.'; | 143 | *bup++ = '.'; |
@@ -175,7 +172,7 @@ getcwd(char *pt, size_t size) | |||
175 | goto notfound; | 172 | goto notfound; |
176 | if (ISDOT(dp)) | 173 | if (ISDOT(dp)) |
177 | continue; | 174 | continue; |
178 | memmove(bup, dp->d_name, dp->d_namlen + 1); | 175 | memcpy(bup, dp->d_name, dp->d_namlen + 1); |
179 | 176 | ||
180 | /* Save the first error for later. */ | 177 | /* Save the first error for later. */ |
181 | if (lstat(up, &s)) { | 178 | if (lstat(up, &s)) { |
@@ -193,19 +190,18 @@ getcwd(char *pt, size_t size) | |||
193 | * leading slash. | 190 | * leading slash. |
194 | */ | 191 | */ |
195 | if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { | 192 | if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { |
196 | size_t len, off; | 193 | size_t len; |
197 | char *npt; | 194 | char *npt; |
198 | 195 | ||
199 | if (!ptsize) { | 196 | if (!ptsize) { |
200 | errno = ERANGE; | 197 | errno = ERANGE; |
201 | goto err; | 198 | goto err; |
202 | } | 199 | } |
203 | off = bpt - pt; | ||
204 | len = ept - bpt; | 200 | len = ept - bpt; |
205 | if ((npt = realloc(pt, ptsize *= 2)) == NULL) | 201 | if ((npt = realloc(pt, ptsize *= 2)) == NULL) |
206 | goto err; | 202 | goto err; |
203 | bpt = npt + (bpt - pt); | ||
207 | pt = npt; | 204 | pt = npt; |
208 | bpt = pt + off; | ||
209 | ept = pt + ptsize; | 205 | ept = pt + ptsize; |
210 | memmove(ept - len, bpt, len); | 206 | memmove(ept - len, bpt, len); |
211 | bpt = ept - len; | 207 | bpt = ept - len; |
@@ -213,7 +209,7 @@ getcwd(char *pt, size_t size) | |||
213 | if (!first) | 209 | if (!first) |
214 | *--bpt = '/'; | 210 | *--bpt = '/'; |
215 | bpt -= dp->d_namlen; | 211 | bpt -= dp->d_namlen; |
216 | memmove(bpt, dp->d_name, dp->d_namlen); | 212 | memcpy(bpt, dp->d_name, dp->d_namlen); |
217 | (void)closedir(dir); | 213 | (void)closedir(dir); |
218 | 214 | ||
219 | /* Truncate any file name. */ | 215 | /* Truncate any file name. */ |
@@ -230,12 +226,16 @@ notfound: | |||
230 | errno = save_errno ? save_errno : ENOENT; | 226 | errno = save_errno ? save_errno : ENOENT; |
231 | /* FALLTHROUGH */ | 227 | /* FALLTHROUGH */ |
232 | err: | 228 | err: |
229 | save_errno = errno; | ||
230 | |||
233 | if (ptsize) | 231 | if (ptsize) |
234 | free(pt); | 232 | free(pt); |
235 | if (up) | 233 | free(up); |
236 | free(up); | ||
237 | if (dir) | 234 | if (dir) |
238 | (void)closedir(dir); | 235 | (void)closedir(dir); |
236 | |||
237 | errno = save_errno; | ||
238 | |||
239 | return (NULL); | 239 | return (NULL); |
240 | } | 240 | } |
241 | 241 | ||