Home | History | Annotate | Line # | Download | only in sh
cd.c revision 1.7
      1  1.1  cgd /*-
      2  1.6  jtc  * Copyright (c) 1991, 1993
      3  1.6  jtc  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Kenneth Almquist.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.6  jtc static char sccsid[] = "@(#)cd.c	8.1 (Berkeley) 5/31/93";
     39  1.1  cgd #endif /* not lint */
     40  1.1  cgd 
     41  1.1  cgd /*
     42  1.1  cgd  * The cd and pwd commands.
     43  1.1  cgd  */
     44  1.1  cgd 
     45  1.1  cgd #include "shell.h"
     46  1.1  cgd #include "var.h"
     47  1.1  cgd #include "nodes.h"	/* for jobs.h */
     48  1.1  cgd #include "jobs.h"
     49  1.1  cgd #include "options.h"
     50  1.1  cgd #include "output.h"
     51  1.1  cgd #include "memalloc.h"
     52  1.1  cgd #include "error.h"
     53  1.1  cgd #include "mystring.h"
     54  1.1  cgd #include <sys/types.h>
     55  1.1  cgd #include <sys/stat.h>
     56  1.7  jtc #include <unistd.h>
     57  1.1  cgd #include <errno.h>
     58  1.1  cgd 
     59  1.1  cgd 
     60  1.1  cgd #ifdef __STDC__
     61  1.1  cgd STATIC int docd(char *, int);
     62  1.1  cgd STATIC void updatepwd(char *);
     63  1.1  cgd STATIC void getpwd(void);
     64  1.1  cgd STATIC char *getcomponent(void);
     65  1.1  cgd #else
     66  1.1  cgd STATIC int docd();
     67  1.1  cgd STATIC void updatepwd();
     68  1.1  cgd STATIC void getpwd();
     69  1.1  cgd STATIC char *getcomponent();
     70  1.1  cgd #endif
     71  1.1  cgd 
     72  1.1  cgd 
     73  1.1  cgd char *curdir;			/* current working directory */
     74  1.6  jtc char *prevdir;			/* previous working directory */
     75  1.1  cgd STATIC char *cdcomppath;
     76  1.1  cgd 
     77  1.1  cgd int
     78  1.1  cgd cdcmd(argc, argv)  char **argv; {
     79  1.1  cgd 	char *dest;
     80  1.1  cgd 	char *path;
     81  1.1  cgd 	char *p;
     82  1.1  cgd 	struct stat statb;
     83  1.1  cgd 	char *padvance();
     84  1.6  jtc 	int print = 0;
     85  1.1  cgd 
     86  1.1  cgd 	nextopt(nullstr);
     87  1.1  cgd 	if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
     88  1.1  cgd 		error("HOME not set");
     89  1.6  jtc 	if (dest[0] == '-' && dest[1] == '\0') {
     90  1.6  jtc 		dest = prevdir ? prevdir : curdir;
     91  1.6  jtc 		print = 1;
     92  1.6  jtc 	}
     93  1.1  cgd 	if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
     94  1.1  cgd 		path = nullstr;
     95  1.1  cgd 	while ((p = padvance(&path, dest)) != NULL) {
     96  1.1  cgd 		if (stat(p, &statb) >= 0
     97  1.6  jtc 		 && (statb.st_mode & S_IFMT) == S_IFDIR) {
     98  1.6  jtc 			if (!print) {
     99  1.6  jtc 				/*
    100  1.6  jtc 				 * XXX - rethink
    101  1.6  jtc 				 */
    102  1.6  jtc 				if (p[0] == '.' && p[1] == '/')
    103  1.6  jtc 					p += 2;
    104  1.6  jtc 				print = strcmp(p, dest);
    105  1.6  jtc 			}
    106  1.6  jtc 			if (docd(p, print) >= 0)
    107  1.6  jtc 				return 0;
    108  1.6  jtc 
    109  1.6  jtc 		}
    110  1.1  cgd 	}
    111  1.1  cgd 	error("can't cd to %s", dest);
    112  1.1  cgd }
    113  1.1  cgd 
    114  1.1  cgd 
    115  1.1  cgd /*
    116  1.1  cgd  * Actually do the chdir.  If the name refers to symbolic links, we
    117  1.1  cgd  * compute the actual directory name before doing the cd.  In an
    118  1.1  cgd  * interactive shell, print the directory name if "print" is nonzero
    119  1.1  cgd  * or if the name refers to a symbolic link.  We also print the name
    120  1.1  cgd  * if "/u/logname" was expanded in it, since this is similar to a
    121  1.1  cgd  * symbolic link.  (The check for this breaks if the user gives the
    122  1.1  cgd  * cd command some additional, unused arguments.)
    123  1.1  cgd  */
    124  1.1  cgd 
    125  1.1  cgd #if SYMLINKS == 0
    126  1.1  cgd STATIC int
    127  1.1  cgd docd(dest, print)
    128  1.1  cgd 	char *dest;
    129  1.1  cgd 	{
    130  1.1  cgd 	INTOFF;
    131  1.1  cgd 	if (chdir(dest) < 0) {
    132  1.1  cgd 		INTON;
    133  1.1  cgd 		return -1;
    134  1.1  cgd 	}
    135  1.1  cgd 	updatepwd(dest);
    136  1.1  cgd 	INTON;
    137  1.1  cgd 	if (print && iflag)
    138  1.1  cgd 		out1fmt("%s\n", stackblock());
    139  1.1  cgd 	return 0;
    140  1.1  cgd }
    141  1.1  cgd 
    142  1.1  cgd #else
    143  1.1  cgd 
    144  1.1  cgd 
    145  1.1  cgd 
    146  1.1  cgd STATIC int
    147  1.1  cgd docd(dest, print)
    148  1.1  cgd 	char *dest;
    149  1.1  cgd 	{
    150  1.1  cgd 	register char *p;
    151  1.1  cgd 	register char *q;
    152  1.1  cgd 	char *symlink;
    153  1.1  cgd 	char *component;
    154  1.1  cgd 	struct stat statb;
    155  1.1  cgd 	int first;
    156  1.1  cgd 	int i;
    157  1.1  cgd 
    158  1.1  cgd 	TRACE(("docd(\"%s\", %d) called\n", dest, print));
    159  1.1  cgd 
    160  1.1  cgd top:
    161  1.1  cgd 	cdcomppath = dest;
    162  1.1  cgd 	STARTSTACKSTR(p);
    163  1.1  cgd 	if (*dest == '/') {
    164  1.1  cgd 		STPUTC('/', p);
    165  1.1  cgd 		cdcomppath++;
    166  1.1  cgd 	}
    167  1.1  cgd 	first = 1;
    168  1.1  cgd 	while ((q = getcomponent()) != NULL) {
    169  1.1  cgd 		if (q[0] == '\0' || q[0] == '.' && q[1] == '\0')
    170  1.1  cgd 			continue;
    171  1.1  cgd 		if (! first)
    172  1.1  cgd 			STPUTC('/', p);
    173  1.1  cgd 		first = 0;
    174  1.1  cgd 		component = q;
    175  1.1  cgd 		while (*q)
    176  1.1  cgd 			STPUTC(*q++, p);
    177  1.1  cgd 		if (equal(component, ".."))
    178  1.1  cgd 			continue;
    179  1.1  cgd 		STACKSTRNUL(p);
    180  1.1  cgd 		if (lstat(stackblock(), &statb) < 0)
    181  1.1  cgd 			error("lstat %s failed", stackblock());
    182  1.1  cgd 		if ((statb.st_mode & S_IFMT) != S_IFLNK)
    183  1.1  cgd 			continue;
    184  1.1  cgd 
    185  1.1  cgd 		/* Hit a symbolic link.  We have to start all over again. */
    186  1.1  cgd 		print = 1;
    187  1.1  cgd 		STPUTC('\0', p);
    188  1.1  cgd 		symlink = grabstackstr(p);
    189  1.1  cgd 		i = (int)statb.st_size + 2;		/* 2 for '/' and '\0' */
    190  1.1  cgd 		if (cdcomppath != NULL)
    191  1.1  cgd 			i += strlen(cdcomppath);
    192  1.1  cgd 		p = stalloc(i);
    193  1.1  cgd 		if (readlink(symlink, p, (int)statb.st_size) < 0) {
    194  1.1  cgd 			error("readlink %s failed", stackblock());
    195  1.1  cgd 		}
    196  1.1  cgd 		if (cdcomppath != NULL) {
    197  1.1  cgd 			p[(int)statb.st_size] = '/';
    198  1.1  cgd 			scopy(cdcomppath, p + (int)statb.st_size + 1);
    199  1.1  cgd 		} else {
    200  1.1  cgd 			p[(int)statb.st_size] = '\0';
    201  1.1  cgd 		}
    202  1.1  cgd 		if (p[0] != '/') {	/* relative path name */
    203  1.1  cgd 			char *r;
    204  1.1  cgd 			q = r = symlink;
    205  1.1  cgd 			while (*q) {
    206  1.1  cgd 				if (*q++ == '/')
    207  1.1  cgd 					r = q;
    208  1.1  cgd 			}
    209  1.1  cgd 			*r = '\0';
    210  1.1  cgd 			dest = stalloc(strlen(symlink) + strlen(p) + 1);
    211  1.1  cgd 			scopy(symlink, dest);
    212  1.1  cgd 			strcat(dest, p);
    213  1.1  cgd 		} else {
    214  1.1  cgd 			dest = p;
    215  1.1  cgd 		}
    216  1.1  cgd 		goto top;
    217  1.1  cgd 	}
    218  1.1  cgd 	STPUTC('\0', p);
    219  1.1  cgd 	p = grabstackstr(p);
    220  1.1  cgd 	INTOFF;
    221  1.1  cgd 	if (chdir(p) < 0) {
    222  1.1  cgd 		INTON;
    223  1.1  cgd 		return -1;
    224  1.1  cgd 	}
    225  1.1  cgd 	updatepwd(p);
    226  1.1  cgd 	INTON;
    227  1.1  cgd 	if (print && iflag)
    228  1.1  cgd 		out1fmt("%s\n", p);
    229  1.1  cgd 	return 0;
    230  1.1  cgd }
    231  1.1  cgd #endif /* SYMLINKS */
    232  1.1  cgd 
    233  1.1  cgd 
    234  1.1  cgd 
    235  1.1  cgd /*
    236  1.1  cgd  * Get the next component of the path name pointed to by cdcomppath.
    237  1.1  cgd  * This routine overwrites the string pointed to by cdcomppath.
    238  1.1  cgd  */
    239  1.1  cgd 
    240  1.1  cgd STATIC char *
    241  1.1  cgd getcomponent() {
    242  1.1  cgd 	register char *p;
    243  1.1  cgd 	char *start;
    244  1.1  cgd 
    245  1.1  cgd 	if ((p = cdcomppath) == NULL)
    246  1.1  cgd 		return NULL;
    247  1.1  cgd 	start = cdcomppath;
    248  1.1  cgd 	while (*p != '/' && *p != '\0')
    249  1.1  cgd 		p++;
    250  1.1  cgd 	if (*p == '\0') {
    251  1.1  cgd 		cdcomppath = NULL;
    252  1.1  cgd 	} else {
    253  1.1  cgd 		*p++ = '\0';
    254  1.1  cgd 		cdcomppath = p;
    255  1.1  cgd 	}
    256  1.1  cgd 	return start;
    257  1.1  cgd }
    258  1.1  cgd 
    259  1.1  cgd 
    260  1.1  cgd 
    261  1.1  cgd /*
    262  1.1  cgd  * Update curdir (the name of the current directory) in response to a
    263  1.1  cgd  * cd command.  We also call hashcd to let the routines in exec.c know
    264  1.1  cgd  * that the current directory has changed.
    265  1.1  cgd  */
    266  1.1  cgd 
    267  1.1  cgd void hashcd();
    268  1.1  cgd 
    269  1.1  cgd STATIC void
    270  1.1  cgd updatepwd(dir)
    271  1.1  cgd 	char *dir;
    272  1.1  cgd 	{
    273  1.1  cgd 	char *new;
    274  1.1  cgd 	char *p;
    275  1.1  cgd 
    276  1.1  cgd 	hashcd();				/* update command hash table */
    277  1.1  cgd 	cdcomppath = stalloc(strlen(dir) + 1);
    278  1.1  cgd 	scopy(dir, cdcomppath);
    279  1.1  cgd 	STARTSTACKSTR(new);
    280  1.1  cgd 	if (*dir != '/') {
    281  1.1  cgd 		if (curdir == NULL)
    282  1.1  cgd 			return;
    283  1.1  cgd 		p = curdir;
    284  1.1  cgd 		while (*p)
    285  1.1  cgd 			STPUTC(*p++, new);
    286  1.1  cgd 		if (p[-1] == '/')
    287  1.1  cgd 			STUNPUTC(new);
    288  1.1  cgd 	}
    289  1.1  cgd 	while ((p = getcomponent()) != NULL) {
    290  1.1  cgd 		if (equal(p, "..")) {
    291  1.1  cgd 			while (new > stackblock() && (STUNPUTC(new), *new) != '/');
    292  1.1  cgd 		} else if (*p != '\0' && ! equal(p, ".")) {
    293  1.1  cgd 			STPUTC('/', new);
    294  1.1  cgd 			while (*p)
    295  1.1  cgd 				STPUTC(*p++, new);
    296  1.1  cgd 		}
    297  1.1  cgd 	}
    298  1.1  cgd 	if (new == stackblock())
    299  1.1  cgd 		STPUTC('/', new);
    300  1.1  cgd 	STACKSTRNUL(new);
    301  1.6  jtc 	INTOFF;
    302  1.6  jtc 	if (prevdir)
    303  1.6  jtc 		ckfree(prevdir);
    304  1.6  jtc 	prevdir = curdir;
    305  1.1  cgd 	curdir = savestr(stackblock());
    306  1.6  jtc 	INTON;
    307  1.1  cgd }
    308  1.1  cgd 
    309  1.1  cgd 
    310  1.1  cgd 
    311  1.1  cgd int
    312  1.1  cgd pwdcmd(argc, argv)  char **argv; {
    313  1.1  cgd 	getpwd();
    314  1.1  cgd 	out1str(curdir);
    315  1.1  cgd 	out1c('\n');
    316  1.1  cgd 	return 0;
    317  1.1  cgd }
    318  1.1  cgd 
    319  1.1  cgd 
    320  1.1  cgd 
    321  1.1  cgd /*
    322  1.1  cgd  * Run /bin/pwd to find out what the current directory is.  We suppress
    323  1.1  cgd  * interrupts throughout most of this, but the user can still break out
    324  1.1  cgd  * of it by killing the pwd program.  If we already know the current
    325  1.1  cgd  * directory, this routine returns immediately.
    326  1.1  cgd  */
    327  1.1  cgd 
    328  1.1  cgd #define MAXPWD 256
    329  1.1  cgd 
    330  1.1  cgd STATIC void
    331  1.1  cgd getpwd() {
    332  1.1  cgd 	char buf[MAXPWD];
    333  1.1  cgd 	char *p;
    334  1.1  cgd 	int i;
    335  1.1  cgd 	int status;
    336  1.1  cgd 	struct job *jp;
    337  1.1  cgd 	int pip[2];
    338  1.1  cgd 
    339  1.1  cgd 	if (curdir)
    340  1.1  cgd 		return;
    341  1.1  cgd 	INTOFF;
    342  1.1  cgd 	if (pipe(pip) < 0)
    343  1.1  cgd 		error("Pipe call failed");
    344  1.1  cgd 	jp = makejob((union node *)NULL, 1);
    345  1.1  cgd 	if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
    346  1.1  cgd 		close(pip[0]);
    347  1.1  cgd 		if (pip[1] != 1) {
    348  1.1  cgd 			close(1);
    349  1.1  cgd 			copyfd(pip[1], 1);
    350  1.1  cgd 			close(pip[1]);
    351  1.1  cgd 		}
    352  1.1  cgd 		execl("/bin/pwd", "pwd", (char *)0);
    353  1.6  jtc 		error("Cannot exec /bin/pwd");
    354  1.1  cgd 	}
    355  1.1  cgd 	close(pip[1]);
    356  1.1  cgd 	pip[1] = -1;
    357  1.1  cgd 	p = buf;
    358  1.1  cgd 	while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
    359  1.1  cgd 	     || i == -1 && errno == EINTR) {
    360  1.1  cgd 		if (i > 0)
    361  1.1  cgd 			p += i;
    362  1.1  cgd 	}
    363  1.1  cgd 	close(pip[0]);
    364  1.1  cgd 	pip[0] = -1;
    365  1.1  cgd 	status = waitforjob(jp);
    366  1.1  cgd 	if (status != 0)
    367  1.1  cgd 		error((char *)0);
    368  1.1  cgd 	if (i < 0 || p == buf || p[-1] != '\n')
    369  1.1  cgd 		error("pwd command failed");
    370  1.1  cgd 	p[-1] = '\0';
    371  1.1  cgd 	curdir = savestr(buf);
    372  1.1  cgd 	INTON;
    373  1.1  cgd }
    374