Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/getcwd.c,v retrieving revision 1.11 retrieving revision 1.29 diff -u -p -r1.11 -r1.29 --- src/lib/libc/gen/getcwd.c 1998/02/26 02:49:21 1.11 +++ src/lib/libc/gen/getcwd.c 2003/08/03 04:12:01 1.29 @@ -1,4 +1,4 @@ -/* $NetBSD: getcwd.c,v 1.11 1998/02/26 02:49:21 perry Exp $ */ +/* $NetBSD: getcwd.c,v 1.29 2003/08/03 04:12:01 itojun Exp $ */ /* * Copyright (c) 1989, 1991, 1993, 1995 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; #else -__RCSID("$NetBSD: getcwd.c,v 1.11 1998/02/26 02:49:21 perry Exp $"); +__RCSID("$NetBSD: getcwd.c,v 1.29 2003/08/03 04:12:01 itojun Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -49,6 +49,7 @@ __RCSID("$NetBSD: getcwd.c,v 1.11 1998/0 #include #include +#include #include #include #include @@ -57,49 +58,21 @@ __RCSID("$NetBSD: getcwd.c,v 1.11 1998/0 #include #include +#include "extern.h" + #ifdef __weak_alias -__weak_alias(getcwd,_getcwd); -__weak_alias(realpath,_realpath); +__weak_alias(getcwd,_getcwd) +__weak_alias(realpath,_realpath) #endif -static char *getcwd_physical __P((char *, size_t)); - #define ISDOT(dp) \ (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) -char * -getcwd(pt, size) - char *pt; - size_t size; -{ - char *pwd; - size_t pwdlen; - dev_t dev; - ino_t ino; - struct stat s; - /* Check $PWD -- if it's right, it's fast. */ - if ((pwd = getenv("PWD")) != NULL && pwd[0] == '/' && !stat(pwd, &s)) { - dev = s.st_dev; - ino = s.st_ino; - if (!stat(".", &s) && dev == s.st_dev && ino == s.st_ino) { - pwdlen = strlen(pwd); - if (size != 0) { - if (pwdlen + 1 > size) { - errno = ERANGE; - return (NULL); - } - } else if ((pt = malloc(pwdlen + 1)) == NULL) - return (NULL); - memmove(pt, pwd, pwdlen); - pt[pwdlen] = '\0'; - return (pt); - } - } - - return (getcwd_physical(pt, size)); -} +#if defined(__SVR4) || defined(__svr4__) +#define d_fileno d_ino +#endif /* * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); @@ -114,12 +87,15 @@ realpath(path, resolved) char *resolved; { struct stat sb; - int fd, n, rootd, serrno; + int fd, n, rootd, serrno, nlnk = 0; char *p, *q, wbuf[MAXPATHLEN]; + _DIAGASSERT(path != NULL); + _DIAGASSERT(resolved != NULL); + /* Save the starting point. */ if ((fd = open(".", O_RDONLY)) < 0) { - (void)strcpy(resolved, "."); + (void)strlcpy(resolved, ".", MAXPATHLEN); return (NULL); } @@ -131,8 +107,7 @@ realpath(path, resolved) * if it is a directory, then change to that directory. * get the current directory name and append the basename. */ - (void)strncpy(resolved, path, MAXPATHLEN - 1); - resolved[MAXPATHLEN - 1] = '\0'; + (void)strlcpy(resolved, path, MAXPATHLEN); loop: q = strrchr(resolved, '/'); if (q != NULL) { @@ -154,7 +129,11 @@ loop: /* Deal with the last component. */ if (lstat(p, &sb) == 0) { if (S_ISLNK(sb.st_mode)) { - n = readlink(p, resolved, MAXPATHLEN); + if (nlnk++ >= MAXSYMLINKS) { + errno = ELOOP; + goto err1; + } + n = readlink(p, resolved, MAXPATHLEN-1); if (n < 0) goto err1; resolved[n] = '\0'; @@ -171,13 +150,13 @@ loop: * Save the last component name and get the full pathname of * the current directory. */ - (void)strncpy(wbuf, p, (sizeof(wbuf) - 1)); + (void)strlcpy(wbuf, p, sizeof(wbuf)); /* * Call the inernal internal version of getcwd which * does a physical search rather than using the $PWD short-cut */ - if (getcwd_physical(resolved, MAXPATHLEN) == 0) + if (getcwd(resolved, MAXPATHLEN) == 0) goto err1; /* @@ -190,7 +169,8 @@ loop: rootd = 0; if (*wbuf) { - if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { + if (strlen(resolved) + strlen(wbuf) + (rootd ? 0 : 1) + 1 > + MAXPATHLEN) { errno = ENAMETOOLONG; goto err1; } @@ -216,8 +196,10 @@ err2: (void)close(fd); return (NULL); } -static char * -getcwd_physical(pt, size) +#ifdef OLD_GETCWD + +char * +getcwd(pt, size) char *pt; size_t size; { @@ -233,6 +215,7 @@ getcwd_physical(pt, size) size_t ptsize, upsize; int save_errno; char *ept, *eup, *up; + size_t dlen; /* * If no buffer specified by the user, allocate one as necessary. @@ -291,7 +274,7 @@ getcwd_physical(pt, size) * path to the beginning of the buffer, but it's always * been that way and stuff would probably break. */ - bcopy(bpt, pt, (size_t)(ept - bpt)); + memmove(pt, bpt, (size_t)(ept - bpt)); free(up); return (pt); } @@ -328,8 +311,14 @@ getcwd_physical(pt, size) for (;;) { if (!(dp = readdir(dir))) goto notfound; - if (dp->d_fileno == ino) + if (dp->d_fileno == ino) { +#if defined(__SVR4) || defined(__svr4__) || defined(__linux__) + dlen = strlen(dp->d_name); +#else + dlen = dp->d_namlen; +#endif break; + } } } else for (;;) { @@ -337,8 +326,12 @@ getcwd_physical(pt, size) goto notfound; if (ISDOT(dp)) continue; - bcopy(dp->d_name, bup, - (size_t)(dp->d_namlen + 1)); +#if defined(__SVR4) || defined(__svr4__) || defined(__linux__) + dlen = strlen(dp->d_name); +#else + dlen = dp->d_namlen; +#endif + memmove(bup, dp->d_name, dlen + 1); /* Save the first error for later. */ if (lstat(up, &s)) { @@ -355,7 +348,7 @@ getcwd_physical(pt, size) * Check for length of the current name, preceding slash, * leading slash. */ - if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) { + if (bpt - pt <= dlen + (first ? 1 : 2)) { size_t len, off; if (!ptsize) { @@ -368,13 +361,13 @@ getcwd_physical(pt, size) goto err; bpt = pt + off; ept = pt + ptsize; - bcopy(bpt, ept - len, len); + memmove(ept - len, bpt, len); bpt = ept - len; } if (!first) *--bpt = '/'; - bpt -= dp->d_namlen; - bcopy(dp->d_name, bpt, (size_t)dp->d_namlen); + bpt -= dlen; + memmove(bpt, dp->d_name, dlen); (void)closedir(dir); /* Truncate any file name. */ @@ -396,3 +389,50 @@ err: free(up); return (NULL); } + +#else /* New getcwd */ + +char * +getcwd(pt, size) + char *pt; + size_t size; +{ + size_t ptsize, bufsize; + int len; + + /* + * If no buffer specified by the user, allocate one as necessary. + * If a buffer is specified, the size has to be non-zero. The path + * is built from the end of the buffer backwards. + */ + if (pt) { + ptsize = 0; + if (!size) { + errno = EINVAL; + return (NULL); + } + bufsize = size; + } else { + if ((pt = malloc(ptsize = 1024 - 4)) == NULL) + return (NULL); + bufsize = ptsize; + } + for (;;) { + len = __getcwd(pt, bufsize); + if ((len < 0) && (size == 0) && (errno == ERANGE)) { + if (ptsize > (MAXPATHLEN*4)) + return NULL; + if ((pt = realloc(pt, ptsize *= 2)) == NULL) + return NULL; + bufsize = ptsize; + continue; + } + break; + } + if (len < 0) + return NULL; + else + return pt; +} + +#endif