gdb - Local mods (compile)
[dragonfly.git] / bin / sh / cd.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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.
69763be3 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
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.
31 */
32
02d0b1ce
MD
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
40
984263bc
MD
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>
48
49/*
50 * The cd and pwd commands.
51 */
52
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"
c3e3bc7a 66#include "builtins.h"
984263bc 67
99512ac4
PA
68static int cdlogical(char *);
69static int cdphysical(char *);
70static int docd(char *, int, int);
71static char *getcomponent(void);
72static char *findcwd(char *);
73static void updatepwd(char *);
74static char *getpwd(void);
75static char *getpwd2(void);
984263bc 76
99512ac4
PA
77static char *curdir = NULL; /* current working directory */
78static char *prevdir; /* previous working directory */
79static char *cdcomppath;
984263bc
MD
80
81int
f00eae14 82cdcmd(int argc __unused, char **argv __unused)
984263bc 83{
492efe05
CP
84 const char *dest;
85 const char *path;
984263bc
MD
86 char *p;
87 struct stat statb;
8c603ea4
PA
88 int ch, phys, print = 0, getcwderr = 0;
89 int rc;
72cbf401 90 int errno1 = ENOENT;
984263bc 91
984263bc 92 phys = Pflag;
f00eae14 93 while ((ch = nextopt("eLP")) != '\0') {
984263bc 94 switch (ch) {
8c603ea4
PA
95 case 'e':
96 getcwderr = 1;
97 break;
984263bc
MD
98 case 'L':
99 phys = 0;
100 break;
101 case 'P':
102 phys = 1;
103 break;
984263bc
MD
104 }
105 }
984263bc 106
f00eae14 107 if (*argptr != NULL && argptr[1] != NULL)
984263bc
MD
108 error("too many arguments");
109
f00eae14 110 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
984263bc
MD
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 = ".";
120 }
db98aac0
PA
121 if (dest[0] == '/' ||
122 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
123 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) ||
124 (path = bltinlookup("CDPATH", 1)) == NULL)
02d0b1ce 125 path = "";
984263bc 126 while ((p = padvance(&path, dest)) != NULL) {
2b271b9b
PA
127 if (stat(p, &statb) < 0) {
128 if (errno != ENOENT)
129 errno1 = errno;
130 } else if (!S_ISDIR(statb.st_mode))
131 errno1 = ENOTDIR;
132 else {
984263bc
MD
133 if (!print) {
134 /*
135 * XXX - rethink
136 */
137 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
27be99cb
PA
138 print = strcmp(p + 2, dest);
139 else
140 print = strcmp(p, dest);
984263bc 141 }
8c603ea4
PA
142 rc = docd(p, print, phys);
143 if (rc >= 0)
144 return getcwderr ? rc : 0;
72cbf401
PA
145 if (errno != ENOENT)
146 errno1 = errno;
984263bc
MD
147 }
148 }
72cbf401 149 error("%s: %s", dest, strerror(errno1));
984263bc
MD
150 /*NOTREACHED*/
151 return 0;
152}
153
154
155/*
156 * Actually change the directory. In an interactive shell, print the
157 * directory name if "print" is nonzero.
158 */
99512ac4 159static int
984263bc
MD
160docd(char *dest, int print, int phys)
161{
8c603ea4 162 int rc;
984263bc
MD
163
164 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
165
166 /* If logical cd fails, fall back to physical. */
8c603ea4 167 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0)
984263bc
MD
168 return (-1);
169
170 if (print && iflag && curdir)
171 out1fmt("%s\n", curdir);
172
8c603ea4 173 return (rc);
984263bc
MD
174}
175
99512ac4 176static int
984263bc
MD
177cdlogical(char *dest)
178{
179 char *p;
180 char *q;
181 char *component;
182 struct stat statb;
183 int first;
184 int badstat;
185
186 /*
187 * Check each component of the path. If we find a symlink or
188 * something we can't stat, clear curdir to force a getcwd()
189 * next time we get the value of the current directory.
190 */
191 badstat = 0;
02d0b1ce 192 cdcomppath = stsavestr(dest);
984263bc
MD
193 STARTSTACKSTR(p);
194 if (*dest == '/') {
195 STPUTC('/', p);
196 cdcomppath++;
197 }
198 first = 1;
199 while ((q = getcomponent()) != NULL) {
200 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
201 continue;
202 if (! first)
203 STPUTC('/', p);
204 first = 0;
205 component = q;
99512ac4 206 STPUTS(q, p);
984263bc
MD
207 if (equal(component, ".."))
208 continue;
209 STACKSTRNUL(p);
210 if (lstat(stackblock(), &statb) < 0) {
211 badstat = 1;
212 break;
213 }
214 }
215
216 INTOFF;
99512ac4 217 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
984263bc
MD
218 INTON;
219 return (-1);
220 }
99512ac4 221 updatepwd(p);
984263bc
MD
222 INTON;
223 return (0);
224}
225
99512ac4 226static int
984263bc
MD
227cdphysical(char *dest)
228{
99512ac4 229 char *p;
8c603ea4 230 int rc = 0;
984263bc
MD
231
232 INTOFF;
99512ac4 233 if (chdir(dest) < 0) {
984263bc
MD
234 INTON;
235 return (-1);
236 }
99512ac4 237 p = findcwd(NULL);
8c603ea4 238 if (p == NULL) {
99512ac4 239 warning("warning: failed to get name of current directory");
8c603ea4
PA
240 rc = 1;
241 }
99512ac4 242 updatepwd(p);
984263bc 243 INTON;
8c603ea4 244 return (rc);
984263bc
MD
245}
246
247/*
248 * Get the next component of the path name pointed to by cdcomppath.
249 * This routine overwrites the string pointed to by cdcomppath.
250 */
99512ac4 251static char *
984263bc
MD
252getcomponent(void)
253{
254 char *p;
255 char *start;
256
257 if ((p = cdcomppath) == NULL)
258 return NULL;
259 start = cdcomppath;
260 while (*p != '/' && *p != '\0')
261 p++;
262 if (*p == '\0') {
263 cdcomppath = NULL;
264 } else {
265 *p++ = '\0';
266 cdcomppath = p;
267 }
268 return start;
269}
270
271
99512ac4
PA
272static char *
273findcwd(char *dir)
984263bc
MD
274{
275 char *new;
276 char *p;
277
984263bc
MD
278 /*
279 * If our argument is NULL, we don't know the current directory
280 * any more because we traversed a symbolic link or something
281 * we couldn't stat().
282 */
99512ac4
PA
283 if (dir == NULL || curdir == NULL)
284 return getpwd2();
02d0b1ce 285 cdcomppath = stsavestr(dir);
984263bc
MD
286 STARTSTACKSTR(new);
287 if (*dir != '/') {
99512ac4
PA
288 STPUTS(curdir, new);
289 if (STTOPC(new) == '/')
984263bc
MD
290 STUNPUTC(new);
291 }
292 while ((p = getcomponent()) != NULL) {
293 if (equal(p, "..")) {
294 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
295 } else if (*p != '\0' && ! equal(p, ".")) {
296 STPUTC('/', new);
99512ac4 297 STPUTS(p, new);
984263bc
MD
298 }
299 }
300 if (new == stackblock())
301 STPUTC('/', new);
302 STACKSTRNUL(new);
99512ac4
PA
303 return stackblock();
304}
305
306/*
307 * Update curdir (the name of the current directory) in response to a
308 * cd command. We also call hashcd to let the routines in exec.c know
309 * that the current directory has changed.
310 */
311static void
312updatepwd(char *dir)
313{
314 hashcd(); /* update command hash table */
315
984263bc
MD
316 if (prevdir)
317 ckfree(prevdir);
318 prevdir = curdir;
99512ac4 319 curdir = dir ? savestr(dir) : NULL;
984263bc
MD
320 setvar("PWD", curdir, VEXPORT);
321 setvar("OLDPWD", prevdir, VEXPORT);
984263bc
MD
322}
323
324int
f00eae14 325pwdcmd(int argc __unused, char **argv __unused)
984263bc 326{
99512ac4 327 char *p;
984263bc
MD
328 int ch, phys;
329
984263bc 330 phys = Pflag;
f00eae14 331 while ((ch = nextopt("LP")) != '\0') {
984263bc
MD
332 switch (ch) {
333 case 'L':
334 phys = 0;
335 break;
336 case 'P':
337 phys = 1;
338 break;
984263bc
MD
339 }
340 }
984263bc 341
f00eae14 342 if (*argptr != NULL)
984263bc
MD
343 error("too many arguments");
344
345 if (!phys && getpwd()) {
346 out1str(curdir);
347 out1c('\n');
348 } else {
99512ac4 349 if ((p = getpwd2()) == NULL)
984263bc 350 error(".: %s", strerror(errno));
99512ac4 351 out1str(p);
984263bc
MD
352 out1c('\n');
353 }
354
355 return 0;
356}
357
358/*
99512ac4 359 * Get the current directory and cache the result in curdir.
984263bc 360 */
99512ac4 361static char *
984263bc
MD
362getpwd(void)
363{
99512ac4 364 char *p;
984263bc
MD
365
366 if (curdir)
367 return curdir;
99512ac4
PA
368
369 p = getpwd2();
370 if (p != NULL)
371 curdir = savestr(p);
984263bc
MD
372
373 return curdir;
374}
99512ac4
PA
375
376#define MAXPWD 256
377
378/*
379 * Return the current directory.
380 */
381static char *
382getpwd2(void)
383{
384 char *pwd;
385 int i;
386
387 for (i = MAXPWD;; i *= 2) {
388 pwd = stalloc(i);
389 if (getcwd(pwd, i) != NULL)
390 return pwd;
391 stunalloc(pwd);
392 if (errno != ERANGE)
393 break;
394 }
395
396 return NULL;
397}
398
399/*
400 * Initialize PWD in a new shell.
401 * If the shell is interactive, we need to warn if this fails.
402 */
403void
404pwd_init(int warn)
405{
406 char *pwd;
407 struct stat stdot, stpwd;
408
409 pwd = lookupvar("PWD");
410 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
411 stat(pwd, &stpwd) != -1 &&
412 stdot.st_dev == stpwd.st_dev &&
413 stdot.st_ino == stpwd.st_ino) {
414 if (curdir)
415 ckfree(curdir);
416 curdir = savestr(pwd);
417 }
418 if (getpwd() == NULL && warn)
419 out2fmt_flush("sh: cannot determine working directory\n");
420 setvar("PWD", curdir, VEXPORT);
421}