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 int updatepwd(char *);
73 STATIC char *curdir = NULL; /* current working directory */
74 STATIC char *prevdir; /* previous working directory */
75 STATIC char *cdcomppath;
78 cdcmd(int argc, char **argv)
84 int ch, phys, print = 0;
86 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
88 while ((ch = getopt(argc, argv, "LP")) != -1) {
97 error("unknown option: -%c", optopt);
105 error("too many arguments");
107 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
108 error("HOME not set");
111 if (dest[0] == '-' && dest[1] == '\0') {
112 dest = prevdir ? prevdir : curdir;
118 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
120 while ((p = padvance(&path, dest)) != NULL) {
121 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
126 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
128 print = strcmp(p, dest);
130 if (docd(p, print, phys) >= 0)
134 error("can't cd to %s", dest);
141 * Actually change the directory. In an interactive shell, print the
142 * directory name if "print" is nonzero.
145 docd(char *dest, int print, int phys)
148 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
150 /* If logical cd fails, fall back to physical. */
151 if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
154 if (print && iflag && curdir)
155 out1fmt("%s\n", curdir);
161 cdlogical(char *dest)
171 * Check each component of the path. If we find a symlink or
172 * something we can't stat, clear curdir to force a getcwd()
173 * next time we get the value of the current directory.
176 cdcomppath = stalloc(strlen(dest) + 1);
177 scopy(dest, cdcomppath);
184 while ((q = getcomponent()) != NULL) {
185 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
193 if (equal(component, ".."))
196 if (lstat(stackblock(), &statb) < 0) {
203 if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
212 cdphysical(char *dest)
216 if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
225 * Get the next component of the path name pointed to by cdcomppath.
226 * This routine overwrites the string pointed to by cdcomppath.
234 if ((p = cdcomppath) == NULL)
237 while (*p != '/' && *p != '\0')
250 * Update curdir (the name of the current directory) in response to a
251 * cd command. We also call hashcd to let the routines in exec.c know
252 * that the current directory has changed.
260 hashcd(); /* update command hash table */
263 * If our argument is NULL, we don't know the current directory
264 * any more because we traversed a symbolic link or something
265 * we couldn't stat().
267 if (dir == NULL || curdir == NULL) {
273 if (getpwd() == NULL) {
277 setvar("PWD", curdir, VEXPORT);
278 setvar("OLDPWD", prevdir, VEXPORT);
282 cdcomppath = stalloc(strlen(dir) + 1);
283 scopy(dir, cdcomppath);
292 while ((p = getcomponent()) != NULL) {
293 if (equal(p, "..")) {
294 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
295 } else if (*p != '\0' && ! equal(p, ".")) {
301 if (new == stackblock())
308 curdir = savestr(stackblock());
309 setvar("PWD", curdir, VEXPORT);
310 setvar("OLDPWD", prevdir, VEXPORT);
317 pwdcmd(int argc, char **argv)
322 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
324 while ((ch = getopt(argc, argv, "LP")) != -1) {
333 error("unknown option: -%c", optopt);
341 error("too many arguments");
343 if (!phys && getpwd()) {
347 if (getcwd(buf, sizeof(buf)) == NULL)
348 error(".: %s", strerror(errno));
357 * Find out what the current directory is. If we already know the current
358 * directory, this routine returns immediately.
367 if (getcwd(buf, sizeof(buf)) == NULL) {
368 char *pwd = getenv("PWD");
369 struct stat stdot, stpwd;
371 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
372 stat(pwd, &stpwd) != -1 &&
373 stdot.st_dev == stpwd.st_dev &&
374 stdot.st_ino == stpwd.st_ino) {
375 curdir = savestr(pwd);
380 curdir = savestr(buf);