X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/8b3319f47c42db6635c1b417ac95177ddf9f4703..7be182d4906b34f434845222c75a1af1a1644733:/openbsd-compat/getcwd.c diff --git a/openbsd-compat/getcwd.c b/openbsd-compat/getcwd.c index de3baccb..711cb9cd 100644 --- a/openbsd-compat/getcwd.c +++ b/openbsd-compat/getcwd.c @@ -1,3 +1,4 @@ +/* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */ /* * Copyright (c) 1989, 1991, 1993 * The Regents of the University of California. All rights reserved. @@ -10,6 +11,9 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -24,13 +28,11 @@ * SUCH DAMAGE. */ -#include "config.h" +/* OPENBSD ORIGINAL: lib/libc/gen/getcwd.c */ -#if !defined(HAVE_GETCWD) +#include "includes.h" -#if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getcwd.c,v 1.6 2000/07/19 15:25:13 deraadt Exp $"; -#endif /* LIBC_SCCS and not lint */ +#if !defined(HAVE_GETCWD) #include #include @@ -47,14 +49,14 @@ static char rcsid[] = "$OpenBSD: getcwd.c,v 1.6 2000/07/19 15:25:13 deraadt Exp (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) char * -getcwd(char *pt,size_t size) +getcwd(char *pt, size_t size) { - register struct dirent *dp; - register DIR *dir = NULL; - register dev_t dev; - register ino_t ino; - register int first; - register char *bpt, *bup; + struct dirent *dp; + DIR *dir = NULL; + dev_t dev; + ino_t ino; + int first; + char *bpt, *bup; struct stat s; dev_t root_dev; ino_t root_ino; @@ -75,7 +77,7 @@ getcwd(char *pt,size_t size) } ept = pt + size; } else { - if ((pt = malloc(ptsize = 1024 - 4)) == NULL) + if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL) return (NULL); ept = pt + ptsize; } @@ -83,13 +85,13 @@ getcwd(char *pt,size_t size) *bpt = '\0'; /* - * Allocate bytes (1024 - malloc space) for the string of "../"'s. + * Allocate bytes for the string of "../"'s. * Should always be enough (it's 340 levels). If it's not, allocate * as necessary. Special * case the first stat, it's ".", not "..". */ - if ((up = malloc(upsize = 1024 - 4)) == NULL) + if ((up = malloc(upsize = MAXPATHLEN)) == NULL) goto err; - eup = up + MAXPATHLEN; + eup = up + upsize; bup = up; up[0] = '.'; up[1] = '\0'; @@ -127,25 +129,23 @@ getcwd(char *pt,size_t size) /* * Build pointer to the parent directory, allocating memory * as necessary. Max length is 3 for "../", the largest - * possible component name, plus a trailing NULL. + * possible component name, plus a trailing NUL. */ if (bup + 3 + MAXNAMLEN + 1 >= eup) { char *nup; if ((nup = realloc(up, upsize *= 2)) == NULL) goto err; + bup = nup + (bup - up); up = nup; - bup = up; eup = up + upsize; } *bup++ = '.'; *bup++ = '.'; *bup = '\0'; - /* Open and stat parent directory. - * RACE?? - replaced fstat(dirfd(dir), &s) w/ lstat(up,&s) - */ - if (!(dir = opendir(up)) || lstat(up,&s)) + /* Open and stat parent directory. */ + if (!(dir = opendir(up)) || fstat(dirfd(dir), &s)) goto err; /* Add trailing slash for next directory. */ @@ -170,7 +170,7 @@ getcwd(char *pt,size_t size) goto notfound; if (ISDOT(dp)) continue; - memmove(bup, dp->d_name, dp->d_namlen + 1); + memcpy(bup, dp->d_name, dp->d_namlen + 1); /* Save the first error for later. */ if (lstat(up, &s)) { @@ -188,19 +188,18 @@ getcwd(char *pt,size_t size) * leading slash. */ if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { - size_t len, off; + size_t len; char *npt; if (!ptsize) { errno = ERANGE; goto err; } - off = bpt - pt; len = ept - bpt; if ((npt = realloc(pt, ptsize *= 2)) == NULL) goto err; + bpt = npt + (bpt - pt); pt = npt; - bpt = pt + off; ept = pt + ptsize; memmove(ept - len, bpt, len); bpt = ept - len; @@ -208,7 +207,7 @@ getcwd(char *pt,size_t size) if (!first) *--bpt = '/'; bpt -= dp->d_namlen; - memmove(bpt, dp->d_name, dp->d_namlen); + memcpy(bpt, dp->d_name, dp->d_namlen); (void)closedir(dir); /* Truncate any file name. */ @@ -225,12 +224,16 @@ notfound: errno = save_errno ? save_errno : ENOENT; /* FALLTHROUGH */ err: + save_errno = errno; + if (ptsize) free(pt); - if (up) - free(up); + free(up); if (dir) (void)closedir(dir); + + errno = save_errno; + return (NULL); }