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
36 * @(#)cd.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/cd.c,v 1.50 2012/01/13 23:32:27 jilles Exp $
40 #include <sys/types.h>
49 * The cd and pwd commands.
54 #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, getcwderr = 0;
91 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
93 while ((ch = getopt(argc, argv, "eLP")) != -1) {
105 error("unknown option: -%c", optopt);
113 error("too many arguments");
115 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
116 error("HOME not set");
119 if (dest[0] == '-' && dest[1] == '\0') {
120 dest = prevdir ? prevdir : curdir;
126 if (dest[0] == '/' ||
127 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
128 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) ||
129 (path = bltinlookup("CDPATH", 1)) == NULL)
131 while ((p = padvance(&path, dest)) != NULL) {
132 if (stat(p, &statb) < 0) {
135 } else if (!S_ISDIR(statb.st_mode))
142 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
143 print = strcmp(p + 2, dest);
145 print = strcmp(p, dest);
147 rc = docd(p, print, phys);
149 return getcwderr ? rc : 0;
154 error("%s: %s", dest, strerror(errno1));
161 * Actually change the directory. In an interactive shell, print the
162 * directory name if "print" is nonzero.
165 docd(char *dest, int print, int phys)
169 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
171 /* If logical cd fails, fall back to physical. */
172 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0)
175 if (print && iflag && curdir)
176 out1fmt("%s\n", curdir);
182 cdlogical(char *dest)
192 * Check each component of the path. If we find a symlink or
193 * something we can't stat, clear curdir to force a getcwd()
194 * next time we get the value of the current directory.
197 cdcomppath = stalloc(strlen(dest) + 1);
198 scopy(dest, cdcomppath);
205 while ((q = getcomponent()) != NULL) {
206 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
213 if (equal(component, ".."))
216 if (lstat(stackblock(), &statb) < 0) {
223 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
233 cdphysical(char *dest)
239 if (chdir(dest) < 0) {
245 warning("warning: failed to get name of current directory");
254 * Get the next component of the path name pointed to by cdcomppath.
255 * This routine overwrites the string pointed to by cdcomppath.
263 if ((p = cdcomppath) == NULL)
266 while (*p != '/' && *p != '\0')
285 * If our argument is NULL, we don't know the current directory
286 * any more because we traversed a symbolic link or something
287 * we couldn't stat().
289 if (dir == NULL || curdir == NULL)
291 cdcomppath = stalloc(strlen(dir) + 1);
292 scopy(dir, cdcomppath);
296 if (STTOPC(new) == '/')
299 while ((p = getcomponent()) != NULL) {
300 if (equal(p, "..")) {
301 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
302 } else if (*p != '\0' && ! equal(p, ".")) {
307 if (new == stackblock())
314 * Update curdir (the name of the current directory) in response to a
315 * cd command. We also call hashcd to let the routines in exec.c know
316 * that the current directory has changed.
321 hashcd(); /* update command hash table */
326 curdir = dir ? savestr(dir) : NULL;
327 setvar("PWD", curdir, VEXPORT);
328 setvar("OLDPWD", prevdir, VEXPORT);
332 pwdcmd(int argc, char **argv)
337 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
339 while ((ch = getopt(argc, argv, "LP")) != -1) {
348 error("unknown option: -%c", optopt);
356 error("too many arguments");
358 if (!phys && getpwd()) {
362 if ((p = getpwd2()) == NULL)
363 error(".: %s", strerror(errno));
372 * Get the current directory and cache the result in curdir.
392 * Return the current directory.
400 for (i = MAXPWD;; i *= 2) {
402 if (getcwd(pwd, i) != NULL)
413 * Initialize PWD in a new shell.
414 * If the shell is interactive, we need to warn if this fails.
420 struct stat stdot, stpwd;
422 pwd = lookupvar("PWD");
423 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
424 stat(pwd, &stpwd) != -1 &&
425 stdot.st_dev == stpwd.st_dev &&
426 stdot.st_ino == stpwd.st_ino) {
429 curdir = savestr(pwd);
431 if (getpwd() == NULL && warn)
432 out2fmt_flush("sh: cannot determine working directory\n");
433 setvar("PWD", curdir, VEXPORT);