xref: /illumos-gate/usr/src/cmd/csh/sh.dir.c (revision 258f91c6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
87c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate /*
117c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
127c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley Software License Agreement
137c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include "sh.h"
177c478bd9Sstevel@tonic-gate #include "sh.dir.h"
187c478bd9Sstevel@tonic-gate #include "sh.tconst.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * C Shell - directory management
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate 
246c02b4a4Smuffin struct directory	*dfind(tchar *);
256c02b4a4Smuffin tchar	*dfollow(tchar *);
266c02b4a4Smuffin tchar	*dcanon(tchar *, tchar *);
276c02b4a4Smuffin void	dtildepr(tchar *, tchar *);
286c02b4a4Smuffin void	dfree(struct directory *);
296c02b4a4Smuffin void	dnewcwd(struct directory *);
306c02b4a4Smuffin 
31*258f91c6SToomas Soome int	didchdir;
32*258f91c6SToomas Soome bool	loginsh;
33*258f91c6SToomas Soome bool	havhash2;
34*258f91c6SToomas Soome struct varent shvhed;
35*258f91c6SToomas Soome struct directory *dcwd;
367c478bd9Sstevel@tonic-gate struct	directory dhead;		/* "head" of loop */
377c478bd9Sstevel@tonic-gate int	printd;				/* force name to be printed */
387c478bd9Sstevel@tonic-gate static tchar *fakev[] = { S_dirs, NOSTR };
39*258f91c6SToomas Soome char	xhash2[HSHSIZ / 8];
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * dinit - initialize current working directory
437c478bd9Sstevel@tonic-gate  */
446c02b4a4Smuffin void
dinit(tchar * hp)456c02b4a4Smuffin dinit(tchar *hp)
467c478bd9Sstevel@tonic-gate {
476c02b4a4Smuffin 	tchar *cp;
486c02b4a4Smuffin 	struct directory *dp;
497c478bd9Sstevel@tonic-gate 	tchar path[MAXPATHLEN];
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef TRACE
527c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dinit()\n");
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 	/*
557c478bd9Sstevel@tonic-gate 	 * If this is a login shell, we should have a home directory.  But,
567c478bd9Sstevel@tonic-gate 	 * if we got here via 'su - <user>' where the user has no directory
5748bbca81SDaniel Hoffman 	 * in their passwd file, then su has passed HOME=<nothing>, so hp is
587c478bd9Sstevel@tonic-gate 	 * non-null, but has zero length.  Thus, we do not know the current
597c478bd9Sstevel@tonic-gate 	 * working directory based on the home directory.
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 	if (loginsh && hp && *hp)
627c478bd9Sstevel@tonic-gate 		cp = hp;
637c478bd9Sstevel@tonic-gate 	else {
647c478bd9Sstevel@tonic-gate 		cp = getwd_(path);
657c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
6665b0c20eSnakanon 			printf("Warning: cannot determine current directory\n");
677c478bd9Sstevel@tonic-gate 			cp = S_DOT;
687c478bd9Sstevel@tonic-gate 		}
697c478bd9Sstevel@tonic-gate 	}
7065b0c20eSnakanon 	dp = (struct directory *)xcalloc(sizeof (struct directory), 1);
717c478bd9Sstevel@tonic-gate 	dp->di_name = savestr(cp);
727c478bd9Sstevel@tonic-gate 	dp->di_count = 0;
737c478bd9Sstevel@tonic-gate 	dhead.di_next = dhead.di_prev = dp;
747c478bd9Sstevel@tonic-gate 	dp->di_next = dp->di_prev = &dhead;
757c478bd9Sstevel@tonic-gate 	printd = 0;
767c478bd9Sstevel@tonic-gate 	dnewcwd(dp);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * dodirs - list all directories in directory loop
817c478bd9Sstevel@tonic-gate  */
826c02b4a4Smuffin void
dodirs(tchar ** v)836c02b4a4Smuffin dodirs(tchar **v)
847c478bd9Sstevel@tonic-gate {
856c02b4a4Smuffin 	struct directory *dp;
867c478bd9Sstevel@tonic-gate 	bool lflag;
877c478bd9Sstevel@tonic-gate 	tchar *hp = value(S_home);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #ifdef TRACE
907c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dodirs()\n");
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 	if (*hp == '\0')
937c478bd9Sstevel@tonic-gate 		hp = NOSTR;
947c478bd9Sstevel@tonic-gate 	if (*++v != NOSTR)
957c478bd9Sstevel@tonic-gate 		if (eq(*v, S_MINl /* "-l" */) && *++v == NOSTR)
967c478bd9Sstevel@tonic-gate 			lflag = 1;
977c478bd9Sstevel@tonic-gate 		else
987c478bd9Sstevel@tonic-gate 			error("Usage: dirs [ -l ]");
997c478bd9Sstevel@tonic-gate 	else
1007c478bd9Sstevel@tonic-gate 		lflag = 0;
1017c478bd9Sstevel@tonic-gate 	dp = dcwd;
1027c478bd9Sstevel@tonic-gate 	do {
1037c478bd9Sstevel@tonic-gate 		if (dp == &dhead)
1047c478bd9Sstevel@tonic-gate 			continue;
1057c478bd9Sstevel@tonic-gate 		if (!lflag && hp != NOSTR) {
1067c478bd9Sstevel@tonic-gate 			dtildepr(hp, dp->di_name);
1077c478bd9Sstevel@tonic-gate 		} else
1087c478bd9Sstevel@tonic-gate 			printf("%t", dp->di_name);
1097c478bd9Sstevel@tonic-gate 		printf(" ");
1107c478bd9Sstevel@tonic-gate 	} while ((dp = dp->di_prev) != dcwd);
1117c478bd9Sstevel@tonic-gate 	printf("\n");
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1146c02b4a4Smuffin void
dtildepr(tchar * home,tchar * dir)1156c02b4a4Smuffin dtildepr(tchar *home, tchar *dir)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef TRACE
1197c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dtildepr()\n");
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 	if (!eq(home, S_SLASH /* "/" */) && prefix(home, dir))
1227c478bd9Sstevel@tonic-gate 		printf("~%t", dir + strlen_(home));
1237c478bd9Sstevel@tonic-gate 	else
1247c478bd9Sstevel@tonic-gate 		printf("%t", dir);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * dochngd - implement chdir command.
1297c478bd9Sstevel@tonic-gate  */
1306c02b4a4Smuffin void
dochngd(tchar ** v)1316c02b4a4Smuffin dochngd(tchar **v)
1327c478bd9Sstevel@tonic-gate {
1336c02b4a4Smuffin 	tchar *cp;
1346c02b4a4Smuffin 	struct directory *dp;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #ifdef TRACE
1377c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dochngd()\n");
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate 	printd = 0;
1407c478bd9Sstevel@tonic-gate 	if (*++v == NOSTR) {
1417c478bd9Sstevel@tonic-gate 		if ((cp = value(S_home)) == NOSTR || *cp == 0)
1427c478bd9Sstevel@tonic-gate 			bferr("No home directory");
1437c478bd9Sstevel@tonic-gate 		if (chdir_(cp) < 0)
1447c478bd9Sstevel@tonic-gate 			bferr("Can't change to home directory");
1457c478bd9Sstevel@tonic-gate 		cp = savestr(cp);
1467c478bd9Sstevel@tonic-gate 	} else if ((dp = dfind(*v)) != 0) {
1477c478bd9Sstevel@tonic-gate 		printd = 1;
1487c478bd9Sstevel@tonic-gate 		if (chdir_(dp->di_name) < 0)
1497c478bd9Sstevel@tonic-gate 			Perror(dp->di_name);
1507c478bd9Sstevel@tonic-gate 		dcwd->di_prev->di_next = dcwd->di_next;
1517c478bd9Sstevel@tonic-gate 		dcwd->di_next->di_prev = dcwd->di_prev;
1527c478bd9Sstevel@tonic-gate 		goto flushcwd;
1537c478bd9Sstevel@tonic-gate 	} else
1547c478bd9Sstevel@tonic-gate 		cp = dfollow(*v);
15565b0c20eSnakanon 	dp = (struct directory *)xcalloc(sizeof (struct directory), 1);
1567c478bd9Sstevel@tonic-gate 	dp->di_name = cp;
1577c478bd9Sstevel@tonic-gate 	dp->di_count = 0;
1587c478bd9Sstevel@tonic-gate 	dp->di_next = dcwd->di_next;
1597c478bd9Sstevel@tonic-gate 	dp->di_prev = dcwd->di_prev;
1607c478bd9Sstevel@tonic-gate 	dp->di_prev->di_next = dp;
1617c478bd9Sstevel@tonic-gate 	dp->di_next->di_prev = dp;
1627c478bd9Sstevel@tonic-gate flushcwd:
1637c478bd9Sstevel@tonic-gate 	dfree(dcwd);
1647c478bd9Sstevel@tonic-gate 	dnewcwd(dp);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * dfollow - change to arg directory; fall back on cdpath if not valid
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate tchar *
dfollow(tchar * cp)1716c02b4a4Smuffin dfollow(tchar *cp)
1727c478bd9Sstevel@tonic-gate {
1736c02b4a4Smuffin 	tchar *dp;
1747c478bd9Sstevel@tonic-gate 	struct varent *c;
1757c478bd9Sstevel@tonic-gate 	int cdhashval, cdhashval1;
1767c478bd9Sstevel@tonic-gate 	int index;
17765b0c20eSnakanon 	int slash; /* slashes in the argument */
1787c478bd9Sstevel@tonic-gate 	tchar *fullpath;
17965b0c20eSnakanon 	tchar *slashcp; /* cp string prepended with a slash */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #ifdef TRACE
1827c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dfollow()\n");
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate 	cp = globone(cp);
1857c478bd9Sstevel@tonic-gate 	if (chdir_(cp) >= 0)
1867c478bd9Sstevel@tonic-gate 		goto gotcha;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * If the directory argument has a slash in it,
1907c478bd9Sstevel@tonic-gate 	 * for example, directory/directory, then can't
19165b0c20eSnakanon 	 * find that in the cache table.
1927c478bd9Sstevel@tonic-gate 	 */
1937c478bd9Sstevel@tonic-gate 	slash = any('/', cp);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Try interpreting wrt successive components of cdpath.
1977c478bd9Sstevel@tonic-gate 	 * cdpath caching is turned off or directory argument
1987c478bd9Sstevel@tonic-gate 	 * has a slash in it.
1997c478bd9Sstevel@tonic-gate 	 */
20065b0c20eSnakanon 	if (cp[0] != '/'
20165b0c20eSnakanon 	    && !prefix(S_DOTSLA /* "./" */, cp)
2027c478bd9Sstevel@tonic-gate 	    && !prefix(S_DOTDOTSLA /* "../" */, cp)
2037c478bd9Sstevel@tonic-gate 	    && (c = adrof(S_cdpath))
20465b0c20eSnakanon 	    && (!havhash2 || slash)) {
2056c02b4a4Smuffin 		tchar **cdp;
2066c02b4a4Smuffin 		tchar *p;
2076c02b4a4Smuffin 		tchar buf[MAXPATHLEN];
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		for (cdp = c->vec; *cdp; cdp++) {
21065b0c20eSnakanon 			for (dp = buf, p = *cdp; *dp++ = *p++; )
2117c478bd9Sstevel@tonic-gate 				;
2127c478bd9Sstevel@tonic-gate 			dp[-1] = '/';
21365b0c20eSnakanon 			for (p = cp; *dp++ = *p++; )
2147c478bd9Sstevel@tonic-gate 				;
2157c478bd9Sstevel@tonic-gate 			if (chdir_(buf) >= 0) {
2167c478bd9Sstevel@tonic-gate 				printd = 1;
2177c478bd9Sstevel@tonic-gate 				xfree(cp);
2187c478bd9Sstevel@tonic-gate 				cp = savestr(buf);
2197c478bd9Sstevel@tonic-gate 				goto gotcha;
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	}
22365b0c20eSnakanon 
2247c478bd9Sstevel@tonic-gate 	/* cdpath caching turned on */
22565b0c20eSnakanon 	if (cp[0] != '/'
22665b0c20eSnakanon 	    && !prefix(S_DOTSLA /* "./" */, cp)
2277c478bd9Sstevel@tonic-gate 	    && !prefix(S_DOTDOTSLA /* "../" */, cp)
2287c478bd9Sstevel@tonic-gate 	    && (c = adrof(S_cdpath))
22965b0c20eSnakanon 	    && havhash2 && !slash) {
2307c478bd9Sstevel@tonic-gate 		tchar **pv;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/* If no cdpath or no paths in cdpath, leave */
23365b0c20eSnakanon 		if (c == 0 || c->vec[0] == 0)
2347c478bd9Sstevel@tonic-gate 			pv = justabs;
2357c478bd9Sstevel@tonic-gate 		else
2367c478bd9Sstevel@tonic-gate 			pv = c->vec;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		slashcp = strspl(S_SLASH, cp);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		cdhashval = hashname(cp);
2417c478bd9Sstevel@tonic-gate 
24265b0c20eSnakanon 		/* index points to next path component to test */
24365b0c20eSnakanon 		index = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * Look at each path in cdpath until get a match.
2477c478bd9Sstevel@tonic-gate 		 * Only look at those path beginning with a slash
2487c478bd9Sstevel@tonic-gate 		 */
2497c478bd9Sstevel@tonic-gate 		do {
2507c478bd9Sstevel@tonic-gate 			/* only check cache for absolute pathnames */
25165b0c20eSnakanon 			if (pv[0][0] == '/') {
2527c478bd9Sstevel@tonic-gate 				cdhashval1 = hash(cdhashval, index);
2537c478bd9Sstevel@tonic-gate 				if (bit(xhash2, cdhashval1)) {
2547c478bd9Sstevel@tonic-gate 					/*
2557c478bd9Sstevel@tonic-gate 					 * concatenate found path with
2567c478bd9Sstevel@tonic-gate 					 * arg directory
2577c478bd9Sstevel@tonic-gate 					 */
2587c478bd9Sstevel@tonic-gate 					fullpath = strspl(*pv, slashcp);
2597c478bd9Sstevel@tonic-gate 					if (chdir_(fullpath) >= 0) {
2607c478bd9Sstevel@tonic-gate 						printd = 1;
2617c478bd9Sstevel@tonic-gate 						xfree(cp);
2627c478bd9Sstevel@tonic-gate 						cp = savestr(fullpath);
2637c478bd9Sstevel@tonic-gate 						xfree(slashcp);
2647c478bd9Sstevel@tonic-gate 						xfree(fullpath);
2657c478bd9Sstevel@tonic-gate 						goto gotcha;
2667c478bd9Sstevel@tonic-gate 					}
2677c478bd9Sstevel@tonic-gate 				}
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 			/*
2707c478bd9Sstevel@tonic-gate 			 * relative pathnames are not cached, and must be
2717c478bd9Sstevel@tonic-gate 			 * checked manually
2727c478bd9Sstevel@tonic-gate 			 */
2737c478bd9Sstevel@tonic-gate 			else {
2746c02b4a4Smuffin 				tchar *p;
2757c478bd9Sstevel@tonic-gate 				tchar buf[MAXPATHLEN];
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 				for (dp = buf, p = *pv; *dp++ = *p++; )
2787c478bd9Sstevel@tonic-gate 					;
2797c478bd9Sstevel@tonic-gate 				dp[-1] = '/';
2807c478bd9Sstevel@tonic-gate 				for (p = cp; *dp++ = *p++; )
2817c478bd9Sstevel@tonic-gate 					;
2827c478bd9Sstevel@tonic-gate 				if (chdir_(buf) >= 0) {
2837c478bd9Sstevel@tonic-gate 					printd = 1;
2847c478bd9Sstevel@tonic-gate 					xfree(cp);
2857c478bd9Sstevel@tonic-gate 					cp = savestr(buf);
2867c478bd9Sstevel@tonic-gate 					xfree(slashcp);
2877c478bd9Sstevel@tonic-gate 					goto gotcha;
2887c478bd9Sstevel@tonic-gate 				}
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 			pv++;
2917c478bd9Sstevel@tonic-gate 			index++;
2927c478bd9Sstevel@tonic-gate 		} while (*pv);
2937c478bd9Sstevel@tonic-gate 	}
29465b0c20eSnakanon 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * Try dereferencing the variable named by the argument.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	dp = value(cp);
2997c478bd9Sstevel@tonic-gate 	if ((dp[0] == '/' || dp[0] == '.') && chdir_(dp) >= 0) {
3007c478bd9Sstevel@tonic-gate 		xfree(cp);
3017c478bd9Sstevel@tonic-gate 		cp = savestr(dp);
3027c478bd9Sstevel@tonic-gate 		printd = 1;
3037c478bd9Sstevel@tonic-gate 		goto gotcha;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	xfree(cp);			/* XXX, use after free */
3067c478bd9Sstevel@tonic-gate 	Perror(cp);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate gotcha:
3097c478bd9Sstevel@tonic-gate 	if (*cp != '/') {
3106c02b4a4Smuffin 		tchar *p, *q;
3117c478bd9Sstevel@tonic-gate 		int cwdlen;
3127c478bd9Sstevel@tonic-gate 		int len;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * All in the name of efficiency?
3167c478bd9Sstevel@tonic-gate 		 */
3177c478bd9Sstevel@tonic-gate 
31865b0c20eSnakanon 		if ((cwdlen = (strlen_(dcwd->di_name))) == 1) {
31965b0c20eSnakanon 			if (*dcwd->di_name == '/') /* root */
3207c478bd9Sstevel@tonic-gate 				cwdlen = 0;
3217c478bd9Sstevel@tonic-gate 			else
3227c478bd9Sstevel@tonic-gate 			{
3237c478bd9Sstevel@tonic-gate 				/*
32465b0c20eSnakanon 				 * if we are here, when the shell started
3257c478bd9Sstevel@tonic-gate 				 * it was unable to getwd(), lets try it again
3267c478bd9Sstevel@tonic-gate 				 */
3277c478bd9Sstevel@tonic-gate 				tchar path[MAXPATHLEN];
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 				p = getwd_(path);
3307c478bd9Sstevel@tonic-gate 				if (p == NULL)
3317c478bd9Sstevel@tonic-gate 					error("cannot determine current directory");
3327c478bd9Sstevel@tonic-gate 				else
3337c478bd9Sstevel@tonic-gate 				{
3347c478bd9Sstevel@tonic-gate 					xfree(dcwd->di_name);
3357c478bd9Sstevel@tonic-gate 					dcwd->di_name = savestr(p);
3367c478bd9Sstevel@tonic-gate 					xfree(cp);
3377c478bd9Sstevel@tonic-gate 					cp = savestr(p);
3387c478bd9Sstevel@tonic-gate 					return dcanon(cp, cp);
3397c478bd9Sstevel@tonic-gate 				}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 		/*
3447c478bd9Sstevel@tonic-gate 		 *
3457c478bd9Sstevel@tonic-gate 		 * for (p = cp; *p++;)
3467c478bd9Sstevel@tonic-gate 		 * 	;
3477c478bd9Sstevel@tonic-gate 		 * dp = (tchar *)xalloc((unsigned) (cwdlen + (p - cp) + 1)*sizeof (tchar))
3487c478bd9Sstevel@tonic-gate 		 */
3497c478bd9Sstevel@tonic-gate 		len = strlen_(cp);
35065b0c20eSnakanon 		dp = (tchar *)xalloc((unsigned)(cwdlen + len + 2) * sizeof (tchar));
35165b0c20eSnakanon 		for (p = dp, q = dcwd->di_name; *p++ = *q++; )
3527c478bd9Sstevel@tonic-gate 			;
3537c478bd9Sstevel@tonic-gate 		if (cwdlen)
3547c478bd9Sstevel@tonic-gate 			p[-1] = '/';
3557c478bd9Sstevel@tonic-gate 		else
3567c478bd9Sstevel@tonic-gate 			p--;			/* don't add a / after root */
35765b0c20eSnakanon 		for (q = cp; *p++ = *q++; )
3587c478bd9Sstevel@tonic-gate 			;
3597c478bd9Sstevel@tonic-gate 		xfree(cp);
3607c478bd9Sstevel@tonic-gate 		cp = dp;
3617c478bd9Sstevel@tonic-gate 		dp += cwdlen;
3627c478bd9Sstevel@tonic-gate 	} else
3637c478bd9Sstevel@tonic-gate 		dp = cp;
3647c478bd9Sstevel@tonic-gate 	return dcanon(cp, dp);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate  * dopushd - push new directory onto directory stack.
3697c478bd9Sstevel@tonic-gate  *	with no arguments exchange top and second.
3707c478bd9Sstevel@tonic-gate  *	with numeric argument (+n) bring it to top.
3717c478bd9Sstevel@tonic-gate  */
3726c02b4a4Smuffin void
dopushd(tchar ** v)3736c02b4a4Smuffin dopushd(tchar **v)
3747c478bd9Sstevel@tonic-gate {
3756c02b4a4Smuffin 	struct directory *dp;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #ifdef TRACE
3787c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dopushd()\n");
3797c478bd9Sstevel@tonic-gate #endif
3807c478bd9Sstevel@tonic-gate 	printd = 1;
3817c478bd9Sstevel@tonic-gate 	if (*++v == NOSTR) {
3827c478bd9Sstevel@tonic-gate 		if ((dp = dcwd->di_prev) == &dhead)
3837c478bd9Sstevel@tonic-gate 			dp = dhead.di_prev;
3847c478bd9Sstevel@tonic-gate 		if (dp == dcwd)
3857c478bd9Sstevel@tonic-gate 			bferr("No other directory");
3867c478bd9Sstevel@tonic-gate 		if (chdir_(dp->di_name) < 0)
3877c478bd9Sstevel@tonic-gate 			Perror(dp->di_name);
3887c478bd9Sstevel@tonic-gate 		dp->di_prev->di_next = dp->di_next;
3897c478bd9Sstevel@tonic-gate 		dp->di_next->di_prev = dp->di_prev;
3907c478bd9Sstevel@tonic-gate 		dp->di_next = dcwd->di_next;
3917c478bd9Sstevel@tonic-gate 		dp->di_prev = dcwd;
3927c478bd9Sstevel@tonic-gate 		dcwd->di_next->di_prev = dp;
3937c478bd9Sstevel@tonic-gate 		dcwd->di_next = dp;
3947c478bd9Sstevel@tonic-gate 	} else if (dp = dfind(*v)) {
3957c478bd9Sstevel@tonic-gate 		if (chdir_(dp->di_name) < 0)
3967c478bd9Sstevel@tonic-gate 			Perror(dp->di_name);
3977c478bd9Sstevel@tonic-gate 	} else {
3986c02b4a4Smuffin 		tchar *cp;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		cp = dfollow(*v);
40165b0c20eSnakanon 		dp = (struct directory *)xcalloc(sizeof (struct directory), 1);
4027c478bd9Sstevel@tonic-gate 		dp->di_name = cp;
4037c478bd9Sstevel@tonic-gate 		dp->di_count = 0;
4047c478bd9Sstevel@tonic-gate 		dp->di_prev = dcwd;
4057c478bd9Sstevel@tonic-gate 		dp->di_next = dcwd->di_next;
4067c478bd9Sstevel@tonic-gate 		dcwd->di_next = dp;
4077c478bd9Sstevel@tonic-gate 		dp->di_next->di_prev = dp;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	dnewcwd(dp);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * dfind - find a directory if specified by numeric (+n) argument
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate struct directory *
dfind(tchar * cp)4166c02b4a4Smuffin dfind(tchar *cp)
4177c478bd9Sstevel@tonic-gate {
4186c02b4a4Smuffin 	struct directory *dp;
4196c02b4a4Smuffin 	int i;
4206c02b4a4Smuffin 	tchar *ep;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate #ifdef TRACE
4237c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dfind()\n");
4247c478bd9Sstevel@tonic-gate #endif
4257c478bd9Sstevel@tonic-gate 	if (*cp++ != '+')
4267c478bd9Sstevel@tonic-gate 		return (0);
4277c478bd9Sstevel@tonic-gate 	for (ep = cp; digit(*ep); ep++)
4287c478bd9Sstevel@tonic-gate 		continue;
4297c478bd9Sstevel@tonic-gate 	if (*ep)
4307c478bd9Sstevel@tonic-gate 		return (0);
4317c478bd9Sstevel@tonic-gate 	i = getn(cp);
4327c478bd9Sstevel@tonic-gate 	if (i <= 0)
4337c478bd9Sstevel@tonic-gate 		return (0);
4347c478bd9Sstevel@tonic-gate 	for (dp = dcwd; i != 0; i--) {
4357c478bd9Sstevel@tonic-gate 		if ((dp = dp->di_prev) == &dhead)
4367c478bd9Sstevel@tonic-gate 			dp = dp->di_prev;
4377c478bd9Sstevel@tonic-gate 		if (dp == dcwd)
4387c478bd9Sstevel@tonic-gate 			bferr("Directory stack not that deep");
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	return (dp);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /*
4447c478bd9Sstevel@tonic-gate  * dopopd - pop a directory out of the directory stack
4457c478bd9Sstevel@tonic-gate  *	with a numeric argument just discard it.
4467c478bd9Sstevel@tonic-gate  */
4476c02b4a4Smuffin void
dopopd(tchar ** v)4486c02b4a4Smuffin dopopd(tchar **v)
4497c478bd9Sstevel@tonic-gate {
4506c02b4a4Smuffin 	struct directory *dp, *p;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate #ifdef TRACE
4537c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dopopd()\n");
4547c478bd9Sstevel@tonic-gate #endif
4557c478bd9Sstevel@tonic-gate 	printd = 1;
4567c478bd9Sstevel@tonic-gate 	if (*++v == NOSTR)
4577c478bd9Sstevel@tonic-gate 		dp = dcwd;
4587c478bd9Sstevel@tonic-gate 	else if ((dp = dfind(*v)) == 0)
4597c478bd9Sstevel@tonic-gate 		bferr("Invalid argument");
4607c478bd9Sstevel@tonic-gate 	if (dp->di_prev == &dhead && dp->di_next == &dhead)
4617c478bd9Sstevel@tonic-gate 		bferr("Directory stack empty");
4627c478bd9Sstevel@tonic-gate 	if (dp == dcwd) {
4637c478bd9Sstevel@tonic-gate 		if ((p = dp->di_prev) == &dhead)
4647c478bd9Sstevel@tonic-gate 			p = dhead.di_prev;
4657c478bd9Sstevel@tonic-gate 		if (chdir_(p->di_name) < 0)
4667c478bd9Sstevel@tonic-gate 			Perror(p->di_name);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	dp->di_prev->di_next = dp->di_next;
4697c478bd9Sstevel@tonic-gate 	dp->di_next->di_prev = dp->di_prev;
4707c478bd9Sstevel@tonic-gate 	if (dp == dcwd)
4717c478bd9Sstevel@tonic-gate 		dnewcwd(p);
4727c478bd9Sstevel@tonic-gate 	else
4737c478bd9Sstevel@tonic-gate 		dodirs(fakev);
4747c478bd9Sstevel@tonic-gate 	dfree(dp);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * dfree - free the directory (or keep it if it still has ref count)
4797c478bd9Sstevel@tonic-gate  */
4806c02b4a4Smuffin void
dfree(struct directory * dp)4816c02b4a4Smuffin dfree(struct directory *dp)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate #ifdef TRACE
4857c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dfree()\n");
4867c478bd9Sstevel@tonic-gate #endif
4877c478bd9Sstevel@tonic-gate 	if (dp->di_count != 0)
4887c478bd9Sstevel@tonic-gate 		dp->di_next = dp->di_prev = 0;
4897c478bd9Sstevel@tonic-gate 	else
49065b0c20eSnakanon 		xfree(dp->di_name), xfree((tchar *)dp);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
4957c478bd9Sstevel@tonic-gate  *	We are of course assuming that the file system is standardly
4967c478bd9Sstevel@tonic-gate  *	constructed (always have ..'s, directories have links).
4977c478bd9Sstevel@tonic-gate  *
4987c478bd9Sstevel@tonic-gate  *	If the hardpaths shell variable is set, resolve the
4997c478bd9Sstevel@tonic-gate  *	resulting pathname to contain no symbolic link components.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate tchar *
dcanon(tchar * cp,tchar * p)5026c02b4a4Smuffin dcanon(tchar *cp, tchar *p)
5037c478bd9Sstevel@tonic-gate {
5046c02b4a4Smuffin 	tchar *sp;	/* rightmost component currently under
50565b0c20eSnakanon 				consideration */
5066c02b4a4Smuffin 	tchar *p1,	/* general purpose */
5076c02b4a4Smuffin 	    *p2;
5087c478bd9Sstevel@tonic-gate 	bool slash, dotdot, hardpaths;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate #ifdef TRACE
5117c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dcannon()\n");
5127c478bd9Sstevel@tonic-gate #endif
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (*cp != '/')
5157c478bd9Sstevel@tonic-gate 		abort();
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if (hardpaths = (adrof(S_hardpaths) != NULL)) {
5187c478bd9Sstevel@tonic-gate 		/*
5197c478bd9Sstevel@tonic-gate 		 * Be paranoid: don't trust the initial prefix
5207c478bd9Sstevel@tonic-gate 		 * to be symlink-free.
5217c478bd9Sstevel@tonic-gate 		 */
5227c478bd9Sstevel@tonic-gate 		p = cp;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * Loop invariant: cp points to the overall path start,
5277c478bd9Sstevel@tonic-gate 	 * p to its as yet uncanonicalized trailing suffix.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	while (*p) {			/* for each component */
5307c478bd9Sstevel@tonic-gate 		sp = p;			/* save slash address */
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		while (*++p == '/')	/* flush extra slashes */
5337c478bd9Sstevel@tonic-gate 			;
5347c478bd9Sstevel@tonic-gate 		if (p != ++sp)
53565b0c20eSnakanon 			for (p1 = sp, p2 = p; *p1++ = *p2++; )
5367c478bd9Sstevel@tonic-gate 				;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		p = sp;			/* save start of component */
5397c478bd9Sstevel@tonic-gate 		slash = 0;
5407c478bd9Sstevel@tonic-gate 		if (*p)
5417c478bd9Sstevel@tonic-gate 			while (*++p)	/* find next slash or end of path */
5427c478bd9Sstevel@tonic-gate 				if (*p == '/') {
5437c478bd9Sstevel@tonic-gate 					slash = 1;
5447c478bd9Sstevel@tonic-gate 					*p = '\0';
5457c478bd9Sstevel@tonic-gate 					break;
5467c478bd9Sstevel@tonic-gate 				}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		if (*sp == '\0') {
5497c478bd9Sstevel@tonic-gate 			/* component is null */
5507c478bd9Sstevel@tonic-gate 			if (--sp == cp)	/* if path is one tchar (i.e. /) */
5517c478bd9Sstevel@tonic-gate 				break;
5527c478bd9Sstevel@tonic-gate 			else
5537c478bd9Sstevel@tonic-gate 				*sp = '\0';
5547c478bd9Sstevel@tonic-gate 			continue;
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		if (sp[0] == '.' && sp[1] == '\0') {
5587c478bd9Sstevel@tonic-gate 			/* Squeeze out component consisting of "." */
5597c478bd9Sstevel@tonic-gate 			if (slash) {
56065b0c20eSnakanon 				for (p1 = sp, p2 = p + 1; *p1++ = *p2++; )
5617c478bd9Sstevel@tonic-gate 					;
5627c478bd9Sstevel@tonic-gate 				p = --sp;
5637c478bd9Sstevel@tonic-gate 			} else if (--sp != cp)
5647c478bd9Sstevel@tonic-gate 				*sp = '\0';
5657c478bd9Sstevel@tonic-gate 			continue;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		/*
5697c478bd9Sstevel@tonic-gate 		 * At this point we have a path of the form "x/yz",
5707c478bd9Sstevel@tonic-gate 		 * where "x" is null or rooted at "/", "y" is a single
5717c478bd9Sstevel@tonic-gate 		 * component, and "z" is possibly null.  The pointer cp
5727c478bd9Sstevel@tonic-gate 		 * points to the start of "x", sp to the start of "y",
5737c478bd9Sstevel@tonic-gate 		 * and p to the beginning of "z", which has been forced
5747c478bd9Sstevel@tonic-gate 		 * to a null.
5757c478bd9Sstevel@tonic-gate 		 */
5767c478bd9Sstevel@tonic-gate 		/*
5777c478bd9Sstevel@tonic-gate 		 * Process symbolic link component.  Provided that either
5787c478bd9Sstevel@tonic-gate 		 * the hardpaths shell variable is set or "y" is really
5797c478bd9Sstevel@tonic-gate 		 * ".." we replace the symlink with its contents.  The
5807c478bd9Sstevel@tonic-gate 		 * second condition for replacement is necessary to make
5817c478bd9Sstevel@tonic-gate 		 * the command "cd x/.." produce the same results as the
5827c478bd9Sstevel@tonic-gate 		 * sequence "cd x; cd ..".
5837c478bd9Sstevel@tonic-gate 		 *
5847c478bd9Sstevel@tonic-gate 		 * Note that the two conditions correspond to different
5857c478bd9Sstevel@tonic-gate 		 * potential symlinks.  When hardpaths is set, we must
5867c478bd9Sstevel@tonic-gate 		 * check "x/y"; otherwise, when "y" is known to be "..",
5877c478bd9Sstevel@tonic-gate 		 * we check "x".
5887c478bd9Sstevel@tonic-gate 		 */
5897c478bd9Sstevel@tonic-gate 		dotdot = sp[0] == '.' && sp[1] == '.' && sp[2] == '\0';
5907c478bd9Sstevel@tonic-gate 		if (hardpaths || dotdot) {
5917c478bd9Sstevel@tonic-gate 			tchar link[MAXPATHLEN];
5927c478bd9Sstevel@tonic-gate 			int cc;
5937c478bd9Sstevel@tonic-gate 			tchar *newcp;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 			/*
5967c478bd9Sstevel@tonic-gate 			 * Isolate the end of the component that is to
5977c478bd9Sstevel@tonic-gate 			 * be checked for symlink-hood.
5987c478bd9Sstevel@tonic-gate 			 */
5997c478bd9Sstevel@tonic-gate 			sp--;
6007c478bd9Sstevel@tonic-gate 			if (! hardpaths)
6017c478bd9Sstevel@tonic-gate 				*sp = '\0';
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 			/*
6047c478bd9Sstevel@tonic-gate 			 * See whether the component is really a symlink by
6057c478bd9Sstevel@tonic-gate 			 * trying to read it.  If the read succeeds, it is.
6067c478bd9Sstevel@tonic-gate 			 */
6077c478bd9Sstevel@tonic-gate 			if ((hardpaths || sp > cp) &&
6087c478bd9Sstevel@tonic-gate 			    (cc = readlink_(cp, link, MAXPATHLEN)) >= 0) {
6097c478bd9Sstevel@tonic-gate 				/*
6107c478bd9Sstevel@tonic-gate 				 * readlink_ put null, so we don't need this.
6117c478bd9Sstevel@tonic-gate 				 */
6127c478bd9Sstevel@tonic-gate 				/* link[cc] = '\0'; */
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 				/* Restore path. */
6157c478bd9Sstevel@tonic-gate 				if (slash)
6167c478bd9Sstevel@tonic-gate 					*p = '/';
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 				/*
6197c478bd9Sstevel@tonic-gate 				 * Point p at the start of the trailing
6207c478bd9Sstevel@tonic-gate 				 * path following the symlink component.
6217c478bd9Sstevel@tonic-gate 				 * It's already there is hardpaths is set.
6227c478bd9Sstevel@tonic-gate 				 */
6237c478bd9Sstevel@tonic-gate 				if (! hardpaths) {
6247c478bd9Sstevel@tonic-gate 					/* Restore path as well. */
6257c478bd9Sstevel@tonic-gate 					*(p = sp) = '/';
6267c478bd9Sstevel@tonic-gate 				}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 				/*
6297c478bd9Sstevel@tonic-gate 				 * Find length of p.
6307c478bd9Sstevel@tonic-gate 				 */
63165b0c20eSnakanon 				for (p1 = p; *p1++; )
6327c478bd9Sstevel@tonic-gate 					;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 				if (*link != '/') {
6357c478bd9Sstevel@tonic-gate 					/*
6367c478bd9Sstevel@tonic-gate 					 * Relative path: replace the symlink
6377c478bd9Sstevel@tonic-gate 					 * component with its value.  First,
6387c478bd9Sstevel@tonic-gate 					 * set sp to point to the slash at
6397c478bd9Sstevel@tonic-gate 					 * its beginning.  If hardpaths is
6407c478bd9Sstevel@tonic-gate 					 * set, this is already the case.
6417c478bd9Sstevel@tonic-gate 					 */
6427c478bd9Sstevel@tonic-gate 					if (! hardpaths) {
6437c478bd9Sstevel@tonic-gate 						while (*--sp != '/')
6447c478bd9Sstevel@tonic-gate 							;
6457c478bd9Sstevel@tonic-gate 					}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 					/*
6487c478bd9Sstevel@tonic-gate 					 * Terminate the leading part of the
6497c478bd9Sstevel@tonic-gate 					 * path, including trailing slash.
6507c478bd9Sstevel@tonic-gate 					 */
6517c478bd9Sstevel@tonic-gate 					sp++;
6527c478bd9Sstevel@tonic-gate 					*sp = '\0';
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 					/*
6557c478bd9Sstevel@tonic-gate 					 * New length is: "x/" + link + "z"
6567c478bd9Sstevel@tonic-gate 					 */
6577c478bd9Sstevel@tonic-gate 					p1 = newcp = (tchar *)xalloc((unsigned)
65865b0c20eSnakanon 						((sp - cp) + cc + (p1 - p)) * sizeof (tchar));
6597c478bd9Sstevel@tonic-gate 					/*
6607c478bd9Sstevel@tonic-gate 					 * Copy new path into newcp
6617c478bd9Sstevel@tonic-gate 					 */
66265b0c20eSnakanon 					for (p2 = cp; *p1++ = *p2++; )
6637c478bd9Sstevel@tonic-gate 						;
66465b0c20eSnakanon 					for (p1--, p2 = link; *p1++ = *p2++; )
6657c478bd9Sstevel@tonic-gate 						;
66665b0c20eSnakanon 					for (p1--, p2 = p; *p1++ = *p2++; )
6677c478bd9Sstevel@tonic-gate 						;
6687c478bd9Sstevel@tonic-gate 					/*
6697c478bd9Sstevel@tonic-gate 					 * Restart canonicalization at
6707c478bd9Sstevel@tonic-gate 					 * expanded "/y".
6717c478bd9Sstevel@tonic-gate 					 */
6727c478bd9Sstevel@tonic-gate 					p = sp - cp - 1 + newcp;
6737c478bd9Sstevel@tonic-gate 				} else {
6747c478bd9Sstevel@tonic-gate 					/*
6757c478bd9Sstevel@tonic-gate 					 * New length is: link + "z"
6767c478bd9Sstevel@tonic-gate 					 */
6777c478bd9Sstevel@tonic-gate 					p1 = newcp = (tchar *)xalloc((unsigned)
6787c478bd9Sstevel@tonic-gate 						(cc + (p1 - p))*sizeof (tchar));
6797c478bd9Sstevel@tonic-gate 					/*
6807c478bd9Sstevel@tonic-gate 					 * Copy new path into newcp
6817c478bd9Sstevel@tonic-gate 					 */
68265b0c20eSnakanon 					for (p2 = link; *p1++ = *p2++; )
6837c478bd9Sstevel@tonic-gate 						;
68465b0c20eSnakanon 					for (p1--, p2 = p; *p1++ = *p2++; )
6857c478bd9Sstevel@tonic-gate 						;
6867c478bd9Sstevel@tonic-gate 					/*
6877c478bd9Sstevel@tonic-gate 					 * Restart canonicalization at beginning
6887c478bd9Sstevel@tonic-gate 					 */
6897c478bd9Sstevel@tonic-gate 					p = newcp;
6907c478bd9Sstevel@tonic-gate 				}
6917c478bd9Sstevel@tonic-gate 				xfree(cp);
6927c478bd9Sstevel@tonic-gate 				cp = newcp;
6937c478bd9Sstevel@tonic-gate 				continue;	/* canonicalize the link */
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 			/* The component wasn't a symlink after all. */
6977c478bd9Sstevel@tonic-gate 			if (! hardpaths)
6987c478bd9Sstevel@tonic-gate 				*sp = '/';
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (dotdot) {
7027c478bd9Sstevel@tonic-gate 			if (sp != cp)
7037c478bd9Sstevel@tonic-gate 				while (*--sp != '/')
7047c478bd9Sstevel@tonic-gate 					;
7057c478bd9Sstevel@tonic-gate 			if (slash) {
70665b0c20eSnakanon 				for (p1 = sp + 1, p2 = p + 1; *p1++ = *p2++; )
7077c478bd9Sstevel@tonic-gate 					;
7087c478bd9Sstevel@tonic-gate 				p = sp;
7097c478bd9Sstevel@tonic-gate 			} else if (cp == sp)
7107c478bd9Sstevel@tonic-gate 				*++sp = '\0';
7117c478bd9Sstevel@tonic-gate 			else
7127c478bd9Sstevel@tonic-gate 				*sp = '\0';
7137c478bd9Sstevel@tonic-gate 			continue;
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		if (slash)
7177c478bd9Sstevel@tonic-gate 			*p = '/';
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	return cp;
7207c478bd9Sstevel@tonic-gate }
72165b0c20eSnakanon 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * dnewcwd - make a new directory in the loop the current one
7247c478bd9Sstevel@tonic-gate  *	and export its name to the PWD environment variable.
7257c478bd9Sstevel@tonic-gate  */
7266c02b4a4Smuffin void
dnewcwd(struct directory * dp)7276c02b4a4Smuffin dnewcwd(struct directory *dp)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate #ifdef TRACE
7317c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dnewcwd()\n");
7327c478bd9Sstevel@tonic-gate #endif
7337c478bd9Sstevel@tonic-gate 	dcwd = dp;
7347c478bd9Sstevel@tonic-gate #ifdef notdef
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * If we have a fast version of getwd available
7377c478bd9Sstevel@tonic-gate 	 * and hardpaths is set, it would be reasonable
7387c478bd9Sstevel@tonic-gate 	 * here to verify that dcwd->di_name really does
7397c478bd9Sstevel@tonic-gate 	 * name the current directory.  Later...
7407c478bd9Sstevel@tonic-gate 	 */
7416c02b4a4Smuffin #endif /* notdef */
7427c478bd9Sstevel@tonic-gate 
74365b0c20eSnakanon 	didchdir = 1;
7447c478bd9Sstevel@tonic-gate 	set(S_cwd, savestr(dcwd->di_name));
74565b0c20eSnakanon 	didchdir = 0;
7467c478bd9Sstevel@tonic-gate 	local_setenv(S_PWD, dcwd->di_name);
7477c478bd9Sstevel@tonic-gate 	if (printd)
7487c478bd9Sstevel@tonic-gate 		dodirs(fakev);
7497c478bd9Sstevel@tonic-gate }
750