add opendir alias
[minix.git] / commands / ash / cd.c
blob8513128ed54ebdc62357f4bfb66607e852f1efe7
1 /*-
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
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/cd.c,v 1.34 2004/04/06 20:06:51 markm Exp $");
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <limits.h>
52 * The cd and pwd commands.
55 #include "shell.h"
56 #include "var.h"
57 #include "nodes.h" /* for jobs.h */
58 #include "jobs.h"
59 #include "options.h"
60 #include "output.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "exec.h"
64 #include "redir.h"
65 #include "mystring.h"
66 #include "builtins.h"
67 #include "show.h"
68 #include "cd.h"
70 STATIC int cdlogical(const char *);
71 STATIC int cdphysical(const char *);
72 STATIC int docd(const char *, int, int);
73 STATIC char *getcomponent(void);
74 STATIC int updatepwd(const char *);
76 STATIC char *curdir = NULL; /* current working directory */
77 STATIC char *prevdir; /* previous working directory */
78 STATIC char *cdcomppath;
80 int
81 cdcmd(int argc, char **argv)
83 char *dest;
84 char *path;
85 char *p;
86 struct stat statb;
87 int ch, phys, print = 0;
89 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
90 phys = Pflag;
91 while ((ch = getopt(argc, argv, "LP")) != -1) {
92 switch (ch) {
93 case 'L':
94 phys = 0;
95 break;
96 case 'P':
97 phys = 1;
98 break;
99 default:
100 error("unknown option: -%c", optopt);
101 break;
104 argc -= optind;
105 argv += optind;
107 if (argc > 1)
108 error("too many arguments");
110 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
111 error("HOME not set");
112 if (*dest == '\0')
113 dest = ".";
114 if (dest[0] == '-' && dest[1] == '\0') {
115 dest = prevdir ? prevdir : curdir;
116 if (dest)
117 print = 1;
118 else
119 dest = ".";
121 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
122 path = nullstr;
123 while ((p = padvance(&path, dest)) != NULL) {
124 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
125 if (!print) {
127 * XXX - rethink
129 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
130 p += 2;
131 print = strcmp(p, dest);
133 if (docd(p, print, phys) >= 0)
134 return 0;
137 error("can't cd to %s", dest);
138 /*NOTREACHED*/
139 return 0;
144 * Actually change the directory. In an interactive shell, print the
145 * directory name if "print" is nonzero.
147 STATIC int
148 docd(const char *dest, int print, int phys)
151 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
153 /* If logical cd fails, fall back to physical. */
154 if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
155 return (-1);
157 if (print && iflag && curdir)
158 out1fmt("%s\n", curdir);
160 return 0;
163 STATIC int
164 cdlogical(const char *dest)
166 char *p;
167 char *q;
168 char *component;
169 struct stat statb;
170 int first;
171 int badstat;
174 * Check each component of the path. If we find a symlink or
175 * something we can't stat, clear curdir to force a getcwd()
176 * next time we get the value of the current directory.
178 badstat = 0;
179 cdcomppath = stalloc(strlen(dest) + 1);
180 scopy(dest, cdcomppath);
181 STARTSTACKSTR(p);
182 if (*dest == '/') {
183 STPUTC('/', p);
184 cdcomppath++;
186 first = 1;
187 while ((q = getcomponent()) != NULL) {
188 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
189 continue;
190 if (! first)
191 STPUTC('/', p);
192 first = 0;
193 component = q;
194 while (*q)
195 STPUTC(*q++, p);
196 if (equal(component, ".."))
197 continue;
198 STACKSTRNUL(p);
199 if (lstat(stackblock(), &statb) < 0) {
200 badstat = 1;
201 break;
205 INTOFF;
206 if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
207 INTON;
208 return (-1);
210 INTON;
211 return (0);
214 STATIC int
215 cdphysical(const char *dest)
218 INTOFF;
219 if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
220 INTON;
221 return (-1);
223 INTON;
224 return (0);
228 * Get the next component of the path name pointed to by cdcomppath.
229 * This routine overwrites the string pointed to by cdcomppath.
231 STATIC char *
232 getcomponent(void)
234 char *p;
235 char *start;
237 if ((p = cdcomppath) == NULL)
238 return NULL;
239 start = cdcomppath;
240 while (*p != '/' && *p != '\0')
241 p++;
242 if (*p == '\0') {
243 cdcomppath = NULL;
244 } else {
245 *p++ = '\0';
246 cdcomppath = p;
248 return start;
253 * Update curdir (the name of the current directory) in response to a
254 * cd command. We also call hashcd to let the routines in exec.c know
255 * that the current directory has changed.
257 STATIC int
258 updatepwd(const char *dir)
260 char *new;
261 char *p;
263 hashcd(); /* update command hash table */
266 * If our argument is NULL, we don't know the current directory
267 * any more because we traversed a symbolic link or something
268 * we couldn't stat().
270 if (dir == NULL || curdir == NULL) {
271 if (prevdir)
272 ckfree(prevdir);
273 INTOFF;
274 prevdir = curdir;
275 curdir = NULL;
276 if (getpwd() == NULL) {
277 INTON;
278 return (-1);
280 setvar("PWD", curdir, VEXPORT);
281 setvar("OLDPWD", prevdir, VEXPORT);
282 INTON;
283 return (0);
285 cdcomppath = stalloc(strlen(dir) + 1);
286 scopy(dir, cdcomppath);
287 STARTSTACKSTR(new);
288 if (*dir != '/') {
289 p = curdir;
290 while (*p)
291 STPUTC(*p++, new);
292 if (p[-1] == '/')
293 STUNPUTC(new);
295 while ((p = getcomponent()) != NULL) {
296 if (equal(p, "..")) {
297 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
298 } else if (*p != '\0' && ! equal(p, ".")) {
299 STPUTC('/', new);
300 while (*p)
301 STPUTC(*p++, new);
304 if (new == stackblock())
305 STPUTC('/', new);
306 STACKSTRNUL(new);
307 INTOFF;
308 if (prevdir)
309 ckfree(prevdir);
310 prevdir = curdir;
311 curdir = savestr(stackblock());
312 setvar("PWD", curdir, VEXPORT);
313 setvar("OLDPWD", prevdir, VEXPORT);
314 INTON;
316 return (0);
320 pwdcmd(int argc, char **argv)
322 char buf[PATH_MAX];
323 int ch, phys;
325 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
326 phys = Pflag;
327 while ((ch = getopt(argc, argv, "LP")) != -1) {
328 switch (ch) {
329 case 'L':
330 phys = 0;
331 break;
332 case 'P':
333 phys = 1;
334 break;
335 default:
336 error("unknown option: -%c", optopt);
337 break;
340 argc -= optind;
341 argv += optind;
343 if (argc != 0)
344 error("too many arguments");
346 if (!phys && getpwd()) {
347 out1str(curdir);
348 out1c('\n');
349 } else {
350 if (getcwd(buf, sizeof(buf)) == NULL)
351 error(".: %s", strerror(errno));
352 out1str(buf);
353 out1c('\n');
356 return 0;
360 * Find out what the current directory is. If we already know the current
361 * directory, this routine returns immediately.
363 char *
364 getpwd(void)
366 char buf[PATH_MAX];
368 if (curdir)
369 return curdir;
370 if (getcwd(buf, sizeof(buf)) == NULL) {
371 char *pwd = getenv("PWD");
372 struct stat stdot, stpwd;
374 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
375 stat(pwd, &stpwd) != -1 &&
376 stdot.st_dev == stpwd.st_dev &&
377 stdot.st_ino == stpwd.st_ino) {
378 curdir = savestr(pwd);
379 return curdir;
381 return NULL;
383 curdir = savestr(buf);
385 return curdir;
389 * $PchId: cd.c,v 1.6 2006/05/22 12:42:03 philip Exp $