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