cd.c revision 100663
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)cd.c	8.2 (Berkeley) 5/4/95";
4036150Scharnier#endif
411556Srgrimes#endif /* not lint */
4299110Sobrien#include <sys/cdefs.h>
4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/cd.c 100663 2002-07-25 10:47:38Z tjr $");
441556Srgrimes
4517987Speter#include <sys/types.h>
4617987Speter#include <sys/stat.h>
4717987Speter#include <stdlib.h>
4820425Ssteve#include <string.h>
4917987Speter#include <unistd.h>
5017987Speter#include <errno.h>
51100661Stjr#include <limits.h>
5217987Speter
531556Srgrimes/*
541556Srgrimes * The cd and pwd commands.
551556Srgrimes */
561556Srgrimes
571556Srgrimes#include "shell.h"
581556Srgrimes#include "var.h"
591556Srgrimes#include "nodes.h"	/* for jobs.h */
601556Srgrimes#include "jobs.h"
611556Srgrimes#include "options.h"
621556Srgrimes#include "output.h"
631556Srgrimes#include "memalloc.h"
641556Srgrimes#include "error.h"
6520425Ssteve#include "exec.h"
6617987Speter#include "redir.h"
671556Srgrimes#include "mystring.h"
6817987Speter#include "show.h"
6920425Ssteve#include "cd.h"
701556Srgrimes
7197092StjrSTATIC int cdlogical(char *);
7297092StjrSTATIC int cdphysical(char *);
7397092StjrSTATIC int docd(char *, int, int);
7490111SimpSTATIC char *getcomponent(void);
7597092StjrSTATIC int updatepwd(char *);
761556Srgrimes
7720425Sstevechar *curdir = NULL;		/* current working directory */
781556Srgrimeschar *prevdir;			/* previous working directory */
791556SrgrimesSTATIC char *cdcomppath;
801556Srgrimes
811556Srgrimesint
8297092Stjrcdcmd(int argc, char **argv)
8317987Speter{
841556Srgrimes	char *dest;
851556Srgrimes	char *path;
861556Srgrimes	char *p;
871556Srgrimes	struct stat statb;
8897092Stjr	int ch, phys, print = 0;
891556Srgrimes
90100663Stjr	optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
9197092Stjr	phys = 0;
9297092Stjr	while ((ch = getopt(argc, argv, "LP")) != -1) {
9397092Stjr		switch (ch) {
9497092Stjr		case 'L':
9597092Stjr			phys = 0;
9697092Stjr			break;
9797092Stjr		case 'P':
9897092Stjr			phys = 1;
9997092Stjr			break;
10097092Stjr		default:
10197092Stjr			error("unknown option: -%c", optopt);
10297092Stjr			break;
10397092Stjr		}
10497092Stjr	}
10597092Stjr	argc -= optind;
10697092Stjr	argv += optind;
10797092Stjr
10897092Stjr	if (argc > 1)
10997092Stjr		error("too many arguments");
11097092Stjr
11197092Stjr	if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
1121556Srgrimes		error("HOME not set");
1135234Sbde	if (*dest == '\0')
1145234Sbde		dest = ".";
1151556Srgrimes	if (dest[0] == '-' && dest[1] == '\0') {
1161556Srgrimes		dest = prevdir ? prevdir : curdir;
11712273Speter		if (dest)
11812273Speter			print = 1;
11912273Speter		else
12012273Speter			dest = ".";
1211556Srgrimes	}
1221556Srgrimes	if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
1231556Srgrimes		path = nullstr;
1241556Srgrimes	while ((p = padvance(&path, dest)) != NULL) {
12517987Speter		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
1261556Srgrimes			if (!print) {
1271556Srgrimes				/*
1281556Srgrimes				 * XXX - rethink
1291556Srgrimes				 */
13038886Stegge				if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
1311556Srgrimes					p += 2;
1321556Srgrimes				print = strcmp(p, dest);
1331556Srgrimes			}
13497092Stjr			if (docd(p, print, phys) >= 0)
1351556Srgrimes				return 0;
1361556Srgrimes		}
1371556Srgrimes	}
1381556Srgrimes	error("can't cd to %s", dest);
13917987Speter	/*NOTREACHED*/
14017987Speter	return 0;
1411556Srgrimes}
1421556Srgrimes
1431556Srgrimes
1441556Srgrimes/*
14597092Stjr * Actually change the directory.  In an interactive shell, print the
14620774Ssteve * directory name if "print" is nonzero.
1471556Srgrimes */
1481556SrgrimesSTATIC int
14997092Stjrdocd(char *dest, int print, int phys)
15020425Ssteve{
15197092Stjr
15297092Stjr	TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
15397092Stjr
15497092Stjr	/* If logical cd fails, fall back to physical. */
15597092Stjr	if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
15697092Stjr		return (-1);
15797092Stjr
15897092Stjr	if (print && iflag && curdir)
15997092Stjr		out1fmt("%s\n", curdir);
16097092Stjr
16197092Stjr	return 0;
16297092Stjr}
16397092Stjr
16497092StjrSTATIC int
16597092Stjrcdlogical(char *dest)
16697092Stjr{
16738886Stegge	char *p;
16838886Stegge	char *q;
16938886Stegge	char *component;
17038886Stegge	struct stat statb;
17138886Stegge	int first;
17238886Stegge	int badstat;
17320425Ssteve
17438886Stegge	/*
17538886Stegge	 *  Check each component of the path. If we find a symlink or
17638886Stegge	 *  something we can't stat, clear curdir to force a getcwd()
17738886Stegge	 *  next time we get the value of the current directory.
17838886Stegge	 */
17938886Stegge	badstat = 0;
18038886Stegge	cdcomppath = stalloc(strlen(dest) + 1);
18138886Stegge	scopy(dest, cdcomppath);
18238886Stegge	STARTSTACKSTR(p);
18338886Stegge	if (*dest == '/') {
18438886Stegge		STPUTC('/', p);
18538886Stegge		cdcomppath++;
18638886Stegge	}
18738886Stegge	first = 1;
18838886Stegge	while ((q = getcomponent()) != NULL) {
18938886Stegge		if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
19038886Stegge			continue;
19138886Stegge		if (! first)
19238886Stegge			STPUTC('/', p);
19338886Stegge		first = 0;
19438886Stegge		component = q;
19538886Stegge		while (*q)
19638886Stegge			STPUTC(*q++, p);
19738886Stegge		if (equal(component, ".."))
19838886Stegge			continue;
19938886Stegge		STACKSTRNUL(p);
20097092Stjr		if (lstat(stackblock(), &statb) < 0) {
20138886Stegge			badstat = 1;
20238886Stegge			break;
20338886Stegge		}
20438886Stegge	}
20538886Stegge
2061556Srgrimes	INTOFF;
20797092Stjr	if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
2081556Srgrimes		INTON;
20997092Stjr		return (-1);
2101556Srgrimes	}
2111556Srgrimes	INTON;
21297092Stjr	return (0);
2131556Srgrimes}
2141556Srgrimes
21597092StjrSTATIC int
21697092Stjrcdphysical(char *dest)
21797092Stjr{
2181556Srgrimes
21997092Stjr	INTOFF;
22097092Stjr	if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
22197092Stjr		INTON;
22297092Stjr		return (-1);
22397092Stjr	}
22497092Stjr	INTON;
22597092Stjr	return (0);
22697092Stjr}
22797092Stjr
2281556Srgrimes/*
2291556Srgrimes * Get the next component of the path name pointed to by cdcomppath.
2301556Srgrimes * This routine overwrites the string pointed to by cdcomppath.
2311556Srgrimes */
2321556SrgrimesSTATIC char *
23390111Simpgetcomponent(void)
23420774Ssteve{
23525222Ssteve	char *p;
2361556Srgrimes	char *start;
2371556Srgrimes
2381556Srgrimes	if ((p = cdcomppath) == NULL)
2391556Srgrimes		return NULL;
2401556Srgrimes	start = cdcomppath;
2411556Srgrimes	while (*p != '/' && *p != '\0')
2421556Srgrimes		p++;
2431556Srgrimes	if (*p == '\0') {
2441556Srgrimes		cdcomppath = NULL;
2451556Srgrimes	} else {
2461556Srgrimes		*p++ = '\0';
2471556Srgrimes		cdcomppath = p;
2481556Srgrimes	}
2491556Srgrimes	return start;
2501556Srgrimes}
2511556Srgrimes
2521556Srgrimes
2531556Srgrimes/*
25438886Stegge * Update curdir (the name of the current directory) in response to a
25538886Stegge * cd command.  We also call hashcd to let the routines in exec.c know
25638886Stegge * that the current directory has changed.
2571556Srgrimes */
25897092StjrSTATIC int
25990111Simpupdatepwd(char *dir)
26020774Ssteve{
2611556Srgrimes	char *new;
2621556Srgrimes	char *p;
2631556Srgrimes
26438886Stegge	hashcd();				/* update command hash table */
26538886Stegge
26638886Stegge	/*
26738886Stegge	 * If our argument is NULL, we don't know the current directory
26838886Stegge	 * any more because we traversed a symbolic link or something
26938886Stegge	 * we couldn't stat().
27038886Stegge	 */
27138886Stegge	if (dir == NULL || curdir == NULL)  {
27238886Stegge		if (prevdir)
27338886Stegge			ckfree(prevdir);
27438886Stegge		INTOFF;
27538886Stegge		prevdir = curdir;
27638886Stegge		curdir = NULL;
27797092Stjr		if (getpwd() == NULL) {
27897092Stjr			INTON;
27997092Stjr			return (-1);
28097092Stjr		}
28186176Stegge		setvar("PWD", curdir, VEXPORT);
28286176Stegge		setvar("OLDPWD", prevdir, VEXPORT);
28338886Stegge		INTON;
28497092Stjr		return (0);
28538886Stegge	}
2861556Srgrimes	cdcomppath = stalloc(strlen(dir) + 1);
2871556Srgrimes	scopy(dir, cdcomppath);
2881556Srgrimes	STARTSTACKSTR(new);
2891556Srgrimes	if (*dir != '/') {
2901556Srgrimes		p = curdir;
2911556Srgrimes		while (*p)
2921556Srgrimes			STPUTC(*p++, new);
2931556Srgrimes		if (p[-1] == '/')
2941556Srgrimes			STUNPUTC(new);
2951556Srgrimes	}
2961556Srgrimes	while ((p = getcomponent()) != NULL) {
2971556Srgrimes		if (equal(p, "..")) {
2981556Srgrimes			while (new > stackblock() && (STUNPUTC(new), *new) != '/');
2991556Srgrimes		} else if (*p != '\0' && ! equal(p, ".")) {
3001556Srgrimes			STPUTC('/', new);
3011556Srgrimes			while (*p)
3021556Srgrimes				STPUTC(*p++, new);
3031556Srgrimes		}
3041556Srgrimes	}
3051556Srgrimes	if (new == stackblock())
3061556Srgrimes		STPUTC('/', new);
3071556Srgrimes	STACKSTRNUL(new);
30838886Stegge	INTOFF;
30938886Stegge	if (prevdir)
31038886Stegge		ckfree(prevdir);
31138886Stegge	prevdir = curdir;
31238886Stegge	curdir = savestr(stackblock());
31386176Stegge	setvar("PWD", curdir, VEXPORT);
31486176Stegge	setvar("OLDPWD", prevdir, VEXPORT);
31538886Stegge	INTON;
31697092Stjr
31797092Stjr	return (0);
3181556Srgrimes}
3191556Srgrimes
3201556Srgrimesint
321100660Stjrpwdcmd(int argc, char **argv)
32217987Speter{
323100661Stjr	char buf[PATH_MAX];
32497092Stjr	int ch, phys;
3251556Srgrimes
326100663Stjr	optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
32797092Stjr	phys = 0;
32897092Stjr	while ((ch = getopt(argc, argv, "LP")) != -1) {
32997092Stjr		switch (ch) {
33097092Stjr		case 'L':
33197092Stjr			phys = 0;
33297092Stjr			break;
33397092Stjr		case 'P':
33497092Stjr			phys = 1;
33597092Stjr			break;
33697092Stjr		default:
33797092Stjr			error("unknown option: -%c", optopt);
33897092Stjr			break;
33997092Stjr		}
34097092Stjr	}
34197092Stjr	argc -= optind;
34297092Stjr	argv += optind;
3431556Srgrimes
34497092Stjr	if (argc != 0)
34597092Stjr		error("too many arguments");
34638886Stegge
34797092Stjr	if (!phys && getpwd()) {
34897092Stjr		out1str(curdir);
34997092Stjr		out1c('\n');
35097092Stjr	} else {
35197092Stjr		if (getcwd(buf, sizeof(buf)) == NULL)
35297092Stjr			error(".: %s", strerror(errno));
35397092Stjr		out1str(buf);
35497092Stjr		out1c('\n');
35597092Stjr	}
35638886Stegge
35797092Stjr	return 0;
35897092Stjr}
35938886Stegge
3601556Srgrimes/*
36120774Ssteve * Find out what the current directory is. If we already know the current
3621556Srgrimes * directory, this routine returns immediately.
3631556Srgrimes */
36420774Sstevechar *
36590111Simpgetpwd(void)
36620425Ssteve{
367100661Stjr	char buf[PATH_MAX];
36838886Stegge
3691556Srgrimes	if (curdir)
37038886Stegge		return curdir;
37138886Stegge	if (getcwd(buf, sizeof(buf)) == NULL) {
37238886Stegge		char *pwd = getenv("PWD");
37338886Stegge		struct stat stdot, stpwd;
37438886Stegge
37538886Stegge		if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
37638886Stegge		    stat(pwd, &stpwd) != -1 &&
37738886Stegge		    stdot.st_dev == stpwd.st_dev &&
37838886Stegge		    stdot.st_ino == stpwd.st_ino) {
37938886Stegge			curdir = savestr(pwd);
38038886Stegge			return curdir;
38138886Stegge		}
38238886Stegge		return NULL;
38338886Stegge	}
38438886Stegge	curdir = savestr(buf);
38538886Stegge
38638886Stegge	return curdir;
3871556Srgrimes}
388