1 /* $NetBSD: cd.c,v 1.39 2006/05/04 11:16:53 simonb 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 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
40 __RCSID("$NetBSD: cd.c,v 1.39 2006/05/04 11:16:53 simonb Exp $");
44 #include <sys/types.h>
52 * The cd and pwd commands.
57 #include "nodes.h" /* for jobs.h */
69 STATIC
int docd(const char *, int);
70 STATIC
char *getcomponent(void);
71 STATIC
void updatepwd(const char *);
72 STATIC
void find_curdir(int noerror
);
74 char *curdir
= NULL
; /* current working directory */
75 char *prevdir
; /* previous working directory */
76 STATIC
char *cdcomppath
;
79 cdcmd(int argc
, char **argv
)
85 int print
= cdprint
; /* set -cdprint to enable */
87 while (nextopt("P") != '\0')
91 * Try (quite hard) to have 'curdir' defined, nothing has set
92 * it on entry to the shell, but we want 'cd fred; cd -' to work.
97 dest
= bltinlookup("HOME", 1);
99 error("HOME not set");
102 /* Do 'ksh' style substitution */
104 error("PWD not set");
105 p
= strstr(curdir
, dest
);
107 error("bad substitution");
108 d
= stalloc(strlen(curdir
) + strlen(argptr
[1]) + 1);
109 memcpy(d
, curdir
, p
- curdir
);
110 strcpy(d
+ (p
- curdir
), argptr
[1]);
111 strcat(d
, p
+ strlen(dest
));
117 if (dest
[0] == '-' && dest
[1] == '\0') {
118 dest
= prevdir
? prevdir
: curdir
;
124 if (*p
== '.' && *++p
== '.')
126 if (*p
== 0 || *p
== '/' || (path
= bltinlookup("CDPATH", 1)) == NULL
)
128 while ((p
= padvance(&path
, dest
)) != NULL
) {
129 if (stat(p
, &statb
) >= 0 && S_ISDIR(statb
.st_mode
)) {
134 if (p
[0] == '.' && p
[1] == '/' && p
[2] != '\0')
135 print
= strcmp(p
+ 2, dest
);
137 print
= strcmp(p
, dest
);
139 if (docd(p
, print
) >= 0)
144 error("can't cd to %s", dest
);
150 * Actually do the chdir. In an interactive shell, print the
151 * directory name if "print" is nonzero.
155 docd(const char *dest
, int print
)
164 TRACE(("docd(\"%s\", %d) called\n", dest
, print
));
167 * Check each component of the path. If we find a symlink or
168 * something we can't stat, clear curdir to force a getcwd()
169 * next time we get the value of the current directory.
172 cdcomppath
= stalloc(strlen(dest
) + 1);
173 scopy(dest
, cdcomppath
);
180 while ((q
= getcomponent()) != NULL
) {
181 if (q
[0] == '\0' || (q
[0] == '.' && q
[1] == '\0'))
189 if (equal(component
, ".."))
192 if ((lstat(stackblock(), &statb
) < 0)
193 || (S_ISLNK(statb
.st_mode
))) {
201 if (chdir(dest
) < 0) {
205 updatepwd(badstat
? NULL
: dest
);
207 if (print
&& iflag
== 1 && curdir
)
208 out1fmt("%s\n", curdir
);
214 * Get the next component of the path name pointed to by cdcomppath.
215 * This routine overwrites the string pointed to by cdcomppath.
224 if ((p
= cdcomppath
) == NULL
)
227 while (*p
!= '/' && *p
!= '\0')
241 * Update curdir (the name of the current directory) in response to a
242 * cd command. We also call hashcd to let the routines in exec.c know
243 * that the current directory has changed.
247 updatepwd(const char *dir
)
252 hashcd(); /* update command hash table */
255 * If our argument is NULL, we don't know the current directory
256 * any more because we traversed a symbolic link or something
257 * we couldn't stat().
259 if (dir
== NULL
|| curdir
== NULL
) {
268 setvar("PWD", curdir
, VEXPORT
);
273 cdcomppath
= stalloc(strlen(dir
) + 1);
274 scopy(dir
, cdcomppath
);
283 while ((p
= getcomponent()) != NULL
) {
284 if (equal(p
, "..")) {
285 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
286 } else if (*p
!= '\0' && ! equal(p
, ".")) {
292 if (new == stackblock())
299 curdir
= savestr(stackblock());
300 setvar("PWD", curdir
, VEXPORT
);
305 * Posix says the default should be 'pwd -L' (as below), however
306 * the 'cd' command (above) does something much nearer to the
307 * posix 'cd -P' (not the posix default of 'cd -L').
308 * If 'cd' is changed to support -P/L then the default here
309 * needs to be revisited if the historic behaviour is to be kept.
313 pwdcmd(int argc
, char **argv
)
318 while ((i
= nextopt("LP")) != '\0')
321 error("unexpected argument");
328 setvar("PWD", curdir
, VEXPORT
);
341 setvar("PWD", curdir
, VEXPORT
);
343 sh_warnx("Cannot determine current working directory");
349 * Find out what the current directory is. If we already know the current
350 * directory, this routine returns immediately.
356 struct stat stdot
, stpwd
;
357 static int first
= 1;
365 if (pwd
&& *pwd
== '/' && stat(".", &stdot
) != -1 &&
366 stat(pwd
, &stpwd
) != -1 &&
367 stdot
.st_dev
== stpwd
.st_dev
&&
368 stdot
.st_ino
== stpwd
.st_ino
) {
369 curdir
= savestr(pwd
);
374 find_curdir(noerror
);
380 find_curdir(int noerror
)
386 * Things are a bit complicated here; we could have just used
387 * getcwd, but traditionally getcwd is implemented using popen
388 * to /bin/pwd. This creates a problem for us, since we cannot
389 * keep track of the job if it is being ran behind our backs.
390 * So we re-implement getcwd(), and we suppress interrupts
391 * throughout the process. This is not completely safe, since
392 * the user can still break out of it by killing the pwd program.
393 * We still try to use getcwd for systems that we know have a
394 * c implementation of getcwd, that does not open a pipe to
397 #if defined(__NetBSD__) || defined(__SVR4)
399 for (i
= MAXPWD
;; i
*= 2) {
401 if (getcwd(pwd
, i
) != NULL
) {
402 curdir
= savestr(pwd
);
409 error("getcwd() failed: %s", strerror(errno
));
419 pwd
= stalloc(MAXPWD
);
422 error("Pipe call failed");
423 jp
= makejob((union node
*)NULL
, 1);
424 if (forkshell(jp
, (union node
*)NULL
, FORK_NOJOB
) == 0) {
425 (void) close(pip
[0]);
431 (void) execl("/bin/pwd", "pwd", (char *)0);
432 error("Cannot exec /bin/pwd");
434 (void) close(pip
[1]);
437 while ((i
= read(pip
[0], p
, pwd
+ MAXPWD
- p
)) > 0
438 || (i
== -1 && errno
== EINTR
)) {
442 (void) close(pip
[0]);
444 status
= waitforjob(jp
);
447 if (i
< 0 || p
== pwd
|| p
[-1] != '\n') {
452 error("pwd command failed");
456 curdir
= savestr(pwd
);