xref: /freebsd/bin/sh/cd.c (revision aa9caaf65748ace0f3cd08c2deaf3ef3048d6e4d)
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  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
3589730b29SDavid Greenman  *
36aa9caaf6SPeter Wemm  *	$Id: cd.c,v 1.5 1995/11/14 01:04:52 peter Exp $
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
40aa9caaf6SPeter Wemm static char sccsid[] = "@(#)cd.c	8.2 (Berkeley) 5/4/95";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
43aa9caaf6SPeter Wemm #include <sys/types.h>
44aa9caaf6SPeter Wemm #include <sys/stat.h>
45aa9caaf6SPeter Wemm #include <stdlib.h>
46aa9caaf6SPeter Wemm #include <unistd.h>
47aa9caaf6SPeter Wemm #include <errno.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"
61aa9caaf6SPeter Wemm #include "redir.h"
624b88c807SRodney W. Grimes #include "mystring.h"
63aa9caaf6SPeter Wemm #include "show.h"
644b88c807SRodney W. Grimes 
65aa9caaf6SPeter Wemm STATIC int docd __P((char *, int));
66aa9caaf6SPeter Wemm STATIC char *getcomponent __P((void));
67aa9caaf6SPeter Wemm STATIC void updatepwd __P((char *));
68aa9caaf6SPeter Wemm STATIC void getpwd __P((void));
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes char *curdir;			/* current working directory */
714b88c807SRodney W. Grimes char *prevdir;			/* previous working directory */
724b88c807SRodney W. Grimes STATIC char *cdcomppath;
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes int
75aa9caaf6SPeter Wemm cdcmd(argc, argv)
76aa9caaf6SPeter Wemm 	int argc;
77aa9caaf6SPeter Wemm 	char **argv;
78aa9caaf6SPeter Wemm {
794b88c807SRodney W. Grimes 	char *dest;
804b88c807SRodney W. Grimes 	char *path;
814b88c807SRodney W. Grimes 	char *p;
824b88c807SRodney W. Grimes 	struct stat statb;
834b88c807SRodney W. Grimes 	char *padvance();
844b88c807SRodney W. Grimes 	int print = 0;
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes 	nextopt(nullstr);
874b88c807SRodney W. Grimes 	if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
884b88c807SRodney W. Grimes 		error("HOME not set");
8961233bdcSBruce Evans 	if (*dest == '\0')
9061233bdcSBruce Evans 		dest = ".";
914b88c807SRodney W. Grimes 	if (dest[0] == '-' && dest[1] == '\0') {
924b88c807SRodney W. Grimes 		dest = prevdir ? prevdir : curdir;
938d7d5ceaSPeter Wemm 		if (dest)
944b88c807SRodney W. Grimes 			print = 1;
958d7d5ceaSPeter Wemm 		else
968d7d5ceaSPeter Wemm 			dest = ".";
974b88c807SRodney W. Grimes 	}
984b88c807SRodney W. Grimes 	if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
994b88c807SRodney W. Grimes 		path = nullstr;
1004b88c807SRodney W. Grimes 	while ((p = padvance(&path, dest)) != NULL) {
101aa9caaf6SPeter Wemm 		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
1024b88c807SRodney W. Grimes 			if (!print) {
1034b88c807SRodney W. Grimes 				/*
1044b88c807SRodney W. Grimes 				 * XXX - rethink
1054b88c807SRodney W. Grimes 				 */
1064b88c807SRodney W. Grimes 				if (p[0] == '.' && p[1] == '/')
1074b88c807SRodney W. Grimes 					p += 2;
1084b88c807SRodney W. Grimes 				print = strcmp(p, dest);
1094b88c807SRodney W. Grimes 			}
1104b88c807SRodney W. Grimes 			if (docd(p, print) >= 0)
1114b88c807SRodney W. Grimes 				return 0;
1124b88c807SRodney W. Grimes 
1134b88c807SRodney W. Grimes 		}
1144b88c807SRodney W. Grimes 	}
1154b88c807SRodney W. Grimes 	error("can't cd to %s", dest);
116aa9caaf6SPeter Wemm 	/*NOTREACHED*/
117aa9caaf6SPeter Wemm 	return 0;
1184b88c807SRodney W. Grimes }
1194b88c807SRodney W. Grimes 
1204b88c807SRodney W. Grimes 
1214b88c807SRodney W. Grimes /*
1224b88c807SRodney W. Grimes  * Actually do the chdir.  If the name refers to symbolic links, we
1234b88c807SRodney W. Grimes  * compute the actual directory name before doing the cd.  In an
1244b88c807SRodney W. Grimes  * interactive shell, print the directory name if "print" is nonzero
1254b88c807SRodney W. Grimes  * or if the name refers to a symbolic link.  We also print the name
1264b88c807SRodney W. Grimes  * if "/u/logname" was expanded in it, since this is similar to a
1274b88c807SRodney W. Grimes  * symbolic link.  (The check for this breaks if the user gives the
1284b88c807SRodney W. Grimes  * cd command some additional, unused arguments.)
1294b88c807SRodney W. Grimes  */
1304b88c807SRodney W. Grimes 
1314b88c807SRodney W. Grimes #if SYMLINKS == 0
1324b88c807SRodney W. Grimes STATIC int
1334b88c807SRodney W. Grimes docd(dest, print)
1344b88c807SRodney W. Grimes 	char *dest;
135aa9caaf6SPeter Wemm 	int print;
1364b88c807SRodney W. Grimes 	{
1374b88c807SRodney W. Grimes 	INTOFF;
1384b88c807SRodney W. Grimes 	if (chdir(dest) < 0) {
1394b88c807SRodney W. Grimes 		INTON;
1404b88c807SRodney W. Grimes 		return -1;
1414b88c807SRodney W. Grimes 	}
1424b88c807SRodney W. Grimes 	updatepwd(dest);
1434b88c807SRodney W. Grimes 	INTON;
1444b88c807SRodney W. Grimes 	if (print && iflag)
1454b88c807SRodney W. Grimes 		out1fmt("%s\n", stackblock());
1464b88c807SRodney W. Grimes 	return 0;
1474b88c807SRodney W. Grimes }
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes #else
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes 
1524b88c807SRodney W. Grimes 
1534b88c807SRodney W. Grimes STATIC int
1544b88c807SRodney W. Grimes docd(dest, print)
1554b88c807SRodney W. Grimes 	char *dest;
156aa9caaf6SPeter Wemm 	int print;
1574b88c807SRodney W. Grimes {
1584b88c807SRodney W. Grimes 	register char *p;
1594b88c807SRodney W. Grimes 	register char *q;
1604b88c807SRodney W. Grimes 	char *symlink;
1614b88c807SRodney W. Grimes 	char *component;
1624b88c807SRodney W. Grimes 	struct stat statb;
1634b88c807SRodney W. Grimes 	int first;
1644b88c807SRodney W. Grimes 	int i;
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes 	TRACE(("docd(\"%s\", %d) called\n", dest, print));
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes top:
1694b88c807SRodney W. Grimes 	cdcomppath = dest;
1704b88c807SRodney W. Grimes 	STARTSTACKSTR(p);
1714b88c807SRodney W. Grimes 	if (*dest == '/') {
1724b88c807SRodney W. Grimes 		STPUTC('/', p);
1734b88c807SRodney W. Grimes 		cdcomppath++;
1744b88c807SRodney W. Grimes 	}
1754b88c807SRodney W. Grimes 	first = 1;
1764b88c807SRodney W. Grimes 	while ((q = getcomponent()) != NULL) {
177aa9caaf6SPeter Wemm 		if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0'))
1784b88c807SRodney W. Grimes 			continue;
1794b88c807SRodney W. Grimes 		if (! first)
1804b88c807SRodney W. Grimes 			STPUTC('/', p);
1814b88c807SRodney W. Grimes 		first = 0;
1824b88c807SRodney W. Grimes 		component = q;
1834b88c807SRodney W. Grimes 		while (*q)
1844b88c807SRodney W. Grimes 			STPUTC(*q++, p);
1854b88c807SRodney W. Grimes 		if (equal(component, ".."))
1864b88c807SRodney W. Grimes 			continue;
1874b88c807SRodney W. Grimes 		STACKSTRNUL(p);
1884b88c807SRodney W. Grimes 		if (lstat(stackblock(), &statb) < 0)
1894b88c807SRodney W. Grimes 			error("lstat %s failed", stackblock());
190aa9caaf6SPeter Wemm 		if (!S_ISLNK(statb.st_mode))
1914b88c807SRodney W. Grimes 			continue;
1924b88c807SRodney W. Grimes 
1934b88c807SRodney W. Grimes 		/* Hit a symbolic link.  We have to start all over again. */
1944b88c807SRodney W. Grimes 		print = 1;
1954b88c807SRodney W. Grimes 		STPUTC('\0', p);
1964b88c807SRodney W. Grimes 		symlink = grabstackstr(p);
1974b88c807SRodney W. Grimes 		i = (int)statb.st_size + 2;		/* 2 for '/' and '\0' */
1984b88c807SRodney W. Grimes 		if (cdcomppath != NULL)
1994b88c807SRodney W. Grimes 			i += strlen(cdcomppath);
2004b88c807SRodney W. Grimes 		p = stalloc(i);
2014b88c807SRodney W. Grimes 		if (readlink(symlink, p, (int)statb.st_size) < 0) {
2024b88c807SRodney W. Grimes 			error("readlink %s failed", stackblock());
2034b88c807SRodney W. Grimes 		}
2044b88c807SRodney W. Grimes 		if (cdcomppath != NULL) {
2054b88c807SRodney W. Grimes 			p[(int)statb.st_size] = '/';
2064b88c807SRodney W. Grimes 			scopy(cdcomppath, p + (int)statb.st_size + 1);
2074b88c807SRodney W. Grimes 		} else {
2084b88c807SRodney W. Grimes 			p[(int)statb.st_size] = '\0';
2094b88c807SRodney W. Grimes 		}
2104b88c807SRodney W. Grimes 		if (p[0] != '/') {	/* relative path name */
2114b88c807SRodney W. Grimes 			char *r;
2124b88c807SRodney W. Grimes 			q = r = symlink;
2134b88c807SRodney W. Grimes 			while (*q) {
2144b88c807SRodney W. Grimes 				if (*q++ == '/')
2154b88c807SRodney W. Grimes 					r = q;
2164b88c807SRodney W. Grimes 			}
2174b88c807SRodney W. Grimes 			*r = '\0';
2184b88c807SRodney W. Grimes 			dest = stalloc(strlen(symlink) + strlen(p) + 1);
2194b88c807SRodney W. Grimes 			scopy(symlink, dest);
2204b88c807SRodney W. Grimes 			strcat(dest, p);
2214b88c807SRodney W. Grimes 		} else {
2224b88c807SRodney W. Grimes 			dest = p;
2234b88c807SRodney W. Grimes 		}
2244b88c807SRodney W. Grimes 		goto top;
2254b88c807SRodney W. Grimes 	}
2264b88c807SRodney W. Grimes 	STPUTC('\0', p);
2274b88c807SRodney W. Grimes 	p = grabstackstr(p);
2284b88c807SRodney W. Grimes 	INTOFF;
22961233bdcSBruce Evans 	if (chdir(*p ? p : ".") < 0) {
2304b88c807SRodney W. Grimes 		INTON;
2314b88c807SRodney W. Grimes 		return -1;
2324b88c807SRodney W. Grimes 	}
2334b88c807SRodney W. Grimes 	updatepwd(p);
2344b88c807SRodney W. Grimes 	INTON;
2354b88c807SRodney W. Grimes 	if (print && iflag)
2364b88c807SRodney W. Grimes 		out1fmt("%s\n", p);
2374b88c807SRodney W. Grimes 	return 0;
2384b88c807SRodney W. Grimes }
2394b88c807SRodney W. Grimes #endif /* SYMLINKS */
2404b88c807SRodney W. Grimes 
2414b88c807SRodney W. Grimes 
2424b88c807SRodney W. Grimes 
2434b88c807SRodney W. Grimes /*
2444b88c807SRodney W. Grimes  * Get the next component of the path name pointed to by cdcomppath.
2454b88c807SRodney W. Grimes  * This routine overwrites the string pointed to by cdcomppath.
2464b88c807SRodney W. Grimes  */
2474b88c807SRodney W. Grimes 
2484b88c807SRodney W. Grimes STATIC char *
2494b88c807SRodney W. Grimes getcomponent() {
2504b88c807SRodney W. Grimes 	register char *p;
2514b88c807SRodney W. Grimes 	char *start;
2524b88c807SRodney W. Grimes 
2534b88c807SRodney W. Grimes 	if ((p = cdcomppath) == NULL)
2544b88c807SRodney W. Grimes 		return NULL;
2554b88c807SRodney W. Grimes 	start = cdcomppath;
2564b88c807SRodney W. Grimes 	while (*p != '/' && *p != '\0')
2574b88c807SRodney W. Grimes 		p++;
2584b88c807SRodney W. Grimes 	if (*p == '\0') {
2594b88c807SRodney W. Grimes 		cdcomppath = NULL;
2604b88c807SRodney W. Grimes 	} else {
2614b88c807SRodney W. Grimes 		*p++ = '\0';
2624b88c807SRodney W. Grimes 		cdcomppath = p;
2634b88c807SRodney W. Grimes 	}
2644b88c807SRodney W. Grimes 	return start;
2654b88c807SRodney W. Grimes }
2664b88c807SRodney W. Grimes 
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes 
2694b88c807SRodney W. Grimes /*
2704b88c807SRodney W. Grimes  * Update curdir (the name of the current directory) in response to a
2714b88c807SRodney W. Grimes  * cd command.  We also call hashcd to let the routines in exec.c know
2724b88c807SRodney W. Grimes  * that the current directory has changed.
2734b88c807SRodney W. Grimes  */
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes void hashcd();
2764b88c807SRodney W. Grimes 
2774b88c807SRodney W. Grimes STATIC void
2784b88c807SRodney W. Grimes updatepwd(dir)
2794b88c807SRodney W. Grimes 	char *dir;
2804b88c807SRodney W. Grimes 	{
2814b88c807SRodney W. Grimes 	char *new;
2824b88c807SRodney W. Grimes 	char *p;
2834b88c807SRodney W. Grimes 
2844b88c807SRodney W. Grimes 	hashcd();				/* update command hash table */
2854b88c807SRodney W. Grimes 	cdcomppath = stalloc(strlen(dir) + 1);
2864b88c807SRodney W. Grimes 	scopy(dir, cdcomppath);
2874b88c807SRodney W. Grimes 	STARTSTACKSTR(new);
2884b88c807SRodney W. Grimes 	if (*dir != '/') {
2894b88c807SRodney W. Grimes 		if (curdir == NULL)
2904b88c807SRodney W. Grimes 			return;
2914b88c807SRodney W. Grimes 		p = curdir;
2924b88c807SRodney W. Grimes 		while (*p)
2934b88c807SRodney W. Grimes 			STPUTC(*p++, new);
2944b88c807SRodney W. Grimes 		if (p[-1] == '/')
2954b88c807SRodney W. Grimes 			STUNPUTC(new);
2964b88c807SRodney W. Grimes 	}
2974b88c807SRodney W. Grimes 	while ((p = getcomponent()) != NULL) {
2984b88c807SRodney W. Grimes 		if (equal(p, "..")) {
2994b88c807SRodney W. Grimes 			while (new > stackblock() && (STUNPUTC(new), *new) != '/');
3004b88c807SRodney W. Grimes 		} else if (*p != '\0' && ! equal(p, ".")) {
3014b88c807SRodney W. Grimes 			STPUTC('/', new);
3024b88c807SRodney W. Grimes 			while (*p)
3034b88c807SRodney W. Grimes 				STPUTC(*p++, new);
3044b88c807SRodney W. Grimes 		}
3054b88c807SRodney W. Grimes 	}
3064b88c807SRodney W. Grimes 	if (new == stackblock())
3074b88c807SRodney W. Grimes 		STPUTC('/', new);
3084b88c807SRodney W. Grimes 	STACKSTRNUL(new);
3094b88c807SRodney W. Grimes 	INTOFF;
3104b88c807SRodney W. Grimes 	if (prevdir)
3114b88c807SRodney W. Grimes 		ckfree(prevdir);
3124b88c807SRodney W. Grimes 	prevdir = curdir;
3134b88c807SRodney W. Grimes 	curdir = savestr(stackblock());
3144b88c807SRodney W. Grimes 	INTON;
3154b88c807SRodney W. Grimes }
3164b88c807SRodney W. Grimes 
3174b88c807SRodney W. Grimes 
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes int
320aa9caaf6SPeter Wemm pwdcmd(argc, argv)
321aa9caaf6SPeter Wemm 	int argc;
322aa9caaf6SPeter Wemm 	char **argv;
323aa9caaf6SPeter Wemm {
3244b88c807SRodney W. Grimes 	getpwd();
3254b88c807SRodney W. Grimes 	out1str(curdir);
3264b88c807SRodney W. Grimes 	out1c('\n');
3274b88c807SRodney W. Grimes 	return 0;
3284b88c807SRodney W. Grimes }
3294b88c807SRodney W. Grimes 
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes /*
3334b88c807SRodney W. Grimes  * Run /bin/pwd to find out what the current directory is.  We suppress
3344b88c807SRodney W. Grimes  * interrupts throughout most of this, but the user can still break out
3354b88c807SRodney W. Grimes  * of it by killing the pwd program.  If we already know the current
3364b88c807SRodney W. Grimes  * directory, this routine returns immediately.
3374b88c807SRodney W. Grimes  */
3384b88c807SRodney W. Grimes 
3394b88c807SRodney W. Grimes #define MAXPWD 256
3404b88c807SRodney W. Grimes 
3414b88c807SRodney W. Grimes STATIC void
3424b88c807SRodney W. Grimes getpwd() {
3434b88c807SRodney W. Grimes 	char buf[MAXPWD];
3444b88c807SRodney W. Grimes 	char *p;
3454b88c807SRodney W. Grimes 	int i;
3464b88c807SRodney W. Grimes 	int status;
3474b88c807SRodney W. Grimes 	struct job *jp;
3484b88c807SRodney W. Grimes 	int pip[2];
349beeac5cdSJordan K. Hubbard 	char *pwd_bin = "/bin/pwd";
3504b88c807SRodney W. Grimes 
3514b88c807SRodney W. Grimes 	if (curdir)
3524b88c807SRodney W. Grimes 		return;
3534b88c807SRodney W. Grimes 	INTOFF;
3544b88c807SRodney W. Grimes 	if (pipe(pip) < 0)
3554b88c807SRodney W. Grimes 		error("Pipe call failed");
356beeac5cdSJordan K. Hubbard 	/* make a fall-back guess, otherwise we're simply screwed */
357beeac5cdSJordan K. Hubbard 	if (access(pwd_bin, X_OK) == -1)
358beeac5cdSJordan K. Hubbard 		pwd_bin = "/stand/pwd";
3594b88c807SRodney W. Grimes 	jp = makejob((union node *)NULL, 1);
3604b88c807SRodney W. Grimes 	if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) {
3614b88c807SRodney W. Grimes 		close(pip[0]);
3624b88c807SRodney W. Grimes 		if (pip[1] != 1) {
3634b88c807SRodney W. Grimes 			close(1);
3644b88c807SRodney W. Grimes 			copyfd(pip[1], 1);
3654b88c807SRodney W. Grimes 			close(pip[1]);
3664b88c807SRodney W. Grimes 		}
367beeac5cdSJordan K. Hubbard 		execl(pwd_bin, "pwd", (char *)0);
368beeac5cdSJordan K. Hubbard 		error("Cannot exec %s", pwd_bin);
3694b88c807SRodney W. Grimes 	}
3704b88c807SRodney W. Grimes 	close(pip[1]);
3714b88c807SRodney W. Grimes 	pip[1] = -1;
3724b88c807SRodney W. Grimes 	p = buf;
3734b88c807SRodney W. Grimes 	while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0
374aa9caaf6SPeter Wemm 	     || (i == -1 && errno == EINTR)) {
3754b88c807SRodney W. Grimes 		if (i > 0)
3764b88c807SRodney W. Grimes 			p += i;
3774b88c807SRodney W. Grimes 	}
3784b88c807SRodney W. Grimes 	close(pip[0]);
3794b88c807SRodney W. Grimes 	pip[0] = -1;
3804b88c807SRodney W. Grimes 	status = waitforjob(jp);
3814b88c807SRodney W. Grimes 	if (status != 0)
3824b88c807SRodney W. Grimes 		error((char *)0);
3834b88c807SRodney W. Grimes 	if (i < 0 || p == buf || p[-1] != '\n')
3844b88c807SRodney W. Grimes 		error("pwd command failed");
3854b88c807SRodney W. Grimes 	p[-1] = '\0';
3864b88c807SRodney W. Grimes 	curdir = savestr(buf);
3874b88c807SRodney W. Grimes 	INTON;
3884b88c807SRodney W. Grimes }
389