Home | History | Annotate | Line # | Download | only in sh
cd.c revision 1.37
      1  1.37  christos /*	$NetBSD: cd.c,v 1.37 2005/10/10 21:14:42 christos Exp $	*/
      2  1.12       cgd 
      3   1.1       cgd /*-
      4   1.6       jtc  * Copyright (c) 1991, 1993
      5   1.6       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.31       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.23  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.12       cgd #if 0
     38  1.13  christos static char sccsid[] = "@(#)cd.c	8.2 (Berkeley) 5/4/95";
     39  1.12       cgd #else
     40  1.37  christos __RCSID("$NetBSD: cd.c,v 1.37 2005/10/10 21:14:42 christos Exp $");
     41  1.12       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44   1.9       cgd #include <sys/types.h>
     45   1.9       cgd #include <sys/stat.h>
     46  1.13  christos #include <stdlib.h>
     47  1.17   thorpej #include <string.h>
     48   1.9       cgd #include <unistd.h>
     49   1.9       cgd #include <errno.h>
     50   1.9       cgd 
     51   1.1       cgd /*
     52   1.1       cgd  * The cd and pwd commands.
     53   1.1       cgd  */
     54   1.1       cgd 
     55   1.1       cgd #include "shell.h"
     56   1.1       cgd #include "var.h"
     57   1.1       cgd #include "nodes.h"	/* for jobs.h */
     58   1.1       cgd #include "jobs.h"
     59   1.1       cgd #include "options.h"
     60   1.1       cgd #include "output.h"
     61   1.1       cgd #include "memalloc.h"
     62   1.1       cgd #include "error.h"
     63  1.16  christos #include "exec.h"
     64  1.13  christos #include "redir.h"
     65   1.1       cgd #include "mystring.h"
     66  1.13  christos #include "show.h"
     67  1.14  christos #include "cd.h"
     68   1.1       cgd 
     69  1.36       dsl STATIC int docd(const char *, int);
     70  1.29  christos STATIC char *getcomponent(void);
     71  1.36       dsl STATIC void updatepwd(const char *);
     72  1.34       dsl STATIC void find_curdir(int noerror);
     73   1.1       cgd 
     74  1.14  christos char *curdir = NULL;		/* current working directory */
     75   1.6       jtc char *prevdir;			/* previous working directory */
     76   1.1       cgd STATIC char *cdcomppath;
     77   1.1       cgd 
     78   1.1       cgd int
     79  1.29  christos cdcmd(int argc, char **argv)
     80  1.10       cgd {
     81  1.27  christos 	const char *dest;
     82  1.36       dsl 	const char *path, *p;
     83  1.36       dsl 	char *d;
     84   1.1       cgd 	struct stat statb;
     85  1.30       dsl 	int print = cdprint;	/* set -cdprint to enable */
     86   1.1       cgd 
     87   1.1       cgd 	nextopt(nullstr);
     88  1.29  christos 
     89  1.34       dsl 	/*
     90  1.34       dsl 	 * Try (quite hard) to have 'curdir' defined, nothing has set
     91  1.34       dsl 	 * it on entry to the shell, but we want 'cd fred; cd -' to work.
     92  1.34       dsl 	 */
     93  1.29  christos 	getpwd(1);
     94  1.29  christos 	dest = *argptr;
     95  1.29  christos 	if (dest == NULL) {
     96  1.29  christos 		dest = bltinlookup("HOME", 1);
     97  1.29  christos 		if (dest == NULL)
     98  1.29  christos 			error("HOME not set");
     99  1.29  christos 	} else {
    100  1.29  christos 		if (argptr[1]) {
    101  1.29  christos 			/* Do 'ksh' style substitution */
    102  1.29  christos 			if (!curdir)
    103  1.29  christos 				error("PWD not set");
    104  1.29  christos 			p = strstr(curdir, dest);
    105  1.29  christos 			if (!p)
    106  1.29  christos 				error("bad substitution");
    107  1.29  christos 			d = stalloc(strlen(curdir) + strlen(argptr[1]) + 1);
    108  1.29  christos 			memcpy(d, curdir, p - curdir);
    109  1.29  christos 			strcpy(d + (p - curdir), argptr[1]);
    110  1.29  christos 			strcat(d, p + strlen(dest));
    111  1.29  christos 			dest = d;
    112  1.29  christos 			print = 1;
    113  1.29  christos 		}
    114  1.29  christos 	}
    115  1.29  christos 
    116   1.6       jtc 	if (dest[0] == '-' && dest[1] == '\0') {
    117   1.6       jtc 		dest = prevdir ? prevdir : curdir;
    118   1.6       jtc 		print = 1;
    119   1.6       jtc 	}
    120  1.34       dsl 	if (*dest == '\0')
    121  1.34       dsl 	        dest = ".";
    122  1.36       dsl 	p = dest;
    123  1.36       dsl 	if (*p == '.' && *++p == '.')
    124  1.36       dsl 	    p++;
    125  1.36       dsl 	if (*p == 0 || *p == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
    126   1.1       cgd 		path = nullstr;
    127   1.1       cgd 	while ((p = padvance(&path, dest)) != NULL) {
    128  1.11   mycroft 		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
    129   1.6       jtc 			if (!print) {
    130   1.6       jtc 				/*
    131   1.6       jtc 				 * XXX - rethink
    132   1.6       jtc 				 */
    133  1.24  christos 				if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
    134   1.6       jtc 					p += 2;
    135   1.6       jtc 				print = strcmp(p, dest);
    136   1.6       jtc 			}
    137   1.6       jtc 			if (docd(p, print) >= 0)
    138   1.6       jtc 				return 0;
    139   1.6       jtc 
    140   1.6       jtc 		}
    141   1.1       cgd 	}
    142   1.1       cgd 	error("can't cd to %s", dest);
    143  1.26   mycroft 	/* NOTREACHED */
    144   1.1       cgd }
    145   1.1       cgd 
    146   1.1       cgd 
    147   1.1       cgd /*
    148  1.15       jtc  * Actually do the chdir.  In an interactive shell, print the
    149  1.15       jtc  * directory name if "print" is nonzero.
    150   1.1       cgd  */
    151   1.1       cgd 
    152   1.1       cgd STATIC int
    153  1.36       dsl docd(const char *dest, int print)
    154  1.15       jtc {
    155  1.20       tls 	char *p;
    156  1.20       tls 	char *q;
    157  1.19       cjs 	char *component;
    158  1.19       cjs 	struct stat statb;
    159  1.19       cjs 	int first;
    160  1.19       cjs 	int badstat;
    161  1.15       jtc 
    162  1.15       jtc 	TRACE(("docd(\"%s\", %d) called\n", dest, print));
    163  1.19       cjs 
    164  1.19       cjs 	/*
    165  1.19       cjs 	 *  Check each component of the path. If we find a symlink or
    166  1.19       cjs 	 *  something we can't stat, clear curdir to force a getcwd()
    167  1.19       cjs 	 *  next time we get the value of the current directory.
    168  1.19       cjs 	 */
    169  1.19       cjs 	badstat = 0;
    170  1.19       cjs 	cdcomppath = stalloc(strlen(dest) + 1);
    171  1.19       cjs 	scopy(dest, cdcomppath);
    172  1.19       cjs 	STARTSTACKSTR(p);
    173  1.19       cjs 	if (*dest == '/') {
    174  1.19       cjs 		STPUTC('/', p);
    175  1.19       cjs 		cdcomppath++;
    176  1.19       cjs 	}
    177  1.19       cjs 	first = 1;
    178  1.19       cjs 	while ((q = getcomponent()) != NULL) {
    179  1.19       cjs 		if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
    180  1.19       cjs 			continue;
    181  1.19       cjs 		if (! first)
    182  1.19       cjs 			STPUTC('/', p);
    183  1.19       cjs 		first = 0;
    184  1.19       cjs 		component = q;
    185  1.19       cjs 		while (*q)
    186  1.19       cjs 			STPUTC(*q++, p);
    187  1.19       cjs 		if (equal(component, ".."))
    188  1.19       cjs 			continue;
    189  1.19       cjs 		STACKSTRNUL(p);
    190  1.19       cjs 		if ((lstat(stackblock(), &statb) < 0)
    191  1.19       cjs 		    || (S_ISLNK(statb.st_mode)))  {
    192  1.19       cjs 			/* print = 1; */
    193  1.19       cjs 			badstat = 1;
    194  1.19       cjs 			break;
    195  1.19       cjs 		}
    196  1.19       cjs 	}
    197  1.19       cjs 
    198   1.1       cgd 	INTOFF;
    199   1.1       cgd 	if (chdir(dest) < 0) {
    200   1.1       cgd 		INTON;
    201   1.1       cgd 		return -1;
    202   1.1       cgd 	}
    203  1.19       cjs 	updatepwd(badstat ? NULL : dest);
    204   1.1       cgd 	INTON;
    205  1.35  christos 	if (print && iflag == 1 && curdir)
    206  1.19       cjs 		out1fmt("%s\n", curdir);
    207   1.1       cgd 	return 0;
    208   1.1       cgd }
    209   1.1       cgd 
    210   1.1       cgd 
    211   1.1       cgd /*
    212   1.1       cgd  * Get the next component of the path name pointed to by cdcomppath.
    213   1.1       cgd  * This routine overwrites the string pointed to by cdcomppath.
    214   1.1       cgd  */
    215   1.1       cgd 
    216   1.1       cgd STATIC char *
    217  1.36       dsl getcomponent(void)
    218  1.29  christos {
    219  1.20       tls 	char *p;
    220   1.1       cgd 	char *start;
    221   1.1       cgd 
    222   1.1       cgd 	if ((p = cdcomppath) == NULL)
    223   1.1       cgd 		return NULL;
    224   1.1       cgd 	start = cdcomppath;
    225   1.1       cgd 	while (*p != '/' && *p != '\0')
    226   1.1       cgd 		p++;
    227   1.1       cgd 	if (*p == '\0') {
    228   1.1       cgd 		cdcomppath = NULL;
    229   1.1       cgd 	} else {
    230   1.1       cgd 		*p++ = '\0';
    231   1.1       cgd 		cdcomppath = p;
    232   1.1       cgd 	}
    233   1.1       cgd 	return start;
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.1       cgd 
    237   1.1       cgd 
    238   1.1       cgd /*
    239   1.1       cgd  * Update curdir (the name of the current directory) in response to a
    240   1.1       cgd  * cd command.  We also call hashcd to let the routines in exec.c know
    241   1.1       cgd  * that the current directory has changed.
    242   1.1       cgd  */
    243   1.1       cgd 
    244   1.1       cgd STATIC void
    245  1.36       dsl updatepwd(const char *dir)
    246  1.29  christos {
    247   1.1       cgd 	char *new;
    248   1.1       cgd 	char *p;
    249   1.1       cgd 
    250   1.1       cgd 	hashcd();				/* update command hash table */
    251  1.19       cjs 
    252  1.19       cjs 	/*
    253  1.19       cjs 	 * If our argument is NULL, we don't know the current directory
    254  1.19       cjs 	 * any more because we traversed a symbolic link or something
    255  1.19       cjs 	 * we couldn't stat().
    256  1.19       cjs 	 */
    257  1.25      ross 	if (dir == NULL || curdir == NULL)  {
    258  1.19       cjs 		if (prevdir)
    259  1.19       cjs 			ckfree(prevdir);
    260  1.19       cjs 		INTOFF;
    261  1.19       cjs 		prevdir = curdir;
    262  1.19       cjs 		curdir = NULL;
    263  1.29  christos 		getpwd(1);
    264  1.19       cjs 		INTON;
    265  1.29  christos 		if (curdir)
    266  1.29  christos 			setvar("PWD", curdir, VEXPORT);
    267  1.29  christos 		else
    268  1.29  christos 			unsetvar("PWD", 0);
    269  1.19       cjs 		return;
    270  1.19       cjs 	}
    271   1.1       cgd 	cdcomppath = stalloc(strlen(dir) + 1);
    272   1.1       cgd 	scopy(dir, cdcomppath);
    273   1.1       cgd 	STARTSTACKSTR(new);
    274   1.1       cgd 	if (*dir != '/') {
    275   1.1       cgd 		p = curdir;
    276   1.1       cgd 		while (*p)
    277   1.1       cgd 			STPUTC(*p++, new);
    278   1.1       cgd 		if (p[-1] == '/')
    279   1.1       cgd 			STUNPUTC(new);
    280   1.1       cgd 	}
    281   1.1       cgd 	while ((p = getcomponent()) != NULL) {
    282   1.1       cgd 		if (equal(p, "..")) {
    283   1.1       cgd 			while (new > stackblock() && (STUNPUTC(new), *new) != '/');
    284   1.1       cgd 		} else if (*p != '\0' && ! equal(p, ".")) {
    285   1.1       cgd 			STPUTC('/', new);
    286   1.1       cgd 			while (*p)
    287   1.1       cgd 				STPUTC(*p++, new);
    288   1.1       cgd 		}
    289   1.1       cgd 	}
    290   1.1       cgd 	if (new == stackblock())
    291   1.1       cgd 		STPUTC('/', new);
    292   1.1       cgd 	STACKSTRNUL(new);
    293   1.6       jtc 	INTOFF;
    294   1.6       jtc 	if (prevdir)
    295   1.6       jtc 		ckfree(prevdir);
    296   1.6       jtc 	prevdir = curdir;
    297   1.1       cgd 	curdir = savestr(stackblock());
    298  1.28        he 	setvar("PWD", curdir, VEXPORT);
    299   1.6       jtc 	INTON;
    300   1.1       cgd }
    301   1.1       cgd 
    302  1.34       dsl /*
    303  1.34       dsl  * Posix says the default should be 'pwd -L' (as below), however
    304  1.34       dsl  * the 'cd' command (above) does something much nearer to the
    305  1.34       dsl  * posix 'cd -P' (not the posix default of 'cd -L').
    306  1.34       dsl  * If 'cd' is changed to support -P/L then the default here
    307  1.34       dsl  * needs to be revisited if the historic behaviour is to be kept.
    308  1.34       dsl  */
    309   1.1       cgd 
    310   1.1       cgd int
    311  1.29  christos pwdcmd(int argc, char **argv)
    312  1.10       cgd {
    313  1.33       dsl 	int i;
    314  1.33       dsl 	char opt = 'L';
    315  1.33       dsl 
    316  1.33       dsl 	while ((i = nextopt("LP")) != '\0')
    317  1.33       dsl 		opt = i;
    318  1.33       dsl 	if (*argptr)
    319  1.33       dsl 		error("unexpected argument");
    320  1.33       dsl 
    321  1.34       dsl 	if (opt == 'L')
    322  1.33       dsl 		getpwd(0);
    323  1.34       dsl 	else
    324  1.34       dsl 		find_curdir(0);
    325  1.33       dsl 
    326  1.34       dsl 	setvar("PWD", curdir, VEXPORT);
    327  1.34       dsl 	out1str(curdir);
    328   1.1       cgd 	out1c('\n');
    329   1.1       cgd 	return 0;
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.1       cgd 
    333   1.1       cgd 
    334  1.37  christos void
    335  1.37  christos initpwd(void)
    336  1.37  christos {
    337  1.37  christos 	getpwd(0);
    338  1.37  christos 	setvar("PWD", curdir, VEXPORT);
    339  1.37  christos }
    340  1.14  christos 
    341  1.14  christos #define MAXPWD 256
    342  1.14  christos 
    343   1.1       cgd /*
    344  1.14  christos  * Find out what the current directory is. If we already know the current
    345   1.1       cgd  * directory, this routine returns immediately.
    346   1.1       cgd  */
    347  1.14  christos void
    348  1.29  christos getpwd(int noerror)
    349  1.14  christos {
    350  1.29  christos 	char *pwd;
    351  1.29  christos 	struct stat stdot, stpwd;
    352  1.29  christos 	static int first = 1;
    353   1.1       cgd 
    354   1.1       cgd 	if (curdir)
    355   1.1       cgd 		return;
    356  1.29  christos 
    357  1.29  christos 	if (first) {
    358  1.33       dsl 		first = 0;
    359  1.29  christos 		pwd = getenv("PWD");
    360  1.29  christos 		if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
    361  1.29  christos 		    stat(pwd, &stpwd) != -1 &&
    362  1.29  christos 		    stdot.st_dev == stpwd.st_dev &&
    363  1.29  christos 		    stdot.st_ino == stpwd.st_ino) {
    364  1.29  christos 			curdir = savestr(pwd);
    365  1.29  christos 			return;
    366  1.29  christos 		}
    367  1.29  christos 	}
    368  1.33       dsl 
    369  1.34       dsl 	find_curdir(noerror);
    370  1.33       dsl 
    371  1.33       dsl 	return;
    372  1.33       dsl }
    373  1.33       dsl 
    374  1.34       dsl STATIC void
    375  1.34       dsl find_curdir(int noerror)
    376  1.33       dsl {
    377  1.33       dsl 	int i;
    378  1.33       dsl 	char *pwd;
    379  1.29  christos 
    380  1.14  christos 	/*
    381  1.14  christos 	 * Things are a bit complicated here; we could have just used
    382  1.14  christos 	 * getcwd, but traditionally getcwd is implemented using popen
    383  1.14  christos 	 * to /bin/pwd. This creates a problem for us, since we cannot
    384  1.14  christos 	 * keep track of the job if it is being ran behind our backs.
    385  1.14  christos 	 * So we re-implement getcwd(), and we suppress interrupts
    386  1.14  christos 	 * throughout the process. This is not completely safe, since
    387  1.14  christos 	 * the user can still break out of it by killing the pwd program.
    388  1.14  christos 	 * We still try to use getcwd for systems that we know have a
    389  1.14  christos 	 * c implementation of getcwd, that does not open a pipe to
    390  1.14  christos 	 * /bin/pwd.
    391  1.14  christos 	 */
    392  1.22  christos #if defined(__NetBSD__) || defined(__SVR4)
    393  1.21  christos 
    394  1.29  christos 	for (i = MAXPWD;; i *= 2) {
    395  1.29  christos 		pwd = stalloc(i);
    396  1.34       dsl 		if (getcwd(pwd, i) != NULL) {
    397  1.34       dsl 			curdir = savestr(pwd);
    398  1.34       dsl 			return;
    399  1.34       dsl 		}
    400  1.29  christos 		stunalloc(pwd);
    401  1.29  christos 		if (errno == ERANGE)
    402  1.29  christos 			continue;
    403  1.29  christos 		if (!noerror)
    404  1.29  christos 			error("getcwd() failed: %s", strerror(errno));
    405  1.34       dsl 		return;
    406  1.21  christos 	}
    407  1.14  christos #else
    408  1.14  christos 	{
    409  1.14  christos 		char *p;
    410  1.14  christos 		int status;
    411  1.14  christos 		struct job *jp;
    412  1.14  christos 		int pip[2];
    413  1.14  christos 
    414  1.34       dsl 		pwd = stalloc(MAXPWD);
    415  1.14  christos 		INTOFF;
    416  1.14  christos 		if (pipe(pip) < 0)
    417  1.14  christos 			error("Pipe call failed");
    418  1.14  christos 		jp = makejob((union node *)NULL, 1);
    419  1.14  christos 		if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
    420  1.14  christos 			(void) close(pip[0]);
    421  1.14  christos 			if (pip[1] != 1) {
    422  1.14  christos 				close(1);
    423  1.14  christos 				copyfd(pip[1], 1);
    424  1.14  christos 				close(pip[1]);
    425  1.14  christos 			}
    426  1.14  christos 			(void) execl("/bin/pwd", "pwd", (char *)0);
    427  1.14  christos 			error("Cannot exec /bin/pwd");
    428  1.14  christos 		}
    429  1.14  christos 		(void) close(pip[1]);
    430  1.14  christos 		pip[1] = -1;
    431  1.34       dsl 		p = pwd;
    432  1.34       dsl 		while ((i = read(pip[0], p, pwd + MAXPWD - p)) > 0
    433  1.14  christos 		     || (i == -1 && errno == EINTR)) {
    434  1.14  christos 			if (i > 0)
    435  1.14  christos 				p += i;
    436   1.1       cgd 		}
    437  1.14  christos 		(void) close(pip[0]);
    438  1.14  christos 		pip[0] = -1;
    439  1.14  christos 		status = waitforjob(jp);
    440  1.14  christos 		if (status != 0)
    441  1.14  christos 			error((char *)0);
    442  1.34       dsl 		if (i < 0 || p == pwd || p[-1] != '\n') {
    443  1.29  christos 			if (noerror) {
    444  1.29  christos 				INTON;
    445  1.34       dsl 				return;
    446  1.29  christos 			}
    447  1.14  christos 			error("pwd command failed");
    448  1.29  christos 		}
    449  1.14  christos 		p[-1] = '\0';
    450  1.33       dsl 		INTON;
    451  1.34       dsl 		curdir = savestr(pwd);
    452  1.34       dsl 		return;
    453   1.1       cgd 	}
    454  1.15       jtc #endif
    455   1.1       cgd }
    456