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 */
68 static int cdlogical(char *);
69 static int cdphysical(char *);
70 static int docd(char *, int, int);
71 static char *getcomponent(void);
72 static char *findcwd(char *);
73 static void updatepwd(char *);
74 static char *getpwd(void);
75 static char *getpwd2(void);
77 static char *curdir = NULL; /* current working directory */
78 static char *prevdir; /* previous working directory */
79 static char *cdcomppath;
82 cdcmd(int argc, char **argv)
88 int ch, phys, print = 0, getcwderr = 0;
92 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
94 while ((ch = getopt(argc, argv, "eLP")) != -1) {
106 error("unknown option: -%c", optopt);
114 error("too many arguments");
116 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
117 error("HOME not set");
120 if (dest[0] == '-' && dest[1] == '\0') {
121 dest = prevdir ? prevdir : curdir;
127 if (dest[0] == '/' ||
128 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
129 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) ||
130 (path = bltinlookup("CDPATH", 1)) == NULL)
132 while ((p = padvance(&path, dest)) != NULL) {
133 if (stat(p, &statb) < 0) {
136 } else if (!S_ISDIR(statb.st_mode))
143 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
144 print = strcmp(p + 2, dest);
146 print = strcmp(p, dest);
148 rc = docd(p, print, phys);
150 return getcwderr ? rc : 0;
155 error("%s: %s", dest, strerror(errno1));
162 * Actually change the directory. In an interactive shell, print the
163 * directory name if "print" is nonzero.
166 docd(char *dest, int print, int phys)
170 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
172 /* If logical cd fails, fall back to physical. */
173 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0)
176 if (print && iflag && curdir)
177 out1fmt("%s\n", curdir);
183 cdlogical(char *dest)
193 * Check each component of the path. If we find a symlink or
194 * something we can't stat, clear curdir to force a getcwd()
195 * next time we get the value of the current directory.
198 cdcomppath = stalloc(strlen(dest) + 1);
199 scopy(dest, cdcomppath);
206 while ((q = getcomponent()) != NULL) {
207 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
214 if (equal(component, ".."))
217 if (lstat(stackblock(), &statb) < 0) {
224 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
234 cdphysical(char *dest)
240 if (chdir(dest) < 0) {
246 warning("warning: failed to get name of current directory");
255 * Get the next component of the path name pointed to by cdcomppath.
256 * This routine overwrites the string pointed to by cdcomppath.
264 if ((p = cdcomppath) == NULL)
267 while (*p != '/' && *p != '\0')
286 * If our argument is NULL, we don't know the current directory
287 * any more because we traversed a symbolic link or something
288 * we couldn't stat().
290 if (dir == NULL || curdir == NULL)
292 cdcomppath = stalloc(strlen(dir) + 1);
293 scopy(dir, cdcomppath);
297 if (STTOPC(new) == '/')
300 while ((p = getcomponent()) != NULL) {
301 if (equal(p, "..")) {
302 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
303 } else if (*p != '\0' && ! equal(p, ".")) {
308 if (new == stackblock())
315 * Update curdir (the name of the current directory) in response to a
316 * cd command. We also call hashcd to let the routines in exec.c know
317 * that the current directory has changed.
322 hashcd(); /* update command hash table */
327 curdir = dir ? savestr(dir) : NULL;
328 setvar("PWD", curdir, VEXPORT);
329 setvar("OLDPWD", prevdir, VEXPORT);
333 pwdcmd(int argc, char **argv)
338 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
340 while ((ch = getopt(argc, argv, "LP")) != -1) {
349 error("unknown option: -%c", optopt);
357 error("too many arguments");
359 if (!phys && getpwd()) {
363 if ((p = getpwd2()) == NULL)
364 error(".: %s", strerror(errno));
373 * Get the current directory and cache the result in curdir.
393 * Return the current directory.
401 for (i = MAXPWD;; i *= 2) {
403 if (getcwd(pwd, i) != NULL)
414 * Initialize PWD in a new shell.
415 * If the shell is interactive, we need to warn if this fails.
421 struct stat stdot, stpwd;
423 pwd = lookupvar("PWD");
424 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
425 stat(pwd, &stpwd) != -1 &&
426 stdot.st_dev == stpwd.st_dev &&
427 stdot.st_ino == stpwd.st_ino) {
430 curdir = savestr(pwd);
432 if (getpwd() == NULL && warn)
433 out2fmt_flush("sh: cannot determine working directory\n");
434 setvar("PWD", curdir, VEXPORT);