Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

getcwd.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1989, 1991, 1993
00009  *      The Regents of the University of California.  All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the University nor the names of its contributors
00020  *    may be used to endorse or promote products derived from this software
00021  *    without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  *
00035  * $Id: getcwd.c,v 12.1 2005/06/16 20:20:48 bostic Exp $
00036  */
00037 
00038 #include "db_config.h"
00039 
00040 #ifndef NO_SYSTEM_INCLUDES
00041 #include <sys/types.h>
00042 #include <sys/stat.h>
00043 
00044 #if HAVE_DIRENT_H
00045 # include <dirent.h>
00046 # define NAMLEN(dirent) strlen((dirent)->d_name)
00047 #else
00048 # define dirent direct
00049 # define NAMLEN(dirent) (dirent)->d_namlen
00050 # if HAVE_SYS_NDIR_H
00051 #  include <sys/ndir.h>
00052 # endif
00053 # if HAVE_SYS_DIR_H
00054 #  include <sys/dir.h>
00055 # endif
00056 # if HAVE_NDIR_H
00057 #  include <ndir.h>
00058 # endif
00059 #endif
00060 
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 #include <string.h>
00064 #include <unistd.h>
00065 #endif
00066 
00067 #include "db_int.h"
00068 
00069 #define ISDOT(dp) \
00070         (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
00071             (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
00072 
00073 #ifndef dirfd
00074 #define   dirfd(dirp)     ((dirp)->dd_fd)
00075 #endif
00076 
00077 /*
00078  * getcwd --
00079  *      Get the current working directory.
00080  *
00081  * PUBLIC: #ifndef HAVE_GETCWD
00082  * PUBLIC: char *getcwd __P((char *, size_t));
00083  * PUBLIC: #endif
00084  */
00085 char *
00086 getcwd(pt, size)
00087         char *pt;
00088         size_t size;
00089 {
00090         register struct dirent *dp;
00091         register DIR *dir;
00092         register dev_t dev;
00093         register ino_t ino;
00094         register int first;
00095         register char *bpt, *bup;
00096         struct stat s;
00097         dev_t root_dev;
00098         ino_t root_ino;
00099         size_t ptsize, upsize;
00100         int ret, save_errno;
00101         char *ept, *eup, *up;
00102 
00103         /*
00104          * If no buffer specified by the user, allocate one as necessary.
00105          * If a buffer is specified, the size has to be non-zero.  The path
00106          * is built from the end of the buffer backwards.
00107          */
00108         if (pt) {
00109                 ptsize = 0;
00110                 if (!size) {
00111                         __os_set_errno(EINVAL);
00112                         return (NULL);
00113                 }
00114                 if (size == 1) {
00115                         __os_set_errno(ERANGE);
00116                         return (NULL);
00117                 }
00118                 ept = pt + size;
00119         } else {
00120                 if ((ret =
00121                     __os_malloc(NULL, ptsize = 1024 - 4, &pt)) != 0) {
00122                         __os_set_errno(ret);
00123                         return (NULL);
00124                 }
00125                 ept = pt + ptsize;
00126         }
00127         bpt = ept - 1;
00128         *bpt = '\0';
00129 
00130         /*
00131          * Allocate bytes (1024 - malloc space) for the string of "../"'s.
00132          * Should always be enough (it's 340 levels).  If it's not, allocate
00133          * as necessary.  Special case the first stat, it's ".", not "..".
00134          */
00135         if ((ret = __os_malloc(NULL, upsize = 1024 - 4, &up)) != 0)
00136                 goto err;
00137         eup = up + 1024;
00138         bup = up;
00139         up[0] = '.';
00140         up[1] = '\0';
00141 
00142         /* Save root values, so know when to stop. */
00143         if (stat("/", &s))
00144                 goto err;
00145         root_dev = s.st_dev;
00146         root_ino = s.st_ino;
00147 
00148         __os_set_errno(0);              /* XXX readdir has no error return. */
00149 
00150         for (first = 1;; first = 0) {
00151                 /* Stat the current level. */
00152                 if (lstat(up, &s))
00153                         goto err;
00154 
00155                 /* Save current node values. */
00156                 ino = s.st_ino;
00157                 dev = s.st_dev;
00158 
00159                 /* Check for reaching root. */
00160                 if (root_dev == dev && root_ino == ino) {
00161                         *--bpt = PATH_SEPARATOR[0];
00162                         /*
00163                          * It's unclear that it's a requirement to copy the
00164                          * path to the beginning of the buffer, but it's always
00165                          * been that way and stuff would probably break.
00166                          */
00167                         bcopy(bpt, pt, ept - bpt);
00168                         __os_free(NULL, up);
00169                         return (pt);
00170                 }
00171 
00172                 /*
00173                  * Build pointer to the parent directory, allocating memory
00174                  * as necessary.  Max length is 3 for "../", the largest
00175                  * possible component name, plus a trailing NULL.
00176                  */
00177                 if (bup + 3  + MAXNAMLEN + 1 >= eup) {
00178                         if (__os_realloc(NULL, upsize *= 2, &up) != 0)
00179                                 goto err;
00180                         bup = up;
00181                         eup = up + upsize;
00182                 }
00183                 *bup++ = '.';
00184                 *bup++ = '.';
00185                 *bup = '\0';
00186 
00187                 /* Open and stat parent directory. */
00188                 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
00189                         goto err;
00190 
00191                 /* Add trailing slash for next directory. */
00192                 *bup++ = PATH_SEPARATOR[0];
00193 
00194                 /*
00195                  * If it's a mount point, have to stat each element because
00196                  * the inode number in the directory is for the entry in the
00197                  * parent directory, not the inode number of the mounted file.
00198                  */
00199                 save_errno = 0;
00200                 if (s.st_dev == dev) {
00201                         for (;;) {
00202                                 if (!(dp = readdir(dir)))
00203                                         goto notfound;
00204                                 if (dp->d_fileno == ino)
00205                                         break;
00206                         }
00207                 } else
00208                         for (;;) {
00209                                 if (!(dp = readdir(dir)))
00210                                         goto notfound;
00211                                 if (ISDOT(dp))
00212                                         continue;
00213                                 bcopy(dp->d_name, bup, dp->d_namlen + 1);
00214 
00215                                 /* Save the first error for later. */
00216                                 if (lstat(up, &s)) {
00217                                         if (save_errno == 0)
00218                                                 save_errno = __os_get_errno();
00219                                         __os_set_errno(0);
00220                                         continue;
00221                                 }
00222                                 if (s.st_dev == dev && s.st_ino == ino)
00223                                         break;
00224                         }
00225 
00226                 /*
00227                  * Check for length of the current name, preceding slash,
00228                  * leading slash.
00229                  */
00230                 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
00231                         size_t len, off;
00232 
00233                         if (!ptsize) {
00234                                 __os_set_errno(ERANGE);
00235                                 goto err;
00236                         }
00237                         off = bpt - pt;
00238                         len = ept - bpt;
00239                         if (__os_realloc(NULL, ptsize *= 2, &pt) != 0)
00240                                 goto err;
00241                         bpt = pt + off;
00242                         ept = pt + ptsize;
00243                         bcopy(bpt, ept - len, len);
00244                         bpt = ept - len;
00245                 }
00246                 if (!first)
00247                         *--bpt = PATH_SEPARATOR[0];
00248                 bpt -= dp->d_namlen;
00249                 bcopy(dp->d_name, bpt, dp->d_namlen);
00250                 (void)closedir(dir);
00251 
00252                 /* Truncate any file name. */
00253                 *bup = '\0';
00254         }
00255 
00256 notfound:
00257         /*
00258          * If readdir set errno, use it, not any saved error; otherwise,
00259          * didn't find the current directory in its parent directory, set
00260          * errno to ENOENT.
00261          */
00262         if (__os_get_errno_ret_zero() == 0)
00263                 __os_set_errno(save_errno == 0 ? ENOENT : save_errno);
00264         /* FALLTHROUGH */
00265 err:
00266         if (ptsize)
00267                 __os_free(NULL, pt);
00268         __os_free(NULL, up);
00269         return (NULL);
00270 }

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2