- (dtucker) [openbsd-compat/getcwd.c] Update from OpenBSD 1.9 -> 1.14.

This commit is contained in:
Darren Tucker 2005-11-10 17:11:29 +11:00
parent 50a221ba7a
commit 31ba53e333
2 changed files with 24 additions and 23 deletions

View File

@ -29,6 +29,7 @@
- (dtucker) [openbsd-compat/readpassphrase.h] Update from OpenBSD 1.3 -> 1.5. - (dtucker) [openbsd-compat/readpassphrase.h] Update from OpenBSD 1.3 -> 1.5.
- (dtucker) [openbsd-compat/glob.c] Update from OpenBSD 1.22 -> 1.25. - (dtucker) [openbsd-compat/glob.c] Update from OpenBSD 1.22 -> 1.25.
- (dtucker) [openbsd-compat/glob.h] Update from OpenBSD 1.8 -> 1.9. - (dtucker) [openbsd-compat/glob.h] Update from OpenBSD 1.8 -> 1.9.
- (dtucker) [openbsd-compat/getcwd.c] Update from OpenBSD 1.9 -> 1.14.
20051105 20051105
- (djm) OpenBSD CVS Sync - (djm) OpenBSD CVS Sync
@ -3271,4 +3272,4 @@
- (djm) Trim deprecated options from INSTALL. Mention UsePAM - (djm) Trim deprecated options from INSTALL. Mention UsePAM
- (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
$Id: ChangeLog,v 1.3972 2005/11/10 06:03:22 dtucker Exp $ $Id: ChangeLog,v 1.3973 2005/11/10 06:11:29 dtucker Exp $

View File

@ -1,3 +1,4 @@
/* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */
/* /*
* Copyright (c) 1989, 1991, 1993 * Copyright (c) 1989, 1991, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -33,10 +34,6 @@
#if !defined(HAVE_GETCWD) #if !defined(HAVE_GETCWD)
#if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
@ -54,12 +51,12 @@ static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp
char * char *
getcwd(char *pt, size_t size) getcwd(char *pt, size_t size)
{ {
register struct dirent *dp; struct dirent *dp;
register DIR *dir = NULL; DIR *dir = NULL;
register dev_t dev; dev_t dev;
register ino_t ino; ino_t ino;
register int first; int first;
register char *bpt, *bup; char *bpt, *bup;
struct stat s; struct stat s;
dev_t root_dev; dev_t root_dev;
ino_t root_ino; ino_t root_ino;
@ -80,7 +77,7 @@ getcwd(char *pt, size_t size)
} }
ept = pt + size; ept = pt + size;
} else { } else {
if ((pt = malloc(ptsize = 1024 - 4)) == NULL) if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
return (NULL); return (NULL);
ept = pt + ptsize; ept = pt + ptsize;
} }
@ -88,13 +85,13 @@ getcwd(char *pt, size_t size)
*bpt = '\0'; *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 * Should always be enough (it's 340 levels). If it's not, allocate
* as necessary. Special * case the first stat, it's ".", not "..". * 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; goto err;
eup = up + MAXPATHLEN; eup = up + upsize;
bup = up; bup = up;
up[0] = '.'; up[0] = '.';
up[1] = '\0'; up[1] = '\0';
@ -139,8 +136,8 @@ getcwd(char *pt, size_t size)
if ((nup = realloc(up, upsize *= 2)) == NULL) if ((nup = realloc(up, upsize *= 2)) == NULL)
goto err; goto err;
bup = nup + (bup - up);
up = nup; up = nup;
bup = up;
eup = up + upsize; eup = up + upsize;
} }
*bup++ = '.'; *bup++ = '.';
@ -175,7 +172,7 @@ getcwd(char *pt, size_t size)
goto notfound; goto notfound;
if (ISDOT(dp)) if (ISDOT(dp))
continue; 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. */ /* Save the first error for later. */
if (lstat(up, &s)) { if (lstat(up, &s)) {
@ -193,19 +190,18 @@ getcwd(char *pt, size_t size)
* leading slash. * leading slash.
*/ */
if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
size_t len, off; size_t len;
char *npt; char *npt;
if (!ptsize) { if (!ptsize) {
errno = ERANGE; errno = ERANGE;
goto err; goto err;
} }
off = bpt - pt;
len = ept - bpt; len = ept - bpt;
if ((npt = realloc(pt, ptsize *= 2)) == NULL) if ((npt = realloc(pt, ptsize *= 2)) == NULL)
goto err; goto err;
bpt = npt + (bpt - pt);
pt = npt; pt = npt;
bpt = pt + off;
ept = pt + ptsize; ept = pt + ptsize;
memmove(ept - len, bpt, len); memmove(ept - len, bpt, len);
bpt = ept - len; bpt = ept - len;
@ -213,7 +209,7 @@ getcwd(char *pt, size_t size)
if (!first) if (!first)
*--bpt = '/'; *--bpt = '/';
bpt -= dp->d_namlen; bpt -= dp->d_namlen;
memmove(bpt, dp->d_name, dp->d_namlen); memcpy(bpt, dp->d_name, dp->d_namlen);
(void)closedir(dir); (void)closedir(dir);
/* Truncate any file name. */ /* Truncate any file name. */
@ -230,12 +226,16 @@ notfound:
errno = save_errno ? save_errno : ENOENT; errno = save_errno ? save_errno : ENOENT;
/* FALLTHROUGH */ /* FALLTHROUGH */
err: err:
save_errno = errno;
if (ptsize) if (ptsize)
free(pt); free(pt);
if (up) free(up);
free(up);
if (dir) if (dir)
(void)closedir(dir); (void)closedir(dir);
errno = save_errno;
return (NULL); return (NULL);
} }