Home | History | Annotate | Line # | Download | only in gen
getcwd.c revision 1.1.1.3
      1      1.1    cgd /*
      2  1.1.1.3  perry  * Copyright (c) 1989, 1991, 1993, 1995
      3  1.1.1.2    cgd  *	The Regents of the University of California.  All rights reserved.
      4      1.1    cgd  *
      5  1.1.1.3  perry  * This code is derived from software contributed to Berkeley by
      6  1.1.1.3  perry  * Jan-Simon Pendry.
      7  1.1.1.3  perry  *
      8      1.1    cgd  * Redistribution and use in source and binary forms, with or without
      9      1.1    cgd  * modification, are permitted provided that the following conditions
     10      1.1    cgd  * are met:
     11      1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     12      1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     13      1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     15      1.1    cgd  *    documentation and/or other materials provided with the distribution.
     16      1.1    cgd  * 3. All advertising materials mentioning features or use of this software
     17      1.1    cgd  *    must display the following acknowledgement:
     18      1.1    cgd  *	This product includes software developed by the University of
     19      1.1    cgd  *	California, Berkeley and its contributors.
     20      1.1    cgd  * 4. Neither the name of the University nor the names of its contributors
     21      1.1    cgd  *    may be used to endorse or promote products derived from this software
     22      1.1    cgd  *    without specific prior written permission.
     23      1.1    cgd  *
     24      1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25      1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26      1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.1    cgd  * SUCH DAMAGE.
     35      1.1    cgd  */
     36      1.1    cgd 
     37      1.1    cgd #if defined(LIBC_SCCS) && !defined(lint)
     38  1.1.1.3  perry static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
     39      1.1    cgd #endif /* LIBC_SCCS and not lint */
     40      1.1    cgd 
     41      1.1    cgd #include <sys/param.h>
     42      1.1    cgd #include <sys/stat.h>
     43  1.1.1.2    cgd 
     44      1.1    cgd #include <dirent.h>
     45  1.1.1.3  perry #include <errno.h>
     46  1.1.1.3  perry #include <fcntl.h>
     47      1.1    cgd #include <stdio.h>
     48      1.1    cgd #include <stdlib.h>
     49      1.1    cgd #include <string.h>
     50      1.1    cgd #include <unistd.h>
     51      1.1    cgd 
     52  1.1.1.3  perry static char *getcwd_physical __P((char *, size_t));
     53  1.1.1.3  perry 
     54      1.1    cgd #define	ISDOT(dp) \
     55      1.1    cgd 	(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
     56      1.1    cgd 	    dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
     57      1.1    cgd 
     58      1.1    cgd char *
     59      1.1    cgd getcwd(pt, size)
     60      1.1    cgd 	char *pt;
     61      1.1    cgd 	size_t size;
     62      1.1    cgd {
     63  1.1.1.3  perry 	char *pwd;
     64  1.1.1.3  perry 	size_t pwdlen;
     65  1.1.1.3  perry 	dev_t dev;
     66  1.1.1.3  perry 	ino_t ino;
     67  1.1.1.3  perry 	struct stat s;
     68  1.1.1.3  perry 
     69  1.1.1.3  perry 	/* Check $PWD -- if it's right, it's fast. */
     70  1.1.1.3  perry 	if ((pwd = getenv("PWD")) != NULL && pwd[0] == '/' && !stat(pwd, &s)) {
     71  1.1.1.3  perry 		dev = s.st_dev;
     72  1.1.1.3  perry 		ino = s.st_ino;
     73  1.1.1.3  perry 		if (!stat(".", &s) && dev == s.st_dev && ino == s.st_ino) {
     74  1.1.1.3  perry 			pwdlen = strlen(pwd);
     75  1.1.1.3  perry 			if (size != 0) {
     76  1.1.1.3  perry 				if (pwdlen + 1 > size) {
     77  1.1.1.3  perry 					errno = ERANGE;
     78  1.1.1.3  perry 					return (NULL);
     79  1.1.1.3  perry 				}
     80  1.1.1.3  perry 			} else if ((pt = malloc(pwdlen + 1)) == NULL)
     81  1.1.1.3  perry 				return (NULL);
     82  1.1.1.3  perry 			memmove(pt, pwd, pwdlen);
     83  1.1.1.3  perry 			pt[pwdlen] = '\0';
     84  1.1.1.3  perry 			return (pt);
     85  1.1.1.3  perry 		}
     86  1.1.1.3  perry 	}
     87  1.1.1.3  perry 
     88  1.1.1.3  perry 	return (getcwd_physical(pt, size));
     89  1.1.1.3  perry }
     90  1.1.1.3  perry 
     91  1.1.1.3  perry /*
     92  1.1.1.3  perry  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
     93  1.1.1.3  perry  *
     94  1.1.1.3  perry  * Find the real name of path, by removing all ".", ".." and symlink
     95  1.1.1.3  perry  * components.  Returns (resolved) on success, or (NULL) on failure,
     96  1.1.1.3  perry  * in which case the path which caused trouble is left in (resolved).
     97  1.1.1.3  perry  */
     98  1.1.1.3  perry char *
     99  1.1.1.3  perry realpath(path, resolved)
    100  1.1.1.3  perry 	const char *path;
    101  1.1.1.3  perry 	char *resolved;
    102  1.1.1.3  perry {
    103  1.1.1.3  perry 	struct stat sb;
    104  1.1.1.3  perry 	int fd, n, rootd, serrno;
    105  1.1.1.3  perry 	char *p, *q, wbuf[MAXPATHLEN];
    106  1.1.1.3  perry 
    107  1.1.1.3  perry 	/* Save the starting point. */
    108  1.1.1.3  perry 	if ((fd = open(".", O_RDONLY)) < 0) {
    109  1.1.1.3  perry 		(void)strcpy(resolved, ".");
    110  1.1.1.3  perry 		return (NULL);
    111  1.1.1.3  perry 	}
    112  1.1.1.3  perry 
    113  1.1.1.3  perry 	/*
    114  1.1.1.3  perry 	 * Find the dirname and basename from the path to be resolved.
    115  1.1.1.3  perry 	 * Change directory to the dirname component.
    116  1.1.1.3  perry 	 * lstat the basename part.
    117  1.1.1.3  perry 	 *     if it is a symlink, read in the value and loop.
    118  1.1.1.3  perry 	 *     if it is a directory, then change to that directory.
    119  1.1.1.3  perry 	 * get the current directory name and append the basename.
    120  1.1.1.3  perry 	 */
    121  1.1.1.3  perry 	(void)strncpy(resolved, path, MAXPATHLEN - 1);
    122  1.1.1.3  perry 	resolved[MAXPATHLEN - 1] = '\0';
    123  1.1.1.3  perry loop:
    124  1.1.1.3  perry 	q = strrchr(resolved, '/');
    125  1.1.1.3  perry 	if (q != NULL) {
    126  1.1.1.3  perry 		p = q + 1;
    127  1.1.1.3  perry 		if (q == resolved)
    128  1.1.1.3  perry 			q = "/";
    129  1.1.1.3  perry 		else {
    130  1.1.1.3  perry 			do {
    131  1.1.1.3  perry 				--q;
    132  1.1.1.3  perry 			} while (q > resolved && *q == '/');
    133  1.1.1.3  perry 			q[1] = '\0';
    134  1.1.1.3  perry 			q = resolved;
    135  1.1.1.3  perry 		}
    136  1.1.1.3  perry 		if (chdir(q) < 0)
    137  1.1.1.3  perry 			goto err1;
    138  1.1.1.3  perry 	} else
    139  1.1.1.3  perry 		p = resolved;
    140  1.1.1.3  perry 
    141  1.1.1.3  perry 	/* Deal with the last component. */
    142  1.1.1.3  perry 	if (lstat(p, &sb) == 0) {
    143  1.1.1.3  perry 		if (S_ISLNK(sb.st_mode)) {
    144  1.1.1.3  perry 			n = readlink(p, resolved, MAXPATHLEN);
    145  1.1.1.3  perry 			if (n < 0)
    146  1.1.1.3  perry 				goto err1;
    147  1.1.1.3  perry 			resolved[n] = '\0';
    148  1.1.1.3  perry 			goto loop;
    149  1.1.1.3  perry 		}
    150  1.1.1.3  perry 		if (S_ISDIR(sb.st_mode)) {
    151  1.1.1.3  perry 			if (chdir(p) < 0)
    152  1.1.1.3  perry 				goto err1;
    153  1.1.1.3  perry 			p = "";
    154  1.1.1.3  perry 		}
    155  1.1.1.3  perry 	}
    156  1.1.1.3  perry 
    157  1.1.1.3  perry 	/*
    158  1.1.1.3  perry 	 * Save the last component name and get the full pathname of
    159  1.1.1.3  perry 	 * the current directory.
    160  1.1.1.3  perry 	 */
    161  1.1.1.3  perry 	(void)strcpy(wbuf, p);
    162  1.1.1.3  perry 
    163  1.1.1.3  perry 	/*
    164  1.1.1.3  perry 	 * Call the inernal internal version of getcwd which
    165  1.1.1.3  perry 	 * does a physical search rather than using the $PWD short-cut
    166  1.1.1.3  perry 	 */
    167  1.1.1.3  perry 	if (getcwd_physical(resolved, MAXPATHLEN) == 0)
    168  1.1.1.3  perry 		goto err1;
    169  1.1.1.3  perry 
    170  1.1.1.3  perry 	/*
    171  1.1.1.3  perry 	 * Join the two strings together, ensuring that the right thing
    172  1.1.1.3  perry 	 * happens if the last component is empty, or the dirname is root.
    173  1.1.1.3  perry 	 */
    174  1.1.1.3  perry 	if (resolved[0] == '/' && resolved[1] == '\0')
    175  1.1.1.3  perry 		rootd = 1;
    176  1.1.1.3  perry 	else
    177  1.1.1.3  perry 		rootd = 0;
    178  1.1.1.3  perry 
    179  1.1.1.3  perry 	if (*wbuf) {
    180  1.1.1.3  perry 		if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) {
    181  1.1.1.3  perry 			errno = ENAMETOOLONG;
    182  1.1.1.3  perry 			goto err1;
    183  1.1.1.3  perry 		}
    184  1.1.1.3  perry 		if (rootd == 0)
    185  1.1.1.3  perry 			(void)strcat(resolved, "/");
    186  1.1.1.3  perry 		(void)strcat(resolved, wbuf);
    187  1.1.1.3  perry 	}
    188  1.1.1.3  perry 
    189  1.1.1.3  perry 	/* Go back to where we came from. */
    190  1.1.1.3  perry 	if (fchdir(fd) < 0) {
    191  1.1.1.3  perry 		serrno = errno;
    192  1.1.1.3  perry 		goto err2;
    193  1.1.1.3  perry 	}
    194  1.1.1.3  perry 
    195  1.1.1.3  perry 	/* It's okay if the close fails, what's an fd more or less? */
    196  1.1.1.3  perry 	(void)close(fd);
    197  1.1.1.3  perry 	return (resolved);
    198  1.1.1.3  perry 
    199  1.1.1.3  perry err1:	serrno = errno;
    200  1.1.1.3  perry 	(void)fchdir(fd);
    201  1.1.1.3  perry err2:	(void)close(fd);
    202  1.1.1.3  perry 	errno = serrno;
    203  1.1.1.3  perry 	return (NULL);
    204  1.1.1.3  perry }
    205  1.1.1.3  perry 
    206  1.1.1.3  perry static char *
    207  1.1.1.3  perry getcwd_physical(pt, size)
    208  1.1.1.3  perry 	char *pt;
    209  1.1.1.3  perry 	size_t size;
    210  1.1.1.3  perry {
    211      1.1    cgd 	register struct dirent *dp;
    212      1.1    cgd 	register DIR *dir;
    213      1.1    cgd 	register dev_t dev;
    214      1.1    cgd 	register ino_t ino;
    215      1.1    cgd 	register int first;
    216      1.1    cgd 	register char *bpt, *bup;
    217      1.1    cgd 	struct stat s;
    218      1.1    cgd 	dev_t root_dev;
    219      1.1    cgd 	ino_t root_ino;
    220      1.1    cgd 	size_t ptsize, upsize;
    221      1.1    cgd 	int save_errno;
    222      1.1    cgd 	char *ept, *eup, *up;
    223      1.1    cgd 
    224      1.1    cgd 	/*
    225      1.1    cgd 	 * If no buffer specified by the user, allocate one as necessary.
    226      1.1    cgd 	 * If a buffer is specified, the size has to be non-zero.  The path
    227      1.1    cgd 	 * is built from the end of the buffer backwards.
    228      1.1    cgd 	 */
    229      1.1    cgd 	if (pt) {
    230      1.1    cgd 		ptsize = 0;
    231      1.1    cgd 		if (!size) {
    232      1.1    cgd 			errno = EINVAL;
    233  1.1.1.2    cgd 			return (NULL);
    234      1.1    cgd 		}
    235      1.1    cgd 		ept = pt + size;
    236      1.1    cgd 	} else {
    237  1.1.1.2    cgd 		if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
    238  1.1.1.2    cgd 			return (NULL);
    239      1.1    cgd 		ept = pt + ptsize;
    240      1.1    cgd 	}
    241      1.1    cgd 	bpt = ept - 1;
    242      1.1    cgd 	*bpt = '\0';
    243      1.1    cgd 
    244      1.1    cgd 	/*
    245      1.1    cgd 	 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
    246      1.1    cgd 	 * Should always be enough (it's 340 levels).  If it's not, allocate
    247  1.1.1.3  perry 	 * as necessary.  Special case the first stat, it's ".", not "..".
    248      1.1    cgd 	 */
    249  1.1.1.2    cgd 	if ((up = malloc(upsize = 1024 - 4)) == NULL)
    250      1.1    cgd 		goto err;
    251      1.1    cgd 	eup = up + MAXPATHLEN;
    252      1.1    cgd 	bup = up;
    253      1.1    cgd 	up[0] = '.';
    254      1.1    cgd 	up[1] = '\0';
    255      1.1    cgd 
    256      1.1    cgd 	/* Save root values, so know when to stop. */
    257      1.1    cgd 	if (stat("/", &s))
    258      1.1    cgd 		goto err;
    259      1.1    cgd 	root_dev = s.st_dev;
    260      1.1    cgd 	root_ino = s.st_ino;
    261      1.1    cgd 
    262      1.1    cgd 	errno = 0;			/* XXX readdir has no error return. */
    263      1.1    cgd 
    264      1.1    cgd 	for (first = 1;; first = 0) {
    265      1.1    cgd 		/* Stat the current level. */
    266      1.1    cgd 		if (lstat(up, &s))
    267      1.1    cgd 			goto err;
    268      1.1    cgd 
    269      1.1    cgd 		/* Save current node values. */
    270      1.1    cgd 		ino = s.st_ino;
    271      1.1    cgd 		dev = s.st_dev;
    272      1.1    cgd 
    273      1.1    cgd 		/* Check for reaching root. */
    274      1.1    cgd 		if (root_dev == dev && root_ino == ino) {
    275      1.1    cgd 			*--bpt = '/';
    276      1.1    cgd 			/*
    277      1.1    cgd 			 * It's unclear that it's a requirement to copy the
    278      1.1    cgd 			 * path to the beginning of the buffer, but it's always
    279      1.1    cgd 			 * been that way and stuff would probably break.
    280      1.1    cgd 			 */
    281      1.1    cgd 			(void)bcopy(bpt, pt, ept - bpt);
    282      1.1    cgd 			free(up);
    283  1.1.1.2    cgd 			return (pt);
    284      1.1    cgd 		}
    285      1.1    cgd 
    286      1.1    cgd 		/*
    287      1.1    cgd 		 * Build pointer to the parent directory, allocating memory
    288      1.1    cgd 		 * as necessary.  Max length is 3 for "../", the largest
    289      1.1    cgd 		 * possible component name, plus a trailing NULL.
    290      1.1    cgd 		 */
    291      1.1    cgd 		if (bup + 3  + MAXNAMLEN + 1 >= eup) {
    292  1.1.1.2    cgd 			if ((up = realloc(up, upsize *= 2)) == NULL)
    293      1.1    cgd 				goto err;
    294  1.1.1.2    cgd 			bup = up;
    295      1.1    cgd 			eup = up + upsize;
    296      1.1    cgd 		}
    297      1.1    cgd 		*bup++ = '.';
    298      1.1    cgd 		*bup++ = '.';
    299      1.1    cgd 		*bup = '\0';
    300      1.1    cgd 
    301      1.1    cgd 		/* Open and stat parent directory. */
    302      1.1    cgd 		if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
    303      1.1    cgd 			goto err;
    304      1.1    cgd 
    305      1.1    cgd 		/* Add trailing slash for next directory. */
    306      1.1    cgd 		*bup++ = '/';
    307      1.1    cgd 
    308      1.1    cgd 		/*
    309      1.1    cgd 		 * If it's a mount point, have to stat each element because
    310      1.1    cgd 		 * the inode number in the directory is for the entry in the
    311      1.1    cgd 		 * parent directory, not the inode number of the mounted file.
    312      1.1    cgd 		 */
    313      1.1    cgd 		save_errno = 0;
    314      1.1    cgd 		if (s.st_dev == dev) {
    315      1.1    cgd 			for (;;) {
    316      1.1    cgd 				if (!(dp = readdir(dir)))
    317      1.1    cgd 					goto notfound;
    318      1.1    cgd 				if (dp->d_fileno == ino)
    319      1.1    cgd 					break;
    320      1.1    cgd 			}
    321      1.1    cgd 		} else
    322      1.1    cgd 			for (;;) {
    323      1.1    cgd 				if (!(dp = readdir(dir)))
    324      1.1    cgd 					goto notfound;
    325      1.1    cgd 				if (ISDOT(dp))
    326      1.1    cgd 					continue;
    327      1.1    cgd 				bcopy(dp->d_name, bup, dp->d_namlen + 1);
    328      1.1    cgd 
    329      1.1    cgd 				/* Save the first error for later. */
    330      1.1    cgd 				if (lstat(up, &s)) {
    331      1.1    cgd 					if (!save_errno)
    332      1.1    cgd 						save_errno = errno;
    333      1.1    cgd 					errno = 0;
    334      1.1    cgd 					continue;
    335      1.1    cgd 				}
    336      1.1    cgd 				if (s.st_dev == dev && s.st_ino == ino)
    337      1.1    cgd 					break;
    338      1.1    cgd 			}
    339      1.1    cgd 
    340      1.1    cgd 		/*
    341      1.1    cgd 		 * Check for length of the current name, preceding slash,
    342      1.1    cgd 		 * leading slash.
    343      1.1    cgd 		 */
    344      1.1    cgd 		if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) {
    345      1.1    cgd 			size_t len, off;
    346      1.1    cgd 
    347      1.1    cgd 			if (!ptsize) {
    348      1.1    cgd 				errno = ERANGE;
    349      1.1    cgd 				goto err;
    350      1.1    cgd 			}
    351      1.1    cgd 			off = bpt - pt;
    352      1.1    cgd 			len = ept - bpt;
    353  1.1.1.2    cgd 			if ((pt = realloc(pt, ptsize *= 2)) == NULL)
    354      1.1    cgd 				goto err;
    355      1.1    cgd 			bpt = pt + off;
    356      1.1    cgd 			ept = pt + ptsize;
    357      1.1    cgd 			(void)bcopy(bpt, ept - len, len);
    358      1.1    cgd 			bpt = ept - len;
    359      1.1    cgd 		}
    360      1.1    cgd 		if (!first)
    361      1.1    cgd 			*--bpt = '/';
    362      1.1    cgd 		bpt -= dp->d_namlen;
    363      1.1    cgd 		bcopy(dp->d_name, bpt, dp->d_namlen);
    364      1.1    cgd 		(void)closedir(dir);
    365      1.1    cgd 
    366      1.1    cgd 		/* Truncate any file name. */
    367      1.1    cgd 		*bup = '\0';
    368      1.1    cgd 	}
    369      1.1    cgd 
    370      1.1    cgd notfound:
    371      1.1    cgd 	/*
    372      1.1    cgd 	 * If readdir set errno, use it, not any saved error; otherwise,
    373      1.1    cgd 	 * didn't find the current directory in its parent directory, set
    374      1.1    cgd 	 * errno to ENOENT.
    375      1.1    cgd 	 */
    376      1.1    cgd 	if (!errno)
    377      1.1    cgd 		errno = save_errno ? save_errno : ENOENT;
    378      1.1    cgd 	/* FALLTHROUGH */
    379      1.1    cgd err:
    380      1.1    cgd 	if (ptsize)
    381      1.1    cgd 		free(pt);
    382      1.1    cgd 	free(up);
    383  1.1.1.2    cgd 	return (NULL);
    384      1.1    cgd }
    385