cd.c revision 1.18 1 /* $NetBSD: cd.c,v 1.18 1996/10/16 14:29:42 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
42 #else
43 static char rcsid[] = "$NetBSD: cd.c,v 1.18 1996/10/16 14:29:42 christos Exp $";
44 #endif
45 #endif /* not lint */
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <errno.h>
53
54 /*
55 * The cd and pwd commands.
56 */
57
58 #include "shell.h"
59 #include "var.h"
60 #include "nodes.h" /* for jobs.h */
61 #include "jobs.h"
62 #include "options.h"
63 #include "output.h"
64 #include "memalloc.h"
65 #include "error.h"
66 #include "exec.h"
67 #include "redir.h"
68 #include "mystring.h"
69 #include "show.h"
70 #include "cd.h"
71
72 STATIC int docd __P((char *, int));
73 STATIC char *getcomponent __P((void));
74 STATIC void updatepwd __P((char *));
75
76 char *curdir = NULL; /* 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 int print = 0;
90
91 nextopt(nullstr);
92 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
93 error("HOME not set");
94 if (*dest == '\0')
95 dest = ".";
96 if (dest[0] == '-' && dest[1] == '\0') {
97 dest = prevdir ? prevdir : curdir;
98 print = 1;
99 if (dest)
100 print = 1;
101 else
102 dest = ".";
103 }
104 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
105 path = nullstr;
106 while ((p = padvance(&path, dest)) != NULL) {
107 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
108 if (!print) {
109 /*
110 * XXX - rethink
111 */
112 if (p[0] == '.' && p[1] == '/')
113 p += 2;
114 print = strcmp(p, dest);
115 }
116 if (docd(p, print) >= 0)
117 return 0;
118
119 }
120 }
121 error("can't cd to %s", dest);
122 /*NOTREACHED*/
123 return 0;
124 }
125
126
127 /*
128 * Actually do the chdir. In an interactive shell, print the
129 * directory name if "print" is nonzero.
130 */
131
132 STATIC int
133 docd(dest, print)
134 char *dest;
135 int print;
136 {
137
138 TRACE(("docd(\"%s\", %d) called\n", dest, print));
139 INTOFF;
140 if (chdir(dest) < 0) {
141 INTON;
142 return -1;
143 }
144 updatepwd(dest);
145 INTON;
146 if (print && iflag)
147 out1fmt("%s\n", stackblock());
148 return 0;
149 }
150
151
152 /*
153 * Get the next component of the path name pointed to by cdcomppath.
154 * This routine overwrites the string pointed to by cdcomppath.
155 */
156
157 STATIC char *
158 getcomponent() {
159 register char *p;
160 char *start;
161
162 if ((p = cdcomppath) == NULL)
163 return NULL;
164 start = cdcomppath;
165 while (*p != '/' && *p != '\0')
166 p++;
167 if (*p == '\0') {
168 cdcomppath = NULL;
169 } else {
170 *p++ = '\0';
171 cdcomppath = p;
172 }
173 return start;
174 }
175
176
177
178 /*
179 * Update curdir (the name of the current directory) in response to a
180 * cd command. We also call hashcd to let the routines in exec.c know
181 * that the current directory has changed.
182 */
183
184 void hashcd();
185
186 STATIC void
187 updatepwd(dir)
188 char *dir;
189 {
190 char *new;
191 char *p;
192
193 hashcd(); /* update command hash table */
194 cdcomppath = stalloc(strlen(dir) + 1);
195 scopy(dir, cdcomppath);
196 STARTSTACKSTR(new);
197 if (*dir != '/') {
198 if (curdir == NULL)
199 return;
200 p = curdir;
201 while (*p)
202 STPUTC(*p++, new);
203 if (p[-1] == '/')
204 STUNPUTC(new);
205 }
206 while ((p = getcomponent()) != NULL) {
207 if (equal(p, "..")) {
208 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
209 } else if (*p != '\0' && ! equal(p, ".")) {
210 STPUTC('/', new);
211 while (*p)
212 STPUTC(*p++, new);
213 }
214 }
215 if (new == stackblock())
216 STPUTC('/', new);
217 STACKSTRNUL(new);
218 INTOFF;
219 if (prevdir)
220 ckfree(prevdir);
221 prevdir = curdir;
222 curdir = savestr(stackblock());
223 INTON;
224 }
225
226
227
228 int
229 pwdcmd(argc, argv)
230 int argc;
231 char **argv;
232 {
233 getpwd();
234 out1str(curdir);
235 out1c('\n');
236 return 0;
237 }
238
239
240
241
242 #define MAXPWD 256
243
244 /*
245 * Find out what the current directory is. If we already know the current
246 * directory, this routine returns immediately.
247 */
248 void
249 getpwd()
250 {
251 char buf[MAXPWD];
252
253 if (curdir)
254 return;
255 /*
256 * Things are a bit complicated here; we could have just used
257 * getcwd, but traditionally getcwd is implemented using popen
258 * to /bin/pwd. This creates a problem for us, since we cannot
259 * keep track of the job if it is being ran behind our backs.
260 * So we re-implement getcwd(), and we suppress interrupts
261 * throughout the process. This is not completely safe, since
262 * the user can still break out of it by killing the pwd program.
263 * We still try to use getcwd for systems that we know have a
264 * c implementation of getcwd, that does not open a pipe to
265 * /bin/pwd.
266 */
267 #if defined(__NetBSD__) || defined(__svr4__)
268 if (getcwd(buf, sizeof(buf)) == NULL)
269 error("getcwd() failed: %s", strerror(errno));
270 curdir = savestr(buf);
271 #else
272 {
273 char *p;
274 int i;
275 int status;
276 struct job *jp;
277 int pip[2];
278
279 INTOFF;
280 if (pipe(pip) < 0)
281 error("Pipe call failed");
282 jp = makejob((union node *)NULL, 1);
283 if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
284 (void) close(pip[0]);
285 if (pip[1] != 1) {
286 close(1);
287 copyfd(pip[1], 1);
288 close(pip[1]);
289 }
290 (void) execl("/bin/pwd", "pwd", (char *)0);
291 error("Cannot exec /bin/pwd");
292 }
293 (void) close(pip[1]);
294 pip[1] = -1;
295 p = buf;
296 while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
297 || (i == -1 && errno == EINTR)) {
298 if (i > 0)
299 p += i;
300 }
301 (void) close(pip[0]);
302 pip[0] = -1;
303 status = waitforjob(jp);
304 if (status != 0)
305 error((char *)0);
306 if (i < 0 || p == buf || p[-1] != '\n')
307 error("pwd command failed");
308 p[-1] = '\0';
309 }
310 curdir = savestr(buf);
311 INTON;
312 #endif
313 }
314