2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
41 static const char rcsid[] =
42 "$Id: cd.c,v 1.17 1998/05/18 06:43:30 charnier Exp $";
45 #include <sys/types.h>
53 * The cd and pwd commands.
58 #include "nodes.h" /* for jobs.h */
70 STATIC int docd __P((char *, int));
71 STATIC char *getcomponent __P((void));
72 STATIC void updatepwd __P((char *));
74 char *curdir = NULL; /* current working directory */
75 char *prevdir; /* previous working directory */
76 STATIC char *cdcomppath;
90 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
91 error("HOME not set");
94 if (dest[0] == '-' && dest[1] == '\0') {
95 dest = prevdir ? prevdir : curdir;
101 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
103 while ((p = padvance(&path, dest)) != NULL) {
104 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
109 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
111 print = strcmp(p, dest);
113 if (docd(p, print) >= 0)
118 error("can't cd to %s", dest);
125 * Actually do the chdir. In an interactive shell, print the
126 * directory name if "print" is nonzero.
140 TRACE(("docd(\"%s\", %d) called\n", dest, print));
143 * Check each component of the path. If we find a symlink or
144 * something we can't stat, clear curdir to force a getcwd()
145 * next time we get the value of the current directory.
148 cdcomppath = stalloc(strlen(dest) + 1);
149 scopy(dest, cdcomppath);
156 while ((q = getcomponent()) != NULL) {
157 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
165 if (equal(component, ".."))
168 if ((lstat(stackblock(), &statb) < 0)
169 || (S_ISLNK(statb.st_mode))) {
177 if (chdir(dest) < 0) {
181 updatepwd(badstat ? NULL : dest);
183 if (print && iflag && curdir)
184 out1fmt("%s\n", curdir);
190 * Get the next component of the path name pointed to by cdcomppath.
191 * This routine overwrites the string pointed to by cdcomppath.
199 if ((p = cdcomppath) == NULL)
202 while (*p != '/' && *p != '\0')
215 * Update curdir (the name of the current directory) in response to a
216 * cd command. We also call hashcd to let the routines in exec.c know
217 * that the current directory has changed.
226 hashcd(); /* update command hash table */
229 * If our argument is NULL, we don't know the current directory
230 * any more because we traversed a symbolic link or something
231 * we couldn't stat().
233 if (dir == NULL || curdir == NULL) {
239 if (getpwd() == NULL)
240 error("getcwd() failed: %s", strerror(errno));
241 setvar("PWD", curdir, VEXPORT | VTEXTFIXED);
242 setvar("OLDPWD", prevdir, VEXPORT | VTEXTFIXED);
246 cdcomppath = stalloc(strlen(dir) + 1);
247 scopy(dir, cdcomppath);
256 while ((p = getcomponent()) != NULL) {
257 if (equal(p, "..")) {
258 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
259 } else if (*p != '\0' && ! equal(p, ".")) {
265 if (new == stackblock())
272 curdir = savestr(stackblock());
273 setvar("PWD", curdir, VEXPORT | VTEXTFIXED);
274 setvar("OLDPWD", prevdir, VEXPORT | VTEXTFIXED);
282 char **argv __unused;
285 error("getcwd() failed: %s", strerror(errno));
297 * Find out what the current directory is. If we already know the current
298 * directory, this routine returns immediately.
308 * Things are a bit complicated here; we could have just used
309 * getcwd, but traditionally getcwd is implemented using popen
310 * to /bin/pwd. This creates a problem for us, since we cannot
311 * keep track of the job if it is being ran behind our backs.
312 * So we re-implement getcwd(), and we suppress interrupts
313 * throughout the process. This is not completely safe, since
314 * the user can still break out of it by killing the pwd program.
315 * We still try to use getcwd for systems that we know have a
316 * c implementation of getcwd, that does not open a pipe to
319 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__SVR4)
321 if (getcwd(buf, sizeof(buf)) == NULL) {
322 char *pwd = getenv("PWD");
323 struct stat stdot, stpwd;
325 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
326 stat(pwd, &stpwd) != -1 &&
327 stdot.st_dev == stpwd.st_dev &&
328 stdot.st_ino == stpwd.st_ino) {
329 curdir = savestr(pwd);
334 curdir = savestr(buf);
345 error("Pipe call failed");
346 jp = makejob((union node *)NULL, 1);
347 if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
348 (void) close(pip[0]);
354 (void) execl("/bin/pwd", "pwd", (char *)0);
355 error("Cannot exec /bin/pwd");
357 (void) close(pip[1]);
360 while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
361 || (i == -1 && errno == EINTR)) {
365 (void) close(pip[0]);
367 status = waitforjob(jp);
370 if (i < 0 || p == buf || p[-1] != '\n')
371 error("pwd command failed");
374 curdir = savestr(buf);