Deleted Added
full compact
cd.c (201053) cd.c (206759)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/cd.c 201053 2009-12-27 18:04:05Z jilles $");
39__FBSDID("$FreeBSD: head/bin/sh/cd.c 206759 2010-04-17 14:35:46Z jilles $");
40
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46#include <errno.h>
47#include <limits.h>

--- 17 unchanged lines hidden (view full) ---

65#include "cd.h"
66
67STATIC int cdlogical(char *);
68STATIC int cdphysical(char *);
69STATIC int docd(char *, int, int);
70STATIC char *getcomponent(void);
71STATIC char *findcwd(char *);
72STATIC void updatepwd(char *);
40
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46#include <errno.h>
47#include <limits.h>

--- 17 unchanged lines hidden (view full) ---

65#include "cd.h"
66
67STATIC int cdlogical(char *);
68STATIC int cdphysical(char *);
69STATIC int docd(char *, int, int);
70STATIC char *getcomponent(void);
71STATIC char *findcwd(char *);
72STATIC void updatepwd(char *);
73STATIC char *getpwd(void);
73STATIC char *getpwd2(void);
74
75STATIC char *curdir = NULL; /* current working directory */
76STATIC char *prevdir; /* previous working directory */
77STATIC char *cdcomppath;
78
79int
80cdcmd(int argc, char **argv)

--- 265 unchanged lines hidden (view full) ---

346 }
347
348 return 0;
349}
350
351/*
352 * Get the current directory and cache the result in curdir.
353 */
74STATIC char *getpwd2(void);
75
76STATIC char *curdir = NULL; /* current working directory */
77STATIC char *prevdir; /* previous working directory */
78STATIC char *cdcomppath;
79
80int
81cdcmd(int argc, char **argv)

--- 265 unchanged lines hidden (view full) ---

347 }
348
349 return 0;
350}
351
352/*
353 * Get the current directory and cache the result in curdir.
354 */
354char *
355STATIC char *
355getpwd(void)
356{
357 char *p;
358
359 if (curdir)
360 return curdir;
361
362 p = getpwd2();

--- 6 unchanged lines hidden (view full) ---

369#define MAXPWD 256
370
371/*
372 * Return the current directory.
373 */
374STATIC char *
375getpwd2(void)
376{
356getpwd(void)
357{
358 char *p;
359
360 if (curdir)
361 return curdir;
362
363 p = getpwd2();

--- 6 unchanged lines hidden (view full) ---

370#define MAXPWD 256
371
372/*
373 * Return the current directory.
374 */
375STATIC char *
376getpwd2(void)
377{
377 struct stat stdot, stpwd;
378 char *pwd;
379 int i;
380
381 for (i = MAXPWD;; i *= 2) {
382 pwd = stalloc(i);
383 if (getcwd(pwd, i) != NULL)
384 return pwd;
385 stunalloc(pwd);
386 if (errno != ERANGE)
387 break;
388 }
389
378 char *pwd;
379 int i;
380
381 for (i = MAXPWD;; i *= 2) {
382 pwd = stalloc(i);
383 if (getcwd(pwd, i) != NULL)
384 return pwd;
385 stunalloc(pwd);
386 if (errno != ERANGE)
387 break;
388 }
389
390 pwd = getenv("PWD");
390 return NULL;
391}
392
393/*
394 * Initialize PWD in a new shell.
395 * If the shell is interactive, we need to warn if this fails.
396 */
397void
398pwd_init(int warn)
399{
400 char *pwd;
401 struct stat stdot, stpwd;
402
403 pwd = lookupvar("PWD");
391 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
392 stat(pwd, &stpwd) != -1 &&
393 stdot.st_dev == stpwd.st_dev &&
394 stdot.st_ino == stpwd.st_ino) {
404 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
405 stat(pwd, &stpwd) != -1 &&
406 stdot.st_dev == stpwd.st_dev &&
407 stdot.st_ino == stpwd.st_ino) {
395 return pwd;
408 if (curdir)
409 ckfree(curdir);
410 curdir = savestr(pwd);
396 }
411 }
397 return NULL;
412 if (getpwd() == NULL && warn)
413 out2fmt_flush("sh: cannot determine working directory\n");
414 setvar("PWD", curdir, VEXPORT);
398}
415}