xref: /minix3/lib/libc/gen/getcwd.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: getcwd.c,v 1.53 2012/06/21 23:29:23 enami Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1989, 1991, 1993, 1995
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Jan-Simon Pendry.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
392fe8fb19SBen Gras #else
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: getcwd.c,v 1.53 2012/06/21 23:29:23 enami Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include "namespace.h"
452fe8fb19SBen Gras #include <sys/param.h>
462fe8fb19SBen Gras #include <sys/stat.h>
472fe8fb19SBen Gras 
482fe8fb19SBen Gras #include <assert.h>
492fe8fb19SBen Gras #include <errno.h>
502fe8fb19SBen Gras #include <stdlib.h>
512fe8fb19SBen Gras #include <string.h>
522fe8fb19SBen Gras #include <unistd.h>
53*f14fb602SLionel Sambuc #include <ssp/ssp.h>
542fe8fb19SBen Gras 
552fe8fb19SBen Gras #include "extern.h"
562fe8fb19SBen Gras 
572fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(getcwd,_getcwd)582fe8fb19SBen Gras __weak_alias(getcwd,_getcwd)
59*f14fb602SLionel Sambuc __weak_alias(_sys_getcwd,_getcwd)
602fe8fb19SBen Gras __weak_alias(realpath,_realpath)
612fe8fb19SBen Gras #endif
622fe8fb19SBen Gras 
632fe8fb19SBen Gras /*
64*f14fb602SLionel Sambuc  * char *realpath(const char *path, char *resolved);
652fe8fb19SBen Gras  *
662fe8fb19SBen Gras  * Find the real name of path, by removing all ".", ".." and symlink
672fe8fb19SBen Gras  * components.  Returns (resolved) on success, or (NULL) on failure,
682fe8fb19SBen Gras  * in which case the path which caused trouble is left in (resolved).
692fe8fb19SBen Gras  */
702fe8fb19SBen Gras char *
71*f14fb602SLionel Sambuc realpath(const char * __restrict path, char * __restrict resolved)
722fe8fb19SBen Gras {
732fe8fb19SBen Gras 	struct stat sb;
74*f14fb602SLionel Sambuc 	int idx = 0, nlnk = 0;
752fe8fb19SBen Gras 	const char *q;
76*f14fb602SLionel Sambuc 	char *p, wbuf[2][MAXPATHLEN], *fres;
772fe8fb19SBen Gras 	size_t len;
78*f14fb602SLionel Sambuc 	ssize_t n;
792fe8fb19SBen Gras 
802fe8fb19SBen Gras 	/* POSIX sez we must test for this */
812fe8fb19SBen Gras 	if (path == NULL) {
822fe8fb19SBen Gras 		errno = EINVAL;
832fe8fb19SBen Gras 		return NULL;
842fe8fb19SBen Gras 	}
852fe8fb19SBen Gras 
86*f14fb602SLionel Sambuc 	if (resolved == NULL) {
87*f14fb602SLionel Sambuc 		fres = resolved = malloc(MAXPATHLEN);
88*f14fb602SLionel Sambuc 		if (resolved == NULL)
89*f14fb602SLionel Sambuc 			return NULL;
90*f14fb602SLionel Sambuc 	} else
91*f14fb602SLionel Sambuc 		fres = NULL;
92*f14fb602SLionel Sambuc 
93*f14fb602SLionel Sambuc 
942fe8fb19SBen Gras 	/*
952fe8fb19SBen Gras 	 * Build real path one by one with paying an attention to .,
962fe8fb19SBen Gras 	 * .. and symbolic link.
972fe8fb19SBen Gras 	 */
982fe8fb19SBen Gras 
992fe8fb19SBen Gras 	/*
1002fe8fb19SBen Gras 	 * `p' is where we'll put a new component with prepending
1012fe8fb19SBen Gras 	 * a delimiter.
1022fe8fb19SBen Gras 	 */
1032fe8fb19SBen Gras 	p = resolved;
1042fe8fb19SBen Gras 
105*f14fb602SLionel Sambuc 	if (*path == '\0') {
106*f14fb602SLionel Sambuc 		*p = '\0';
1072fe8fb19SBen Gras 		errno = ENOENT;
108*f14fb602SLionel Sambuc 		goto out;
1092fe8fb19SBen Gras 	}
1102fe8fb19SBen Gras 
1112fe8fb19SBen Gras 	/* If relative path, start from current working directory. */
1122fe8fb19SBen Gras 	if (*path != '/') {
1132fe8fb19SBen Gras 		/* check for resolved pointer to appease coverity */
1142fe8fb19SBen Gras 		if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
1152fe8fb19SBen Gras 			p[0] = '.';
116*f14fb602SLionel Sambuc 			p[1] = '\0';
117*f14fb602SLionel Sambuc 			goto out;
1182fe8fb19SBen Gras 		}
1192fe8fb19SBen Gras 		len = strlen(resolved);
1202fe8fb19SBen Gras 		if (len > 1)
1212fe8fb19SBen Gras 			p += len;
1222fe8fb19SBen Gras 	}
1232fe8fb19SBen Gras 
1242fe8fb19SBen Gras loop:
1252fe8fb19SBen Gras 	/* Skip any slash. */
1262fe8fb19SBen Gras 	while (*path == '/')
1272fe8fb19SBen Gras 		path++;
1282fe8fb19SBen Gras 
129*f14fb602SLionel Sambuc 	if (*path == '\0') {
1302fe8fb19SBen Gras 		if (p == resolved)
1312fe8fb19SBen Gras 			*p++ = '/';
132*f14fb602SLionel Sambuc 		*p = '\0';
133*f14fb602SLionel Sambuc 		return resolved;
1342fe8fb19SBen Gras 	}
1352fe8fb19SBen Gras 
1362fe8fb19SBen Gras 	/* Find the end of this component. */
1372fe8fb19SBen Gras 	q = path;
1382fe8fb19SBen Gras 	do
1392fe8fb19SBen Gras 		q++;
140*f14fb602SLionel Sambuc 	while (*q != '/' && *q != '\0');
1412fe8fb19SBen Gras 
1422fe8fb19SBen Gras 	/* Test . or .. */
1432fe8fb19SBen Gras 	if (path[0] == '.') {
1442fe8fb19SBen Gras 		if (q - path == 1) {
1452fe8fb19SBen Gras 			path = q;
1462fe8fb19SBen Gras 			goto loop;
1472fe8fb19SBen Gras 		}
1482fe8fb19SBen Gras 		if (path[1] == '.' && q - path == 2) {
1492fe8fb19SBen Gras 			/* Trim the last component. */
1502fe8fb19SBen Gras 			if (p != resolved)
1512fe8fb19SBen Gras 				while (*--p != '/')
152*f14fb602SLionel Sambuc 					continue;
1532fe8fb19SBen Gras 			path = q;
1542fe8fb19SBen Gras 			goto loop;
1552fe8fb19SBen Gras 		}
1562fe8fb19SBen Gras 	}
1572fe8fb19SBen Gras 
1582fe8fb19SBen Gras 	/* Append this component. */
1592fe8fb19SBen Gras 	if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
1602fe8fb19SBen Gras 		errno = ENAMETOOLONG;
1612fe8fb19SBen Gras 		if (p == resolved)
1622fe8fb19SBen Gras 			*p++ = '/';
163*f14fb602SLionel Sambuc 		*p = '\0';
164*f14fb602SLionel Sambuc 		goto out;
1652fe8fb19SBen Gras 	}
1662fe8fb19SBen Gras 	p[0] = '/';
1672fe8fb19SBen Gras 	memcpy(&p[1], path,
1682fe8fb19SBen Gras 	    /* LINTED We know q > path. */
1692fe8fb19SBen Gras 	    q - path);
170*f14fb602SLionel Sambuc 	p[1 + q - path] = '\0';
1712fe8fb19SBen Gras 
1722fe8fb19SBen Gras 	/*
1732fe8fb19SBen Gras 	 * If this component is a symlink, toss it and prepend link
1742fe8fb19SBen Gras 	 * target to unresolved path.
1752fe8fb19SBen Gras 	 */
176*f14fb602SLionel Sambuc 	if (lstat(resolved, &sb) == -1)
177*f14fb602SLionel Sambuc 		goto out;
178*f14fb602SLionel Sambuc 
1792fe8fb19SBen Gras 	if (S_ISLNK(sb.st_mode)) {
1802fe8fb19SBen Gras 		if (nlnk++ >= MAXSYMLINKS) {
1812fe8fb19SBen Gras 			errno = ELOOP;
182*f14fb602SLionel Sambuc 			goto out;
1832fe8fb19SBen Gras 		}
1842fe8fb19SBen Gras 		n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
1852fe8fb19SBen Gras 		if (n < 0)
186*f14fb602SLionel Sambuc 			goto out;
1872fe8fb19SBen Gras 		if (n == 0) {
1882fe8fb19SBen Gras 			errno = ENOENT;
189*f14fb602SLionel Sambuc 			goto out;
1902fe8fb19SBen Gras 		}
1912fe8fb19SBen Gras 
1922fe8fb19SBen Gras 		/* Append unresolved path to link target and switch to it. */
1932fe8fb19SBen Gras 		if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
1942fe8fb19SBen Gras 			errno = ENAMETOOLONG;
195*f14fb602SLionel Sambuc 			goto out;
1962fe8fb19SBen Gras 		}
1972fe8fb19SBen Gras 		memcpy(&wbuf[idx][n], q, len + 1);
1982fe8fb19SBen Gras 		path = wbuf[idx];
1992fe8fb19SBen Gras 		idx ^= 1;
2002fe8fb19SBen Gras 
2012fe8fb19SBen Gras 		/* If absolute symlink, start from root. */
2022fe8fb19SBen Gras 		if (*path == '/')
2032fe8fb19SBen Gras 			p = resolved;
2042fe8fb19SBen Gras 		goto loop;
2052fe8fb19SBen Gras 	}
2062fe8fb19SBen Gras 	if (*q == '/' && !S_ISDIR(sb.st_mode)) {
2072fe8fb19SBen Gras 		errno = ENOTDIR;
208*f14fb602SLionel Sambuc 		goto out;
2092fe8fb19SBen Gras 	}
2102fe8fb19SBen Gras 
2112fe8fb19SBen Gras 	/* Advance both resolved and unresolved path. */
2122fe8fb19SBen Gras 	p += 1 + q - path;
2132fe8fb19SBen Gras 	path = q;
2142fe8fb19SBen Gras 	goto loop;
215*f14fb602SLionel Sambuc out:
216*f14fb602SLionel Sambuc 	free(fres);
217*f14fb602SLionel Sambuc 	return NULL;
2182fe8fb19SBen Gras }
2192fe8fb19SBen Gras 
2202fe8fb19SBen Gras char *
__ssp_real(getcwd)221*f14fb602SLionel Sambuc __ssp_real(getcwd)(char *pt, size_t size)
2222fe8fb19SBen Gras {
2232fe8fb19SBen Gras 	char *npt;
2242fe8fb19SBen Gras 
2252fe8fb19SBen Gras 	/*
2262fe8fb19SBen Gras 	 * If a buffer is specified, the size has to be non-zero.
2272fe8fb19SBen Gras 	 */
2282fe8fb19SBen Gras 	if (pt != NULL) {
2292fe8fb19SBen Gras 		if (size == 0) {
2302fe8fb19SBen Gras 			/* __getcwd(pt, 0) results ERANGE. */
2312fe8fb19SBen Gras 			errno = EINVAL;
2322fe8fb19SBen Gras 			return (NULL);
2332fe8fb19SBen Gras 		}
2342fe8fb19SBen Gras 		if (__getcwd(pt, size) >= 0)
2352fe8fb19SBen Gras 			return (pt);
2362fe8fb19SBen Gras 		return (NULL);
2372fe8fb19SBen Gras 	}
2382fe8fb19SBen Gras 
2392fe8fb19SBen Gras 	/*
2402fe8fb19SBen Gras 	 * If no buffer specified by the user, allocate one as necessary.
2412fe8fb19SBen Gras 	 */
2422fe8fb19SBen Gras 	size = 1024 >> 1;
2432fe8fb19SBen Gras 	do {
2442fe8fb19SBen Gras 		if ((npt = realloc(pt, size <<= 1)) == NULL)
2452fe8fb19SBen Gras 			break;
2462fe8fb19SBen Gras 		pt = npt;
2472fe8fb19SBen Gras 		if (__getcwd(pt, size) >= 0)
2482fe8fb19SBen Gras 			return (pt);
2492fe8fb19SBen Gras 	} while (size <= MAXPATHLEN * 4 && errno == ERANGE);
2502fe8fb19SBen Gras 
2512fe8fb19SBen Gras 	free(pt);
2522fe8fb19SBen Gras 	return (NULL);
2532fe8fb19SBen Gras }
254