14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <sys/types.h> 42aa9caaf6SPeter Wemm #include <sys/stat.h> 43aa9caaf6SPeter Wemm #include <stdlib.h> 44ab0a2172SSteve Price #include <string.h> 45aa9caaf6SPeter Wemm #include <unistd.h> 46aa9caaf6SPeter Wemm #include <errno.h> 4793c0dc5eSTim J. Robbins #include <limits.h> 48aa9caaf6SPeter Wemm 494b88c807SRodney W. Grimes /* 504b88c807SRodney W. Grimes * The cd and pwd commands. 514b88c807SRodney W. Grimes */ 524b88c807SRodney W. Grimes 534b88c807SRodney W. Grimes #include "shell.h" 544b88c807SRodney W. Grimes #include "var.h" 554b88c807SRodney W. Grimes #include "nodes.h" /* for jobs.h */ 564b88c807SRodney W. Grimes #include "jobs.h" 574b88c807SRodney W. Grimes #include "options.h" 584b88c807SRodney W. Grimes #include "output.h" 594b88c807SRodney W. Grimes #include "memalloc.h" 604b88c807SRodney W. Grimes #include "error.h" 61ab0a2172SSteve Price #include "exec.h" 62aa9caaf6SPeter Wemm #include "redir.h" 634b88c807SRodney W. Grimes #include "mystring.h" 64aa9caaf6SPeter Wemm #include "show.h" 65ab0a2172SSteve Price #include "cd.h" 66454a02b3SJilles Tjoelker #include "builtins.h" 674b88c807SRodney W. Grimes 6888328642SDavid E. O'Brien static int cdlogical(char *); 6988328642SDavid E. O'Brien static int cdphysical(char *); 7088328642SDavid E. O'Brien static int docd(char *, int, int); 71d5119a2aSJilles Tjoelker static char *getcomponent(char **); 7288328642SDavid E. O'Brien static char *findcwd(char *); 7388328642SDavid E. O'Brien static void updatepwd(char *); 7488328642SDavid E. O'Brien static char *getpwd(void); 7588328642SDavid E. O'Brien static char *getpwd2(void); 764b88c807SRodney W. Grimes 77aa7b6f82SDavid E. O'Brien static char *curdir = NULL; /* current working directory */ 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes int 807cbda738SJilles Tjoelker cdcmd(int argc __unused, char **argv __unused) 81aa9caaf6SPeter Wemm { 82384aedabSJilles Tjoelker const char *dest; 832cac6e36SJilles Tjoelker const char *path; 844b88c807SRodney W. Grimes char *p; 854b88c807SRodney W. Grimes struct stat statb; 86d6ee26adSJilles Tjoelker int ch, phys, print = 0, getcwderr = 0; 87d6ee26adSJilles Tjoelker int rc; 88168b9dd1SJilles Tjoelker int errno1 = ENOENT; 894b88c807SRodney W. Grimes 907e1975c2STim J. Robbins phys = Pflag; 917cbda738SJilles Tjoelker while ((ch = nextopt("eLP")) != '\0') { 9209086b04STim J. Robbins switch (ch) { 93d6ee26adSJilles Tjoelker case 'e': 94d6ee26adSJilles Tjoelker getcwderr = 1; 95d6ee26adSJilles Tjoelker break; 9609086b04STim J. Robbins case 'L': 9709086b04STim J. Robbins phys = 0; 9809086b04STim J. Robbins break; 9909086b04STim J. Robbins case 'P': 10009086b04STim J. Robbins phys = 1; 10109086b04STim J. Robbins break; 10209086b04STim J. Robbins } 10309086b04STim J. Robbins } 10409086b04STim J. Robbins 1057cbda738SJilles Tjoelker if (*argptr != NULL && argptr[1] != NULL) 10609086b04STim J. Robbins error("too many arguments"); 10709086b04STim J. Robbins 1087cbda738SJilles Tjoelker if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 1094b88c807SRodney W. Grimes error("HOME not set"); 11061233bdcSBruce Evans if (*dest == '\0') 11161233bdcSBruce Evans dest = "."; 1124b88c807SRodney W. Grimes if (dest[0] == '-' && dest[1] == '\0') { 1137b40c6dfSJilles Tjoelker dest = bltinlookup("OLDPWD", 1); 1147b40c6dfSJilles Tjoelker if (dest == NULL) 1157b40c6dfSJilles Tjoelker error("OLDPWD not set"); 1164b88c807SRodney W. Grimes print = 1; 1174b88c807SRodney W. Grimes } 11864fa41f3SJilles Tjoelker if (dest[0] == '/' || 11964fa41f3SJilles Tjoelker (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) || 12064fa41f3SJilles Tjoelker (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) || 12164fa41f3SJilles Tjoelker (path = bltinlookup("CDPATH", 1)) == NULL) 122781bfb5aSJilles Tjoelker path = ""; 1234b88c807SRodney W. Grimes while ((p = padvance(&path, dest)) != NULL) { 124e94e3511SJilles Tjoelker if (stat(p, &statb) < 0) { 125e94e3511SJilles Tjoelker if (errno != ENOENT) 126e94e3511SJilles Tjoelker errno1 = errno; 127e94e3511SJilles Tjoelker } else if (!S_ISDIR(statb.st_mode)) 128e94e3511SJilles Tjoelker errno1 = ENOTDIR; 129e94e3511SJilles Tjoelker else { 1304b88c807SRodney W. Grimes if (!print) { 1314b88c807SRodney W. Grimes /* 1324b88c807SRodney W. Grimes * XXX - rethink 1334b88c807SRodney W. Grimes */ 13437f0a670STor Egge if (p[0] == '.' && p[1] == '/' && p[2] != '\0') 135ed5c24e2SStefan Farfeleder print = strcmp(p + 2, dest); 136ed5c24e2SStefan Farfeleder else 1374b88c807SRodney W. Grimes print = strcmp(p, dest); 1384b88c807SRodney W. Grimes } 139d6ee26adSJilles Tjoelker rc = docd(p, print, phys); 140d6ee26adSJilles Tjoelker if (rc >= 0) 141d6ee26adSJilles Tjoelker return getcwderr ? rc : 0; 142168b9dd1SJilles Tjoelker if (errno != ENOENT) 143168b9dd1SJilles Tjoelker errno1 = errno; 1444b88c807SRodney W. Grimes } 1454b88c807SRodney W. Grimes } 146168b9dd1SJilles Tjoelker error("%s: %s", dest, strerror(errno1)); 147aa9caaf6SPeter Wemm /*NOTREACHED*/ 148aa9caaf6SPeter Wemm return 0; 1494b88c807SRodney W. Grimes } 1504b88c807SRodney W. Grimes 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes /* 15309086b04STim J. Robbins * Actually change the directory. In an interactive shell, print the 154958ba632SSteve Price * directory name if "print" is nonzero. 1554b88c807SRodney W. Grimes */ 15688328642SDavid E. O'Brien static int 15709086b04STim J. Robbins docd(char *dest, int print, int phys) 15809086b04STim J. Robbins { 159d6ee26adSJilles Tjoelker int rc; 16009086b04STim J. Robbins 16109086b04STim J. Robbins TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 16209086b04STim J. Robbins 16309086b04STim J. Robbins /* If logical cd fails, fall back to physical. */ 164d6ee26adSJilles Tjoelker if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0) 16509086b04STim J. Robbins return (-1); 16609086b04STim J. Robbins 167*6f49cd26SJilles Tjoelker if (print && iflag && curdir) { 16809086b04STim J. Robbins out1fmt("%s\n", curdir); 169*6f49cd26SJilles Tjoelker /* 170*6f49cd26SJilles Tjoelker * Ignore write errors to preserve the invariant that the 171*6f49cd26SJilles Tjoelker * current directory is changed iff the exit status is 0 172*6f49cd26SJilles Tjoelker * (or 1 if -e was given and the full pathname could not be 173*6f49cd26SJilles Tjoelker * determined). 174*6f49cd26SJilles Tjoelker */ 175*6f49cd26SJilles Tjoelker flushout(out1); 176*6f49cd26SJilles Tjoelker outclearerror(out1); 177*6f49cd26SJilles Tjoelker } 17809086b04STim J. Robbins 179d6ee26adSJilles Tjoelker return (rc); 18009086b04STim J. Robbins } 18109086b04STim J. Robbins 18288328642SDavid E. O'Brien static int 18309086b04STim J. Robbins cdlogical(char *dest) 1844b88c807SRodney W. Grimes { 18537f0a670STor Egge char *p; 18637f0a670STor Egge char *q; 18737f0a670STor Egge char *component; 188d5119a2aSJilles Tjoelker char *path; 18937f0a670STor Egge struct stat statb; 19037f0a670STor Egge int first; 19137f0a670STor Egge int badstat; 192ab0a2172SSteve Price 19337f0a670STor Egge /* 19437f0a670STor Egge * Check each component of the path. If we find a symlink or 19537f0a670STor Egge * something we can't stat, clear curdir to force a getcwd() 19637f0a670STor Egge * next time we get the value of the current directory. 19737f0a670STor Egge */ 19837f0a670STor Egge badstat = 0; 199d5119a2aSJilles Tjoelker path = stsavestr(dest); 20037f0a670STor Egge STARTSTACKSTR(p); 20137f0a670STor Egge if (*dest == '/') { 20237f0a670STor Egge STPUTC('/', p); 203d5119a2aSJilles Tjoelker path++; 20437f0a670STor Egge } 20537f0a670STor Egge first = 1; 206d5119a2aSJilles Tjoelker while ((q = getcomponent(&path)) != NULL) { 20737f0a670STor Egge if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 20837f0a670STor Egge continue; 20937f0a670STor Egge if (! first) 21037f0a670STor Egge STPUTC('/', p); 21137f0a670STor Egge first = 0; 21237f0a670STor Egge component = q; 2139d37e157SJilles Tjoelker STPUTS(q, p); 21437f0a670STor Egge if (equal(component, "..")) 21537f0a670STor Egge continue; 21637f0a670STor Egge STACKSTRNUL(p); 21709086b04STim J. Robbins if (lstat(stackblock(), &statb) < 0) { 21837f0a670STor Egge badstat = 1; 21937f0a670STor Egge break; 22037f0a670STor Egge } 22137f0a670STor Egge } 22237f0a670STor Egge 2234b88c807SRodney W. Grimes INTOFF; 22455d2c5b5SStefan Farfeleder if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 2254b88c807SRodney W. Grimes INTON; 22609086b04STim J. Robbins return (-1); 2274b88c807SRodney W. Grimes } 22855d2c5b5SStefan Farfeleder updatepwd(p); 2294b88c807SRodney W. Grimes INTON; 23009086b04STim J. Robbins return (0); 2314b88c807SRodney W. Grimes } 2324b88c807SRodney W. Grimes 23388328642SDavid E. O'Brien static int 23409086b04STim J. Robbins cdphysical(char *dest) 23509086b04STim J. Robbins { 23655d2c5b5SStefan Farfeleder char *p; 237d6ee26adSJilles Tjoelker int rc = 0; 23809086b04STim J. Robbins 23909086b04STim J. Robbins INTOFF; 240ae7f4735SJilles Tjoelker if (chdir(dest) < 0) { 24109086b04STim J. Robbins INTON; 24209086b04STim J. Robbins return (-1); 24309086b04STim J. Robbins } 244ae7f4735SJilles Tjoelker p = findcwd(NULL); 245d6ee26adSJilles Tjoelker if (p == NULL) { 2465fe9123fSJilles Tjoelker warning("warning: failed to get name of current directory"); 247d6ee26adSJilles Tjoelker rc = 1; 248d6ee26adSJilles Tjoelker } 24955d2c5b5SStefan Farfeleder updatepwd(p); 25009086b04STim J. Robbins INTON; 251d6ee26adSJilles Tjoelker return (rc); 25209086b04STim J. Robbins } 2534b88c807SRodney W. Grimes 2544b88c807SRodney W. Grimes /* 255d5119a2aSJilles Tjoelker * Get the next component of the path name pointed to by *path. 256d5119a2aSJilles Tjoelker * This routine overwrites *path and the string pointed to by it. 2574b88c807SRodney W. Grimes */ 25888328642SDavid E. O'Brien static char * 259d5119a2aSJilles Tjoelker getcomponent(char **path) 260958ba632SSteve Price { 261afb033d5SSteve Price char *p; 2624b88c807SRodney W. Grimes char *start; 2634b88c807SRodney W. Grimes 264d5119a2aSJilles Tjoelker if ((p = *path) == NULL) 2654b88c807SRodney W. Grimes return NULL; 266d5119a2aSJilles Tjoelker start = *path; 2674b88c807SRodney W. Grimes while (*p != '/' && *p != '\0') 2684b88c807SRodney W. Grimes p++; 2694b88c807SRodney W. Grimes if (*p == '\0') { 270d5119a2aSJilles Tjoelker *path = NULL; 2714b88c807SRodney W. Grimes } else { 2724b88c807SRodney W. Grimes *p++ = '\0'; 273d5119a2aSJilles Tjoelker *path = p; 2744b88c807SRodney W. Grimes } 2754b88c807SRodney W. Grimes return start; 2764b88c807SRodney W. Grimes } 2774b88c807SRodney W. Grimes 2784b88c807SRodney W. Grimes 27988328642SDavid E. O'Brien static char * 28055d2c5b5SStefan Farfeleder findcwd(char *dir) 2814b88c807SRodney W. Grimes { 2824b88c807SRodney W. Grimes char *new; 2834b88c807SRodney W. Grimes char *p; 284d5119a2aSJilles Tjoelker char *path; 2854b88c807SRodney W. Grimes 28637f0a670STor Egge /* 28737f0a670STor Egge * If our argument is NULL, we don't know the current directory 28837f0a670STor Egge * any more because we traversed a symbolic link or something 28937f0a670STor Egge * we couldn't stat(). 29037f0a670STor Egge */ 2911c645e0fSStefan Farfeleder if (dir == NULL || curdir == NULL) 2921c645e0fSStefan Farfeleder return getpwd2(); 293d5119a2aSJilles Tjoelker path = stsavestr(dir); 2944b88c807SRodney W. Grimes STARTSTACKSTR(new); 2954b88c807SRodney W. Grimes if (*dir != '/') { 2969d37e157SJilles Tjoelker STPUTS(curdir, new); 2979d37e157SJilles Tjoelker if (STTOPC(new) == '/') 2984b88c807SRodney W. Grimes STUNPUTC(new); 2994b88c807SRodney W. Grimes } 300d5119a2aSJilles Tjoelker while ((p = getcomponent(&path)) != NULL) { 3014b88c807SRodney W. Grimes if (equal(p, "..")) { 3024b88c807SRodney W. Grimes while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 3034b88c807SRodney W. Grimes } else if (*p != '\0' && ! equal(p, ".")) { 3044b88c807SRodney W. Grimes STPUTC('/', new); 3059d37e157SJilles Tjoelker STPUTS(p, new); 3064b88c807SRodney W. Grimes } 3074b88c807SRodney W. Grimes } 3084b88c807SRodney W. Grimes if (new == stackblock()) 3094b88c807SRodney W. Grimes STPUTC('/', new); 3104b88c807SRodney W. Grimes STACKSTRNUL(new); 31155d2c5b5SStefan Farfeleder return stackblock(); 31255d2c5b5SStefan Farfeleder } 31355d2c5b5SStefan Farfeleder 31455d2c5b5SStefan Farfeleder /* 31555d2c5b5SStefan Farfeleder * Update curdir (the name of the current directory) in response to a 31655d2c5b5SStefan Farfeleder * cd command. We also call hashcd to let the routines in exec.c know 31755d2c5b5SStefan Farfeleder * that the current directory has changed. 31855d2c5b5SStefan Farfeleder */ 31988328642SDavid E. O'Brien static void 32055d2c5b5SStefan Farfeleder updatepwd(char *dir) 32155d2c5b5SStefan Farfeleder { 3227b40c6dfSJilles Tjoelker char *prevdir; 3237b40c6dfSJilles Tjoelker 32455d2c5b5SStefan Farfeleder hashcd(); /* update command hash table */ 32555d2c5b5SStefan Farfeleder 3267b40c6dfSJilles Tjoelker setvar("PWD", dir, VEXPORT); 3277b40c6dfSJilles Tjoelker setvar("OLDPWD", curdir, VEXPORT); 32837f0a670STor Egge prevdir = curdir; 329ae7f4735SJilles Tjoelker curdir = dir ? savestr(dir) : NULL; 3307b40c6dfSJilles Tjoelker ckfree(prevdir); 3314b88c807SRodney W. Grimes } 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes int 3347cbda738SJilles Tjoelker pwdcmd(int argc __unused, char **argv __unused) 335aa9caaf6SPeter Wemm { 3361c645e0fSStefan Farfeleder char *p; 33709086b04STim J. Robbins int ch, phys; 33809086b04STim J. Robbins 3397e1975c2STim J. Robbins phys = Pflag; 3407cbda738SJilles Tjoelker while ((ch = nextopt("LP")) != '\0') { 34109086b04STim J. Robbins switch (ch) { 34209086b04STim J. Robbins case 'L': 34309086b04STim J. Robbins phys = 0; 34409086b04STim J. Robbins break; 34509086b04STim J. Robbins case 'P': 34609086b04STim J. Robbins phys = 1; 34709086b04STim J. Robbins break; 34809086b04STim J. Robbins } 34909086b04STim J. Robbins } 35009086b04STim J. Robbins 3517cbda738SJilles Tjoelker if (*argptr != NULL) 35209086b04STim J. Robbins error("too many arguments"); 35309086b04STim J. Robbins 35409086b04STim J. Robbins if (!phys && getpwd()) { 3554b88c807SRodney W. Grimes out1str(curdir); 3564b88c807SRodney W. Grimes out1c('\n'); 35709086b04STim J. Robbins } else { 3581c645e0fSStefan Farfeleder if ((p = getpwd2()) == NULL) 35909086b04STim J. Robbins error(".: %s", strerror(errno)); 3601c645e0fSStefan Farfeleder out1str(p); 36109086b04STim J. Robbins out1c('\n'); 362178897f1STim J. Robbins } 36337f0a670STor Egge 36409086b04STim J. Robbins return 0; 36509086b04STim J. Robbins } 366baf3e7c1STim J. Robbins 3674b88c807SRodney W. Grimes /* 36855d2c5b5SStefan Farfeleder * Get the current directory and cache the result in curdir. 3694b88c807SRodney W. Grimes */ 37088328642SDavid E. O'Brien static char * 3715134c3f7SWarner Losh getpwd(void) 372ab0a2172SSteve Price { 37355d2c5b5SStefan Farfeleder char *p; 37437f0a670STor Egge 3754b88c807SRodney W. Grimes if (curdir) 37637f0a670STor Egge return curdir; 37755d2c5b5SStefan Farfeleder 3781c645e0fSStefan Farfeleder p = getpwd2(); 37955d2c5b5SStefan Farfeleder if (p != NULL) 38055d2c5b5SStefan Farfeleder curdir = savestr(p); 38155d2c5b5SStefan Farfeleder 38255d2c5b5SStefan Farfeleder return curdir; 38355d2c5b5SStefan Farfeleder } 38455d2c5b5SStefan Farfeleder 3851c645e0fSStefan Farfeleder #define MAXPWD 256 3861c645e0fSStefan Farfeleder 38755d2c5b5SStefan Farfeleder /* 38855d2c5b5SStefan Farfeleder * Return the current directory. 38955d2c5b5SStefan Farfeleder */ 39088328642SDavid E. O'Brien static char * 3911c645e0fSStefan Farfeleder getpwd2(void) 39255d2c5b5SStefan Farfeleder { 3931c645e0fSStefan Farfeleder char *pwd; 3941c645e0fSStefan Farfeleder int i; 39537f0a670STor Egge 3961c645e0fSStefan Farfeleder for (i = MAXPWD;; i *= 2) { 3971c645e0fSStefan Farfeleder pwd = stalloc(i); 3981c645e0fSStefan Farfeleder if (getcwd(pwd, i) != NULL) 3991c645e0fSStefan Farfeleder return pwd; 4001c645e0fSStefan Farfeleder stunalloc(pwd); 4011c645e0fSStefan Farfeleder if (errno != ERANGE) 4021c645e0fSStefan Farfeleder break; 4031c645e0fSStefan Farfeleder } 4041c645e0fSStefan Farfeleder 4058eac1f94SJilles Tjoelker return NULL; 4068eac1f94SJilles Tjoelker } 4078eac1f94SJilles Tjoelker 4088eac1f94SJilles Tjoelker /* 4098eac1f94SJilles Tjoelker * Initialize PWD in a new shell. 4108eac1f94SJilles Tjoelker * If the shell is interactive, we need to warn if this fails. 4118eac1f94SJilles Tjoelker */ 4128eac1f94SJilles Tjoelker void 4138eac1f94SJilles Tjoelker pwd_init(int warn) 4148eac1f94SJilles Tjoelker { 4158eac1f94SJilles Tjoelker char *pwd; 4168eac1f94SJilles Tjoelker struct stat stdot, stpwd; 4178eac1f94SJilles Tjoelker 4188eac1f94SJilles Tjoelker pwd = lookupvar("PWD"); 41937f0a670STor Egge if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 42037f0a670STor Egge stat(pwd, &stpwd) != -1 && 42137f0a670STor Egge stdot.st_dev == stpwd.st_dev && 42237f0a670STor Egge stdot.st_ino == stpwd.st_ino) { 4238eac1f94SJilles Tjoelker if (curdir) 4248eac1f94SJilles Tjoelker ckfree(curdir); 4258eac1f94SJilles Tjoelker curdir = savestr(pwd); 42637f0a670STor Egge } 4278eac1f94SJilles Tjoelker if (getpwd() == NULL && warn) 4288eac1f94SJilles Tjoelker out2fmt_flush("sh: cannot determine working directory\n"); 4298eac1f94SJilles Tjoelker setvar("PWD", curdir, VEXPORT); 43037f0a670STor Egge } 431