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
36
#include <sys/cdefs.h>
43
static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
45
__RCSID("$NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl Exp $");
49
#include <sys/types.h>
57
* The cd and pwd commands.
62
#include "nodes.h" /* for jobs.h */
74
STATIC int docd(char *, int);
75
STATIC char *getcomponent(void);
76
STATIC void updatepwd(char *);
77
STATIC void find_curdir(int noerror);
79
char *curdir = NULL; /* current working directory */
80
char *prevdir; /* previous working directory */
81
STATIC char *cdcomppath;
84
cdcmd(int argc, char **argv)
90
int print = cdprint; /* set -cdprint to enable */
92
(void)argc; (void)argv;
97
* Try (quite hard) to have 'curdir' defined, nothing has set
98
* it on entry to the shell, but we want 'cd fred; cd -' to work.
103
dest = bltinlookup("HOME", 1);
105
error("HOME not set");
108
/* Do 'ksh' style substitution */
110
error("PWD not set");
111
p = strstr(curdir, dest);
113
error("bad substitution");
114
d = stalloc(strlen(curdir) + strlen(argptr[1]) + 1);
115
memcpy(d, curdir, p - curdir);
116
strcpy(d + (p - curdir), argptr[1]);
117
strcat(d, p + strlen(dest));
123
if (dest[0] == '-' && dest[1] == '\0') {
124
dest = prevdir ? prevdir : curdir;
129
if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
131
while ((p = padvance(&path, dest)) != NULL) {
132
if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
137
if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
139
print = strcmp(p, dest);
141
if (docd(p, print) >= 0)
146
error("can't cd to %s", dest);
152
* Actually do the chdir. In an interactive shell, print the
153
* directory name if "print" is nonzero.
157
docd(char *dest, int print)
166
TRACE(("docd(\"%s\", %d) called\n", dest, print));
169
* Check each component of the path. If we find a symlink or
170
* something we can't stat, clear curdir to force a getcwd()
171
* next time we get the value of the current directory.
174
cdcomppath = stalloc(strlen(dest) + 1);
175
scopy(dest, cdcomppath);
182
while ((q = getcomponent()) != NULL) {
183
if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
191
if (equal(component, ".."))
194
if ((lstat(stackblock(), &statb) < 0)
195
|| (S_ISLNK(statb.st_mode))) {
203
if (chdir(dest) < 0) {
207
updatepwd(badstat ? NULL : dest);
209
if (print && iflag && curdir)
210
out1fmt("%s\n", curdir);
216
* Get the next component of the path name pointed to by cdcomppath.
217
* This routine overwrites the string pointed to by cdcomppath.
226
if ((p = cdcomppath) == NULL)
229
while (*p != '/' && *p != '\0')
243
* Update curdir (the name of the current directory) in response to a
244
* cd command. We also call hashcd to let the routines in exec.c know
245
* that the current directory has changed.
254
hashcd(); /* update command hash table */
257
* If our argument is NULL, we don't know the current directory
258
* any more because we traversed a symbolic link or something
259
* we couldn't stat().
261
if (dir == NULL || curdir == NULL) {
270
setvar("PWD", curdir, VEXPORT);
275
cdcomppath = stalloc(strlen(dir) + 1);
276
scopy(dir, cdcomppath);
285
while ((p = getcomponent()) != NULL) {
286
if (equal(p, "..")) {
287
while (new > stackblock() && (STUNPUTC(new), *new) != '/');
288
} else if (*p != '\0' && ! equal(p, ".")) {
294
if (new == stackblock())
301
curdir = savestr(stackblock());
302
setvar("PWD", curdir, VEXPORT);
307
* Posix says the default should be 'pwd -L' (as below), however
308
* the 'cd' command (above) does something much nearer to the
309
* posix 'cd -P' (not the posix default of 'cd -L').
310
* If 'cd' is changed to support -P/L then the default here
311
* needs to be revisited if the historic behaviour is to be kept.
315
pwdcmd(int argc, char **argv)
320
(void)argc; (void)argv;
322
while ((i = nextopt("LP")) != '\0')
325
error("unexpected argument");
332
setvar("PWD", curdir, VEXPORT);
341
#define MAXPWD PATH_MAX
344
* Find out what the current directory is. If we already know the current
345
* directory, this routine returns immediately.
351
struct stat stdot, stpwd;
352
static int first = 1;
360
if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
361
stat(pwd, &stpwd) != -1 &&
362
stdot.st_dev == stpwd.st_dev &&
363
stdot.st_ino == stpwd.st_ino) {
364
curdir = savestr(pwd);
369
find_curdir(noerror);
375
find_curdir(int noerror)
381
* Things are a bit complicated here; we could have just used
382
* getcwd, but traditionally getcwd is implemented using popen
383
* to /bin/pwd. This creates a problem for us, since we cannot
384
* keep track of the job if it is being ran behind our backs.
385
* So we re-implement getcwd(), and we suppress interrupts
386
* throughout the process. This is not completely safe, since
387
* the user can still break out of it by killing the pwd program.
388
* We still try to use getcwd for systems that we know have a
389
* c implementation of getcwd, that does not open a pipe to
392
#if defined(__NetBSD__) || defined(__SVR4) || defined(__linux__)
394
for (i = MAXPWD;; i *= 2) {
396
if (getcwd(pwd, i) != NULL) {
397
curdir = savestr(pwd);
404
error("getcwd() failed: %s", strerror(errno));
414
pwd = stalloc(MAXPWD);
417
error("Pipe call failed");
418
jp = makejob((union node *)NULL, 1);
419
if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
420
(void) close(pip[0]);
426
(void) execl("/bin/pwd", "pwd", (char *)0);
427
error("Cannot exec /bin/pwd");
429
(void) close(pip[1]);
432
while ((i = read(pip[0], p, pwd + MAXPWD - p)) > 0
433
|| (i == -1 && errno == EINTR)) {
437
(void) close(pip[0]);
439
status = waitforjob(jp);
442
if (i < 0 || p == pwd || p[-1] != '\n') {
447
error("pwd command failed");
451
curdir = savestr(pwd);