Home | History | Annotate | Line # | Download | only in gen
getcwd.c revision 1.36.2.1.2.1
      1  1.36.2.1.2.1       riz /*	$NetBSD: getcwd.c,v 1.36.2.1.2.1 2005/12/29 16:25:46 riz 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.36.2.1.2.1       riz __RCSID("$NetBSD: getcwd.c,v 1.36.2.1.2.1 2005/12/29 16:25:46 riz 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.1       cgd #include <errno.h>
     50           1.1       cgd #include <stdlib.h>
     51           1.1       cgd #include <string.h>
     52           1.1       cgd #include <unistd.h>
     53          1.17  sommerfe 
     54          1.17  sommerfe #include "extern.h"
     55           1.7       jtc 
     56           1.7       jtc #ifdef __weak_alias
     57          1.24   mycroft __weak_alias(getcwd,_getcwd)
     58          1.24   mycroft __weak_alias(realpath,_realpath)
     59           1.7       jtc #endif
     60           1.1       cgd 
     61           1.8     perry /*
     62          1.36     enami  * char *realpath(const char *path, char resolved[MAXPATHLEN]);
     63           1.8     perry  *
     64           1.8     perry  * Find the real name of path, by removing all ".", ".." and symlink
     65           1.8     perry  * components.  Returns (resolved) on success, or (NULL) on failure,
     66           1.8     perry  * in which case the path which caused trouble is left in (resolved).
     67           1.8     perry  */
     68           1.8     perry char *
     69          1.36     enami realpath(const char *path, char *resolved)
     70           1.8     perry {
     71           1.8     perry 	struct stat sb;
     72      1.36.2.1       riz 	int idx = 0, n, nlnk = 0;
     73          1.36     enami 	const char *q;
     74          1.36     enami 	char *p, wbuf[2][MAXPATHLEN];
     75          1.36     enami 	size_t len;
     76          1.22     lukem 
     77          1.22     lukem 	_DIAGASSERT(path != NULL);
     78          1.22     lukem 	_DIAGASSERT(resolved != NULL);
     79           1.8     perry 
     80          1.36     enami 	/*
     81          1.36     enami 	 * Build real path one by one with paying an attention to .,
     82          1.36     enami 	 * .. and symbolic link.
     83          1.36     enami 	 */
     84          1.36     enami 
     85          1.36     enami 	/*
     86          1.36     enami 	 * `p' is where we'll put a new component with prepending
     87          1.36     enami 	 * a delimiter.
     88          1.36     enami 	 */
     89          1.36     enami 	p = resolved;
     90          1.36     enami 
     91          1.36     enami 	if (*path == 0) {
     92          1.36     enami 		*p = 0;
     93          1.36     enami 		errno = ENOENT;
     94           1.8     perry 		return (NULL);
     95           1.8     perry 	}
     96           1.8     perry 
     97          1.36     enami 	/* If relative path, start from current working directory. */
     98          1.36     enami 	if (*path != '/') {
     99          1.36     enami 		if (getcwd(resolved, MAXPATHLEN) == NULL) {
    100          1.36     enami 			p[0] = '.';
    101          1.36     enami 			p[1] = 0;
    102          1.36     enami 			return (NULL);
    103          1.36     enami 		}
    104          1.36     enami 		len = strlen(resolved);
    105          1.36     enami 		if (len > 1)
    106          1.36     enami 			p += len;
    107          1.31    itojun 	}
    108          1.36     enami 
    109           1.8     perry loop:
    110          1.36     enami 	/* Skip any slash. */
    111          1.36     enami 	while (*path == '/')
    112          1.36     enami 		path++;
    113          1.36     enami 
    114          1.36     enami 	if (*path == 0) {
    115          1.36     enami 		if (p == resolved)
    116          1.36     enami 			*p++ = '/';
    117          1.36     enami 		*p = 0;
    118          1.36     enami 		return (resolved);
    119          1.36     enami 	}
    120          1.36     enami 
    121          1.36     enami 	/* Find the end of this component. */
    122          1.36     enami 	q = path;
    123          1.36     enami 	do
    124          1.36     enami 		q++;
    125          1.36     enami 	while (*q != '/' && *q != 0);
    126          1.36     enami 
    127          1.36     enami 	/* Test . or .. */
    128          1.36     enami 	if (path[0] == '.') {
    129          1.36     enami 		if (q - path == 1) {
    130          1.36     enami 			path = q;
    131           1.8     perry 			goto loop;
    132           1.8     perry 		}
    133          1.36     enami 		if (path[1] == '.' && q - path == 2) {
    134          1.36     enami 			/* Trim the last component. */
    135          1.36     enami 			if (p != resolved)
    136          1.36     enami 				while (*--p != '/')
    137          1.36     enami 					;
    138          1.36     enami 			path = q;
    139          1.36     enami 			goto loop;
    140           1.8     perry 		}
    141           1.8     perry 	}
    142           1.8     perry 
    143          1.36     enami 	/* Append this component. */
    144          1.36     enami 	if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
    145          1.31    itojun 		errno = ENAMETOOLONG;
    146          1.36     enami 		if (p == resolved)
    147          1.36     enami 			*p++ = '/';
    148          1.36     enami 		*p = 0;
    149          1.36     enami 		return (NULL);
    150          1.31    itojun 	}
    151          1.36     enami 	p[0] = '/';
    152          1.36     enami 	memcpy(&p[1], path,
    153          1.36     enami 	    /* LINTED We know q > path. */
    154          1.36     enami 	    q - path);
    155          1.36     enami 	p[1 + q - path] = 0;
    156           1.8     perry 
    157           1.8     perry 	/*
    158          1.36     enami 	 * If this component is a symlink, toss it and prepend link
    159          1.36     enami 	 * target to unresolved path.
    160          1.36     enami 	 */
    161          1.36     enami 	if (lstat(resolved, &sb) == -1) {
    162  1.36.2.1.2.1       riz 		/* Allow nonexistent component if this is the last one. */
    163  1.36.2.1.2.1       riz 		while (*q == '/')
    164  1.36.2.1.2.1       riz 			q++;
    165  1.36.2.1.2.1       riz 
    166  1.36.2.1.2.1       riz 		if (*q == 0  && errno == ENOENT)
    167  1.36.2.1.2.1       riz 			return (resolved);
    168  1.36.2.1.2.1       riz 
    169          1.36     enami 		return (NULL);
    170          1.36     enami 	}
    171          1.36     enami 	if (S_ISLNK(sb.st_mode)) {
    172          1.36     enami 		if (nlnk++ >= MAXSYMLINKS) {
    173          1.36     enami 			errno = ELOOP;
    174          1.36     enami 			return (NULL);
    175          1.36     enami 		}
    176          1.36     enami 		n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
    177          1.36     enami 		if (n < 0)
    178          1.36     enami 			return (NULL);
    179          1.36     enami 		if (n == 0) {
    180          1.36     enami 			errno = ENOENT;
    181          1.36     enami 			return (NULL);
    182          1.36     enami 		}
    183           1.8     perry 
    184          1.36     enami 		/* Append unresolved path to link target and switch to it. */
    185          1.36     enami 		if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
    186           1.8     perry 			errno = ENAMETOOLONG;
    187          1.36     enami 			return (NULL);
    188          1.31    itojun 		}
    189          1.36     enami 		memcpy(&wbuf[idx][n], q, len + 1);
    190          1.36     enami 		path = wbuf[idx];
    191          1.36     enami 		idx ^= 1;
    192          1.36     enami 
    193          1.36     enami 		/* If absolute symlink, start from root. */
    194          1.36     enami 		if (*path == '/')
    195          1.36     enami 			p = resolved;
    196          1.36     enami 		goto loop;
    197           1.8     perry 	}
    198      1.36.2.1       riz 	if (*q == '/' && !S_ISDIR(sb.st_mode)) {
    199      1.36.2.1       riz 		errno = ENOTDIR;
    200      1.36.2.1       riz 		return (NULL);
    201      1.36.2.1       riz 	}
    202           1.8     perry 
    203          1.36     enami 	/* Advance both resolved and unresolved path. */
    204          1.36     enami 	p += 1 + q - path;
    205          1.36     enami 	path = q;
    206          1.36     enami 	goto loop;
    207           1.8     perry }
    208           1.8     perry 
    209          1.16  sommerfe char *
    210          1.35     enami getcwd(char *pt, size_t size)
    211          1.16  sommerfe {
    212          1.35     enami 	char *npt;
    213          1.35     enami 
    214          1.16  sommerfe 	/*
    215          1.35     enami 	 * If a buffer is specified, the size has to be non-zero.
    216          1.16  sommerfe 	 */
    217          1.35     enami 	if (pt != NULL) {
    218          1.35     enami 		if (size == 0) {
    219          1.35     enami 			/* __getcwd(pt, 0) results ERANGE. */
    220          1.16  sommerfe 			errno = EINVAL;
    221          1.16  sommerfe 			return (NULL);
    222          1.16  sommerfe 		}
    223          1.35     enami 		if (__getcwd(pt, size) >= 0)
    224          1.35     enami 			return (pt);
    225          1.35     enami 		return (NULL);
    226          1.16  sommerfe 	}
    227          1.35     enami 
    228          1.35     enami 	/*
    229          1.35     enami 	 * If no buffer specified by the user, allocate one as necessary.
    230          1.35     enami 	 */
    231          1.35     enami 	size = 1024 >> 1;
    232          1.35     enami 	do {
    233          1.35     enami 		if ((npt = realloc(pt, size <<= 1)) == NULL)
    234          1.35     enami 			break;
    235          1.35     enami 		pt = npt;
    236          1.35     enami 		if (__getcwd(pt, size) >= 0)
    237          1.35     enami 			return (pt);
    238          1.35     enami 	} while (size <= MAXPATHLEN * 4 && errno == ERANGE);
    239          1.35     enami 
    240          1.35     enami 	free(pt);
    241          1.35     enami 	return (NULL);
    242          1.16  sommerfe }
    243