xref: /freebsd/lib/libc/gen/getcwd.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1458f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1558f0484fSRodney W. Grimes  *    without specific prior written permission.
1658f0484fSRodney W. Grimes  *
1758f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1858f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1958f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2058f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2158f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2258f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2358f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2458f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2558f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2658f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2758f0484fSRodney W. Grimes  * SUCH DAMAGE.
2858f0484fSRodney W. Grimes  */
2958f0484fSRodney W. Grimes 
3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
319dc11641SPeter Wemm static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3322626efaSDavid E. O'Brien #include <sys/cdefs.h>
3422626efaSDavid E. O'Brien __FBSDID("$FreeBSD$");
3558f0484fSRodney W. Grimes 
36d201fe46SDaniel Eischen #include "namespace.h"
3758f0484fSRodney W. Grimes #include <sys/param.h>
3858f0484fSRodney W. Grimes #include <sys/stat.h>
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <dirent.h>
419dc11641SPeter Wemm #include <errno.h>
429dc11641SPeter Wemm #include <fcntl.h>
4358f0484fSRodney W. Grimes #include <stdio.h>
4458f0484fSRodney W. Grimes #include <stdlib.h>
4558f0484fSRodney W. Grimes #include <string.h>
4658f0484fSRodney W. Grimes #include <unistd.h>
47d201fe46SDaniel Eischen #include "un-namespace.h"
4858f0484fSRodney W. Grimes 
490bb2aabfSGleb Kurtsou #include "gen-private.h"
500bb2aabfSGleb Kurtsou 
5158f0484fSRodney W. Grimes #define	ISDOT(dp) \
5258f0484fSRodney W. Grimes 	(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
5351295a4dSJordan K. Hubbard 	    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
5458f0484fSRodney W. Grimes 
5573e8989dSTim J. Robbins extern int __getcwd(char *, size_t);
5673e8989dSTim J. Robbins 
5758f0484fSRodney W. Grimes char *
5855b6b759SCraig Rodrigues getcwd(char *pt, size_t size)
5958f0484fSRodney W. Grimes {
6022626efaSDavid E. O'Brien 	struct dirent *dp;
6122626efaSDavid E. O'Brien 	DIR *dir = NULL;
6222626efaSDavid E. O'Brien 	dev_t dev;
6322626efaSDavid E. O'Brien 	ino_t ino;
6422626efaSDavid E. O'Brien 	int first;
65680db495SJilles Tjoelker 	char *bpt;
6658f0484fSRodney W. Grimes 	struct stat s;
6758f0484fSRodney W. Grimes 	dev_t root_dev;
6858f0484fSRodney W. Grimes 	ino_t root_ino;
69680db495SJilles Tjoelker 	size_t ptsize;
7058f0484fSRodney W. Grimes 	int save_errno;
71680db495SJilles Tjoelker 	char *ept, c;
72680db495SJilles Tjoelker 	int fd;
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 {
91b9fb13f5SAndrey A. Chernov 		if ((pt = malloc(ptsize = PATH_MAX)) == NULL)
9258f0484fSRodney W. Grimes 			return (NULL);
9358f0484fSRodney W. Grimes 		ept = pt + ptsize;
9458f0484fSRodney W. Grimes 	}
957aa1d9cdSPeter Wemm 	if (__getcwd(pt, ept - pt) == 0) {
9636dff600SPoul-Henning Kamp 		if (*pt != '/') {
9727262cacSPoul-Henning Kamp 			bpt = pt;
9827262cacSPoul-Henning Kamp 			ept = pt + strlen(pt) - 1;
9927262cacSPoul-Henning Kamp 			while (bpt < ept) {
10027262cacSPoul-Henning Kamp 				c = *bpt;
10127262cacSPoul-Henning Kamp 				*bpt++ = *ept;
10227262cacSPoul-Henning Kamp 				*ept-- = c;
10327262cacSPoul-Henning Kamp 			}
10436dff600SPoul-Henning Kamp 		}
10527262cacSPoul-Henning Kamp 		return (pt);
10627262cacSPoul-Henning Kamp 	}
10758f0484fSRodney W. Grimes 	bpt = ept - 1;
10858f0484fSRodney W. Grimes 	*bpt = '\0';
10958f0484fSRodney W. Grimes 
11058f0484fSRodney W. Grimes 	/* Save root values, so know when to stop. */
11158f0484fSRodney W. Grimes 	if (stat("/", &s))
11258f0484fSRodney W. Grimes 		goto err;
11358f0484fSRodney W. Grimes 	root_dev = s.st_dev;
11458f0484fSRodney W. Grimes 	root_ino = s.st_ino;
11558f0484fSRodney W. Grimes 
11658f0484fSRodney W. Grimes 	errno = 0;			/* XXX readdir has no error return. */
11758f0484fSRodney W. Grimes 
11858f0484fSRodney W. Grimes 	for (first = 1;; first = 0) {
11958f0484fSRodney W. Grimes 		/* Stat the current level. */
1200bb2aabfSGleb Kurtsou 		if (dir != NULL ? _fstat(_dirfd(dir), &s) : lstat(".", &s))
12158f0484fSRodney W. Grimes 			goto err;
12258f0484fSRodney W. Grimes 
12358f0484fSRodney W. Grimes 		/* Save current node values. */
12458f0484fSRodney W. Grimes 		ino = s.st_ino;
12558f0484fSRodney W. Grimes 		dev = s.st_dev;
12658f0484fSRodney W. Grimes 
12758f0484fSRodney W. Grimes 		/* Check for reaching root. */
12858f0484fSRodney W. Grimes 		if (root_dev == dev && root_ino == ino) {
12958f0484fSRodney W. Grimes 			*--bpt = '/';
13058f0484fSRodney W. Grimes 			/*
13158f0484fSRodney W. Grimes 			 * It's unclear that it's a requirement to copy the
13258f0484fSRodney W. Grimes 			 * path to the beginning of the buffer, but it's always
13358f0484fSRodney W. Grimes 			 * been that way and stuff would probably break.
13458f0484fSRodney W. Grimes 			 */
135e78bad23SJeffrey Hsu 			bcopy(bpt, pt, ept - bpt);
136680db495SJilles Tjoelker 			if (dir)
137680db495SJilles Tjoelker 				(void) closedir(dir);
13858f0484fSRodney W. Grimes 			return (pt);
13958f0484fSRodney W. Grimes 		}
14058f0484fSRodney W. Grimes 
14158f0484fSRodney W. Grimes 		/* Open and stat parent directory. */
1420bb2aabfSGleb Kurtsou 		fd = _openat(dir != NULL ? _dirfd(dir) : AT_FDCWD,
14305eb11cbSJilles Tjoelker 				"..", O_RDONLY | O_CLOEXEC);
144680db495SJilles Tjoelker 		if (fd == -1)
14558f0484fSRodney W. Grimes 			goto err;
146680db495SJilles Tjoelker 		if (dir)
147680db495SJilles Tjoelker 			(void) closedir(dir);
1480bb2aabfSGleb Kurtsou 		if (!(dir = fdopendir(fd)) || _fstat(_dirfd(dir), &s)) {
149680db495SJilles Tjoelker 			_close(fd);
150680db495SJilles Tjoelker 			goto err;
151680db495SJilles Tjoelker 		}
15258f0484fSRodney W. Grimes 
15358f0484fSRodney W. Grimes 		/*
15458f0484fSRodney W. Grimes 		 * If it's a mount point, have to stat each element because
15558f0484fSRodney W. Grimes 		 * the inode number in the directory is for the entry in the
15658f0484fSRodney W. Grimes 		 * parent directory, not the inode number of the mounted file.
15758f0484fSRodney W. Grimes 		 */
15858f0484fSRodney W. Grimes 		save_errno = 0;
15958f0484fSRodney W. Grimes 		if (s.st_dev == dev) {
16058f0484fSRodney W. Grimes 			for (;;) {
16158f0484fSRodney W. Grimes 				if (!(dp = readdir(dir)))
16258f0484fSRodney W. Grimes 					goto notfound;
16358f0484fSRodney W. Grimes 				if (dp->d_fileno == ino)
16458f0484fSRodney W. Grimes 					break;
16558f0484fSRodney W. Grimes 			}
16658f0484fSRodney W. Grimes 		} else
16758f0484fSRodney W. Grimes 			for (;;) {
16858f0484fSRodney W. Grimes 				if (!(dp = readdir(dir)))
16958f0484fSRodney W. Grimes 					goto notfound;
17058f0484fSRodney W. Grimes 				if (ISDOT(dp))
17158f0484fSRodney W. Grimes 					continue;
17258f0484fSRodney W. Grimes 
17358f0484fSRodney W. Grimes 				/* Save the first error for later. */
1740bb2aabfSGleb Kurtsou 				if (fstatat(_dirfd(dir), dp->d_name, &s,
175680db495SJilles Tjoelker 				    AT_SYMLINK_NOFOLLOW)) {
17658f0484fSRodney W. Grimes 					if (!save_errno)
17758f0484fSRodney W. Grimes 						save_errno = errno;
17858f0484fSRodney W. Grimes 					errno = 0;
17958f0484fSRodney W. Grimes 					continue;
18058f0484fSRodney W. Grimes 				}
18158f0484fSRodney W. Grimes 				if (s.st_dev == dev && s.st_ino == ino)
18258f0484fSRodney W. Grimes 					break;
18358f0484fSRodney W. Grimes 			}
18458f0484fSRodney W. Grimes 
18558f0484fSRodney W. Grimes 		/*
18658f0484fSRodney W. Grimes 		 * Check for length of the current name, preceding slash,
18758f0484fSRodney W. Grimes 		 * leading slash.
18858f0484fSRodney W. Grimes 		 */
189323d07b4SAndrey A. Chernov 		while (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
19058f0484fSRodney W. Grimes 			size_t len, off;
19158f0484fSRodney W. Grimes 
19258f0484fSRodney W. Grimes 			if (!ptsize) {
19358f0484fSRodney W. Grimes 				errno = ERANGE;
19458f0484fSRodney W. Grimes 				goto err;
19558f0484fSRodney W. Grimes 			}
19658f0484fSRodney W. Grimes 			off = bpt - pt;
19758f0484fSRodney W. Grimes 			len = ept - bpt;
198098b8611STim J. Robbins 			if ((pt = reallocf(pt, ptsize *= 2)) == NULL)
19958f0484fSRodney W. Grimes 				goto err;
20058f0484fSRodney W. Grimes 			bpt = pt + off;
20158f0484fSRodney W. Grimes 			ept = pt + ptsize;
202e78bad23SJeffrey Hsu 			bcopy(bpt, ept - len, len);
20358f0484fSRodney W. Grimes 			bpt = ept - len;
20458f0484fSRodney W. Grimes 		}
20558f0484fSRodney W. Grimes 		if (!first)
20658f0484fSRodney W. Grimes 			*--bpt = '/';
20758f0484fSRodney W. Grimes 		bpt -= dp->d_namlen;
20858f0484fSRodney W. Grimes 		bcopy(dp->d_name, bpt, dp->d_namlen);
20958f0484fSRodney W. Grimes 	}
21058f0484fSRodney W. Grimes 
21158f0484fSRodney W. Grimes notfound:
21258f0484fSRodney W. Grimes 	/*
21358f0484fSRodney W. Grimes 	 * If readdir set errno, use it, not any saved error; otherwise,
21458f0484fSRodney W. Grimes 	 * didn't find the current directory in its parent directory, set
21558f0484fSRodney W. Grimes 	 * errno to ENOENT.
21658f0484fSRodney W. Grimes 	 */
21758f0484fSRodney W. Grimes 	if (!errno)
21858f0484fSRodney W. Grimes 		errno = save_errno ? save_errno : ENOENT;
21958f0484fSRodney W. Grimes 	/* FALLTHROUGH */
22058f0484fSRodney W. Grimes err:
2214773010dSStephen McKay 	save_errno = errno;
2224773010dSStephen McKay 
22358f0484fSRodney W. Grimes 	if (ptsize)
22458f0484fSRodney W. Grimes 		free(pt);
225f5f31fbaSDavid Greenman 	if (dir)
226f5f31fbaSDavid Greenman 		(void) closedir(dir);
2274773010dSStephen McKay 
2284773010dSStephen McKay 	errno = save_errno;
22958f0484fSRodney W. Grimes 	return (NULL);
23058f0484fSRodney W. Grimes }
231