cd.c revision 1.52 1 1.52 kre /* $NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $ */
2 1.12 cgd
3 1.1 cgd /*-
4 1.6 jtc * Copyright (c) 1991, 1993
5 1.6 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.31 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.23 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.12 cgd #if 0
38 1.13 christos static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
39 1.12 cgd #else
40 1.52 kre __RCSID("$NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $");
41 1.12 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.9 cgd #include <sys/types.h>
45 1.9 cgd #include <sys/stat.h>
46 1.51 kre #include <stdbool.h>
47 1.13 christos #include <stdlib.h>
48 1.17 thorpej #include <string.h>
49 1.9 cgd #include <unistd.h>
50 1.9 cgd #include <errno.h>
51 1.9 cgd
52 1.1 cgd /*
53 1.1 cgd * The cd and pwd commands.
54 1.1 cgd */
55 1.1 cgd
56 1.1 cgd #include "shell.h"
57 1.1 cgd #include "var.h"
58 1.1 cgd #include "nodes.h" /* for jobs.h */
59 1.1 cgd #include "jobs.h"
60 1.1 cgd #include "options.h"
61 1.43 christos #include "builtins.h"
62 1.1 cgd #include "output.h"
63 1.1 cgd #include "memalloc.h"
64 1.1 cgd #include "error.h"
65 1.16 christos #include "exec.h"
66 1.13 christos #include "redir.h"
67 1.1 cgd #include "mystring.h"
68 1.13 christos #include "show.h"
69 1.14 christos #include "cd.h"
70 1.1 cgd
71 1.51 kre STATIC int docd(const char *, bool, bool);
72 1.29 christos STATIC char *getcomponent(void);
73 1.51 kre STATIC bool updatepwd(const char *);
74 1.34 dsl STATIC void find_curdir(int noerror);
75 1.51 kre STATIC bool is_curdir(const char *);
76 1.1 cgd
77 1.14 christos char *curdir = NULL; /* current working directory */
78 1.6 jtc char *prevdir; /* previous working directory */
79 1.1 cgd STATIC char *cdcomppath;
80 1.1 cgd
81 1.1 cgd int
82 1.29 christos cdcmd(int argc, char **argv)
83 1.10 cgd {
84 1.27 christos const char *dest;
85 1.48 kre const char *path, *cp;
86 1.48 kre char *p;
87 1.36 dsl char *d;
88 1.1 cgd struct stat statb;
89 1.51 kre char opt;
90 1.51 kre bool eopt = false;
91 1.48 kre int print = cdprint; /* set -o cdprint to enable */
92 1.1 cgd
93 1.51 kre while ((opt = nextopt("Pe")) != '\0')
94 1.51 kre if (opt == 'e')
95 1.51 kre eopt = true;
96 1.29 christos
97 1.34 dsl /*
98 1.34 dsl * Try (quite hard) to have 'curdir' defined, nothing has set
99 1.34 dsl * it on entry to the shell, but we want 'cd fred; cd -' to work.
100 1.34 dsl */
101 1.29 christos getpwd(1);
102 1.29 christos dest = *argptr;
103 1.29 christos if (dest == NULL) {
104 1.29 christos dest = bltinlookup("HOME", 1);
105 1.29 christos if (dest == NULL)
106 1.29 christos error("HOME not set");
107 1.48 kre } else if (argptr[1]) {
108 1.48 kre /* Do 'ksh' style substitution */
109 1.48 kre if (!curdir)
110 1.48 kre error("PWD not set");
111 1.48 kre p = strstr(curdir, dest);
112 1.48 kre if (!p)
113 1.48 kre error("bad substitution");
114 1.48 kre d = stalloc(strlen(curdir) + strlen(argptr[1]) + 1);
115 1.48 kre memcpy(d, curdir, p - curdir);
116 1.48 kre strcpy(d + (p - curdir), argptr[1]);
117 1.48 kre strcat(d, p + strlen(dest));
118 1.48 kre dest = d;
119 1.48 kre print = 1;
120 1.48 kre } else if (dest[0] == '-' && dest[1] == '\0') {
121 1.6 jtc dest = prevdir ? prevdir : curdir;
122 1.6 jtc print = 1;
123 1.6 jtc }
124 1.34 dsl if (*dest == '\0')
125 1.34 dsl dest = ".";
126 1.48 kre
127 1.48 kre cp = dest;
128 1.48 kre if (*cp == '.' && *++cp == '.')
129 1.48 kre cp++;
130 1.48 kre if (*cp == 0 || *cp == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
131 1.1 cgd path = nullstr;
132 1.48 kre while ((p = padvance(&path, dest, 0)) != NULL) {
133 1.48 kre stunalloc(p);
134 1.11 mycroft if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
135 1.48 kre int dopr = print;
136 1.51 kre int x;
137 1.48 kre
138 1.51 kre if (!print)
139 1.51 kre dopr = strcmp(p, dest);
140 1.6 jtc
141 1.51 kre if ((x = docd(p, dopr != 0, eopt)) >= 0)
142 1.51 kre return x;
143 1.6 jtc }
144 1.1 cgd }
145 1.1 cgd error("can't cd to %s", dest);
146 1.26 mycroft /* NOTREACHED */
147 1.1 cgd }
148 1.1 cgd
149 1.1 cgd
150 1.1 cgd /*
151 1.15 jtc * Actually do the chdir. In an interactive shell, print the
152 1.15 jtc * directory name if "print" is nonzero.
153 1.1 cgd */
154 1.1 cgd
155 1.1 cgd STATIC int
156 1.51 kre docd(const char *dest, bool print, bool eopt)
157 1.15 jtc {
158 1.51 kre bool gotpwd;
159 1.51 kre
160 1.48 kre #if 0 /* no "cd -L" (ever) so all this is just a waste of time ... */
161 1.20 tls char *p;
162 1.20 tls char *q;
163 1.19 cjs char *component;
164 1.19 cjs struct stat statb;
165 1.19 cjs int first;
166 1.19 cjs int badstat;
167 1.15 jtc
168 1.19 cjs /*
169 1.19 cjs * Check each component of the path. If we find a symlink or
170 1.19 cjs * something we can't stat, clear curdir to force a getcwd()
171 1.19 cjs * next time we get the value of the current directory.
172 1.19 cjs */
173 1.19 cjs badstat = 0;
174 1.19 cjs cdcomppath = stalloc(strlen(dest) + 1);
175 1.19 cjs scopy(dest, cdcomppath);
176 1.19 cjs STARTSTACKSTR(p);
177 1.19 cjs if (*dest == '/') {
178 1.19 cjs STPUTC('/', p);
179 1.19 cjs cdcomppath++;
180 1.19 cjs }
181 1.19 cjs first = 1;
182 1.19 cjs while ((q = getcomponent()) != NULL) {
183 1.19 cjs if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
184 1.19 cjs continue;
185 1.19 cjs if (! first)
186 1.19 cjs STPUTC('/', p);
187 1.19 cjs first = 0;
188 1.19 cjs component = q;
189 1.19 cjs while (*q)
190 1.19 cjs STPUTC(*q++, p);
191 1.19 cjs if (equal(component, ".."))
192 1.19 cjs continue;
193 1.19 cjs STACKSTRNUL(p);
194 1.47 christos if (lstat(stackblock(), &statb) < 0) {
195 1.19 cjs badstat = 1;
196 1.19 cjs break;
197 1.19 cjs }
198 1.19 cjs }
199 1.48 kre #endif
200 1.19 cjs
201 1.51 kre CTRACE(DBG_CMDS, ("docd(\"%s\", %s, %s) called\n", dest,
202 1.51 kre print ? "true" : "false", eopt ? "true" : "false"));
203 1.51 kre
204 1.1 cgd INTOFF;
205 1.1 cgd if (chdir(dest) < 0) {
206 1.1 cgd INTON;
207 1.1 cgd return -1;
208 1.1 cgd }
209 1.51 kre gotpwd = updatepwd(NULL); /* only do cd -P, no "pretend" -L mode */
210 1.1 cgd INTON;
211 1.51 kre if (print && (iflag || posix))
212 1.51 kre out1fmt("%s\n", gotpwd ? curdir : dest);
213 1.51 kre return gotpwd || !eopt ? 0 : 1;
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd
217 1.1 cgd /*
218 1.1 cgd * Get the next component of the path name pointed to by cdcomppath.
219 1.1 cgd * This routine overwrites the string pointed to by cdcomppath.
220 1.1 cgd */
221 1.1 cgd
222 1.1 cgd STATIC char *
223 1.36 dsl getcomponent(void)
224 1.29 christos {
225 1.20 tls char *p;
226 1.1 cgd char *start;
227 1.1 cgd
228 1.1 cgd if ((p = cdcomppath) == NULL)
229 1.1 cgd return NULL;
230 1.1 cgd start = cdcomppath;
231 1.1 cgd while (*p != '/' && *p != '\0')
232 1.1 cgd p++;
233 1.1 cgd if (*p == '\0') {
234 1.1 cgd cdcomppath = NULL;
235 1.1 cgd } else {
236 1.1 cgd *p++ = '\0';
237 1.1 cgd cdcomppath = p;
238 1.1 cgd }
239 1.1 cgd return start;
240 1.1 cgd }
241 1.1 cgd
242 1.1 cgd
243 1.1 cgd
244 1.1 cgd /*
245 1.1 cgd * Update curdir (the name of the current directory) in response to a
246 1.1 cgd * cd command. We also call hashcd to let the routines in exec.c know
247 1.1 cgd * that the current directory has changed.
248 1.1 cgd */
249 1.1 cgd
250 1.51 kre STATIC bool
251 1.36 dsl updatepwd(const char *dir)
252 1.29 christos {
253 1.1 cgd char *new;
254 1.1 cgd char *p;
255 1.1 cgd
256 1.1 cgd hashcd(); /* update command hash table */
257 1.19 cjs
258 1.19 cjs /*
259 1.19 cjs * If our argument is NULL, we don't know the current directory
260 1.19 cjs * any more because we traversed a symbolic link or something
261 1.51 kre * we couldn't stat(). Or we simply don't trust what we had.
262 1.19 cjs */
263 1.25 ross if (dir == NULL || curdir == NULL) {
264 1.19 cjs if (prevdir)
265 1.19 cjs ckfree(prevdir);
266 1.19 cjs INTOFF;
267 1.19 cjs prevdir = curdir;
268 1.19 cjs curdir = NULL;
269 1.29 christos getpwd(1);
270 1.19 cjs INTON;
271 1.42 uebayasi if (curdir) {
272 1.42 uebayasi setvar("OLDPWD", prevdir, VEXPORT);
273 1.29 christos setvar("PWD", curdir, VEXPORT);
274 1.51 kre return true;
275 1.42 uebayasi } else
276 1.29 christos unsetvar("PWD", 0);
277 1.51 kre return false;
278 1.19 cjs }
279 1.51 kre
280 1.51 kre /* XXX none of the following code is ever executed any more */
281 1.51 kre
282 1.1 cgd cdcomppath = stalloc(strlen(dir) + 1);
283 1.1 cgd scopy(dir, cdcomppath);
284 1.1 cgd STARTSTACKSTR(new);
285 1.1 cgd if (*dir != '/') {
286 1.1 cgd p = curdir;
287 1.1 cgd while (*p)
288 1.1 cgd STPUTC(*p++, new);
289 1.1 cgd if (p[-1] == '/')
290 1.1 cgd STUNPUTC(new);
291 1.1 cgd }
292 1.1 cgd while ((p = getcomponent()) != NULL) {
293 1.1 cgd if (equal(p, "..")) {
294 1.1 cgd while (new > stackblock() && (STUNPUTC(new), *new) != '/');
295 1.1 cgd } else if (*p != '\0' && ! equal(p, ".")) {
296 1.1 cgd STPUTC('/', new);
297 1.1 cgd while (*p)
298 1.1 cgd STPUTC(*p++, new);
299 1.1 cgd }
300 1.1 cgd }
301 1.1 cgd if (new == stackblock())
302 1.1 cgd STPUTC('/', new);
303 1.1 cgd STACKSTRNUL(new);
304 1.6 jtc INTOFF;
305 1.6 jtc if (prevdir)
306 1.6 jtc ckfree(prevdir);
307 1.6 jtc prevdir = curdir;
308 1.1 cgd curdir = savestr(stackblock());
309 1.42 uebayasi setvar("OLDPWD", prevdir, VEXPORT);
310 1.28 he setvar("PWD", curdir, VEXPORT);
311 1.6 jtc INTON;
312 1.51 kre return true;
313 1.51 kre }
314 1.51 kre
315 1.51 kre /*
316 1.51 kre * Test whether we are currently in the direcory given
317 1.51 kre * (provided it is given, and is absolute)
318 1.51 kre * ie: determine if path is fully qualified pathname of "."
319 1.51 kre */
320 1.51 kre STATIC bool
321 1.51 kre is_curdir(const char *path)
322 1.51 kre {
323 1.51 kre struct stat stdot, stpath;
324 1.51 kre
325 1.51 kre return path != NULL &&
326 1.51 kre *path == '/' &&
327 1.51 kre stat(".", &stdot) != -1 &&
328 1.51 kre stat(path, &stpath) != -1 &&
329 1.51 kre stdot.st_dev == stpath.st_dev &&
330 1.51 kre stdot.st_ino == stpath.st_ino;
331 1.1 cgd }
332 1.1 cgd
333 1.34 dsl /*
334 1.34 dsl * Posix says the default should be 'pwd -L' (as below), however
335 1.34 dsl * the 'cd' command (above) does something much nearer to the
336 1.34 dsl * posix 'cd -P' (not the posix default of 'cd -L').
337 1.34 dsl * If 'cd' is changed to support -P/L then the default here
338 1.34 dsl * needs to be revisited if the historic behaviour is to be kept.
339 1.34 dsl */
340 1.1 cgd
341 1.1 cgd int
342 1.29 christos pwdcmd(int argc, char **argv)
343 1.10 cgd {
344 1.33 dsl int i;
345 1.33 dsl char opt = 'L';
346 1.33 dsl
347 1.33 dsl while ((i = nextopt("LP")) != '\0')
348 1.33 dsl opt = i;
349 1.33 dsl if (*argptr)
350 1.33 dsl error("unexpected argument");
351 1.33 dsl
352 1.34 dsl if (opt == 'L')
353 1.33 dsl getpwd(0);
354 1.34 dsl else
355 1.34 dsl find_curdir(0);
356 1.33 dsl
357 1.51 kre #if 0 /* posix has been changed to forbid this */
358 1.42 uebayasi setvar("OLDPWD", prevdir, VEXPORT);
359 1.34 dsl setvar("PWD", curdir, VEXPORT);
360 1.51 kre #endif
361 1.51 kre
362 1.51 kre if (!is_curdir(curdir)) {
363 1.51 kre find_curdir(1);
364 1.51 kre if (curdir == NULL)
365 1.51 kre error("Unable to find current directory");
366 1.51 kre }
367 1.52 kre
368 1.52 kre flushout(out1); /* make sure buffer is empty */
369 1.52 kre clr_err(out1); /* and forget any earlier errors */
370 1.34 dsl out1str(curdir);
371 1.1 cgd out1c('\n');
372 1.52 kre flushout(out1);
373 1.52 kre if (io_err(out1))
374 1.52 kre error("stdout: %s", strerror(errno));
375 1.52 kre
376 1.1 cgd return 0;
377 1.1 cgd }
378 1.1 cgd
379 1.1 cgd
380 1.1 cgd
381 1.37 christos void
382 1.37 christos initpwd(void)
383 1.37 christos {
384 1.39 simonb getpwd(1);
385 1.39 simonb if (curdir)
386 1.39 simonb setvar("PWD", curdir, VEXPORT);
387 1.39 simonb else
388 1.39 simonb sh_warnx("Cannot determine current working directory");
389 1.37 christos }
390 1.14 christos
391 1.14 christos #define MAXPWD 256
392 1.14 christos
393 1.1 cgd /*
394 1.14 christos * Find out what the current directory is. If we already know the current
395 1.1 cgd * directory, this routine returns immediately.
396 1.1 cgd */
397 1.14 christos void
398 1.29 christos getpwd(int noerror)
399 1.14 christos {
400 1.29 christos char *pwd;
401 1.29 christos static int first = 1;
402 1.1 cgd
403 1.1 cgd if (curdir)
404 1.1 cgd return;
405 1.29 christos
406 1.29 christos if (first) {
407 1.33 dsl first = 0;
408 1.29 christos pwd = getenv("PWD");
409 1.51 kre if (is_curdir(pwd)) {
410 1.29 christos curdir = savestr(pwd);
411 1.29 christos return;
412 1.29 christos }
413 1.29 christos }
414 1.33 dsl
415 1.34 dsl find_curdir(noerror);
416 1.33 dsl
417 1.33 dsl return;
418 1.33 dsl }
419 1.33 dsl
420 1.34 dsl STATIC void
421 1.34 dsl find_curdir(int noerror)
422 1.33 dsl {
423 1.33 dsl int i;
424 1.33 dsl char *pwd;
425 1.29 christos
426 1.14 christos /*
427 1.14 christos * Things are a bit complicated here; we could have just used
428 1.14 christos * getcwd, but traditionally getcwd is implemented using popen
429 1.14 christos * to /bin/pwd. This creates a problem for us, since we cannot
430 1.14 christos * keep track of the job if it is being ran behind our backs.
431 1.51 kre * XXX That's not actually the problem, a process created and
432 1.51 kre * XXX destroyed that we know nothing about is harmless. The
433 1.51 kre * XXX problem is that old popen() implementations would use
434 1.51 kre * XXX wait(2) to await completion of the command, and that might
435 1.51 kre * XXX collect (and ignore) our children. As long as we are
436 1.51 kre * XXX confident that popen() uses waitpid() (or the equv) there
437 1.51 kre * XXX would not be a problem. But how do we know that?
438 1.14 christos * So we re-implement getcwd(), and we suppress interrupts
439 1.14 christos * throughout the process. This is not completely safe, since
440 1.14 christos * the user can still break out of it by killing the pwd program.
441 1.14 christos * We still try to use getcwd for systems that we know have a
442 1.14 christos * c implementation of getcwd, that does not open a pipe to
443 1.14 christos * /bin/pwd.
444 1.14 christos */
445 1.22 christos #if defined(__NetBSD__) || defined(__SVR4)
446 1.21 christos
447 1.29 christos for (i = MAXPWD;; i *= 2) {
448 1.29 christos pwd = stalloc(i);
449 1.34 dsl if (getcwd(pwd, i) != NULL) {
450 1.34 dsl curdir = savestr(pwd);
451 1.49 kre stunalloc(pwd);
452 1.34 dsl return;
453 1.34 dsl }
454 1.29 christos stunalloc(pwd);
455 1.29 christos if (errno == ERANGE)
456 1.29 christos continue;
457 1.29 christos if (!noerror)
458 1.29 christos error("getcwd() failed: %s", strerror(errno));
459 1.34 dsl return;
460 1.21 christos }
461 1.14 christos #else
462 1.14 christos {
463 1.14 christos char *p;
464 1.14 christos int status;
465 1.14 christos struct job *jp;
466 1.14 christos int pip[2];
467 1.14 christos
468 1.34 dsl pwd = stalloc(MAXPWD);
469 1.14 christos INTOFF;
470 1.14 christos if (pipe(pip) < 0)
471 1.14 christos error("Pipe call failed");
472 1.44 plunky jp = makejob(NULL, 1);
473 1.44 plunky if (forkshell(jp, NULL, FORK_NOJOB) == 0) {
474 1.14 christos (void) close(pip[0]);
475 1.46 christos movefd(pip[1], 1);
476 1.14 christos (void) execl("/bin/pwd", "pwd", (char *)0);
477 1.14 christos error("Cannot exec /bin/pwd");
478 1.14 christos }
479 1.14 christos (void) close(pip[1]);
480 1.14 christos pip[1] = -1;
481 1.34 dsl p = pwd;
482 1.34 dsl while ((i = read(pip[0], p, pwd + MAXPWD - p)) > 0
483 1.14 christos || (i == -1 && errno == EINTR)) {
484 1.14 christos if (i > 0)
485 1.14 christos p += i;
486 1.1 cgd }
487 1.14 christos (void) close(pip[0]);
488 1.14 christos pip[0] = -1;
489 1.14 christos status = waitforjob(jp);
490 1.14 christos if (status != 0)
491 1.14 christos error((char *)0);
492 1.34 dsl if (i < 0 || p == pwd || p[-1] != '\n') {
493 1.29 christos if (noerror) {
494 1.29 christos INTON;
495 1.34 dsl return;
496 1.29 christos }
497 1.14 christos error("pwd command failed");
498 1.29 christos }
499 1.14 christos p[-1] = '\0';
500 1.33 dsl INTON;
501 1.34 dsl curdir = savestr(pwd);
502 1.49 kre stunalloc(pwd);
503 1.34 dsl return;
504 1.1 cgd }
505 1.15 jtc #endif
506 1.1 cgd }
507