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. 164b88c807SRodney W. Grimes * 4. 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" 664b88c807SRodney W. Grimes 6788328642SDavid E. O'Brien static int cdlogical(char *); 6888328642SDavid E. O'Brien static int cdphysical(char *); 6988328642SDavid E. O'Brien static int docd(char *, int, int); 7088328642SDavid E. O'Brien static char *getcomponent(void); 7188328642SDavid E. O'Brien static char *findcwd(char *); 7288328642SDavid E. O'Brien static void updatepwd(char *); 7388328642SDavid E. O'Brien static char *getpwd(void); 7488328642SDavid E. O'Brien static char *getpwd2(void); 754b88c807SRodney W. Grimes 76aa7b6f82SDavid E. O'Brien static char *curdir = NULL; /* current working directory */ 77aa7b6f82SDavid E. O'Brien static char *prevdir; /* previous working directory */ 78aa7b6f82SDavid E. O'Brien static char *cdcomppath; 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes int 8109086b04STim J. Robbins cdcmd(int argc, char **argv) 82aa9caaf6SPeter Wemm { 83384aedabSJilles Tjoelker const char *dest; 842cac6e36SJilles Tjoelker const char *path; 854b88c807SRodney W. Grimes char *p; 864b88c807SRodney W. Grimes struct stat statb; 87d6ee26adSJilles Tjoelker int ch, phys, print = 0, getcwderr = 0; 88d6ee26adSJilles Tjoelker int rc; 89*168b9dd1SJilles Tjoelker int errno1 = ENOENT; 904b88c807SRodney W. Grimes 91050f7913STim J. Robbins optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 927e1975c2STim J. Robbins phys = Pflag; 93d6ee26adSJilles Tjoelker while ((ch = getopt(argc, argv, "eLP")) != -1) { 9409086b04STim J. Robbins switch (ch) { 95d6ee26adSJilles Tjoelker case 'e': 96d6ee26adSJilles Tjoelker getcwderr = 1; 97d6ee26adSJilles Tjoelker break; 9809086b04STim J. Robbins case 'L': 9909086b04STim J. Robbins phys = 0; 10009086b04STim J. Robbins break; 10109086b04STim J. Robbins case 'P': 10209086b04STim J. Robbins phys = 1; 10309086b04STim J. Robbins break; 10409086b04STim J. Robbins default: 10509086b04STim J. Robbins error("unknown option: -%c", optopt); 10609086b04STim J. Robbins break; 10709086b04STim J. Robbins } 10809086b04STim J. Robbins } 10909086b04STim J. Robbins argc -= optind; 11009086b04STim J. Robbins argv += optind; 11109086b04STim J. Robbins 11209086b04STim J. Robbins if (argc > 1) 11309086b04STim J. Robbins error("too many arguments"); 11409086b04STim J. Robbins 11509086b04STim J. Robbins if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 1164b88c807SRodney W. Grimes error("HOME not set"); 11761233bdcSBruce Evans if (*dest == '\0') 11861233bdcSBruce Evans dest = "."; 1194b88c807SRodney W. Grimes if (dest[0] == '-' && dest[1] == '\0') { 1204b88c807SRodney W. Grimes dest = prevdir ? prevdir : curdir; 1218d7d5ceaSPeter Wemm if (dest) 1224b88c807SRodney W. Grimes print = 1; 1238d7d5ceaSPeter Wemm else 1248d7d5ceaSPeter Wemm dest = "."; 1254b88c807SRodney W. Grimes } 1264b88c807SRodney W. Grimes if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL) 1274b88c807SRodney W. Grimes path = nullstr; 1284b88c807SRodney W. Grimes while ((p = padvance(&path, dest)) != NULL) { 129aa9caaf6SPeter Wemm if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { 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; 142*168b9dd1SJilles Tjoelker if (errno != ENOENT) 143*168b9dd1SJilles Tjoelker errno1 = errno; 1444b88c807SRodney W. Grimes } 1454b88c807SRodney W. Grimes } 146*168b9dd1SJilles 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 16709086b04STim J. Robbins if (print && iflag && curdir) 16809086b04STim J. Robbins out1fmt("%s\n", curdir); 16909086b04STim J. Robbins 170d6ee26adSJilles Tjoelker return (rc); 17109086b04STim J. Robbins } 17209086b04STim J. Robbins 17388328642SDavid E. O'Brien static int 17409086b04STim J. Robbins cdlogical(char *dest) 1754b88c807SRodney W. Grimes { 17637f0a670STor Egge char *p; 17737f0a670STor Egge char *q; 17837f0a670STor Egge char *component; 17937f0a670STor Egge struct stat statb; 18037f0a670STor Egge int first; 18137f0a670STor Egge int badstat; 182ab0a2172SSteve Price 18337f0a670STor Egge /* 18437f0a670STor Egge * Check each component of the path. If we find a symlink or 18537f0a670STor Egge * something we can't stat, clear curdir to force a getcwd() 18637f0a670STor Egge * next time we get the value of the current directory. 18737f0a670STor Egge */ 18837f0a670STor Egge badstat = 0; 18937f0a670STor Egge cdcomppath = stalloc(strlen(dest) + 1); 19037f0a670STor Egge scopy(dest, cdcomppath); 19137f0a670STor Egge STARTSTACKSTR(p); 19237f0a670STor Egge if (*dest == '/') { 19337f0a670STor Egge STPUTC('/', p); 19437f0a670STor Egge cdcomppath++; 19537f0a670STor Egge } 19637f0a670STor Egge first = 1; 19737f0a670STor Egge while ((q = getcomponent()) != NULL) { 19837f0a670STor Egge if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 19937f0a670STor Egge continue; 20037f0a670STor Egge if (! first) 20137f0a670STor Egge STPUTC('/', p); 20237f0a670STor Egge first = 0; 20337f0a670STor Egge component = q; 2049d37e157SJilles Tjoelker STPUTS(q, p); 20537f0a670STor Egge if (equal(component, "..")) 20637f0a670STor Egge continue; 20737f0a670STor Egge STACKSTRNUL(p); 20809086b04STim J. Robbins if (lstat(stackblock(), &statb) < 0) { 20937f0a670STor Egge badstat = 1; 21037f0a670STor Egge break; 21137f0a670STor Egge } 21237f0a670STor Egge } 21337f0a670STor Egge 2144b88c807SRodney W. Grimes INTOFF; 21555d2c5b5SStefan Farfeleder if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 2164b88c807SRodney W. Grimes INTON; 21709086b04STim J. Robbins return (-1); 2184b88c807SRodney W. Grimes } 21955d2c5b5SStefan Farfeleder updatepwd(p); 2204b88c807SRodney W. Grimes INTON; 22109086b04STim J. Robbins return (0); 2224b88c807SRodney W. Grimes } 2234b88c807SRodney W. Grimes 22488328642SDavid E. O'Brien static int 22509086b04STim J. Robbins cdphysical(char *dest) 22609086b04STim J. Robbins { 22755d2c5b5SStefan Farfeleder char *p; 228d6ee26adSJilles Tjoelker int rc = 0; 22909086b04STim J. Robbins 23009086b04STim J. Robbins INTOFF; 231ae7f4735SJilles Tjoelker if (chdir(dest) < 0) { 23209086b04STim J. Robbins INTON; 23309086b04STim J. Robbins return (-1); 23409086b04STim J. Robbins } 235ae7f4735SJilles Tjoelker p = findcwd(NULL); 236d6ee26adSJilles Tjoelker if (p == NULL) { 2375fe9123fSJilles Tjoelker warning("warning: failed to get name of current directory"); 238d6ee26adSJilles Tjoelker rc = 1; 239d6ee26adSJilles Tjoelker } 24055d2c5b5SStefan Farfeleder updatepwd(p); 24109086b04STim J. Robbins INTON; 242d6ee26adSJilles Tjoelker return (rc); 24309086b04STim J. Robbins } 2444b88c807SRodney W. Grimes 2454b88c807SRodney W. Grimes /* 2464b88c807SRodney W. Grimes * Get the next component of the path name pointed to by cdcomppath. 2474b88c807SRodney W. Grimes * This routine overwrites the string pointed to by cdcomppath. 2484b88c807SRodney W. Grimes */ 24988328642SDavid E. O'Brien static char * 2505134c3f7SWarner Losh getcomponent(void) 251958ba632SSteve Price { 252afb033d5SSteve Price char *p; 2534b88c807SRodney W. Grimes char *start; 2544b88c807SRodney W. Grimes 2554b88c807SRodney W. Grimes if ((p = cdcomppath) == NULL) 2564b88c807SRodney W. Grimes return NULL; 2574b88c807SRodney W. Grimes start = cdcomppath; 2584b88c807SRodney W. Grimes while (*p != '/' && *p != '\0') 2594b88c807SRodney W. Grimes p++; 2604b88c807SRodney W. Grimes if (*p == '\0') { 2614b88c807SRodney W. Grimes cdcomppath = NULL; 2624b88c807SRodney W. Grimes } else { 2634b88c807SRodney W. Grimes *p++ = '\0'; 2644b88c807SRodney W. Grimes cdcomppath = p; 2654b88c807SRodney W. Grimes } 2664b88c807SRodney W. Grimes return start; 2674b88c807SRodney W. Grimes } 2684b88c807SRodney W. Grimes 2694b88c807SRodney W. Grimes 27088328642SDavid E. O'Brien static char * 27155d2c5b5SStefan Farfeleder findcwd(char *dir) 2724b88c807SRodney W. Grimes { 2734b88c807SRodney W. Grimes char *new; 2744b88c807SRodney W. Grimes char *p; 2754b88c807SRodney W. Grimes 27637f0a670STor Egge /* 27737f0a670STor Egge * If our argument is NULL, we don't know the current directory 27837f0a670STor Egge * any more because we traversed a symbolic link or something 27937f0a670STor Egge * we couldn't stat(). 28037f0a670STor Egge */ 2811c645e0fSStefan Farfeleder if (dir == NULL || curdir == NULL) 2821c645e0fSStefan Farfeleder return getpwd2(); 2834b88c807SRodney W. Grimes cdcomppath = stalloc(strlen(dir) + 1); 2844b88c807SRodney W. Grimes scopy(dir, cdcomppath); 2854b88c807SRodney W. Grimes STARTSTACKSTR(new); 2864b88c807SRodney W. Grimes if (*dir != '/') { 2879d37e157SJilles Tjoelker STPUTS(curdir, new); 2889d37e157SJilles Tjoelker if (STTOPC(new) == '/') 2894b88c807SRodney W. Grimes STUNPUTC(new); 2904b88c807SRodney W. Grimes } 2914b88c807SRodney W. Grimes while ((p = getcomponent()) != NULL) { 2924b88c807SRodney W. Grimes if (equal(p, "..")) { 2934b88c807SRodney W. Grimes while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 2944b88c807SRodney W. Grimes } else if (*p != '\0' && ! equal(p, ".")) { 2954b88c807SRodney W. Grimes STPUTC('/', new); 2969d37e157SJilles Tjoelker STPUTS(p, new); 2974b88c807SRodney W. Grimes } 2984b88c807SRodney W. Grimes } 2994b88c807SRodney W. Grimes if (new == stackblock()) 3004b88c807SRodney W. Grimes STPUTC('/', new); 3014b88c807SRodney W. Grimes STACKSTRNUL(new); 30255d2c5b5SStefan Farfeleder return stackblock(); 30355d2c5b5SStefan Farfeleder } 30455d2c5b5SStefan Farfeleder 30555d2c5b5SStefan Farfeleder /* 30655d2c5b5SStefan Farfeleder * Update curdir (the name of the current directory) in response to a 30755d2c5b5SStefan Farfeleder * cd command. We also call hashcd to let the routines in exec.c know 30855d2c5b5SStefan Farfeleder * that the current directory has changed. 30955d2c5b5SStefan Farfeleder */ 31088328642SDavid E. O'Brien static void 31155d2c5b5SStefan Farfeleder updatepwd(char *dir) 31255d2c5b5SStefan Farfeleder { 31355d2c5b5SStefan Farfeleder hashcd(); /* update command hash table */ 31455d2c5b5SStefan Farfeleder 31537f0a670STor Egge if (prevdir) 31637f0a670STor Egge ckfree(prevdir); 31737f0a670STor Egge prevdir = curdir; 318ae7f4735SJilles Tjoelker curdir = dir ? savestr(dir) : NULL; 319f14c1c47STor Egge setvar("PWD", curdir, VEXPORT); 320f14c1c47STor Egge setvar("OLDPWD", prevdir, VEXPORT); 3214b88c807SRodney W. Grimes } 3224b88c807SRodney W. Grimes 3234b88c807SRodney W. Grimes int 324b5ae6928STim J. Robbins pwdcmd(int argc, char **argv) 325aa9caaf6SPeter Wemm { 3261c645e0fSStefan Farfeleder char *p; 32709086b04STim J. Robbins int ch, phys; 32809086b04STim J. Robbins 329050f7913STim J. Robbins optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 3307e1975c2STim J. Robbins phys = Pflag; 33109086b04STim J. Robbins while ((ch = getopt(argc, argv, "LP")) != -1) { 33209086b04STim J. Robbins switch (ch) { 33309086b04STim J. Robbins case 'L': 33409086b04STim J. Robbins phys = 0; 33509086b04STim J. Robbins break; 33609086b04STim J. Robbins case 'P': 33709086b04STim J. Robbins phys = 1; 33809086b04STim J. Robbins break; 33909086b04STim J. Robbins default: 34009086b04STim J. Robbins error("unknown option: -%c", optopt); 34109086b04STim J. Robbins break; 34209086b04STim J. Robbins } 34309086b04STim J. Robbins } 34409086b04STim J. Robbins argc -= optind; 34509086b04STim J. Robbins argv += optind; 34609086b04STim J. Robbins 34709086b04STim J. Robbins if (argc != 0) 34809086b04STim J. Robbins error("too many arguments"); 34909086b04STim J. Robbins 35009086b04STim J. Robbins if (!phys && getpwd()) { 3514b88c807SRodney W. Grimes out1str(curdir); 3524b88c807SRodney W. Grimes out1c('\n'); 35309086b04STim J. Robbins } else { 3541c645e0fSStefan Farfeleder if ((p = getpwd2()) == NULL) 35509086b04STim J. Robbins error(".: %s", strerror(errno)); 3561c645e0fSStefan Farfeleder out1str(p); 35709086b04STim J. Robbins out1c('\n'); 358178897f1STim J. Robbins } 35937f0a670STor Egge 36009086b04STim J. Robbins return 0; 36109086b04STim J. Robbins } 362baf3e7c1STim J. Robbins 3634b88c807SRodney W. Grimes /* 36455d2c5b5SStefan Farfeleder * Get the current directory and cache the result in curdir. 3654b88c807SRodney W. Grimes */ 36688328642SDavid E. O'Brien static char * 3675134c3f7SWarner Losh getpwd(void) 368ab0a2172SSteve Price { 36955d2c5b5SStefan Farfeleder char *p; 37037f0a670STor Egge 3714b88c807SRodney W. Grimes if (curdir) 37237f0a670STor Egge return curdir; 37355d2c5b5SStefan Farfeleder 3741c645e0fSStefan Farfeleder p = getpwd2(); 37555d2c5b5SStefan Farfeleder if (p != NULL) 37655d2c5b5SStefan Farfeleder curdir = savestr(p); 37755d2c5b5SStefan Farfeleder 37855d2c5b5SStefan Farfeleder return curdir; 37955d2c5b5SStefan Farfeleder } 38055d2c5b5SStefan Farfeleder 3811c645e0fSStefan Farfeleder #define MAXPWD 256 3821c645e0fSStefan Farfeleder 38355d2c5b5SStefan Farfeleder /* 38455d2c5b5SStefan Farfeleder * Return the current directory. 38555d2c5b5SStefan Farfeleder */ 38688328642SDavid E. O'Brien static char * 3871c645e0fSStefan Farfeleder getpwd2(void) 38855d2c5b5SStefan Farfeleder { 3891c645e0fSStefan Farfeleder char *pwd; 3901c645e0fSStefan Farfeleder int i; 39137f0a670STor Egge 3921c645e0fSStefan Farfeleder for (i = MAXPWD;; i *= 2) { 3931c645e0fSStefan Farfeleder pwd = stalloc(i); 3941c645e0fSStefan Farfeleder if (getcwd(pwd, i) != NULL) 3951c645e0fSStefan Farfeleder return pwd; 3961c645e0fSStefan Farfeleder stunalloc(pwd); 3971c645e0fSStefan Farfeleder if (errno != ERANGE) 3981c645e0fSStefan Farfeleder break; 3991c645e0fSStefan Farfeleder } 4001c645e0fSStefan Farfeleder 4018eac1f94SJilles Tjoelker return NULL; 4028eac1f94SJilles Tjoelker } 4038eac1f94SJilles Tjoelker 4048eac1f94SJilles Tjoelker /* 4058eac1f94SJilles Tjoelker * Initialize PWD in a new shell. 4068eac1f94SJilles Tjoelker * If the shell is interactive, we need to warn if this fails. 4078eac1f94SJilles Tjoelker */ 4088eac1f94SJilles Tjoelker void 4098eac1f94SJilles Tjoelker pwd_init(int warn) 4108eac1f94SJilles Tjoelker { 4118eac1f94SJilles Tjoelker char *pwd; 4128eac1f94SJilles Tjoelker struct stat stdot, stpwd; 4138eac1f94SJilles Tjoelker 4148eac1f94SJilles Tjoelker pwd = lookupvar("PWD"); 41537f0a670STor Egge if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 41637f0a670STor Egge stat(pwd, &stpwd) != -1 && 41737f0a670STor Egge stdot.st_dev == stpwd.st_dev && 41837f0a670STor Egge stdot.st_ino == stpwd.st_ino) { 4198eac1f94SJilles Tjoelker if (curdir) 4208eac1f94SJilles Tjoelker ckfree(curdir); 4218eac1f94SJilles Tjoelker curdir = savestr(pwd); 42237f0a670STor Egge } 4238eac1f94SJilles Tjoelker if (getpwd() == NULL && warn) 4248eac1f94SJilles Tjoelker out2fmt_flush("sh: cannot determine working directory\n"); 4258eac1f94SJilles Tjoelker setvar("PWD", curdir, VEXPORT); 42637f0a670STor Egge } 427