xref: /freebsd/bin/sh/cd.c (revision ed5c24e27da353299c3c319243f57d60cba88db0)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)cd.c	8.2 (Berkeley) 5/4/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
41aa9caaf6SPeter Wemm #include <sys/types.h>
42aa9caaf6SPeter Wemm #include <sys/stat.h>
43aa9caaf6SPeter Wemm #include <stdlib.h>
44ab0a2172SSteve Price #include <string.h>
45aa9caaf6SPeter Wemm #include <unistd.h>
46aa9caaf6SPeter Wemm #include <errno.h>
4793c0dc5eSTim J. Robbins #include <limits.h>
48aa9caaf6SPeter Wemm 
494b88c807SRodney W. Grimes /*
504b88c807SRodney W. Grimes  * The cd and pwd commands.
514b88c807SRodney W. Grimes  */
524b88c807SRodney W. Grimes 
534b88c807SRodney W. Grimes #include "shell.h"
544b88c807SRodney W. Grimes #include "var.h"
554b88c807SRodney W. Grimes #include "nodes.h"	/* for jobs.h */
564b88c807SRodney W. Grimes #include "jobs.h"
574b88c807SRodney W. Grimes #include "options.h"
584b88c807SRodney W. Grimes #include "output.h"
594b88c807SRodney W. Grimes #include "memalloc.h"
604b88c807SRodney W. Grimes #include "error.h"
61ab0a2172SSteve Price #include "exec.h"
62aa9caaf6SPeter Wemm #include "redir.h"
634b88c807SRodney W. Grimes #include "mystring.h"
64aa9caaf6SPeter Wemm #include "show.h"
65ab0a2172SSteve Price #include "cd.h"
664b88c807SRodney W. Grimes 
6709086b04STim J. Robbins STATIC int cdlogical(char *);
6809086b04STim J. Robbins STATIC int cdphysical(char *);
6909086b04STim J. Robbins STATIC int docd(char *, int, int);
705134c3f7SWarner Losh STATIC char *getcomponent(void);
7109086b04STim J. Robbins STATIC int updatepwd(char *);
724b88c807SRodney W. Grimes 
732ba1b30bSDiomidis Spinellis STATIC char *curdir = NULL;	/* current working directory */
742ba1b30bSDiomidis Spinellis STATIC char *prevdir;		/* previous working directory */
754b88c807SRodney W. Grimes STATIC char *cdcomppath;
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes int
7809086b04STim J. Robbins cdcmd(int argc, char **argv)
79aa9caaf6SPeter Wemm {
804b88c807SRodney W. Grimes 	char *dest;
814b88c807SRodney W. Grimes 	char *path;
824b88c807SRodney W. Grimes 	char *p;
834b88c807SRodney W. Grimes 	struct stat statb;
8409086b04STim J. Robbins 	int ch, phys, print = 0;
854b88c807SRodney W. Grimes 
86050f7913STim J. Robbins 	optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
877e1975c2STim J. Robbins 	phys = Pflag;
8809086b04STim J. Robbins 	while ((ch = getopt(argc, argv, "LP")) != -1) {
8909086b04STim J. Robbins 		switch (ch) {
9009086b04STim J. Robbins 		case 'L':
9109086b04STim J. Robbins 			phys = 0;
9209086b04STim J. Robbins 			break;
9309086b04STim J. Robbins 		case 'P':
9409086b04STim J. Robbins 			phys = 1;
9509086b04STim J. Robbins 			break;
9609086b04STim J. Robbins 		default:
9709086b04STim J. Robbins 			error("unknown option: -%c", optopt);
9809086b04STim J. Robbins 			break;
9909086b04STim J. Robbins 		}
10009086b04STim J. Robbins 	}
10109086b04STim J. Robbins 	argc -= optind;
10209086b04STim J. Robbins 	argv += optind;
10309086b04STim J. Robbins 
10409086b04STim J. Robbins 	if (argc > 1)
10509086b04STim J. Robbins 		error("too many arguments");
10609086b04STim J. Robbins 
10709086b04STim J. Robbins 	if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
1084b88c807SRodney W. Grimes 		error("HOME not set");
10961233bdcSBruce Evans 	if (*dest == '\0')
11061233bdcSBruce Evans 		dest = ".";
1114b88c807SRodney W. Grimes 	if (dest[0] == '-' && dest[1] == '\0') {
1124b88c807SRodney W. Grimes 		dest = prevdir ? prevdir : curdir;
1138d7d5ceaSPeter Wemm 		if (dest)
1144b88c807SRodney W. Grimes 			print = 1;
1158d7d5ceaSPeter Wemm 		else
1168d7d5ceaSPeter Wemm 			dest = ".";
1174b88c807SRodney W. Grimes 	}
1184b88c807SRodney W. Grimes 	if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
1194b88c807SRodney W. Grimes 		path = nullstr;
1204b88c807SRodney W. Grimes 	while ((p = padvance(&path, dest)) != NULL) {
121aa9caaf6SPeter Wemm 		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
1224b88c807SRodney W. Grimes 			if (!print) {
1234b88c807SRodney W. Grimes 				/*
1244b88c807SRodney W. Grimes 				 * XXX - rethink
1254b88c807SRodney W. Grimes 				 */
12637f0a670STor Egge 				if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
127ed5c24e2SStefan Farfeleder 					print = strcmp(p + 2, dest);
128ed5c24e2SStefan Farfeleder 				else
1294b88c807SRodney W. Grimes 					print = strcmp(p, dest);
1304b88c807SRodney W. Grimes 			}
13109086b04STim J. Robbins 			if (docd(p, print, phys) >= 0)
1324b88c807SRodney W. Grimes 				return 0;
1334b88c807SRodney W. Grimes 		}
1344b88c807SRodney W. Grimes 	}
1354b88c807SRodney W. Grimes 	error("can't cd to %s", dest);
136aa9caaf6SPeter Wemm 	/*NOTREACHED*/
137aa9caaf6SPeter Wemm 	return 0;
1384b88c807SRodney W. Grimes }
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes 
1414b88c807SRodney W. Grimes /*
14209086b04STim J. Robbins  * Actually change the directory.  In an interactive shell, print the
143958ba632SSteve Price  * directory name if "print" is nonzero.
1444b88c807SRodney W. Grimes  */
1454b88c807SRodney W. Grimes STATIC int
14609086b04STim J. Robbins docd(char *dest, int print, int phys)
14709086b04STim J. Robbins {
14809086b04STim J. Robbins 
14909086b04STim J. Robbins 	TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
15009086b04STim J. Robbins 
15109086b04STim J. Robbins 	/* If logical cd fails, fall back to physical. */
15209086b04STim J. Robbins 	if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
15309086b04STim J. Robbins 		return (-1);
15409086b04STim J. Robbins 
15509086b04STim J. Robbins 	if (print && iflag && curdir)
15609086b04STim J. Robbins 		out1fmt("%s\n", curdir);
15709086b04STim J. Robbins 
15809086b04STim J. Robbins 	return 0;
15909086b04STim J. Robbins }
16009086b04STim J. Robbins 
16109086b04STim J. Robbins STATIC int
16209086b04STim J. Robbins cdlogical(char *dest)
1634b88c807SRodney W. Grimes {
16437f0a670STor Egge 	char *p;
16537f0a670STor Egge 	char *q;
16637f0a670STor Egge 	char *component;
16737f0a670STor Egge 	struct stat statb;
16837f0a670STor Egge 	int first;
16937f0a670STor Egge 	int badstat;
170ab0a2172SSteve Price 
17137f0a670STor Egge 	/*
17237f0a670STor Egge 	 *  Check each component of the path. If we find a symlink or
17337f0a670STor Egge 	 *  something we can't stat, clear curdir to force a getcwd()
17437f0a670STor Egge 	 *  next time we get the value of the current directory.
17537f0a670STor Egge 	 */
17637f0a670STor Egge 	badstat = 0;
17737f0a670STor Egge 	cdcomppath = stalloc(strlen(dest) + 1);
17837f0a670STor Egge 	scopy(dest, cdcomppath);
17937f0a670STor Egge 	STARTSTACKSTR(p);
18037f0a670STor Egge 	if (*dest == '/') {
18137f0a670STor Egge 		STPUTC('/', p);
18237f0a670STor Egge 		cdcomppath++;
18337f0a670STor Egge 	}
18437f0a670STor Egge 	first = 1;
18537f0a670STor Egge 	while ((q = getcomponent()) != NULL) {
18637f0a670STor Egge 		if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
18737f0a670STor Egge 			continue;
18837f0a670STor Egge 		if (! first)
18937f0a670STor Egge 			STPUTC('/', p);
19037f0a670STor Egge 		first = 0;
19137f0a670STor Egge 		component = q;
19237f0a670STor Egge 		while (*q)
19337f0a670STor Egge 			STPUTC(*q++, p);
19437f0a670STor Egge 		if (equal(component, ".."))
19537f0a670STor Egge 			continue;
19637f0a670STor Egge 		STACKSTRNUL(p);
19709086b04STim J. Robbins 		if (lstat(stackblock(), &statb) < 0) {
19837f0a670STor Egge 			badstat = 1;
19937f0a670STor Egge 			break;
20037f0a670STor Egge 		}
20137f0a670STor Egge 	}
20237f0a670STor Egge 
2034b88c807SRodney W. Grimes 	INTOFF;
20409086b04STim J. Robbins 	if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
2054b88c807SRodney W. Grimes 		INTON;
20609086b04STim J. Robbins 		return (-1);
2074b88c807SRodney W. Grimes 	}
2084b88c807SRodney W. Grimes 	INTON;
20909086b04STim J. Robbins 	return (0);
2104b88c807SRodney W. Grimes }
2114b88c807SRodney W. Grimes 
21209086b04STim J. Robbins STATIC int
21309086b04STim J. Robbins cdphysical(char *dest)
21409086b04STim J. Robbins {
21509086b04STim J. Robbins 
21609086b04STim J. Robbins 	INTOFF;
21709086b04STim J. Robbins 	if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
21809086b04STim J. Robbins 		INTON;
21909086b04STim J. Robbins 		return (-1);
22009086b04STim J. Robbins 	}
22109086b04STim J. Robbins 	INTON;
22209086b04STim J. Robbins 	return (0);
22309086b04STim J. Robbins }
2244b88c807SRodney W. Grimes 
2254b88c807SRodney W. Grimes /*
2264b88c807SRodney W. Grimes  * Get the next component of the path name pointed to by cdcomppath.
2274b88c807SRodney W. Grimes  * This routine overwrites the string pointed to by cdcomppath.
2284b88c807SRodney W. Grimes  */
2294b88c807SRodney W. Grimes STATIC char *
2305134c3f7SWarner Losh getcomponent(void)
231958ba632SSteve Price {
232afb033d5SSteve Price 	char *p;
2334b88c807SRodney W. Grimes 	char *start;
2344b88c807SRodney W. Grimes 
2354b88c807SRodney W. Grimes 	if ((p = cdcomppath) == NULL)
2364b88c807SRodney W. Grimes 		return NULL;
2374b88c807SRodney W. Grimes 	start = cdcomppath;
2384b88c807SRodney W. Grimes 	while (*p != '/' && *p != '\0')
2394b88c807SRodney W. Grimes 		p++;
2404b88c807SRodney W. Grimes 	if (*p == '\0') {
2414b88c807SRodney W. Grimes 		cdcomppath = NULL;
2424b88c807SRodney W. Grimes 	} else {
2434b88c807SRodney W. Grimes 		*p++ = '\0';
2444b88c807SRodney W. Grimes 		cdcomppath = p;
2454b88c807SRodney W. Grimes 	}
2464b88c807SRodney W. Grimes 	return start;
2474b88c807SRodney W. Grimes }
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes 
2504b88c807SRodney W. Grimes /*
25137f0a670STor Egge  * Update curdir (the name of the current directory) in response to a
25237f0a670STor Egge  * cd command.  We also call hashcd to let the routines in exec.c know
25337f0a670STor Egge  * that the current directory has changed.
2544b88c807SRodney W. Grimes  */
25509086b04STim J. Robbins STATIC int
2565134c3f7SWarner Losh updatepwd(char *dir)
2574b88c807SRodney W. Grimes {
2584b88c807SRodney W. Grimes 	char *new;
2594b88c807SRodney W. Grimes 	char *p;
2604b88c807SRodney W. Grimes 
26137f0a670STor Egge 	hashcd();				/* update command hash table */
26237f0a670STor Egge 
26337f0a670STor Egge 	/*
26437f0a670STor Egge 	 * If our argument is NULL, we don't know the current directory
26537f0a670STor Egge 	 * any more because we traversed a symbolic link or something
26637f0a670STor Egge 	 * we couldn't stat().
26737f0a670STor Egge 	 */
26837f0a670STor Egge 	if (dir == NULL || curdir == NULL)  {
26937f0a670STor Egge 		if (prevdir)
27037f0a670STor Egge 			ckfree(prevdir);
27137f0a670STor Egge 		INTOFF;
27237f0a670STor Egge 		prevdir = curdir;
27337f0a670STor Egge 		curdir = NULL;
27409086b04STim J. Robbins 		if (getpwd() == NULL) {
27509086b04STim J. Robbins 			INTON;
27609086b04STim J. Robbins 			return (-1);
27709086b04STim J. Robbins 		}
278f14c1c47STor Egge 		setvar("PWD", curdir, VEXPORT);
279f14c1c47STor Egge 		setvar("OLDPWD", prevdir, VEXPORT);
28037f0a670STor Egge 		INTON;
28109086b04STim J. Robbins 		return (0);
28237f0a670STor Egge 	}
2834b88c807SRodney W. Grimes 	cdcomppath = stalloc(strlen(dir) + 1);
2844b88c807SRodney W. Grimes 	scopy(dir, cdcomppath);
2854b88c807SRodney W. Grimes 	STARTSTACKSTR(new);
2864b88c807SRodney W. Grimes 	if (*dir != '/') {
2874b88c807SRodney W. Grimes 		p = curdir;
2884b88c807SRodney W. Grimes 		while (*p)
2894b88c807SRodney W. Grimes 			STPUTC(*p++, new);
2904b88c807SRodney W. Grimes 		if (p[-1] == '/')
2914b88c807SRodney W. Grimes 			STUNPUTC(new);
2924b88c807SRodney W. Grimes 	}
2934b88c807SRodney W. Grimes 	while ((p = getcomponent()) != NULL) {
2944b88c807SRodney W. Grimes 		if (equal(p, "..")) {
2954b88c807SRodney W. Grimes 			while (new > stackblock() && (STUNPUTC(new), *new) != '/');
2964b88c807SRodney W. Grimes 		} else if (*p != '\0' && ! equal(p, ".")) {
2974b88c807SRodney W. Grimes 			STPUTC('/', new);
2984b88c807SRodney W. Grimes 			while (*p)
2994b88c807SRodney W. Grimes 				STPUTC(*p++, new);
3004b88c807SRodney W. Grimes 		}
3014b88c807SRodney W. Grimes 	}
3024b88c807SRodney W. Grimes 	if (new == stackblock())
3034b88c807SRodney W. Grimes 		STPUTC('/', new);
3044b88c807SRodney W. Grimes 	STACKSTRNUL(new);
30537f0a670STor Egge 	INTOFF;
30637f0a670STor Egge 	if (prevdir)
30737f0a670STor Egge 		ckfree(prevdir);
30837f0a670STor Egge 	prevdir = curdir;
30937f0a670STor Egge 	curdir = savestr(stackblock());
310f14c1c47STor Egge 	setvar("PWD", curdir, VEXPORT);
311f14c1c47STor Egge 	setvar("OLDPWD", prevdir, VEXPORT);
31237f0a670STor Egge 	INTON;
31309086b04STim J. Robbins 
31409086b04STim J. Robbins 	return (0);
3154b88c807SRodney W. Grimes }
3164b88c807SRodney W. Grimes 
3174b88c807SRodney W. Grimes int
318b5ae6928STim J. Robbins pwdcmd(int argc, char **argv)
319aa9caaf6SPeter Wemm {
32093c0dc5eSTim J. Robbins 	char buf[PATH_MAX];
32109086b04STim J. Robbins 	int ch, phys;
32209086b04STim J. Robbins 
323050f7913STim J. Robbins 	optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
3247e1975c2STim J. Robbins 	phys = Pflag;
32509086b04STim J. Robbins 	while ((ch = getopt(argc, argv, "LP")) != -1) {
32609086b04STim J. Robbins 		switch (ch) {
32709086b04STim J. Robbins 		case 'L':
32809086b04STim J. Robbins 			phys = 0;
32909086b04STim J. Robbins 			break;
33009086b04STim J. Robbins 		case 'P':
33109086b04STim J. Robbins 			phys = 1;
33209086b04STim J. Robbins 			break;
33309086b04STim J. Robbins 		default:
33409086b04STim J. Robbins 			error("unknown option: -%c", optopt);
33509086b04STim J. Robbins 			break;
33609086b04STim J. Robbins 		}
33709086b04STim J. Robbins 	}
33809086b04STim J. Robbins 	argc -= optind;
33909086b04STim J. Robbins 	argv += optind;
34009086b04STim J. Robbins 
34109086b04STim J. Robbins 	if (argc != 0)
34209086b04STim J. Robbins 		error("too many arguments");
34309086b04STim J. Robbins 
34409086b04STim J. Robbins 	if (!phys && getpwd()) {
3454b88c807SRodney W. Grimes 		out1str(curdir);
3464b88c807SRodney W. Grimes 		out1c('\n');
34709086b04STim J. Robbins 	} else {
34809086b04STim J. Robbins 		if (getcwd(buf, sizeof(buf)) == NULL)
34909086b04STim J. Robbins 			error(".: %s", strerror(errno));
35009086b04STim J. Robbins 		out1str(buf);
35109086b04STim J. Robbins 		out1c('\n');
352178897f1STim J. Robbins 	}
35337f0a670STor Egge 
35409086b04STim J. Robbins 	return 0;
35509086b04STim J. Robbins }
356baf3e7c1STim J. Robbins 
3574b88c807SRodney W. Grimes /*
358958ba632SSteve Price  * Find out what the current directory is. If we already know the current
3594b88c807SRodney W. Grimes  * directory, this routine returns immediately.
3604b88c807SRodney W. Grimes  */
361958ba632SSteve Price char *
3625134c3f7SWarner Losh getpwd(void)
363ab0a2172SSteve Price {
36493c0dc5eSTim J. Robbins 	char buf[PATH_MAX];
36537f0a670STor Egge 
3664b88c807SRodney W. Grimes 	if (curdir)
36737f0a670STor Egge 		return curdir;
36837f0a670STor Egge 	if (getcwd(buf, sizeof(buf)) == NULL) {
36937f0a670STor Egge 		char *pwd = getenv("PWD");
37037f0a670STor Egge 		struct stat stdot, stpwd;
37137f0a670STor Egge 
37237f0a670STor Egge 		if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
37337f0a670STor Egge 		    stat(pwd, &stpwd) != -1 &&
37437f0a670STor Egge 		    stdot.st_dev == stpwd.st_dev &&
37537f0a670STor Egge 		    stdot.st_ino == stpwd.st_ino) {
37637f0a670STor Egge 			curdir = savestr(pwd);
37737f0a670STor Egge 			return curdir;
37837f0a670STor Egge 		}
37937f0a670STor Egge 		return NULL;
38037f0a670STor Egge 	}
38137f0a670STor Egge 	curdir = savestr(buf);
38237f0a670STor Egge 
38337f0a670STor Egge 	return curdir;
3844b88c807SRodney W. Grimes }
385