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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
50 * The cd and pwd commands.
55 #include "nodes.h" /* for jobs.h */
67 static int cdlogical(char *);
68 static int cdphysical(char *);
69 static int docd(char *, int, int);
70 static char *getcomponent(void);
71 static char *findcwd(char *);
72 static void updatepwd(char *);
73 static char *getpwd(void);
74 static char *getpwd2(void);
76 static char *curdir = NULL; /* current working directory */
77 static char *prevdir; /* previous working directory */
78 static char *cdcomppath;
81 cdcmd(int argc, char **argv)
87 int ch, phys, print = 0;
89 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
91 while ((ch = getopt(argc, argv, "LP")) != -1) {
100 error("unknown option: -%c", optopt);
108 error("too many arguments");
110 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
111 error("HOME not set");
114 if (dest[0] == '-' && dest[1] == '\0') {
115 dest = prevdir ? prevdir : curdir;
121 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
123 while ((p = padvance(&path, dest)) != NULL) {
124 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
129 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
130 print = strcmp(p + 2, dest);
132 print = strcmp(p, dest);
134 if (docd(p, print, phys) >= 0)
138 error("can't cd to %s", dest);
145 * Actually change the directory. In an interactive shell, print the
146 * directory name if "print" is nonzero.
149 docd(char *dest, int print, int phys)
152 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
154 /* If logical cd fails, fall back to physical. */
155 if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
158 if (print && iflag && curdir)
159 out1fmt("%s\n", curdir);
165 cdlogical(char *dest)
175 * Check each component of the path. If we find a symlink or
176 * something we can't stat, clear curdir to force a getcwd()
177 * next time we get the value of the current directory.
180 cdcomppath = stalloc(strlen(dest) + 1);
181 scopy(dest, cdcomppath);
188 while ((q = getcomponent()) != NULL) {
189 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
196 if (equal(component, ".."))
199 if (lstat(stackblock(), &statb) < 0) {
206 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
216 cdphysical(char *dest)
221 if (chdir(dest) < 0) {
227 warning("warning: failed to get name of current directory");
234 * Get the next component of the path name pointed to by cdcomppath.
235 * This routine overwrites the string pointed to by cdcomppath.
243 if ((p = cdcomppath) == NULL)
246 while (*p != '/' && *p != '\0')
265 * If our argument is NULL, we don't know the current directory
266 * any more because we traversed a symbolic link or something
267 * we couldn't stat().
269 if (dir == NULL || curdir == NULL)
271 cdcomppath = stalloc(strlen(dir) + 1);
272 scopy(dir, cdcomppath);
276 if (STTOPC(new) == '/')
279 while ((p = getcomponent()) != NULL) {
280 if (equal(p, "..")) {
281 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
282 } else if (*p != '\0' && ! equal(p, ".")) {
287 if (new == stackblock())
294 * Update curdir (the name of the current directory) in response to a
295 * cd command. We also call hashcd to let the routines in exec.c know
296 * that the current directory has changed.
301 hashcd(); /* update command hash table */
306 curdir = dir ? savestr(dir) : NULL;
307 setvar("PWD", curdir, VEXPORT);
308 setvar("OLDPWD", prevdir, VEXPORT);
312 pwdcmd(int argc, char **argv)
317 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
319 while ((ch = getopt(argc, argv, "LP")) != -1) {
328 error("unknown option: -%c", optopt);
336 error("too many arguments");
338 if (!phys && getpwd()) {
342 if ((p = getpwd2()) == NULL)
343 error(".: %s", strerror(errno));
352 * Get the current directory and cache the result in curdir.
372 * Return the current directory.
380 for (i = MAXPWD;; i *= 2) {
382 if (getcwd(pwd, i) != NULL)
393 * Initialize PWD in a new shell.
394 * If the shell is interactive, we need to warn if this fails.
400 struct stat stdot, stpwd;
402 pwd = lookupvar("PWD");
403 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
404 stat(pwd, &stpwd) != -1 &&
405 stdot.st_dev == stpwd.st_dev &&
406 stdot.st_ino == stpwd.st_ino) {
409 curdir = savestr(pwd);
411 if (getpwd() == NULL && warn)
412 out2fmt_flush("sh: cannot determine working directory\n");
413 setvar("PWD", curdir, VEXPORT);