cd.c revision 1.17 1 /* $NetBSD: cd.c,v 1.17 1996/09/18 22:47:48 thorpej 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.17 1996/09/18 22:47:48 thorpej 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] == '-' && dest[1] == '\0') {
95 dest = prevdir ? prevdir : curdir;
96 print = 1;
97 }
98 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
99 path = nullstr;
100 while ((p = padvance(&path, dest)) != NULL) {
101 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
102 if (!print) {
103 /*
104 * XXX - rethink
105 */
106 if (p[0] == '.' && p[1] == '/')
107 p += 2;
108 print = strcmp(p, dest);
109 }
110 if (docd(p, print) >= 0)
111 return 0;
112
113 }
114 }
115 error("can't cd to %s", dest);
116 /*NOTREACHED*/
117 return 0;
118 }
119
120
121 /*
122 * Actually do the chdir. In an interactive shell, print the
123 * directory name if "print" is nonzero.
124 */
125
126 STATIC int
127 docd(dest, print)
128 char *dest;
129 {
130
131 TRACE(("docd(\"%s\", %d) called\n", dest, print));
132 INTOFF;
133 if (chdir(dest) < 0) {
134 INTON;
135 return -1;
136 }
137 updatepwd(dest);
138 INTON;
139 if (print && iflag)
140 out1fmt("%s\n", stackblock());
141 return 0;
142 }
143
144
145 /*
146 * Get the next component of the path name pointed to by cdcomppath.
147 * This routine overwrites the string pointed to by cdcomppath.
148 */
149
150 STATIC char *
151 getcomponent() {
152 register char *p;
153 char *start;
154
155 if ((p = cdcomppath) == NULL)
156 return NULL;
157 start = cdcomppath;
158 while (*p != '/' && *p != '\0')
159 p++;
160 if (*p == '\0') {
161 cdcomppath = NULL;
162 } else {
163 *p++ = '\0';
164 cdcomppath = p;
165 }
166 return start;
167 }
168
169
170
171 /*
172 * Update curdir (the name of the current directory) in response to a
173 * cd command. We also call hashcd to let the routines in exec.c know
174 * that the current directory has changed.
175 */
176
177 void hashcd();
178
179 STATIC void
180 updatepwd(dir)
181 char *dir;
182 {
183 char *new;
184 char *p;
185
186 hashcd(); /* update command hash table */
187 cdcomppath = stalloc(strlen(dir) + 1);
188 scopy(dir, cdcomppath);
189 STARTSTACKSTR(new);
190 if (*dir != '/') {
191 if (curdir == NULL)
192 return;
193 p = curdir;
194 while (*p)
195 STPUTC(*p++, new);
196 if (p[-1] == '/')
197 STUNPUTC(new);
198 }
199 while ((p = getcomponent()) != NULL) {
200 if (equal(p, "..")) {
201 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
202 } else if (*p != '\0' && ! equal(p, ".")) {
203 STPUTC('/', new);
204 while (*p)
205 STPUTC(*p++, new);
206 }
207 }
208 if (new == stackblock())
209 STPUTC('/', new);
210 STACKSTRNUL(new);
211 INTOFF;
212 if (prevdir)
213 ckfree(prevdir);
214 prevdir = curdir;
215 curdir = savestr(stackblock());
216 INTON;
217 }
218
219
220
221 int
222 pwdcmd(argc, argv)
223 int argc;
224 char **argv;
225 {
226 getpwd();
227 out1str(curdir);
228 out1c('\n');
229 return 0;
230 }
231
232
233
234
235 #define MAXPWD 256
236
237 /*
238 * Find out what the current directory is. If we already know the current
239 * directory, this routine returns immediately.
240 */
241 void
242 getpwd()
243 {
244 char buf[MAXPWD];
245
246 if (curdir)
247 return;
248 /*
249 * Things are a bit complicated here; we could have just used
250 * getcwd, but traditionally getcwd is implemented using popen
251 * to /bin/pwd. This creates a problem for us, since we cannot
252 * keep track of the job if it is being ran behind our backs.
253 * So we re-implement getcwd(), and we suppress interrupts
254 * throughout the process. This is not completely safe, since
255 * the user can still break out of it by killing the pwd program.
256 * We still try to use getcwd for systems that we know have a
257 * c implementation of getcwd, that does not open a pipe to
258 * /bin/pwd.
259 */
260 #if defined(__NetBSD__) || defined(__svr4__)
261 if (getcwd(buf, sizeof(buf)) == NULL)
262 error("getcwd() failed: %s", strerror(errno));
263 curdir = savestr(buf);
264 #else
265 {
266 char *p;
267 int i;
268 int status;
269 struct job *jp;
270 int pip[2];
271
272 INTOFF;
273 if (pipe(pip) < 0)
274 error("Pipe call failed");
275 jp = makejob((union node *)NULL, 1);
276 if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
277 (void) close(pip[0]);
278 if (pip[1] != 1) {
279 close(1);
280 copyfd(pip[1], 1);
281 close(pip[1]);
282 }
283 (void) execl("/bin/pwd", "pwd", (char *)0);
284 error("Cannot exec /bin/pwd");
285 }
286 (void) close(pip[1]);
287 pip[1] = -1;
288 p = buf;
289 while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
290 || (i == -1 && errno == EINTR)) {
291 if (i > 0)
292 p += i;
293 }
294 (void) close(pip[0]);
295 pip[0] = -1;
296 status = waitforjob(jp);
297 if (status != 0)
298 error((char *)0);
299 if (i < 0 || p == buf || p[-1] != '\n')
300 error("pwd command failed");
301 p[-1] = '\0';
302 }
303 curdir = savestr(buf);
304 INTON;
305 #endif
306 }
307