MFC: Set count to a negative value for an initial burst.
[dragonfly.git] / bin / sh / cd.c
blobf281ca9484a0e38f0b5df1c701e19f21a8fa665e
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 * 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
34 * SUCH DAMAGE.
36 * @(#)cd.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/cd.c,v 1.35 2006/06/12 21:06:00 stefanf Exp $
38 * $DragonFly: src/bin/sh/cd.c,v 1.5 2007/01/05 22:18:52 pavalos Exp $
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <limits.h>
50 * The cd and pwd commands.
53 #include "shell.h"
54 #include "var.h"
55 #include "nodes.h" /* for jobs.h */
56 #include "jobs.h"
57 #include "options.h"
58 #include "output.h"
59 #include "memalloc.h"
60 #include "error.h"
61 #include "exec.h"
62 #include "redir.h"
63 #include "mystring.h"
64 #include "show.h"
65 #include "cd.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;
77 int
78 cdcmd(int argc, char **argv)
80 const char *dest;
81 const char *path;
82 char *p;
83 struct stat statb;
84 int ch, phys, print = 0;
86 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
87 phys = Pflag;
88 while ((ch = getopt(argc, argv, "LP")) != -1) {
89 switch (ch) {
90 case 'L':
91 phys = 0;
92 break;
93 case 'P':
94 phys = 1;
95 break;
96 default:
97 error("unknown option: -%c", optopt);
98 break;
101 argc -= optind;
102 argv += optind;
104 if (argc > 1)
105 error("too many arguments");
107 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
108 error("HOME not set");
109 if (*dest == '\0')
110 dest = ".";
111 if (dest[0] == '-' && dest[1] == '\0') {
112 dest = prevdir ? prevdir : curdir;
113 if (dest)
114 print = 1;
115 else
116 dest = ".";
118 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
119 path = nullstr;
120 while ((p = padvance(&path, dest)) != NULL) {
121 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
122 if (!print) {
124 * XXX - rethink
126 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
127 print = strcmp(p + 2, dest);
128 else
129 print = strcmp(p, dest);
131 if (docd(p, print, phys) >= 0)
132 return 0;
135 error("can't cd to %s", dest);
136 /*NOTREACHED*/
137 return 0;
142 * Actually change the directory. In an interactive shell, print the
143 * directory name if "print" is nonzero.
145 STATIC int
146 docd(char *dest, int print, int phys)
149 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
151 /* If logical cd fails, fall back to physical. */
152 if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
153 return (-1);
155 if (print && iflag && curdir)
156 out1fmt("%s\n", curdir);
158 return 0;
161 STATIC int
162 cdlogical(char *dest)
164 char *p;
165 char *q;
166 char *component;
167 struct stat statb;
168 int first;
169 int badstat;
172 * Check each component of the path. If we find a symlink or
173 * something we can't stat, clear curdir to force a getcwd()
174 * next time we get the value of the current directory.
176 badstat = 0;
177 cdcomppath = stalloc(strlen(dest) + 1);
178 scopy(dest, cdcomppath);
179 STARTSTACKSTR(p);
180 if (*dest == '/') {
181 STPUTC('/', p);
182 cdcomppath++;
184 first = 1;
185 while ((q = getcomponent()) != NULL) {
186 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
187 continue;
188 if (! first)
189 STPUTC('/', p);
190 first = 0;
191 component = q;
192 while (*q)
193 STPUTC(*q++, p);
194 if (equal(component, ".."))
195 continue;
196 STACKSTRNUL(p);
197 if (lstat(stackblock(), &statb) < 0) {
198 badstat = 1;
199 break;
203 INTOFF;
204 if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
205 INTON;
206 return (-1);
208 INTON;
209 return (0);
212 STATIC int
213 cdphysical(char *dest)
216 INTOFF;
217 if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
218 INTON;
219 return (-1);
221 INTON;
222 return (0);
226 * Get the next component of the path name pointed to by cdcomppath.
227 * This routine overwrites the string pointed to by cdcomppath.
229 STATIC char *
230 getcomponent(void)
232 char *p;
233 char *start;
235 if ((p = cdcomppath) == NULL)
236 return NULL;
237 start = cdcomppath;
238 while (*p != '/' && *p != '\0')
239 p++;
240 if (*p == '\0') {
241 cdcomppath = NULL;
242 } else {
243 *p++ = '\0';
244 cdcomppath = p;
246 return start;
251 * Update curdir (the name of the current directory) in response to a
252 * cd command. We also call hashcd to let the routines in exec.c know
253 * that the current directory has changed.
255 STATIC int
256 updatepwd(char *dir)
258 char *new;
259 char *p;
261 hashcd(); /* update command hash table */
264 * If our argument is NULL, we don't know the current directory
265 * any more because we traversed a symbolic link or something
266 * we couldn't stat().
268 if (dir == NULL || curdir == NULL) {
269 if (prevdir)
270 ckfree(prevdir);
271 INTOFF;
272 prevdir = curdir;
273 curdir = NULL;
274 if (getpwd() == NULL) {
275 INTON;
276 return (-1);
278 setvar("PWD", curdir, VEXPORT);
279 setvar("OLDPWD", prevdir, VEXPORT);
280 INTON;
281 return (0);
283 cdcomppath = stalloc(strlen(dir) + 1);
284 scopy(dir, cdcomppath);
285 STARTSTACKSTR(new);
286 if (*dir != '/') {
287 p = curdir;
288 while (*p)
289 STPUTC(*p++, new);
290 if (p[-1] == '/')
291 STUNPUTC(new);
293 while ((p = getcomponent()) != NULL) {
294 if (equal(p, "..")) {
295 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
296 } else if (*p != '\0' && ! equal(p, ".")) {
297 STPUTC('/', new);
298 while (*p)
299 STPUTC(*p++, new);
302 if (new == stackblock())
303 STPUTC('/', new);
304 STACKSTRNUL(new);
305 INTOFF;
306 if (prevdir)
307 ckfree(prevdir);
308 prevdir = curdir;
309 curdir = savestr(stackblock());
310 setvar("PWD", curdir, VEXPORT);
311 setvar("OLDPWD", prevdir, VEXPORT);
312 INTON;
314 return (0);
318 pwdcmd(int argc, char **argv)
320 char buf[PATH_MAX];
321 int ch, phys;
323 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
324 phys = Pflag;
325 while ((ch = getopt(argc, argv, "LP")) != -1) {
326 switch (ch) {
327 case 'L':
328 phys = 0;
329 break;
330 case 'P':
331 phys = 1;
332 break;
333 default:
334 error("unknown option: -%c", optopt);
335 break;
338 argc -= optind;
339 argv += optind;
341 if (argc != 0)
342 error("too many arguments");
344 if (!phys && getpwd()) {
345 out1str(curdir);
346 out1c('\n');
347 } else {
348 if (getcwd(buf, sizeof(buf)) == NULL)
349 error(".: %s", strerror(errno));
350 out1str(buf);
351 out1c('\n');
354 return 0;
358 * Find out what the current directory is. If we already know the current
359 * directory, this routine returns immediately.
361 char *
362 getpwd(void)
364 char buf[PATH_MAX];
366 if (curdir)
367 return curdir;
368 if (getcwd(buf, sizeof(buf)) == NULL) {
369 char *pwd = getenv("PWD");
370 struct stat stdot, stpwd;
372 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
373 stat(pwd, &stpwd) != -1 &&
374 stdot.st_dev == stpwd.st_dev &&
375 stdot.st_ino == stpwd.st_ino) {
376 curdir = savestr(pwd);
377 return curdir;
379 return NULL;
381 curdir = savestr(buf);
383 return curdir;