cd.c revision 1.11 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.
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.
23 *
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.
35 */
36
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)cd.c 8.1 (Berkeley) 5/31/93";*/
39 static char *rcsid = "$Id: cd.c,v 1.11 1995/01/30 19:38:04 mycroft Exp $";
40 #endif /* not lint */
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45 #include <errno.h>
46
47 /*
48 * The cd and pwd commands.
49 */
50
51 #include "shell.h"
52 #include "var.h"
53 #include "nodes.h" /* for jobs.h */
54 #include "jobs.h"
55 #include "options.h"
56 #include "output.h"
57 #include "memalloc.h"
58 #include "error.h"
59 #include "mystring.h"
60 #include "extern.h"
61
62
63 #ifdef __STDC__
64 STATIC int docd(char *, int);
65 STATIC void updatepwd(char *);
66 STATIC void getpwd(void);
67 STATIC char *getcomponent(void);
68 #else
69 STATIC int docd();
70 STATIC void updatepwd();
71 STATIC void getpwd();
72 STATIC char *getcomponent();
73 #endif
74
75
76 char *curdir; /* current working directory */
77 char *prevdir; /* previous working directory */
78 STATIC char *cdcomppath;
79
80 int
81 cdcmd(argc, argv)
82 int argc;
83 char **argv;
84 {
85 char *dest;
86 char *path;
87 char *p;
88 struct stat statb;
89 char *padvance();
90 int print = 0;
91
92 nextopt(nullstr);
93 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
94 error("HOME not set");
95 if (dest[0] == '-' && dest[1] == '\0') {
96 dest = prevdir ? prevdir : curdir;
97 print = 1;
98 }
99 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
100 path = nullstr;
101 while ((p = padvance(&path, dest)) != NULL) {
102 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
103 if (!print) {
104 /*
105 * XXX - rethink
106 */
107 if (p[0] == '.' && p[1] == '/')
108 p += 2;
109 print = strcmp(p, dest);
110 }
111 if (docd(p, print) >= 0)
112 return 0;
113
114 }
115 }
116 error("can't cd to %s", dest);
117 }
118
119
120 /*
121 * Actually do the chdir. If the name refers to symbolic links, we
122 * compute the actual directory name before doing the cd. In an
123 * interactive shell, print the directory name if "print" is nonzero
124 * or if the name refers to a symbolic link. We also print the name
125 * if "/u/logname" was expanded in it, since this is similar to a
126 * symbolic link. (The check for this breaks if the user gives the
127 * cd command some additional, unused arguments.)
128 */
129
130 #if SYMLINKS == 0
131 STATIC int
132 docd(dest, print)
133 char *dest;
134 {
135 INTOFF;
136 if (chdir(dest) < 0) {
137 INTON;
138 return -1;
139 }
140 updatepwd(dest);
141 INTON;
142 if (print && iflag)
143 out1fmt("%s\n", stackblock());
144 return 0;
145 }
146
147 #else
148
149
150
151 STATIC int
152 docd(dest, print)
153 char *dest;
154 int print;
155 {
156 register char *p;
157 register char *q;
158 char *symlink;
159 char *component;
160 struct stat statb;
161 int first;
162 int i;
163
164 TRACE(("docd(\"%s\", %d) called\n", dest, print));
165
166 top:
167 cdcomppath = dest;
168 STARTSTACKSTR(p);
169 if (*dest == '/') {
170 STPUTC('/', p);
171 cdcomppath++;
172 }
173 first = 1;
174 while ((q = getcomponent()) != NULL) {
175 if (q[0] == '\0' || q[0] == '.' && q[1] == '\0')
176 continue;
177 if (! first)
178 STPUTC('/', p);
179 first = 0;
180 component = q;
181 while (*q)
182 STPUTC(*q++, p);
183 if (equal(component, ".."))
184 continue;
185 STACKSTRNUL(p);
186 if (lstat(stackblock(), &statb) < 0)
187 error("lstat %s failed", stackblock());
188 if (!S_ISLNK(statb.st_mode))
189 continue;
190
191 /* Hit a symbolic link. We have to start all over again. */
192 print = 1;
193 STPUTC('\0', p);
194 symlink = grabstackstr(p);
195 i = (int)statb.st_size + 2; /* 2 for '/' and '\0' */
196 if (cdcomppath != NULL)
197 i += strlen(cdcomppath);
198 p = stalloc(i);
199 if (readlink(symlink, p, (int)statb.st_size) < 0) {
200 error("readlink %s failed", stackblock());
201 }
202 if (cdcomppath != NULL) {
203 p[(int)statb.st_size] = '/';
204 scopy(cdcomppath, p + (int)statb.st_size + 1);
205 } else {
206 p[(int)statb.st_size] = '\0';
207 }
208 if (p[0] != '/') { /* relative path name */
209 char *r;
210 q = r = symlink;
211 while (*q) {
212 if (*q++ == '/')
213 r = q;
214 }
215 *r = '\0';
216 dest = stalloc(strlen(symlink) + strlen(p) + 1);
217 scopy(symlink, dest);
218 strcat(dest, p);
219 } else {
220 dest = p;
221 }
222 goto top;
223 }
224 STPUTC('\0', p);
225 p = grabstackstr(p);
226 INTOFF;
227 if (chdir(p) < 0) {
228 INTON;
229 return -1;
230 }
231 updatepwd(p);
232 INTON;
233 if (print && iflag)
234 out1fmt("%s\n", p);
235 return 0;
236 }
237 #endif /* SYMLINKS */
238
239
240
241 /*
242 * Get the next component of the path name pointed to by cdcomppath.
243 * This routine overwrites the string pointed to by cdcomppath.
244 */
245
246 STATIC char *
247 getcomponent() {
248 register char *p;
249 char *start;
250
251 if ((p = cdcomppath) == NULL)
252 return NULL;
253 start = cdcomppath;
254 while (*p != '/' && *p != '\0')
255 p++;
256 if (*p == '\0') {
257 cdcomppath = NULL;
258 } else {
259 *p++ = '\0';
260 cdcomppath = p;
261 }
262 return start;
263 }
264
265
266
267 /*
268 * Update curdir (the name of the current directory) in response to a
269 * cd command. We also call hashcd to let the routines in exec.c know
270 * that the current directory has changed.
271 */
272
273 void hashcd();
274
275 STATIC void
276 updatepwd(dir)
277 char *dir;
278 {
279 char *new;
280 char *p;
281
282 hashcd(); /* update command hash table */
283 cdcomppath = stalloc(strlen(dir) + 1);
284 scopy(dir, cdcomppath);
285 STARTSTACKSTR(new);
286 if (*dir != '/') {
287 if (curdir == NULL)
288 return;
289 p = curdir;
290 while (*p)
291 STPUTC(*p++, new);
292 if (p[-1] == '/')
293 STUNPUTC(new);
294 }
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);
302 }
303 }
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 INTON;
313 }
314
315
316
317 int
318 pwdcmd(argc, argv)
319 int argc;
320 char **argv;
321 {
322 getpwd();
323 out1str(curdir);
324 out1c('\n');
325 return 0;
326 }
327
328
329
330 /*
331 * Run /bin/pwd to find out what the current directory is. We suppress
332 * interrupts throughout most of this, but the user can still break out
333 * of it by killing the pwd program. If we already know the current
334 * directory, this routine returns immediately.
335 */
336
337 #define MAXPWD 256
338
339 STATIC void
340 getpwd() {
341 char buf[MAXPWD];
342 char *p;
343 int i;
344 int status;
345 struct job *jp;
346 int pip[2];
347
348 if (curdir)
349 return;
350 INTOFF;
351 if (pipe(pip) < 0)
352 error("Pipe call failed");
353 jp = makejob((union node *)NULL, 1);
354 if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
355 close(pip[0]);
356 if (pip[1] != 1) {
357 close(1);
358 copyfd(pip[1], 1);
359 close(pip[1]);
360 }
361 execl("/bin/pwd", "pwd", (char *)0);
362 error("Cannot exec /bin/pwd");
363 }
364 close(pip[1]);
365 pip[1] = -1;
366 p = buf;
367 while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
368 || i == -1 && errno == EINTR) {
369 if (i > 0)
370 p += i;
371 }
372 close(pip[0]);
373 pip[0] = -1;
374 status = waitforjob(jp);
375 if (status != 0)
376 error((char *)0);
377 if (i < 0 || p == buf || p[-1] != '\n')
378 error("pwd command failed");
379 p[-1] = '\0';
380 curdir = savestr(buf);
381 INTON;
382 }
383