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
33aa9caaf6SPeter Wemm #include <sys/types.h>
34aa9caaf6SPeter Wemm #include <sys/stat.h>
35aa9caaf6SPeter Wemm #include <stdlib.h>
36ab0a2172SSteve Price #include <string.h>
37aa9caaf6SPeter Wemm #include <unistd.h>
38aa9caaf6SPeter Wemm #include <errno.h>
3993c0dc5eSTim J. Robbins #include <limits.h>
40aa9caaf6SPeter Wemm
414b88c807SRodney W. Grimes /*
424b88c807SRodney W. Grimes * The cd and pwd commands.
434b88c807SRodney W. Grimes */
444b88c807SRodney W. Grimes
454b88c807SRodney W. Grimes #include "shell.h"
464b88c807SRodney W. Grimes #include "var.h"
474b88c807SRodney W. Grimes #include "nodes.h" /* for jobs.h */
484b88c807SRodney W. Grimes #include "jobs.h"
494b88c807SRodney W. Grimes #include "options.h"
504b88c807SRodney W. Grimes #include "output.h"
514b88c807SRodney W. Grimes #include "memalloc.h"
524b88c807SRodney W. Grimes #include "error.h"
53ab0a2172SSteve Price #include "exec.h"
54aa9caaf6SPeter Wemm #include "redir.h"
554b88c807SRodney W. Grimes #include "mystring.h"
56aa9caaf6SPeter Wemm #include "show.h"
57ab0a2172SSteve Price #include "cd.h"
58454a02b3SJilles Tjoelker #include "builtins.h"
594b88c807SRodney W. Grimes
6088328642SDavid E. O'Brien static int cdlogical(char *);
6188328642SDavid E. O'Brien static int cdphysical(char *);
6288328642SDavid E. O'Brien static int docd(char *, int, int);
63d5119a2aSJilles Tjoelker static char *getcomponent(char **);
6488328642SDavid E. O'Brien static char *findcwd(char *);
6588328642SDavid E. O'Brien static void updatepwd(char *);
6688328642SDavid E. O'Brien static char *getpwd(void);
6788328642SDavid E. O'Brien static char *getpwd2(void);
684b88c807SRodney W. Grimes
69aa7b6f82SDavid E. O'Brien static char *curdir = NULL; /* current working directory */
704b88c807SRodney W. Grimes
714b88c807SRodney W. Grimes int
cdcmd(int argc __unused,char ** argv __unused)727cbda738SJilles Tjoelker cdcmd(int argc __unused, char **argv __unused)
73aa9caaf6SPeter Wemm {
74384aedabSJilles Tjoelker const char *dest;
752cac6e36SJilles Tjoelker const char *path;
764b88c807SRodney W. Grimes char *p;
774b88c807SRodney W. Grimes struct stat statb;
78d6ee26adSJilles Tjoelker int ch, phys, print = 0, getcwderr = 0;
79d6ee26adSJilles Tjoelker int rc;
80168b9dd1SJilles Tjoelker int errno1 = ENOENT;
814b88c807SRodney W. Grimes
827e1975c2STim J. Robbins phys = Pflag;
837cbda738SJilles Tjoelker while ((ch = nextopt("eLP")) != '\0') {
8409086b04STim J. Robbins switch (ch) {
85d6ee26adSJilles Tjoelker case 'e':
86d6ee26adSJilles Tjoelker getcwderr = 1;
87d6ee26adSJilles Tjoelker break;
8809086b04STim J. Robbins case 'L':
8909086b04STim J. Robbins phys = 0;
9009086b04STim J. Robbins break;
9109086b04STim J. Robbins case 'P':
9209086b04STim J. Robbins phys = 1;
9309086b04STim J. Robbins break;
9409086b04STim J. Robbins }
9509086b04STim J. Robbins }
9609086b04STim J. Robbins
977cbda738SJilles Tjoelker if (*argptr != NULL && argptr[1] != NULL)
9809086b04STim J. Robbins error("too many arguments");
9909086b04STim J. Robbins
1007cbda738SJilles Tjoelker if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
1014b88c807SRodney W. Grimes error("HOME not set");
10261233bdcSBruce Evans if (*dest == '\0')
10361233bdcSBruce Evans dest = ".";
1044b88c807SRodney W. Grimes if (dest[0] == '-' && dest[1] == '\0') {
1057b40c6dfSJilles Tjoelker dest = bltinlookup("OLDPWD", 1);
1067b40c6dfSJilles Tjoelker if (dest == NULL)
1077b40c6dfSJilles Tjoelker error("OLDPWD not set");
1084b88c807SRodney W. Grimes print = 1;
1094b88c807SRodney W. Grimes }
11064fa41f3SJilles Tjoelker if (dest[0] == '/' ||
11164fa41f3SJilles Tjoelker (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
11264fa41f3SJilles Tjoelker (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) ||
11364fa41f3SJilles Tjoelker (path = bltinlookup("CDPATH", 1)) == NULL)
114781bfb5aSJilles Tjoelker path = "";
1154600b569SJilles Tjoelker while ((p = padvance(&path, NULL, dest)) != NULL) {
116e94e3511SJilles Tjoelker if (stat(p, &statb) < 0) {
117e94e3511SJilles Tjoelker if (errno != ENOENT)
118e94e3511SJilles Tjoelker errno1 = errno;
119e94e3511SJilles Tjoelker } else if (!S_ISDIR(statb.st_mode))
120e94e3511SJilles Tjoelker errno1 = ENOTDIR;
121e94e3511SJilles Tjoelker else {
1224b88c807SRodney W. Grimes if (!print) {
1234b88c807SRodney W. Grimes /*
1244b88c807SRodney W. Grimes * XXX - rethink
1254b88c807SRodney W. Grimes */
12637f0a670STor Egge if (p[0] == '.' && p[1] == '/' && p[2] != '\0')
127ed5c24e2SStefan Farfeleder print = strcmp(p + 2, dest);
128ed5c24e2SStefan Farfeleder else
1294b88c807SRodney W. Grimes print = strcmp(p, dest);
1304b88c807SRodney W. Grimes }
131d6ee26adSJilles Tjoelker rc = docd(p, print, phys);
132d6ee26adSJilles Tjoelker if (rc >= 0)
133d6ee26adSJilles Tjoelker return getcwderr ? rc : 0;
134168b9dd1SJilles Tjoelker if (errno != ENOENT)
135168b9dd1SJilles Tjoelker errno1 = errno;
1364b88c807SRodney W. Grimes }
1374b88c807SRodney W. Grimes }
138168b9dd1SJilles Tjoelker error("%s: %s", dest, strerror(errno1));
139aa9caaf6SPeter Wemm /*NOTREACHED*/
140aa9caaf6SPeter Wemm return 0;
1414b88c807SRodney W. Grimes }
1424b88c807SRodney W. Grimes
1434b88c807SRodney W. Grimes
1444b88c807SRodney W. Grimes /*
14509086b04STim J. Robbins * Actually change the directory. In an interactive shell, print the
146958ba632SSteve Price * directory name if "print" is nonzero.
1474b88c807SRodney W. Grimes */
14888328642SDavid E. O'Brien static int
docd(char * dest,int print,int phys)14909086b04STim J. Robbins docd(char *dest, int print, int phys)
15009086b04STim J. Robbins {
151d6ee26adSJilles Tjoelker int rc;
15209086b04STim J. Robbins
15309086b04STim J. Robbins TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys));
15409086b04STim J. Robbins
15509086b04STim J. Robbins /* If logical cd fails, fall back to physical. */
156d6ee26adSJilles Tjoelker if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0)
15709086b04STim J. Robbins return (-1);
15809086b04STim J. Robbins
1596f49cd26SJilles Tjoelker if (print && iflag && curdir) {
16009086b04STim J. Robbins out1fmt("%s\n", curdir);
1616f49cd26SJilles Tjoelker /*
1626f49cd26SJilles Tjoelker * Ignore write errors to preserve the invariant that the
1636f49cd26SJilles Tjoelker * current directory is changed iff the exit status is 0
1646f49cd26SJilles Tjoelker * (or 1 if -e was given and the full pathname could not be
1656f49cd26SJilles Tjoelker * determined).
1666f49cd26SJilles Tjoelker */
1676f49cd26SJilles Tjoelker flushout(out1);
1686f49cd26SJilles Tjoelker outclearerror(out1);
1696f49cd26SJilles Tjoelker }
17009086b04STim J. Robbins
171d6ee26adSJilles Tjoelker return (rc);
17209086b04STim J. Robbins }
17309086b04STim J. Robbins
17488328642SDavid E. O'Brien static int
cdlogical(char * dest)17509086b04STim J. Robbins cdlogical(char *dest)
1764b88c807SRodney W. Grimes {
17737f0a670STor Egge char *p;
17837f0a670STor Egge char *q;
17937f0a670STor Egge char *component;
180d5119a2aSJilles Tjoelker char *path;
18137f0a670STor Egge struct stat statb;
18237f0a670STor Egge int first;
18337f0a670STor Egge int badstat;
184ab0a2172SSteve Price
18537f0a670STor Egge /*
18637f0a670STor Egge * Check each component of the path. If we find a symlink or
18737f0a670STor Egge * something we can't stat, clear curdir to force a getcwd()
18837f0a670STor Egge * next time we get the value of the current directory.
18937f0a670STor Egge */
19037f0a670STor Egge badstat = 0;
191d5119a2aSJilles Tjoelker path = stsavestr(dest);
19237f0a670STor Egge STARTSTACKSTR(p);
19337f0a670STor Egge if (*dest == '/') {
19437f0a670STor Egge STPUTC('/', p);
195d5119a2aSJilles Tjoelker path++;
19637f0a670STor Egge }
19737f0a670STor Egge first = 1;
198d5119a2aSJilles Tjoelker while ((q = getcomponent(&path)) != NULL) {
19937f0a670STor Egge if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
20037f0a670STor Egge continue;
20137f0a670STor Egge if (! first)
20237f0a670STor Egge STPUTC('/', p);
20337f0a670STor Egge first = 0;
20437f0a670STor Egge component = q;
2059d37e157SJilles Tjoelker STPUTS(q, p);
20637f0a670STor Egge if (equal(component, ".."))
20737f0a670STor Egge continue;
20837f0a670STor Egge STACKSTRNUL(p);
20909086b04STim J. Robbins if (lstat(stackblock(), &statb) < 0) {
21037f0a670STor Egge badstat = 1;
21137f0a670STor Egge break;
21237f0a670STor Egge }
21337f0a670STor Egge }
21437f0a670STor Egge
2154b88c807SRodney W. Grimes INTOFF;
21655d2c5b5SStefan Farfeleder if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
2174b88c807SRodney W. Grimes INTON;
21809086b04STim J. Robbins return (-1);
2194b88c807SRodney W. Grimes }
22055d2c5b5SStefan Farfeleder updatepwd(p);
2214b88c807SRodney W. Grimes INTON;
22209086b04STim J. Robbins return (0);
2234b88c807SRodney W. Grimes }
2244b88c807SRodney W. Grimes
22588328642SDavid E. O'Brien static int
cdphysical(char * dest)22609086b04STim J. Robbins cdphysical(char *dest)
22709086b04STim J. Robbins {
22855d2c5b5SStefan Farfeleder char *p;
229d6ee26adSJilles Tjoelker int rc = 0;
23009086b04STim J. Robbins
23109086b04STim J. Robbins INTOFF;
232ae7f4735SJilles Tjoelker if (chdir(dest) < 0) {
23309086b04STim J. Robbins INTON;
23409086b04STim J. Robbins return (-1);
23509086b04STim J. Robbins }
236ae7f4735SJilles Tjoelker p = findcwd(NULL);
237d6ee26adSJilles Tjoelker if (p == NULL) {
2385fe9123fSJilles Tjoelker warning("warning: failed to get name of current directory");
239d6ee26adSJilles Tjoelker rc = 1;
240d6ee26adSJilles Tjoelker }
24155d2c5b5SStefan Farfeleder updatepwd(p);
24209086b04STim J. Robbins INTON;
243d6ee26adSJilles Tjoelker return (rc);
24409086b04STim J. Robbins }
2454b88c807SRodney W. Grimes
2464b88c807SRodney W. Grimes /*
247d5119a2aSJilles Tjoelker * Get the next component of the path name pointed to by *path.
248d5119a2aSJilles Tjoelker * This routine overwrites *path and the string pointed to by it.
2494b88c807SRodney W. Grimes */
25088328642SDavid E. O'Brien static char *
getcomponent(char ** path)251d5119a2aSJilles Tjoelker getcomponent(char **path)
252958ba632SSteve Price {
253afb033d5SSteve Price char *p;
2544b88c807SRodney W. Grimes char *start;
2554b88c807SRodney W. Grimes
256d5119a2aSJilles Tjoelker if ((p = *path) == NULL)
2574b88c807SRodney W. Grimes return NULL;
258d5119a2aSJilles Tjoelker start = *path;
2594b88c807SRodney W. Grimes while (*p != '/' && *p != '\0')
2604b88c807SRodney W. Grimes p++;
2614b88c807SRodney W. Grimes if (*p == '\0') {
262d5119a2aSJilles Tjoelker *path = NULL;
2634b88c807SRodney W. Grimes } else {
2644b88c807SRodney W. Grimes *p++ = '\0';
265d5119a2aSJilles Tjoelker *path = p;
2664b88c807SRodney W. Grimes }
2674b88c807SRodney W. Grimes return start;
2684b88c807SRodney W. Grimes }
2694b88c807SRodney W. Grimes
2704b88c807SRodney W. Grimes
27188328642SDavid E. O'Brien static char *
findcwd(char * dir)27255d2c5b5SStefan Farfeleder findcwd(char *dir)
2734b88c807SRodney W. Grimes {
2744b88c807SRodney W. Grimes char *new;
2754b88c807SRodney W. Grimes char *p;
276d5119a2aSJilles Tjoelker char *path;
2774b88c807SRodney W. Grimes
27837f0a670STor Egge /*
27937f0a670STor Egge * If our argument is NULL, we don't know the current directory
28037f0a670STor Egge * any more because we traversed a symbolic link or something
28137f0a670STor Egge * we couldn't stat().
28237f0a670STor Egge */
2831c645e0fSStefan Farfeleder if (dir == NULL || curdir == NULL)
2841c645e0fSStefan Farfeleder return getpwd2();
285d5119a2aSJilles Tjoelker path = stsavestr(dir);
2864b88c807SRodney W. Grimes STARTSTACKSTR(new);
2874b88c807SRodney W. Grimes if (*dir != '/') {
2889d37e157SJilles Tjoelker STPUTS(curdir, new);
2899d37e157SJilles Tjoelker if (STTOPC(new) == '/')
2904b88c807SRodney W. Grimes STUNPUTC(new);
2914b88c807SRodney W. Grimes }
292d5119a2aSJilles Tjoelker while ((p = getcomponent(&path)) != NULL) {
2934b88c807SRodney W. Grimes if (equal(p, "..")) {
2944b88c807SRodney W. Grimes while (new > stackblock() && (STUNPUTC(new), *new) != '/');
2954b88c807SRodney W. Grimes } else if (*p != '\0' && ! equal(p, ".")) {
2964b88c807SRodney W. Grimes STPUTC('/', new);
2979d37e157SJilles Tjoelker STPUTS(p, new);
2984b88c807SRodney W. Grimes }
2994b88c807SRodney W. Grimes }
3004b88c807SRodney W. Grimes if (new == stackblock())
3014b88c807SRodney W. Grimes STPUTC('/', new);
3024b88c807SRodney W. Grimes STACKSTRNUL(new);
30355d2c5b5SStefan Farfeleder return stackblock();
30455d2c5b5SStefan Farfeleder }
30555d2c5b5SStefan Farfeleder
30655d2c5b5SStefan Farfeleder /*
30755d2c5b5SStefan Farfeleder * Update curdir (the name of the current directory) in response to a
30855d2c5b5SStefan Farfeleder * cd command. We also call hashcd to let the routines in exec.c know
30955d2c5b5SStefan Farfeleder * that the current directory has changed.
31055d2c5b5SStefan Farfeleder */
31188328642SDavid E. O'Brien static void
updatepwd(char * dir)31255d2c5b5SStefan Farfeleder updatepwd(char *dir)
31355d2c5b5SStefan Farfeleder {
3147b40c6dfSJilles Tjoelker char *prevdir;
3157b40c6dfSJilles Tjoelker
31655d2c5b5SStefan Farfeleder hashcd(); /* update command hash table */
31755d2c5b5SStefan Farfeleder
3187b40c6dfSJilles Tjoelker setvar("PWD", dir, VEXPORT);
3197b40c6dfSJilles Tjoelker setvar("OLDPWD", curdir, VEXPORT);
32037f0a670STor Egge prevdir = curdir;
321ae7f4735SJilles Tjoelker curdir = dir ? savestr(dir) : NULL;
3227b40c6dfSJilles Tjoelker ckfree(prevdir);
3234b88c807SRodney W. Grimes }
3244b88c807SRodney W. Grimes
3254b88c807SRodney W. Grimes int
pwdcmd(int argc __unused,char ** argv __unused)3267cbda738SJilles Tjoelker pwdcmd(int argc __unused, char **argv __unused)
327aa9caaf6SPeter Wemm {
3281c645e0fSStefan Farfeleder char *p;
32909086b04STim J. Robbins int ch, phys;
33009086b04STim J. Robbins
3317e1975c2STim J. Robbins phys = Pflag;
3327cbda738SJilles Tjoelker while ((ch = nextopt("LP")) != '\0') {
33309086b04STim J. Robbins switch (ch) {
33409086b04STim J. Robbins case 'L':
33509086b04STim J. Robbins phys = 0;
33609086b04STim J. Robbins break;
33709086b04STim J. Robbins case 'P':
33809086b04STim J. Robbins phys = 1;
33909086b04STim J. Robbins break;
34009086b04STim J. Robbins }
34109086b04STim J. Robbins }
34209086b04STim J. Robbins
3437cbda738SJilles Tjoelker if (*argptr != NULL)
34409086b04STim J. Robbins error("too many arguments");
34509086b04STim J. Robbins
34609086b04STim J. Robbins if (!phys && getpwd()) {
3474b88c807SRodney W. Grimes out1str(curdir);
3484b88c807SRodney W. Grimes out1c('\n');
34909086b04STim J. Robbins } else {
3501c645e0fSStefan Farfeleder if ((p = getpwd2()) == NULL)
35109086b04STim J. Robbins error(".: %s", strerror(errno));
3521c645e0fSStefan Farfeleder out1str(p);
35309086b04STim J. Robbins out1c('\n');
354178897f1STim J. Robbins }
35537f0a670STor Egge
35609086b04STim J. Robbins return 0;
35709086b04STim J. Robbins }
358baf3e7c1STim J. Robbins
3594b88c807SRodney W. Grimes /*
36055d2c5b5SStefan Farfeleder * Get the current directory and cache the result in curdir.
3614b88c807SRodney W. Grimes */
36288328642SDavid E. O'Brien static char *
getpwd(void)3635134c3f7SWarner Losh getpwd(void)
364ab0a2172SSteve Price {
36555d2c5b5SStefan Farfeleder char *p;
36637f0a670STor Egge
3674b88c807SRodney W. Grimes if (curdir)
36837f0a670STor Egge return curdir;
36955d2c5b5SStefan Farfeleder
3701c645e0fSStefan Farfeleder p = getpwd2();
371*d3eae2a6SJilles Tjoelker if (p != NULL) {
372*d3eae2a6SJilles Tjoelker INTOFF;
37355d2c5b5SStefan Farfeleder curdir = savestr(p);
374*d3eae2a6SJilles Tjoelker INTON;
375*d3eae2a6SJilles Tjoelker }
37655d2c5b5SStefan Farfeleder
37755d2c5b5SStefan Farfeleder return curdir;
37855d2c5b5SStefan Farfeleder }
37955d2c5b5SStefan Farfeleder
3801c645e0fSStefan Farfeleder #define MAXPWD 256
3811c645e0fSStefan Farfeleder
38255d2c5b5SStefan Farfeleder /*
38355d2c5b5SStefan Farfeleder * Return the current directory.
38455d2c5b5SStefan Farfeleder */
38588328642SDavid E. O'Brien static char *
getpwd2(void)3861c645e0fSStefan Farfeleder getpwd2(void)
38755d2c5b5SStefan Farfeleder {
3881c645e0fSStefan Farfeleder char *pwd;
3891c645e0fSStefan Farfeleder int i;
39037f0a670STor Egge
3911c645e0fSStefan Farfeleder for (i = MAXPWD;; i *= 2) {
3921c645e0fSStefan Farfeleder pwd = stalloc(i);
3931c645e0fSStefan Farfeleder if (getcwd(pwd, i) != NULL)
3941c645e0fSStefan Farfeleder return pwd;
3951c645e0fSStefan Farfeleder stunalloc(pwd);
3961c645e0fSStefan Farfeleder if (errno != ERANGE)
3971c645e0fSStefan Farfeleder break;
3981c645e0fSStefan Farfeleder }
3991c645e0fSStefan Farfeleder
4008eac1f94SJilles Tjoelker return NULL;
4018eac1f94SJilles Tjoelker }
4028eac1f94SJilles Tjoelker
4038eac1f94SJilles Tjoelker /*
4048eac1f94SJilles Tjoelker * Initialize PWD in a new shell.
4058eac1f94SJilles Tjoelker * If the shell is interactive, we need to warn if this fails.
4068eac1f94SJilles Tjoelker */
4078eac1f94SJilles Tjoelker void
pwd_init(int warn)4088eac1f94SJilles Tjoelker pwd_init(int warn)
4098eac1f94SJilles Tjoelker {
4108eac1f94SJilles Tjoelker char *pwd;
4118eac1f94SJilles Tjoelker struct stat stdot, stpwd;
4128eac1f94SJilles Tjoelker
4138eac1f94SJilles Tjoelker pwd = lookupvar("PWD");
41437f0a670STor Egge if (pwd && *pwd == '/' && stat(".", &stdot) != -1 &&
41537f0a670STor Egge stat(pwd, &stpwd) != -1 &&
41637f0a670STor Egge stdot.st_dev == stpwd.st_dev &&
41737f0a670STor Egge stdot.st_ino == stpwd.st_ino) {
4188eac1f94SJilles Tjoelker if (curdir)
4198eac1f94SJilles Tjoelker ckfree(curdir);
4208eac1f94SJilles Tjoelker curdir = savestr(pwd);
42137f0a670STor Egge }
4228eac1f94SJilles Tjoelker if (getpwd() == NULL && warn)
4238eac1f94SJilles Tjoelker out2fmt_flush("sh: cannot determine working directory\n");
4248eac1f94SJilles Tjoelker setvar("PWD", curdir, VEXPORT);
42537f0a670STor Egge }
426