[BACK]Return to getcwd.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/lib/libc/gen/getcwd.c between version 1.13 and 1.22

version 1.13, 1998/08/10 02:43:09 version 1.22, 1999/09/16 11:44:58
Line 49  __RCSID("$NetBSD$");
Line 49  __RCSID("$NetBSD$");
 #include <sys/param.h>  #include <sys/param.h>
 #include <sys/stat.h>  #include <sys/stat.h>
   
   #include <assert.h>
 #include <dirent.h>  #include <dirent.h>
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
Line 57  __RCSID("$NetBSD$");
Line 58  __RCSID("$NetBSD$");
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
   #include "extern.h"
   
 #ifdef __weak_alias  #ifdef __weak_alias
 __weak_alias(getcwd,_getcwd);  __weak_alias(getcwd,_getcwd);
 __weak_alias(realpath,_realpath);  __weak_alias(realpath,_realpath);
Line 67  __weak_alias(realpath,_realpath);
Line 70  __weak_alias(realpath,_realpath);
             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))              (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
   
   
   #if defined(__SVR4) || defined(__svr4__)
   #define d_fileno d_ino
   #endif
   
 /*  /*
  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);   * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
  *   *
Line 80  realpath(path, resolved)
Line 87  realpath(path, resolved)
         char *resolved;          char *resolved;
 {  {
         struct stat sb;          struct stat sb;
         int fd, n, rootd, serrno;          int fd, n, rootd, serrno, nlnk = 0;
         char *p, *q, wbuf[MAXPATHLEN];          char *p, *q, wbuf[MAXPATHLEN];
   
           _DIAGASSERT(path != NULL);
           _DIAGASSERT(resolved != NULL);
   #ifdef _DIAGNOSTIC
           if (path == NULL || *path == '\0') {
                   errno = ENOENT;
                   return (NULL);
           }
           if (resolved == NULL) {
                   errno = EINVAL;
                   return (NULL);
           }
   #endif
   
         /* Save the starting point. */          /* Save the starting point. */
         if ((fd = open(".", O_RDONLY)) < 0) {          if ((fd = open(".", O_RDONLY)) < 0) {
                 (void)strcpy(resolved, ".");                  (void)strcpy(resolved, ".");
Line 120  loop:
Line 140  loop:
         /* Deal with the last component. */          /* Deal with the last component. */
         if (lstat(p, &sb) == 0) {          if (lstat(p, &sb) == 0) {
                 if (S_ISLNK(sb.st_mode)) {                  if (S_ISLNK(sb.st_mode)) {
                           if (nlnk++ >= MAXSYMLINKS) {
                                   errno = ELOOP;
                                   goto err1;
                           }
                         n = readlink(p, resolved, MAXPATHLEN);                          n = readlink(p, resolved, MAXPATHLEN);
                         if (n < 0)                          if (n < 0)
                                 goto err1;                                  goto err1;
Line 182  err2: (void)close(fd);
Line 206  err2: (void)close(fd);
         return (NULL);          return (NULL);
 }  }
   
   #ifdef OLD_GETCWD
   
 char *  char *
 getcwd(pt, size)  getcwd(pt, size)
         char *pt;          char *pt;
Line 199  getcwd(pt, size)
Line 225  getcwd(pt, size)
         size_t ptsize, upsize;          size_t ptsize, upsize;
         int save_errno;          int save_errno;
         char *ept, *eup, *up;          char *ept, *eup, *up;
           size_t dlen;
   
         /*          /*
          * If no buffer specified by the user, allocate one as necessary.           * If no buffer specified by the user, allocate one as necessary.
Line 257  getcwd(pt, size)
Line 284  getcwd(pt, size)
                          * path to the beginning of the buffer, but it's always                           * path to the beginning of the buffer, but it's always
                          * been that way and stuff would probably break.                           * been that way and stuff would probably break.
                          */                           */
                         memcpy(pt, bpt,  (size_t)(ept - bpt));                          memmove(pt, bpt,  (size_t)(ept - bpt));
                         free(up);                          free(up);
                         return (pt);                          return (pt);
                 }                  }
Line 294  getcwd(pt, size)
Line 321  getcwd(pt, size)
                         for (;;) {                          for (;;) {
                                 if (!(dp = readdir(dir)))                                  if (!(dp = readdir(dir)))
                                         goto notfound;                                          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;                                          break;
                                   }
                         }                          }
                 } else                  } else
                         for (;;) {                          for (;;) {
Line 303  getcwd(pt, size)
Line 336  getcwd(pt, size)
                                         goto notfound;                                          goto notfound;
                                 if (ISDOT(dp))                                  if (ISDOT(dp))
                                         continue;                                          continue;
                                 memcpy(bup, dp->d_name,  #if defined(__SVR4) || defined(__svr4__) || defined(__linux__)
                                     (size_t)(dp->d_namlen + 1));                                  dlen = strlen(dp->d_name);
   #else
                                   dlen = dp->d_namlen;
   #endif
                                   memmove(bup, dp->d_name, dlen + 1);
   
                                 /* Save the first error for later. */                                  /* Save the first error for later. */
                                 if (lstat(up, &s)) {                                  if (lstat(up, &s)) {
Line 321  getcwd(pt, size)
Line 358  getcwd(pt, size)
                  * Check for length of the current name, preceding slash,                   * Check for length of the current name, preceding slash,
                  * leading slash.                   * leading slash.
                  */                   */
                 if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) {                  if (bpt - pt <= dlen + (first ? 1 : 2)) {
                         size_t len, off;                          size_t len, off;
   
                         if (!ptsize) {                          if (!ptsize) {
Line 334  getcwd(pt, size)
Line 371  getcwd(pt, size)
                                 goto err;                                  goto err;
                         bpt = pt + off;                          bpt = pt + off;
                         ept = pt + ptsize;                          ept = pt + ptsize;
                         memcpy(ept - len, bpt, len);                          memmove(ept - len, bpt, len);
                         bpt = ept - len;                          bpt = ept - len;
                 }                  }
                 if (!first)                  if (!first)
                         *--bpt = '/';                          *--bpt = '/';
                 bpt -= dp->d_namlen;                  bpt -= dlen;
                 memcpy(bpt, dp->d_name, (size_t)dp->d_namlen);                  memmove(bpt, dp->d_name, dlen);
                 (void)closedir(dir);                  (void)closedir(dir);
   
                 /* Truncate any file name. */                  /* Truncate any file name. */
Line 362  err:
Line 399  err:
         free(up);          free(up);
         return (NULL);          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

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.22

CVSweb <webmaster@jp.NetBSD.org>