1 /* $NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #ifdef HAVE_SYS_CDEFS_H
36 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl Exp $");
46 #include <sys/types.h>
54 * The cd and pwd commands.
59 #include "nodes.h" /* for jobs.h */
71 STATIC
int docd(char *, int);
72 STATIC
char *getcomponent(void);
73 STATIC
void updatepwd(char *);
74 STATIC
void find_curdir(int noerror
);
76 char *curdir
= NULL
; /* current working directory */
77 char *prevdir
; /* previous working directory */
78 STATIC
char *cdcomppath
;
81 cdcmd(int argc
, char **argv
)
87 int print
= cdprint
; /* set -cdprint to enable */
92 * Try (quite hard) to have 'curdir' defined, nothing has set
93 * it on entry to the shell, but we want 'cd fred; cd -' to work.
98 dest
= bltinlookup("HOME", 1);
100 error("HOME not set");
103 /* Do 'ksh' style substitution */
105 error("PWD not set");
106 p
= strstr(curdir
, dest
);
108 error("bad substitution");
109 d
= stalloc(strlen(curdir
) + strlen(argptr
[1]) + 1);
110 memcpy(d
, curdir
, p
- curdir
);
111 strcpy(d
+ (p
- curdir
), argptr
[1]);
112 strcat(d
, p
+ strlen(dest
));
118 if (dest
[0] == '-' && dest
[1] == '\0') {
119 dest
= prevdir
? prevdir
: curdir
;
124 if (IS_ROOT(dest
) || (path
= bltinlookup("CDPATH", 1)) == NULL
)
126 while ((p
= padvance(&path
, dest
)) != NULL
) {
127 if (stat(p
, &statb
) >= 0 && S_ISDIR(statb
.st_mode
)) {
132 if (p
[0] == '.' && p
[1] == '/' && p
[2] != '\0')
134 print
= strcmp(p
, dest
);
136 if (docd(p
, print
) >= 0)
141 error("can't cd to %s", dest
);
147 * Actually do the chdir. In an interactive shell, print the
148 * directory name if "print" is nonzero.
152 docd(char *dest
, int print
)
161 TRACE(("docd(\"%s\", %d) called\n", dest
, print
));
164 * Check each component of the path. If we find a symlink or
165 * something we can't stat, clear curdir to force a getcwd()
166 * next time we get the value of the current directory.
169 cdcomppath
= stalloc(strlen(dest
) + 1);
170 scopy(dest
, cdcomppath
);
177 while ((q
= getcomponent()) != NULL
) {
178 if (q
[0] == '\0' || (q
[0] == '.' && q
[1] == '\0'))
186 if (equal(component
, ".."))
189 if ((lstat(stackblock(), &statb
) < 0)
190 || (S_ISLNK(statb
.st_mode
))) {
198 if (chdir(dest
) < 0) {
202 updatepwd(badstat
? NULL
: dest
);
204 if (print
&& iflag
&& curdir
)
205 out1fmt("%s\n", curdir
);
211 * Get the next component of the path name pointed to by cdcomppath.
212 * This routine overwrites the string pointed to by cdcomppath.
221 if ((p
= cdcomppath
) == NULL
)
224 while (*p
!= '/' && *p
!= '\0')
238 * Update curdir (the name of the current directory) in response to a
239 * cd command. We also call hashcd to let the routines in exec.c know
240 * that the current directory has changed.
249 hashcd(); /* update command hash table */
252 * If our argument is NULL, we don't know the current directory
253 * any more because we traversed a symbolic link or something
254 * we couldn't stat().
256 if (dir
== NULL
|| curdir
== NULL
) {
265 setvar("PWD", curdir
, VEXPORT
);
270 cdcomppath
= stalloc(strlen(dir
) + 1);
271 scopy(dir
, cdcomppath
);
280 while ((p
= getcomponent()) != NULL
) {
281 if (equal(p
, "..")) {
282 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
283 } else if (*p
!= '\0' && ! equal(p
, ".")) {
289 if (new == stackblock())
296 curdir
= savestr(stackblock());
297 setvar("PWD", curdir
, VEXPORT
);
302 * Posix says the default should be 'pwd -L' (as below), however
303 * the 'cd' command (above) does something much nearer to the
304 * posix 'cd -P' (not the posix default of 'cd -L').
305 * If 'cd' is changed to support -P/L then the default here
306 * needs to be revisited if the historic behaviour is to be kept.
310 pwdcmd(int argc
, char **argv
)
315 while ((i
= nextopt("LP")) != '\0')
318 error("unexpected argument");
325 setvar("PWD", curdir
, VEXPORT
);
337 * Find out what the current directory is. If we already know the current
338 * directory, this routine returns immediately.
344 struct stat stdot
, stpwd
;
345 static int first
= 1;
353 if (pwd
&& IS_ROOT(pwd
) && stat(".", &stdot
) != -1 &&
354 stat(pwd
, &stpwd
) != -1 &&
355 stdot
.st_dev
== stpwd
.st_dev
&&
356 stdot
.st_ino
== stpwd
.st_ino
) {
357 curdir
= savestr(pwd
);
362 find_curdir(noerror
);
368 find_curdir(int noerror
)
374 * Things are a bit complicated here; we could have just used
375 * getcwd, but traditionally getcwd is implemented using popen
376 * to /bin/pwd. This creates a problem for us, since we cannot
377 * keep track of the job if it is being ran behind our backs.
378 * So we re-implement getcwd(), and we suppress interrupts
379 * throughout the process. This is not completely safe, since
380 * the user can still break out of it by killing the pwd program.
381 * We still try to use getcwd for systems that we know have a
382 * c implementation of getcwd, that does not open a pipe to
385 #if defined(__NetBSD__) || defined(__SVR4) || defined(__INNOTEK_LIBC__) || 1 /* bird: nobody spawns pwd any more (we hope). */
387 for (i
= MAXPWD
;; i
*= 2) {
389 if (getcwd(pwd
, i
) != NULL
) {
390 curdir
= savestr(pwd
);
397 error("getcwd() failed: %s", strerror(errno
));
407 pwd
= stalloc(MAXPWD
);
410 error("Pipe call failed");
411 jp
= makejob((union node
*)NULL
, 1);
412 if (forkshell(jp
, (union node
*)NULL
, FORK_NOJOB
) == 0) {
413 (void) close(pip
[0]);
419 (void) execl("/bin/pwd", "pwd", (char *)0);
420 error("Cannot exec /bin/pwd");
422 (void) close(pip
[1]);
425 while ((i
= read(pip
[0], p
, pwd
+ MAXPWD
- p
)) > 0
426 || (i
== -1 && errno
== EINTR
)) {
430 (void) close(pip
[0]);
432 status
= waitforjob(jp
);
435 if (i
< 0 || p
== pwd
|| p
[-1] != '\n') {
440 error("pwd command failed");
444 curdir
= savestr(pwd
);