xref: /freebsd/lib/libc/gen/getcwd.c (revision 4773010d2f14060811f0c34e2c17a1ed021ffb01)
158f0484fSRodney W. Grimes /*
29dc11641SPeter Wemm  * Copyright (c) 1989, 1991, 1993, 1995
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
359dc11641SPeter Wemm static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #include <sys/param.h>
3958f0484fSRodney W. Grimes #include <sys/stat.h>
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #include <dirent.h>
429dc11641SPeter Wemm #include <errno.h>
439dc11641SPeter Wemm #include <fcntl.h>
4458f0484fSRodney W. Grimes #include <stdio.h>
4558f0484fSRodney W. Grimes #include <stdlib.h>
4658f0484fSRodney W. Grimes #include <string.h>
4758f0484fSRodney W. Grimes #include <unistd.h>
48b7ecb08aSPeter Wemm #include <signal.h>
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes #define	ISDOT(dp) \
5158f0484fSRodney W. Grimes 	(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
5251295a4dSJordan K. Hubbard 	    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
5358f0484fSRodney W. Grimes 
54b7ecb08aSPeter Wemm static int have__getcwd = 1;	/* 0 = no, 1 = perhaps, 2 = yes */
55b7ecb08aSPeter Wemm 
5658f0484fSRodney W. Grimes char *
5758f0484fSRodney W. Grimes getcwd(pt, size)
5858f0484fSRodney W. Grimes 	char *pt;
5958f0484fSRodney W. Grimes 	size_t size;
6058f0484fSRodney W. Grimes {
6158f0484fSRodney W. Grimes 	register struct dirent *dp;
62f5f31fbaSDavid Greenman 	register DIR *dir = NULL;
6358f0484fSRodney W. Grimes 	register dev_t dev;
6458f0484fSRodney W. Grimes 	register ino_t ino;
6558f0484fSRodney W. Grimes 	register int first;
6658f0484fSRodney W. Grimes 	register char *bpt, *bup;
6758f0484fSRodney W. Grimes 	struct stat s;
6858f0484fSRodney W. Grimes 	dev_t root_dev;
6958f0484fSRodney W. Grimes 	ino_t root_ino;
7016be3810SDavid Greenman 	size_t ptsize, upsize;
7158f0484fSRodney W. Grimes 	int save_errno;
729c2d6fcfSPoul-Henning Kamp 	char *ept, *eup, *up, c;
7358f0484fSRodney W. Grimes 
7458f0484fSRodney W. Grimes 	/*
7558f0484fSRodney W. Grimes 	 * If no buffer specified by the user, allocate one as necessary.
7658f0484fSRodney W. Grimes 	 * If a buffer is specified, the size has to be non-zero.  The path
7758f0484fSRodney W. Grimes 	 * is built from the end of the buffer backwards.
7858f0484fSRodney W. Grimes 	 */
7958f0484fSRodney W. Grimes 	if (pt) {
8058f0484fSRodney W. Grimes 		ptsize = 0;
8158f0484fSRodney W. Grimes 		if (!size) {
8258f0484fSRodney W. Grimes 			errno = EINVAL;
8358f0484fSRodney W. Grimes 			return (NULL);
8458f0484fSRodney W. Grimes 		}
85b01f0b7dSBruce Evans 		if (size == 1) {
86b01f0b7dSBruce Evans 			errno = ERANGE;
87b01f0b7dSBruce Evans 			return (NULL);
88b01f0b7dSBruce Evans 		}
8958f0484fSRodney W. Grimes 		ept = pt + size;
9058f0484fSRodney W. Grimes 	} else {
9158f0484fSRodney W. Grimes 		if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
9258f0484fSRodney W. Grimes 			return (NULL);
9358f0484fSRodney W. Grimes 		ept = pt + ptsize;
9458f0484fSRodney W. Grimes 	}
95b7ecb08aSPeter Wemm 	if (have__getcwd) {
96b7ecb08aSPeter Wemm 		struct sigaction sa, osa;
97b7ecb08aSPeter Wemm 		int sigsys_installed = 0;
98b7ecb08aSPeter Wemm 		int ret;
99b7ecb08aSPeter Wemm 
100b7ecb08aSPeter Wemm 		if (have__getcwd == 1) {	/* unsure? */
101b7ecb08aSPeter Wemm 			bzero(&sa, sizeof(sa));
102b7ecb08aSPeter Wemm 			sa.sa_handler = SIG_IGN;
103b7ecb08aSPeter Wemm 			if (sigaction(SIGSYS, &sa, &osa) >= 0)
104b7ecb08aSPeter Wemm 				sigsys_installed = 1;
105b7ecb08aSPeter Wemm 		}
106b7ecb08aSPeter Wemm 		ret = __getcwd(pt, ept - pt);
107b7ecb08aSPeter Wemm 		if (sigsys_installed == 1) {
108b7ecb08aSPeter Wemm 			int oerrno = errno;
109b7ecb08aSPeter Wemm 			sigaction(SIGSYS, &osa, NULL);
110b7ecb08aSPeter Wemm 			errno = oerrno;
111b7ecb08aSPeter Wemm 		}
112b7ecb08aSPeter Wemm 		/* XXX a bogus syscall seems to return EINVAL(!) */
113b7ecb08aSPeter Wemm 		if (ret < 0 && (errno == ENOSYS || errno == EINVAL))
114b7ecb08aSPeter Wemm 			have__getcwd = 0;
115b7ecb08aSPeter Wemm 		else if (have__getcwd == 1)
116b7ecb08aSPeter Wemm 			have__getcwd = 2;	/* yep, remember we have it */
117b7ecb08aSPeter Wemm 		if (ret == 0) {
11836dff600SPoul-Henning Kamp 			if (*pt != '/') {
11927262cacSPoul-Henning Kamp 				bpt = pt;
12027262cacSPoul-Henning Kamp 				ept = pt + strlen(pt) - 1;
12127262cacSPoul-Henning Kamp 				while (bpt < ept) {
12227262cacSPoul-Henning Kamp 					c = *bpt;
12327262cacSPoul-Henning Kamp 					*bpt++ = *ept;
12427262cacSPoul-Henning Kamp 					*ept-- = c;
12527262cacSPoul-Henning Kamp 				}
12636dff600SPoul-Henning Kamp 			}
12727262cacSPoul-Henning Kamp 			return (pt);
12827262cacSPoul-Henning Kamp 		}
129b7ecb08aSPeter Wemm 	}
13058f0484fSRodney W. Grimes 	bpt = ept - 1;
13158f0484fSRodney W. Grimes 	*bpt = '\0';
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes 	/*
13458f0484fSRodney W. Grimes 	 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
13558f0484fSRodney W. Grimes 	 * Should always be enough (it's 340 levels).  If it's not, allocate
1369dc11641SPeter Wemm 	 * as necessary.  Special case the first stat, it's ".", not "..".
13758f0484fSRodney W. Grimes 	 */
13858f0484fSRodney W. Grimes 	if ((up = malloc(upsize = 1024 - 4)) == NULL)
13958f0484fSRodney W. Grimes 		goto err;
14058f0484fSRodney W. Grimes 	eup = up + MAXPATHLEN;
14158f0484fSRodney W. Grimes 	bup = up;
14258f0484fSRodney W. Grimes 	up[0] = '.';
14358f0484fSRodney W. Grimes 	up[1] = '\0';
14458f0484fSRodney W. Grimes 
14558f0484fSRodney W. Grimes 	/* Save root values, so know when to stop. */
14658f0484fSRodney W. Grimes 	if (stat("/", &s))
14758f0484fSRodney W. Grimes 		goto err;
14858f0484fSRodney W. Grimes 	root_dev = s.st_dev;
14958f0484fSRodney W. Grimes 	root_ino = s.st_ino;
15058f0484fSRodney W. Grimes 
15158f0484fSRodney W. Grimes 	errno = 0;			/* XXX readdir has no error return. */
15258f0484fSRodney W. Grimes 
15358f0484fSRodney W. Grimes 	for (first = 1;; first = 0) {
15458f0484fSRodney W. Grimes 		/* Stat the current level. */
15558f0484fSRodney W. Grimes 		if (lstat(up, &s))
15658f0484fSRodney W. Grimes 			goto err;
15758f0484fSRodney W. Grimes 
15858f0484fSRodney W. Grimes 		/* Save current node values. */
15958f0484fSRodney W. Grimes 		ino = s.st_ino;
16058f0484fSRodney W. Grimes 		dev = s.st_dev;
16158f0484fSRodney W. Grimes 
16258f0484fSRodney W. Grimes 		/* Check for reaching root. */
16358f0484fSRodney W. Grimes 		if (root_dev == dev && root_ino == ino) {
16458f0484fSRodney W. Grimes 			*--bpt = '/';
16558f0484fSRodney W. Grimes 			/*
16658f0484fSRodney W. Grimes 			 * It's unclear that it's a requirement to copy the
16758f0484fSRodney W. Grimes 			 * path to the beginning of the buffer, but it's always
16858f0484fSRodney W. Grimes 			 * been that way and stuff would probably break.
16958f0484fSRodney W. Grimes 			 */
170e78bad23SJeffrey Hsu 			bcopy(bpt, pt, ept - bpt);
17158f0484fSRodney W. Grimes 			free(up);
17258f0484fSRodney W. Grimes 			return (pt);
17358f0484fSRodney W. Grimes 		}
17458f0484fSRodney W. Grimes 
17558f0484fSRodney W. Grimes 		/*
17658f0484fSRodney W. Grimes 		 * Build pointer to the parent directory, allocating memory
17758f0484fSRodney W. Grimes 		 * as necessary.  Max length is 3 for "../", the largest
17858f0484fSRodney W. Grimes 		 * possible component name, plus a trailing NULL.
17958f0484fSRodney W. Grimes 		 */
18058f0484fSRodney W. Grimes 		if (bup + 3  + MAXNAMLEN + 1 >= eup) {
18158f0484fSRodney W. Grimes 			if ((up = realloc(up, upsize *= 2)) == NULL)
18258f0484fSRodney W. Grimes 				goto err;
18358f0484fSRodney W. Grimes 			bup = up;
18458f0484fSRodney W. Grimes 			eup = up + upsize;
18558f0484fSRodney W. Grimes 		}
18658f0484fSRodney W. Grimes 		*bup++ = '.';
18758f0484fSRodney W. Grimes 		*bup++ = '.';
18858f0484fSRodney W. Grimes 		*bup = '\0';
18958f0484fSRodney W. Grimes 
19058f0484fSRodney W. Grimes 		/* Open and stat parent directory. */
19158f0484fSRodney W. Grimes 		if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
19258f0484fSRodney W. Grimes 			goto err;
19358f0484fSRodney W. Grimes 
19458f0484fSRodney W. Grimes 		/* Add trailing slash for next directory. */
19558f0484fSRodney W. Grimes 		*bup++ = '/';
1960d4453d3SPeter Wemm 		*bup = '\0';
19758f0484fSRodney W. Grimes 
19858f0484fSRodney W. Grimes 		/*
19958f0484fSRodney W. Grimes 		 * If it's a mount point, have to stat each element because
20058f0484fSRodney W. Grimes 		 * the inode number in the directory is for the entry in the
20158f0484fSRodney W. Grimes 		 * parent directory, not the inode number of the mounted file.
20258f0484fSRodney W. Grimes 		 */
20358f0484fSRodney W. Grimes 		save_errno = 0;
20458f0484fSRodney W. Grimes 		if (s.st_dev == dev) {
20558f0484fSRodney W. Grimes 			for (;;) {
20658f0484fSRodney W. Grimes 				if (!(dp = readdir(dir)))
20758f0484fSRodney W. Grimes 					goto notfound;
20858f0484fSRodney W. Grimes 				if (dp->d_fileno == ino)
20958f0484fSRodney W. Grimes 					break;
21058f0484fSRodney W. Grimes 			}
21158f0484fSRodney W. Grimes 		} else
21258f0484fSRodney W. Grimes 			for (;;) {
21358f0484fSRodney W. Grimes 				if (!(dp = readdir(dir)))
21458f0484fSRodney W. Grimes 					goto notfound;
21558f0484fSRodney W. Grimes 				if (ISDOT(dp))
21658f0484fSRodney W. Grimes 					continue;
21758f0484fSRodney W. Grimes 				bcopy(dp->d_name, bup, dp->d_namlen + 1);
21858f0484fSRodney W. Grimes 
21958f0484fSRodney W. Grimes 				/* Save the first error for later. */
22058f0484fSRodney W. Grimes 				if (lstat(up, &s)) {
22158f0484fSRodney W. Grimes 					if (!save_errno)
22258f0484fSRodney W. Grimes 						save_errno = errno;
22358f0484fSRodney W. Grimes 					errno = 0;
22458f0484fSRodney W. Grimes 					continue;
22558f0484fSRodney W. Grimes 				}
22658f0484fSRodney W. Grimes 				if (s.st_dev == dev && s.st_ino == ino)
22758f0484fSRodney W. Grimes 					break;
22858f0484fSRodney W. Grimes 			}
22958f0484fSRodney W. Grimes 
23058f0484fSRodney W. Grimes 		/*
23158f0484fSRodney W. Grimes 		 * Check for length of the current name, preceding slash,
23258f0484fSRodney W. Grimes 		 * leading slash.
23358f0484fSRodney W. Grimes 		 */
234b01f0b7dSBruce Evans 		if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
23558f0484fSRodney W. Grimes 			size_t len, off;
23658f0484fSRodney W. Grimes 
23758f0484fSRodney W. Grimes 			if (!ptsize) {
23858f0484fSRodney W. Grimes 				errno = ERANGE;
23958f0484fSRodney W. Grimes 				goto err;
24058f0484fSRodney W. Grimes 			}
24158f0484fSRodney W. Grimes 			off = bpt - pt;
24258f0484fSRodney W. Grimes 			len = ept - bpt;
24358f0484fSRodney W. Grimes 			if ((pt = realloc(pt, ptsize *= 2)) == NULL)
24458f0484fSRodney W. Grimes 				goto err;
24558f0484fSRodney W. Grimes 			bpt = pt + off;
24658f0484fSRodney W. Grimes 			ept = pt + ptsize;
247e78bad23SJeffrey Hsu 			bcopy(bpt, ept - len, len);
24858f0484fSRodney W. Grimes 			bpt = ept - len;
24958f0484fSRodney W. Grimes 		}
25058f0484fSRodney W. Grimes 		if (!first)
25158f0484fSRodney W. Grimes 			*--bpt = '/';
25258f0484fSRodney W. Grimes 		bpt -= dp->d_namlen;
25358f0484fSRodney W. Grimes 		bcopy(dp->d_name, bpt, dp->d_namlen);
25458f0484fSRodney W. Grimes 		(void) closedir(dir);
255f5f31fbaSDavid Greenman 		dir = NULL;
25658f0484fSRodney W. Grimes 
25758f0484fSRodney W. Grimes 		/* Truncate any file name. */
25858f0484fSRodney W. Grimes 		*bup = '\0';
25958f0484fSRodney W. Grimes 	}
26058f0484fSRodney W. Grimes 
26158f0484fSRodney W. Grimes notfound:
26258f0484fSRodney W. Grimes 	/*
26358f0484fSRodney W. Grimes 	 * If readdir set errno, use it, not any saved error; otherwise,
26458f0484fSRodney W. Grimes 	 * didn't find the current directory in its parent directory, set
26558f0484fSRodney W. Grimes 	 * errno to ENOENT.
26658f0484fSRodney W. Grimes 	 */
26758f0484fSRodney W. Grimes 	if (!errno)
26858f0484fSRodney W. Grimes 		errno = save_errno ? save_errno : ENOENT;
26958f0484fSRodney W. Grimes 	/* FALLTHROUGH */
27058f0484fSRodney W. Grimes err:
2714773010dSStephen McKay 	save_errno = errno;
2724773010dSStephen McKay 
27358f0484fSRodney W. Grimes 	if (ptsize)
27458f0484fSRodney W. Grimes 		free(pt);
275f5f31fbaSDavid Greenman 	if (dir)
276f5f31fbaSDavid Greenman 		(void) closedir(dir);
27758f0484fSRodney W. Grimes 	free(up);
2784773010dSStephen McKay 
2794773010dSStephen McKay 	errno = save_errno;
28058f0484fSRodney W. Grimes 	return (NULL);
28158f0484fSRodney W. Grimes }
282