cd.c revision 1.24 1 1.24 christos /* $NetBSD: cd.c,v 1.24 1998/02/17 02:57:16 christos 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.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.23 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.12 cgd #if 0
42 1.13 christos static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
43 1.12 cgd #else
44 1.24 christos __RCSID("$NetBSD: cd.c,v 1.24 1998/02/17 02:57:16 christos Exp $");
45 1.12 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.9 cgd #include <sys/types.h>
49 1.9 cgd #include <sys/stat.h>
50 1.13 christos #include <stdlib.h>
51 1.17 thorpej #include <string.h>
52 1.9 cgd #include <unistd.h>
53 1.9 cgd #include <errno.h>
54 1.9 cgd
55 1.1 cgd /*
56 1.1 cgd * The cd and pwd commands.
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include "shell.h"
60 1.1 cgd #include "var.h"
61 1.1 cgd #include "nodes.h" /* for jobs.h */
62 1.1 cgd #include "jobs.h"
63 1.1 cgd #include "options.h"
64 1.1 cgd #include "output.h"
65 1.1 cgd #include "memalloc.h"
66 1.1 cgd #include "error.h"
67 1.16 christos #include "exec.h"
68 1.13 christos #include "redir.h"
69 1.1 cgd #include "mystring.h"
70 1.13 christos #include "show.h"
71 1.14 christos #include "cd.h"
72 1.1 cgd
73 1.13 christos STATIC int docd __P((char *, int));
74 1.13 christos STATIC char *getcomponent __P((void));
75 1.13 christos STATIC void updatepwd __P((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.10 cgd cdcmd(argc, argv)
83 1.10 cgd int argc;
84 1.18 christos char **argv;
85 1.10 cgd {
86 1.1 cgd char *dest;
87 1.1 cgd char *path;
88 1.1 cgd char *p;
89 1.1 cgd struct stat statb;
90 1.6 jtc int print = 0;
91 1.1 cgd
92 1.1 cgd nextopt(nullstr);
93 1.1 cgd if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
94 1.1 cgd error("HOME not set");
95 1.18 christos if (*dest == '\0')
96 1.18 christos dest = ".";
97 1.6 jtc if (dest[0] == '-' && dest[1] == '\0') {
98 1.6 jtc dest = prevdir ? prevdir : curdir;
99 1.6 jtc print = 1;
100 1.18 christos if (dest)
101 1.18 christos print = 1;
102 1.18 christos else
103 1.18 christos dest = ".";
104 1.6 jtc }
105 1.1 cgd if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
106 1.1 cgd path = nullstr;
107 1.1 cgd while ((p = padvance(&path, dest)) != NULL) {
108 1.11 mycroft if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
109 1.6 jtc if (!print) {
110 1.6 jtc /*
111 1.6 jtc * XXX - rethink
112 1.6 jtc */
113 1.24 christos if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
114 1.6 jtc p += 2;
115 1.6 jtc print = strcmp(p, dest);
116 1.6 jtc }
117 1.6 jtc if (docd(p, print) >= 0)
118 1.6 jtc return 0;
119 1.6 jtc
120 1.6 jtc }
121 1.1 cgd }
122 1.1 cgd error("can't cd to %s", dest);
123 1.13 christos /*NOTREACHED*/
124 1.13 christos return 0;
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd
128 1.1 cgd /*
129 1.15 jtc * Actually do the chdir. In an interactive shell, print the
130 1.15 jtc * directory name if "print" is nonzero.
131 1.1 cgd */
132 1.1 cgd
133 1.1 cgd STATIC int
134 1.1 cgd docd(dest, print)
135 1.1 cgd char *dest;
136 1.18 christos int print;
137 1.15 jtc {
138 1.20 tls char *p;
139 1.20 tls char *q;
140 1.19 cjs char *component;
141 1.19 cjs struct stat statb;
142 1.19 cjs int first;
143 1.19 cjs int badstat;
144 1.15 jtc
145 1.15 jtc TRACE(("docd(\"%s\", %d) called\n", dest, print));
146 1.19 cjs
147 1.19 cjs /*
148 1.19 cjs * Check each component of the path. If we find a symlink or
149 1.19 cjs * something we can't stat, clear curdir to force a getcwd()
150 1.19 cjs * next time we get the value of the current directory.
151 1.19 cjs */
152 1.19 cjs badstat = 0;
153 1.19 cjs cdcomppath = stalloc(strlen(dest) + 1);
154 1.19 cjs scopy(dest, cdcomppath);
155 1.19 cjs STARTSTACKSTR(p);
156 1.19 cjs if (*dest == '/') {
157 1.19 cjs STPUTC('/', p);
158 1.19 cjs cdcomppath++;
159 1.19 cjs }
160 1.19 cjs first = 1;
161 1.19 cjs while ((q = getcomponent()) != NULL) {
162 1.19 cjs if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
163 1.19 cjs continue;
164 1.19 cjs if (! first)
165 1.19 cjs STPUTC('/', p);
166 1.19 cjs first = 0;
167 1.19 cjs component = q;
168 1.19 cjs while (*q)
169 1.19 cjs STPUTC(*q++, p);
170 1.19 cjs if (equal(component, ".."))
171 1.19 cjs continue;
172 1.19 cjs STACKSTRNUL(p);
173 1.19 cjs if ((lstat(stackblock(), &statb) < 0)
174 1.19 cjs || (S_ISLNK(statb.st_mode))) {
175 1.19 cjs /* print = 1; */
176 1.19 cjs badstat = 1;
177 1.19 cjs break;
178 1.19 cjs }
179 1.19 cjs }
180 1.19 cjs
181 1.1 cgd INTOFF;
182 1.1 cgd if (chdir(dest) < 0) {
183 1.1 cgd INTON;
184 1.1 cgd return -1;
185 1.1 cgd }
186 1.19 cjs updatepwd(badstat ? NULL : dest);
187 1.1 cgd INTON;
188 1.23 christos if (print && iflag && curdir)
189 1.19 cjs out1fmt("%s\n", curdir);
190 1.1 cgd return 0;
191 1.1 cgd }
192 1.1 cgd
193 1.1 cgd
194 1.1 cgd /*
195 1.1 cgd * Get the next component of the path name pointed to by cdcomppath.
196 1.1 cgd * This routine overwrites the string pointed to by cdcomppath.
197 1.1 cgd */
198 1.1 cgd
199 1.1 cgd STATIC char *
200 1.1 cgd getcomponent() {
201 1.20 tls char *p;
202 1.1 cgd char *start;
203 1.1 cgd
204 1.1 cgd if ((p = cdcomppath) == NULL)
205 1.1 cgd return NULL;
206 1.1 cgd start = cdcomppath;
207 1.1 cgd while (*p != '/' && *p != '\0')
208 1.1 cgd p++;
209 1.1 cgd if (*p == '\0') {
210 1.1 cgd cdcomppath = NULL;
211 1.1 cgd } else {
212 1.1 cgd *p++ = '\0';
213 1.1 cgd cdcomppath = p;
214 1.1 cgd }
215 1.1 cgd return start;
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd
219 1.1 cgd
220 1.1 cgd /*
221 1.1 cgd * Update curdir (the name of the current directory) in response to a
222 1.1 cgd * cd command. We also call hashcd to let the routines in exec.c know
223 1.1 cgd * that the current directory has changed.
224 1.1 cgd */
225 1.1 cgd
226 1.1 cgd STATIC void
227 1.1 cgd updatepwd(dir)
228 1.1 cgd char *dir;
229 1.1 cgd {
230 1.1 cgd char *new;
231 1.1 cgd char *p;
232 1.1 cgd
233 1.1 cgd hashcd(); /* update command hash table */
234 1.19 cjs
235 1.19 cjs /*
236 1.19 cjs * If our argument is NULL, we don't know the current directory
237 1.19 cjs * any more because we traversed a symbolic link or something
238 1.19 cjs * we couldn't stat().
239 1.19 cjs */
240 1.19 cjs if (dir == NULL) {
241 1.19 cjs if (prevdir)
242 1.19 cjs ckfree(prevdir);
243 1.19 cjs INTOFF;
244 1.19 cjs prevdir = curdir;
245 1.19 cjs curdir = NULL;
246 1.19 cjs getpwd();
247 1.19 cjs INTON;
248 1.19 cjs return;
249 1.19 cjs }
250 1.19 cjs
251 1.1 cgd cdcomppath = stalloc(strlen(dir) + 1);
252 1.1 cgd scopy(dir, cdcomppath);
253 1.1 cgd STARTSTACKSTR(new);
254 1.1 cgd if (*dir != '/') {
255 1.1 cgd if (curdir == NULL)
256 1.1 cgd return;
257 1.1 cgd p = curdir;
258 1.1 cgd while (*p)
259 1.1 cgd STPUTC(*p++, new);
260 1.1 cgd if (p[-1] == '/')
261 1.1 cgd STUNPUTC(new);
262 1.1 cgd }
263 1.1 cgd while ((p = getcomponent()) != NULL) {
264 1.1 cgd if (equal(p, "..")) {
265 1.1 cgd while (new > stackblock() && (STUNPUTC(new), *new) != '/');
266 1.1 cgd } else if (*p != '\0' && ! equal(p, ".")) {
267 1.1 cgd STPUTC('/', new);
268 1.1 cgd while (*p)
269 1.1 cgd STPUTC(*p++, new);
270 1.1 cgd }
271 1.1 cgd }
272 1.1 cgd if (new == stackblock())
273 1.1 cgd STPUTC('/', new);
274 1.1 cgd STACKSTRNUL(new);
275 1.6 jtc INTOFF;
276 1.6 jtc if (prevdir)
277 1.6 jtc ckfree(prevdir);
278 1.6 jtc prevdir = curdir;
279 1.1 cgd curdir = savestr(stackblock());
280 1.21 christos setvar("PWD", curdir, VEXPORT|VTEXTFIXED);
281 1.6 jtc INTON;
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd
285 1.1 cgd
286 1.1 cgd int
287 1.10 cgd pwdcmd(argc, argv)
288 1.10 cgd int argc;
289 1.18 christos char **argv;
290 1.10 cgd {
291 1.1 cgd getpwd();
292 1.1 cgd out1str(curdir);
293 1.1 cgd out1c('\n');
294 1.1 cgd return 0;
295 1.1 cgd }
296 1.1 cgd
297 1.1 cgd
298 1.1 cgd
299 1.14 christos
300 1.14 christos #define MAXPWD 256
301 1.14 christos
302 1.1 cgd /*
303 1.14 christos * Find out what the current directory is. If we already know the current
304 1.1 cgd * directory, this routine returns immediately.
305 1.1 cgd */
306 1.14 christos void
307 1.14 christos getpwd()
308 1.14 christos {
309 1.1 cgd char buf[MAXPWD];
310 1.1 cgd
311 1.1 cgd if (curdir)
312 1.1 cgd return;
313 1.14 christos /*
314 1.14 christos * Things are a bit complicated here; we could have just used
315 1.14 christos * getcwd, but traditionally getcwd is implemented using popen
316 1.14 christos * to /bin/pwd. This creates a problem for us, since we cannot
317 1.14 christos * keep track of the job if it is being ran behind our backs.
318 1.14 christos * So we re-implement getcwd(), and we suppress interrupts
319 1.14 christos * throughout the process. This is not completely safe, since
320 1.14 christos * the user can still break out of it by killing the pwd program.
321 1.14 christos * We still try to use getcwd for systems that we know have a
322 1.14 christos * c implementation of getcwd, that does not open a pipe to
323 1.14 christos * /bin/pwd.
324 1.14 christos */
325 1.22 christos #if defined(__NetBSD__) || defined(__SVR4)
326 1.21 christos
327 1.21 christos if (getcwd(buf, sizeof(buf)) == NULL) {
328 1.21 christos char *pwd = getenv("PWD");
329 1.21 christos struct stat stdot, stpwd;
330 1.21 christos
331 1.21 christos if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
332 1.21 christos stat(pwd, &stpwd) != -1 &&
333 1.21 christos stdot.st_dev == stpwd.st_dev &&
334 1.21 christos stdot.st_ino == stpwd.st_ino) {
335 1.21 christos curdir = savestr(pwd);
336 1.21 christos return;
337 1.21 christos }
338 1.17 thorpej error("getcwd() failed: %s", strerror(errno));
339 1.21 christos }
340 1.15 jtc curdir = savestr(buf);
341 1.14 christos #else
342 1.14 christos {
343 1.14 christos char *p;
344 1.14 christos int i;
345 1.14 christos int status;
346 1.14 christos struct job *jp;
347 1.14 christos int pip[2];
348 1.14 christos
349 1.14 christos INTOFF;
350 1.14 christos if (pipe(pip) < 0)
351 1.14 christos error("Pipe call failed");
352 1.14 christos jp = makejob((union node *)NULL, 1);
353 1.14 christos if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
354 1.14 christos (void) close(pip[0]);
355 1.14 christos if (pip[1] != 1) {
356 1.14 christos close(1);
357 1.14 christos copyfd(pip[1], 1);
358 1.14 christos close(pip[1]);
359 1.14 christos }
360 1.14 christos (void) execl("/bin/pwd", "pwd", (char *)0);
361 1.14 christos error("Cannot exec /bin/pwd");
362 1.14 christos }
363 1.14 christos (void) close(pip[1]);
364 1.14 christos pip[1] = -1;
365 1.14 christos p = buf;
366 1.14 christos while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
367 1.14 christos || (i == -1 && errno == EINTR)) {
368 1.14 christos if (i > 0)
369 1.14 christos p += i;
370 1.1 cgd }
371 1.14 christos (void) close(pip[0]);
372 1.14 christos pip[0] = -1;
373 1.14 christos status = waitforjob(jp);
374 1.14 christos if (status != 0)
375 1.14 christos error((char *)0);
376 1.14 christos if (i < 0 || p == buf || p[-1] != '\n')
377 1.14 christos error("pwd command failed");
378 1.14 christos p[-1] = '\0';
379 1.1 cgd }
380 1.1 cgd curdir = savestr(buf);
381 1.1 cgd INTON;
382 1.15 jtc #endif
383 1.1 cgd }
384