VirtualBox

source: kBuild/trunk/src/ash-messup/cd.c@ 1195

Last change on this file since 1195 was 883, checked in by bird, 18 years ago

hacking...

  • Property svn:eol-style set to native
File size: 10.6 KB
Line 
1/* $NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: cd.c,v 1.34 2003/11/14 20:00:28 dsl Exp $");
43#endif
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51#include <errno.h>
52
53/*
54 * The cd and pwd commands.
55 */
56
57#include "shell.h"
58#include "var.h"
59#include "nodes.h" /* for jobs.h */
60#include "jobs.h"
61#include "options.h"
62#include "output.h"
63#include "memalloc.h"
64#include "error.h"
65#include "exec.h"
66#include "redir.h"
67#include "mystring.h"
68#include "show.h"
69#include "cd.h"
70#include "shinstance.h"
71
72STATIC int docd(shinstance *psh, char *, int);
73STATIC char *getcomponent(shinstance *psh);
74STATIC void updatepwd(shinstance *psh, char *);
75STATIC void find_curdir(shinstance *psh, int noerror);
76
77/*char *curdir = NULL;*/ /* current working directory */
78/*char *prevdir;*/ /* previous working directory */
79/*STATIC char *cdcomppath;*/
80
81int
82cdcmd(shinstance *psh, int argc, char **argv)
83{
84 const char *dest;
85 const char *path;
86 char *p, *d;
87 struct stat statb;
88 int print = psh->cdprint; /* set -cdprint to enable */
89
90 nextopt(psh, nullstr);
91
92 /*
93 * Try (quite hard) to have 'curdir' defined, nothing has set
94 * it on entry to the shell, but we want 'cd fred; cd -' to work.
95 */
96 getpwd(psh, 1);
97 dest = *psh->argptr;
98 if (dest == NULL) {
99 dest = bltinlookup(psh, "HOME", 1);
100 if (dest == NULL)
101 error(psh, "HOME not set");
102 } else {
103 if (psh->argptr[1]) {
104 /* Do 'ksh' style substitution */
105 if (!psh->curdir)
106 error(psh, "PWD not set");
107 p = strstr(psh->curdir, dest);
108 if (!p)
109 error(psh, "bad substitution");
110 d = stalloc(psh, strlen(psh->curdir) + strlen(psh->argptr[1]) + 1);
111 memcpy(d, psh->curdir, p - psh->curdir);
112 strcpy(d + (p - psh->curdir), psh->argptr[1]);
113 strcat(d, p + strlen(dest));
114 dest = d;
115 print = 1;
116 }
117 }
118
119 if (dest[0] == '-' && dest[1] == '\0') {
120 dest = psh->prevdir ? psh->prevdir : psh->curdir;
121 print = 1;
122 }
123 if (*dest == '\0')
124 dest = ".";
125 if (IS_ROOT(dest) || (path = bltinlookup(psh, "CDPATH", 1)) == NULL)
126 path = nullstr;
127 while ((p = padvance(psh, &path, dest)) != NULL) {
128 if (shfile_stat(&psh->fdtab, p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
129 if (!print) {
130 /*
131 * XXX - rethink
132 */
133 if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
134 p += 2;
135 print = strcmp(p, dest);
136 }
137 if (docd(psh, p, print) >= 0)
138 return 0;
139
140 }
141 }
142 error(psh, "can't cd to %s", dest);
143 /* NOTREACHED */
144 return 1;
145}
146
147
148/*
149 * Actually do the chdir. In an interactive shell, print the
150 * directory name if "print" is nonzero.
151 */
152
153STATIC int
154docd(shinstance *psh, char *dest, int print)
155{
156 char *p;
157 char *q;
158 char *component;
159 struct stat statb;
160 int first;
161 int badstat;
162
163 TRACE(("docd(\"%s\", %d) called\n", dest, print));
164
165 /*
166 * Check each component of the path. If we find a symlink or
167 * something we can't stat, clear curdir to force a getcwd()
168 * next time we get the value of the current directory.
169 */
170 badstat = 0;
171 psh->cdcomppath = stalloc(psh, strlen(dest) + 1);
172 scopy(dest, psh->cdcomppath);
173 STARTSTACKSTR(p);
174 if (IS_ROOT(dest)) {
175 STPUTC('/', p);
176 psh->cdcomppath++;
177 }
178 first = 1;
179 while ((q = getcomponent(psh)) != NULL) {
180 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
181 continue;
182 if (! first)
183 STPUTC('/', p);
184 first = 0;
185 component = q;
186 while (*q)
187 STPUTC(*q++, p);
188 if (equal(component, ".."))
189 continue;
190 STACKSTRNUL(p);
191 if ((shfile_lstat(&psh->fdtab, stackblock(), &statb) < 0)
192 || (S_ISLNK(statb.st_mode))) {
193 /* print = 1; */
194 badstat = 1;
195 break;
196 }
197 }
198
199 INTOFF;
200 if (shfile_chdir(&psh->fdtab, dest) < 0) {
201 INTON;
202 return -1;
203 }
204 updatepwd(psh, badstat ? NULL : dest);
205 INTON;
206 if (print && psh->iflag && psh->curdir)
207 out1fmt(psh, "%s\n", psh->curdir);
208 return 0;
209}
210
211
212/*
213 * Get the next component of the path name pointed to by psh->cdcomppath.
214 * This routine overwrites the string pointed to by psh->cdcomppath.
215 */
216
217STATIC char *
218getcomponent(shinstance *psh)
219{
220 char *p;
221 char *start;
222
223 if ((p = psh->cdcomppath) == NULL)
224 return NULL;
225 start = psh->cdcomppath;
226 while (*p != '/' && *p != '\0')
227 p++;
228 if (*p == '\0') {
229 psh->cdcomppath = NULL;
230 } else {
231 *p++ = '\0';
232 psh->cdcomppath = p;
233 }
234 return start;
235}
236
237
238
239/*
240 * Update curdir (the name of the current directory) in response to a
241 * cd command. We also call hashcd to let the routines in exec.c know
242 * that the current directory has changed.
243 */
244
245STATIC void
246updatepwd(shinstance *psh, char *dir)
247{
248 char *new;
249 char *p;
250
251 hashcd(psh); /* update command hash table */
252
253 /*
254 * If our argument is NULL, we don't know the current directory
255 * any more because we traversed a symbolic link or something
256 * we couldn't stat().
257 */
258 if (dir == NULL || psh->curdir == NULL) {
259 if (psh->prevdir)
260 ckfree(psh->prevdir);
261 INTOFF;
262 psh->prevdir = psh->curdir;
263 psh->curdir = NULL;
264 getpwd(psh, 1);
265 INTON;
266 if (psh->curdir)
267 setvar(psh, "PWD", psh->curdir, VEXPORT);
268 else
269 unsetvar(psh, "PWD", 0);
270 return;
271 }
272 psh->cdcomppath = stalloc(psh, strlen(dir) + 1);
273 scopy(dir, psh->cdcomppath);
274 STARTSTACKSTR(new);
275 if (!IS_ROOT(dir)) {
276 p = psh->curdir;
277 while (*p)
278 STPUTC(*p++, new);
279 if (p[-1] == '/')
280 STUNPUTC(new);
281 }
282 while ((p = getcomponent(psh)) != NULL) {
283 if (equal(p, "..")) {
284 while (new > stackblock() && (STUNPUTC(new), *new) != '/');
285 } else if (*p != '\0' && ! equal(p, ".")) {
286 STPUTC('/', new);
287 while (*p)
288 STPUTC(*p++, new);
289 }
290 }
291 if (new == stackblock())
292 STPUTC('/', new);
293 STACKSTRNUL(new);
294 INTOFF;
295 if (psh->prevdir)
296 ckfree(psh->prevdir);
297 psh->prevdir = psh->curdir;
298 psh->curdir = savestr(stackblock());
299 setvar(psh, "PWD", psh->curdir, VEXPORT);
300 INTON;
301}
302
303/*
304 * Posix says the default should be 'pwd -L' (as below), however
305 * the 'cd' command (above) does something much nearer to the
306 * posix 'cd -P' (not the posix default of 'cd -L').
307 * If 'cd' is changed to support -P/L then the default here
308 * needs to be revisited if the historic behaviour is to be kept.
309 */
310
311int
312pwdcmd(shinstance *psh, int argc, char **argv)
313{
314 int i;
315 char opt = 'L';
316
317 while ((i = nextopt(psh, "LP")) != '\0')
318 opt = i;
319 if (*psh->argptr)
320 error(psh, "unexpected argument");
321
322 if (opt == 'L')
323 getpwd(psh, 0);
324 else
325 find_curdir(psh, 0);
326
327 setvar(psh, "PWD", psh->curdir, VEXPORT);
328 out1str(psh, psh->curdir);
329 out1c(psh, '\n');
330 return 0;
331}
332
333
334
335
336#define MAXPWD 256
337
338/*
339 * Find out what the current directory is. If we already know the current
340 * directory, this routine returns immediately.
341 */
342void
343getpwd(shinstance *psh, int noerror)
344{
345 char *pwd;
346 struct stat stdot, stpwd;
347 /*static int first = 1;*/
348
349 if (psh->curdir)
350 return;
351
352 if (psh->getpwd_first) {
353 psh->getpwd_first = 0;
354 pwd = sh_getenv(psh, "PWD");
355 if (pwd && IS_ROOT(pwd) && shfile_stat(&psh->fdtab, ".", &stdot) != -1 &&
356 shfile_stat(&psh->fdtab, pwd, &stpwd) != -1 &&
357 stdot.st_dev == stpwd.st_dev &&
358 stdot.st_ino == stpwd.st_ino) {
359 psh->curdir = savestr(pwd);
360 return;
361 }
362 }
363
364 find_curdir(psh, noerror);
365
366 return;
367}
368
369STATIC void
370find_curdir(shinstance *psh, int noerror)
371{
372 int i;
373 char *pwd;
374
375 /*
376 * Things are a bit complicated here; we could have just used
377 * getcwd, but traditionally getcwd is implemented using popen
378 * to /bin/pwd. This creates a problem for us, since we cannot
379 * keep track of the job if it is being ran behind our backs.
380 * So we re-implement getcwd(), and we suppress interrupts
381 * throughout the process. This is not completely safe, since
382 * the user can still break out of it by killing the pwd program.
383 * We still try to use getcwd for systems that we know have a
384 * c implementation of getcwd, that does not open a pipe to
385 * /bin/pwd.
386 */
387#if 1 //defined(__NetBSD__) || defined(__SVR4) || defined(__INNOTEK_LIBC__)
388
389 for (i = MAXPWD;; i *= 2) {
390 pwd = stalloc(psh, i);
391 if (shfile_getcwd(&psh->fdtab, pwd, i) != NULL) {
392 psh->curdir = savestr(pwd);
393 return;
394 }
395 stunalloc(psh, pwd);
396 if (errno == ERANGE)
397 continue;
398 if (!noerror)
399 error(psh, "getcwd() failed: %s", strerror(errno));
400 return;
401 }
402#else
403 {
404 char *p;
405 int status;
406 struct job *jp;
407 int pip[2];
408
409 pwd = stalloc(psh, MAXPWD);
410 INTOFF;
411 if (pipe(pip) < 0)
412 error("Pipe call failed");
413 jp = makejob((union node *)NULL, 1);
414 if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
415 (void) close(pip[0]);
416 if (pip[1] != 1) {
417 close(1);
418 copyfd(pip[1], 1);
419 close(pip[1]);
420 }
421 (void) execl("/bin/pwd", "pwd", (char *)0);
422 error("Cannot exec /bin/pwd");
423 }
424 (void) close(pip[1]);
425 pip[1] = -1;
426 p = pwd;
427 while ((i = read(pip[0], p, pwd + MAXPWD - p)) > 0
428 || (i == -1 && errno == EINTR)) {
429 if (i > 0)
430 p += i;
431 }
432 (void) close(pip[0]);
433 pip[0] = -1;
434 status = waitforjob(jp);
435 if (status != 0)
436 error((char *)0);
437 if (i < 0 || p == pwd || p[-1] != '\n') {
438 if (noerror) {
439 INTON;
440 return;
441 }
442 error("pwd command failed");
443 }
444 p[-1] = '\0';
445 INTON;
446 psh->curdir = savestr(pwd);
447 return;
448 }
449#endif
450}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette