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