Fri Jun 10 02:19:10 2011 UTC ()
Support $OLDPWD.  (christos@ will update the manual.)

Reviewd By:	christos


(uebayasi)
diff -r1.41 -r1.42 src/bin/sh/cd.c

cvs diff -r1.41 -r1.42 src/bin/sh/cd.c (expand / switch to unified diff)

--- src/bin/sh/cd.c 2011/02/17 15:13:49 1.41
+++ src/bin/sh/cd.c 2011/06/10 02:19:10 1.42
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: cd.c,v 1.41 2011/02/17 15:13:49 pooka Exp $ */ 1/* $NetBSD: cd.c,v 1.42 2011/06/10 02:19:10 uebayasi Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1991, 1993 4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist. 8 * Kenneth Almquist.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36#ifndef lint 36#ifndef lint
37#if 0 37#if 0
38static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 38static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
39#else 39#else
40__RCSID("$NetBSD: cd.c,v 1.41 2011/02/17 15:13:49 pooka Exp $"); 40__RCSID("$NetBSD: cd.c,v 1.42 2011/06/10 02:19:10 uebayasi Exp $");
41#endif 41#endif
42#endif /* not lint */ 42#endif /* not lint */
43 43
44#include <sys/types.h> 44#include <sys/types.h>
45#include <sys/stat.h> 45#include <sys/stat.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <string.h> 47#include <string.h>
48#include <unistd.h> 48#include <unistd.h>
49#include <errno.h> 49#include <errno.h>
50 50
51/* 51/*
52 * The cd and pwd commands. 52 * The cd and pwd commands.
53 */ 53 */
@@ -254,29 +254,30 @@ updatepwd(const char *dir) @@ -254,29 +254,30 @@ updatepwd(const char *dir)
254 /* 254 /*
255 * If our argument is NULL, we don't know the current directory 255 * If our argument is NULL, we don't know the current directory
256 * any more because we traversed a symbolic link or something 256 * any more because we traversed a symbolic link or something
257 * we couldn't stat(). 257 * we couldn't stat().
258 */ 258 */
259 if (dir == NULL || curdir == NULL) { 259 if (dir == NULL || curdir == NULL) {
260 if (prevdir) 260 if (prevdir)
261 ckfree(prevdir); 261 ckfree(prevdir);
262 INTOFF; 262 INTOFF;
263 prevdir = curdir; 263 prevdir = curdir;
264 curdir = NULL; 264 curdir = NULL;
265 getpwd(1); 265 getpwd(1);
266 INTON; 266 INTON;
267 if (curdir) 267 if (curdir) {
 268 setvar("OLDPWD", prevdir, VEXPORT);
268 setvar("PWD", curdir, VEXPORT); 269 setvar("PWD", curdir, VEXPORT);
269 else 270 } else
270 unsetvar("PWD", 0); 271 unsetvar("PWD", 0);
271 return; 272 return;
272 } 273 }
273 cdcomppath = stalloc(strlen(dir) + 1); 274 cdcomppath = stalloc(strlen(dir) + 1);
274 scopy(dir, cdcomppath); 275 scopy(dir, cdcomppath);
275 STARTSTACKSTR(new); 276 STARTSTACKSTR(new);
276 if (*dir != '/') { 277 if (*dir != '/') {
277 p = curdir; 278 p = curdir;
278 while (*p) 279 while (*p)
279 STPUTC(*p++, new); 280 STPUTC(*p++, new);
280 if (p[-1] == '/') 281 if (p[-1] == '/')
281 STUNPUTC(new); 282 STUNPUTC(new);
282 } 283 }
@@ -287,26 +288,27 @@ updatepwd(const char *dir) @@ -287,26 +288,27 @@ updatepwd(const char *dir)
287 STPUTC('/', new); 288 STPUTC('/', new);
288 while (*p) 289 while (*p)
289 STPUTC(*p++, new); 290 STPUTC(*p++, new);
290 } 291 }
291 } 292 }
292 if (new == stackblock()) 293 if (new == stackblock())
293 STPUTC('/', new); 294 STPUTC('/', new);
294 STACKSTRNUL(new); 295 STACKSTRNUL(new);
295 INTOFF; 296 INTOFF;
296 if (prevdir) 297 if (prevdir)
297 ckfree(prevdir); 298 ckfree(prevdir);
298 prevdir = curdir; 299 prevdir = curdir;
299 curdir = savestr(stackblock()); 300 curdir = savestr(stackblock());
 301 setvar("OLDPWD", prevdir, VEXPORT);
300 setvar("PWD", curdir, VEXPORT); 302 setvar("PWD", curdir, VEXPORT);
301 INTON; 303 INTON;
302} 304}
303 305
304/* 306/*
305 * Posix says the default should be 'pwd -L' (as below), however 307 * Posix says the default should be 'pwd -L' (as below), however
306 * the 'cd' command (above) does something much nearer to the 308 * the 'cd' command (above) does something much nearer to the
307 * posix 'cd -P' (not the posix default of 'cd -L'). 309 * posix 'cd -P' (not the posix default of 'cd -L').
308 * If 'cd' is changed to support -P/L then the default here 310 * If 'cd' is changed to support -P/L then the default here
309 * needs to be revisited if the historic behaviour is to be kept. 311 * needs to be revisited if the historic behaviour is to be kept.
310 */ 312 */
311 313
312int 314int
@@ -315,26 +317,27 @@ pwdcmd(int argc, char **argv) @@ -315,26 +317,27 @@ pwdcmd(int argc, char **argv)
315 int i; 317 int i;
316 char opt = 'L'; 318 char opt = 'L';
317 319
318 while ((i = nextopt("LP")) != '\0') 320 while ((i = nextopt("LP")) != '\0')
319 opt = i; 321 opt = i;
320 if (*argptr) 322 if (*argptr)
321 error("unexpected argument"); 323 error("unexpected argument");
322 324
323 if (opt == 'L') 325 if (opt == 'L')
324 getpwd(0); 326 getpwd(0);
325 else 327 else
326 find_curdir(0); 328 find_curdir(0);
327 329
 330 setvar("OLDPWD", prevdir, VEXPORT);
328 setvar("PWD", curdir, VEXPORT); 331 setvar("PWD", curdir, VEXPORT);
329 out1str(curdir); 332 out1str(curdir);
330 out1c('\n'); 333 out1c('\n');
331 return 0; 334 return 0;
332} 335}
333 336
334 337
335 338
336void 339void
337initpwd(void) 340initpwd(void)
338{ 341{
339 getpwd(1); 342 getpwd(1);
340 if (curdir) 343 if (curdir)