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. 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
32 * @(#)cd.c 8.2 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/cd.c 240541 2012-09-15 21:56:30Z jilles $
36 #include <sys/types.h>
45 * The cd and pwd commands.
50 #include "nodes.h" /* for jobs.h */
63 static int cdlogical(char *);
64 static int cdphysical(char *);
65 static int docd(char *, int, int);
66 static char *getcomponent(void);
67 static char *findcwd(char *);
68 static void updatepwd(char *);
69 static char *getpwd(void);
70 static char *getpwd2(void);
72 static char *curdir = NULL; /* current working directory */
73 static char *prevdir; /* previous working directory */
74 static char *cdcomppath;
77 cdcmd(int argc __unused, char **argv __unused)
83 int ch, phys, print = 0, getcwderr = 0;
88 while ((ch = nextopt("eLP")) != '\0') {
102 if (*argptr != NULL && argptr[1] != NULL)
103 error("too many arguments");
105 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
106 error("HOME not set");
109 if (dest[0] == '-' && dest[1] == '\0') {
110 dest = prevdir ? prevdir : curdir;
116 if (dest[0] == '/' ||
117 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
118 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) ||
119 (path = bltinlookup("CDPATH", 1)) == NULL)
121 while ((p = padvance(&path, dest)) != NULL) {
122 if (stat(p, &statb) < 0) {
125 } else if (!S_ISDIR(statb.st_mode))
132 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
133 print = strcmp(p + 2, dest);
135 print = strcmp(p, dest);
137 rc = docd(p, print, phys);
139 return getcwderr ? rc : 0;
144 error("%s: %s", dest, strerror(errno1));
151 * Actually change the directory. In an interactive shell, print the
152 * directory name if "print" is nonzero.
155 docd(char *dest, int print, int phys)
159 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
161 /* If logical cd fails, fall back to physical. */
162 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0)
165 if (print && iflag && curdir)
166 out1fmt("%s\n", curdir);
172 cdlogical(char *dest)
182 * Check each component of the path. If we find a symlink or
183 * something we can't stat, clear curdir to force a getcwd()
184 * next time we get the value of the current directory.
187 cdcomppath = stalloc(strlen(dest) + 1);
188 scopy(dest, cdcomppath);
195 while ((q = getcomponent()) != NULL) {
196 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
203 if (equal(component, ".."))
206 if (lstat(stackblock(), &statb) < 0) {
213 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
223 cdphysical(char *dest)
229 if (chdir(dest) < 0) {
235 warning("warning: failed to get name of current directory");
244 * Get the next component of the path name pointed to by cdcomppath.
245 * This routine overwrites the string pointed to by cdcomppath.
253 if ((p = cdcomppath) == NULL)
256 while (*p != '/' && *p != '\0')
275 * If our argument is NULL, we don't know the current directory
276 * any more because we traversed a symbolic link or something
277 * we couldn't stat().
279 if (dir == NULL || curdir == NULL)
281 cdcomppath = stalloc(strlen(dir) + 1);
282 scopy(dir, cdcomppath);
286 if (STTOPC(new) == '/')
289 while ((p = getcomponent()) != NULL) {
290 if (equal(p, "..")) {
291 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
292 } else if (*p != '\0' && ! equal(p, ".")) {
297 if (new == stackblock())
304 * Update curdir (the name of the current directory) in response to a
305 * cd command. We also call hashcd to let the routines in exec.c know
306 * that the current directory has changed.
311 hashcd(); /* update command hash table */
316 curdir = dir ? savestr(dir) : NULL;
317 setvar("PWD", curdir, VEXPORT);
318 setvar("OLDPWD", prevdir, VEXPORT);
322 pwdcmd(int argc __unused, char **argv __unused)
328 while ((ch = nextopt("LP")) != '\0') {
340 error("too many arguments");
342 if (!phys && getpwd()) {
346 if ((p = getpwd2()) == NULL)
347 error(".: %s", strerror(errno));
356 * Get the current directory and cache the result in curdir.
376 * Return the current directory.
384 for (i = MAXPWD;; i *= 2) {
386 if (getcwd(pwd, i) != NULL)
397 * Initialize PWD in a new shell.
398 * If the shell is interactive, we need to warn if this fails.
404 struct stat stdot, stpwd;
406 pwd = lookupvar("PWD");
407 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
408 stat(pwd, &stpwd) != -1 &&
409 stdot.st_dev == stpwd.st_dev &&
410 stdot.st_ino == stpwd.st_ino) {
413 curdir = savestr(pwd);
415 if (getpwd() == NULL && warn)
416 out2fmt_flush("sh: cannot determine working directory\n");
417 setvar("PWD", curdir, VEXPORT);