Home | History | Annotate | Line # | Download | only in gen
getcwd.c revision 1.35
      1  1.35     enami /*	$NetBSD: getcwd.c,v 1.35 2005/01/23 01:00:51 enami 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.32       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.6  christos #include <sys/cdefs.h>
     36   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     37   1.4       cgd #if 0
     38   1.8     perry static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
     39   1.4       cgd #else
     40  1.35     enami __RCSID("$NetBSD: getcwd.c,v 1.35 2005/01/23 01:00:51 enami Exp $");
     41   1.4       cgd #endif
     42   1.1       cgd #endif /* LIBC_SCCS and not lint */
     43   1.1       cgd 
     44   1.7       jtc #include "namespace.h"
     45   1.1       cgd #include <sys/param.h>
     46   1.1       cgd #include <sys/stat.h>
     47   1.8     perry 
     48  1.22     lukem #include <assert.h>
     49   1.8     perry #include <dirent.h>
     50   1.1       cgd #include <errno.h>
     51   1.8     perry #include <fcntl.h>
     52   1.1       cgd #include <stdio.h>
     53   1.1       cgd #include <stdlib.h>
     54   1.1       cgd #include <string.h>
     55   1.1       cgd #include <unistd.h>
     56  1.17  sommerfe 
     57  1.17  sommerfe #include "extern.h"
     58   1.7       jtc 
     59   1.7       jtc #ifdef __weak_alias
     60  1.24   mycroft __weak_alias(getcwd,_getcwd)
     61  1.24   mycroft __weak_alias(realpath,_realpath)
     62   1.7       jtc #endif
     63   1.1       cgd 
     64   1.8     perry /*
     65   1.8     perry  * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
     66   1.8     perry  *
     67   1.8     perry  * Find the real name of path, by removing all ".", ".." and symlink
     68   1.8     perry  * components.  Returns (resolved) on success, or (NULL) on failure,
     69   1.8     perry  * in which case the path which caused trouble is left in (resolved).
     70   1.8     perry  */
     71   1.8     perry char *
     72   1.8     perry realpath(path, resolved)
     73   1.8     perry 	const char *path;
     74   1.8     perry 	char *resolved;
     75   1.8     perry {
     76   1.8     perry 	struct stat sb;
     77  1.21      fvdl 	int fd, n, rootd, serrno, nlnk = 0;
     78   1.8     perry 	char *p, *q, wbuf[MAXPATHLEN];
     79  1.22     lukem 
     80  1.22     lukem 	_DIAGASSERT(path != NULL);
     81  1.22     lukem 	_DIAGASSERT(resolved != NULL);
     82   1.8     perry 
     83   1.8     perry 	/* Save the starting point. */
     84   1.8     perry 	if ((fd = open(".", O_RDONLY)) < 0) {
     85  1.28    itojun 		(void)strlcpy(resolved, ".", MAXPATHLEN);
     86   1.8     perry 		return (NULL);
     87   1.8     perry 	}
     88   1.8     perry 
     89   1.8     perry 	/*
     90   1.8     perry 	 * Find the dirname and basename from the path to be resolved.
     91   1.8     perry 	 * Change directory to the dirname component.
     92   1.8     perry 	 * lstat the basename part.
     93   1.8     perry 	 *     if it is a symlink, read in the value and loop.
     94   1.8     perry 	 *     if it is a directory, then change to that directory.
     95   1.8     perry 	 * get the current directory name and append the basename.
     96   1.8     perry 	 */
     97  1.31    itojun 	if (strlcpy(resolved, path, MAXPATHLEN) >= MAXPATHLEN) {
     98  1.31    itojun 		errno = ENAMETOOLONG;
     99  1.31    itojun 		goto err1;
    100  1.31    itojun 	}
    101   1.8     perry loop:
    102   1.8     perry 	q = strrchr(resolved, '/');
    103   1.8     perry 	if (q != NULL) {
    104   1.8     perry 		p = q + 1;
    105   1.8     perry 		if (q == resolved)
    106   1.8     perry 			q = "/";
    107   1.8     perry 		else {
    108   1.8     perry 			do {
    109   1.8     perry 				--q;
    110   1.8     perry 			} while (q > resolved && *q == '/');
    111   1.8     perry 			q[1] = '\0';
    112   1.8     perry 			q = resolved;
    113   1.8     perry 		}
    114   1.8     perry 		if (chdir(q) < 0)
    115   1.8     perry 			goto err1;
    116   1.8     perry 	} else
    117   1.8     perry 		p = resolved;
    118   1.8     perry 
    119   1.8     perry 	/* Deal with the last component. */
    120   1.8     perry 	if (lstat(p, &sb) == 0) {
    121   1.8     perry 		if (S_ISLNK(sb.st_mode)) {
    122  1.21      fvdl 			if (nlnk++ >= MAXSYMLINKS) {
    123  1.21      fvdl 				errno = ELOOP;
    124  1.21      fvdl 				goto err1;
    125  1.21      fvdl 			}
    126  1.26    provos 			n = readlink(p, resolved, MAXPATHLEN-1);
    127   1.8     perry 			if (n < 0)
    128   1.8     perry 				goto err1;
    129   1.8     perry 			resolved[n] = '\0';
    130   1.8     perry 			goto loop;
    131   1.8     perry 		}
    132   1.8     perry 		if (S_ISDIR(sb.st_mode)) {
    133   1.8     perry 			if (chdir(p) < 0)
    134   1.8     perry 				goto err1;
    135   1.8     perry 			p = "";
    136   1.8     perry 		}
    137   1.8     perry 	}
    138   1.8     perry 
    139   1.8     perry 	/*
    140   1.8     perry 	 * Save the last component name and get the full pathname of
    141   1.8     perry 	 * the current directory.
    142   1.8     perry 	 */
    143  1.31    itojun 	if (strlcpy(wbuf, p, sizeof(wbuf)) >= sizeof(wbuf)) {
    144  1.31    itojun 		errno = ENAMETOOLONG;
    145  1.31    itojun 		goto err1;
    146  1.31    itojun 	}
    147   1.8     perry 
    148   1.8     perry 	/*
    149   1.8     perry 	 * Call the inernal internal version of getcwd which
    150   1.8     perry 	 * does a physical search rather than using the $PWD short-cut
    151   1.8     perry 	 */
    152  1.12     lukem 	if (getcwd(resolved, MAXPATHLEN) == 0)
    153   1.8     perry 		goto err1;
    154   1.8     perry 
    155   1.8     perry 	/*
    156   1.8     perry 	 * Join the two strings together, ensuring that the right thing
    157   1.8     perry 	 * happens if the last component is empty, or the dirname is root.
    158   1.8     perry 	 */
    159   1.8     perry 	if (resolved[0] == '/' && resolved[1] == '\0')
    160   1.8     perry 		rootd = 1;
    161   1.8     perry 	else
    162   1.8     perry 		rootd = 0;
    163   1.8     perry 
    164   1.8     perry 	if (*wbuf) {
    165  1.29    itojun 		if (strlen(resolved) + strlen(wbuf) + (rootd ? 0 : 1) + 1 >
    166  1.29    itojun 		    MAXPATHLEN) {
    167   1.8     perry 			errno = ENAMETOOLONG;
    168   1.8     perry 			goto err1;
    169   1.8     perry 		}
    170   1.8     perry 		if (rootd == 0)
    171  1.31    itojun 			if (strlcat(resolved, "/", MAXPATHLEN) >= MAXPATHLEN) {
    172  1.31    itojun 				errno = ENAMETOOLONG;
    173  1.31    itojun 				goto err1;
    174  1.31    itojun 			}
    175  1.31    itojun 		if (strlcat(resolved, wbuf, MAXPATHLEN) >= MAXPATHLEN) {
    176  1.31    itojun 			errno = ENAMETOOLONG;
    177  1.31    itojun 			goto err1;
    178  1.31    itojun 		}
    179   1.8     perry 	}
    180   1.8     perry 
    181   1.8     perry 	/* Go back to where we came from. */
    182   1.8     perry 	if (fchdir(fd) < 0) {
    183   1.8     perry 		serrno = errno;
    184   1.8     perry 		goto err2;
    185   1.8     perry 	}
    186   1.8     perry 
    187   1.8     perry 	/* It's okay if the close fails, what's an fd more or less? */
    188   1.8     perry 	(void)close(fd);
    189   1.8     perry 	return (resolved);
    190   1.8     perry 
    191   1.8     perry err1:	serrno = errno;
    192   1.8     perry 	(void)fchdir(fd);
    193   1.8     perry err2:	(void)close(fd);
    194   1.8     perry 	errno = serrno;
    195   1.8     perry 	return (NULL);
    196   1.8     perry }
    197   1.8     perry 
    198  1.16  sommerfe char *
    199  1.35     enami getcwd(char *pt, size_t size)
    200  1.16  sommerfe {
    201  1.35     enami 	char *npt;
    202  1.35     enami 
    203  1.16  sommerfe 	/*
    204  1.35     enami 	 * If a buffer is specified, the size has to be non-zero.
    205  1.16  sommerfe 	 */
    206  1.35     enami 	if (pt != NULL) {
    207  1.35     enami 		if (size == 0) {
    208  1.35     enami 			/* __getcwd(pt, 0) results ERANGE. */
    209  1.16  sommerfe 			errno = EINVAL;
    210  1.16  sommerfe 			return (NULL);
    211  1.16  sommerfe 		}
    212  1.35     enami 		if (__getcwd(pt, size) >= 0)
    213  1.35     enami 			return (pt);
    214  1.35     enami 		return (NULL);
    215  1.16  sommerfe 	}
    216  1.35     enami 
    217  1.35     enami 	/*
    218  1.35     enami 	 * If no buffer specified by the user, allocate one as necessary.
    219  1.35     enami 	 */
    220  1.35     enami 	size = 1024 >> 1;
    221  1.35     enami 	do {
    222  1.35     enami 		if ((npt = realloc(pt, size <<= 1)) == NULL)
    223  1.35     enami 			break;
    224  1.35     enami 		pt = npt;
    225  1.35     enami 		if (__getcwd(pt, size) >= 0)
    226  1.35     enami 			return (pt);
    227  1.35     enami 	} while (size <= MAXPATHLEN * 4 && errno == ERANGE);
    228  1.35     enami 
    229  1.35     enami 	free(pt);
    230  1.35     enami 	return (NULL);
    231  1.16  sommerfe }
    232