xref: /titanic_41/usr/src/cmd/csh/sh.dir.c (revision 65b0c20e9bbaf87a200ce20a4decf18585e61a25)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley Software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include "sh.h"
187c478bd9Sstevel@tonic-gate #include "sh.dir.h"
197c478bd9Sstevel@tonic-gate #include "sh.tconst.h"
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * C Shell - directory management
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
256c02b4a4Smuffin struct directory	*dfind(tchar *);
266c02b4a4Smuffin tchar	*dfollow(tchar *);
276c02b4a4Smuffin tchar	*dcanon(tchar *, tchar *);
286c02b4a4Smuffin void	dtildepr(tchar *, tchar *);
296c02b4a4Smuffin void	dfree(struct directory *);
306c02b4a4Smuffin void	dnewcwd(struct directory *);
316c02b4a4Smuffin 
327c478bd9Sstevel@tonic-gate struct	directory dhead;		/* "head" of loop */
337c478bd9Sstevel@tonic-gate int	printd;				/* force name to be printed */
347c478bd9Sstevel@tonic-gate static tchar *fakev[] = { S_dirs, NOSTR };
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * dinit - initialize current working directory
387c478bd9Sstevel@tonic-gate  */
396c02b4a4Smuffin void
dinit(tchar * hp)406c02b4a4Smuffin dinit(tchar *hp)
417c478bd9Sstevel@tonic-gate {
426c02b4a4Smuffin 	tchar *cp;
436c02b4a4Smuffin 	struct directory *dp;
447c478bd9Sstevel@tonic-gate 	tchar path[MAXPATHLEN];
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifdef TRACE
477c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dinit()\n");
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 	/*
507c478bd9Sstevel@tonic-gate 	 * If this is a login shell, we should have a home directory.  But,
517c478bd9Sstevel@tonic-gate 	 * if we got here via 'su - <user>' where the user has no directory
527c478bd9Sstevel@tonic-gate 	 * in his passwd file, then su has passed HOME=<nothing>, so hp is
537c478bd9Sstevel@tonic-gate 	 * non-null, but has zero length.  Thus, we do not know the current
547c478bd9Sstevel@tonic-gate 	 * working directory based on the home directory.
557c478bd9Sstevel@tonic-gate 	 */
567c478bd9Sstevel@tonic-gate 	if (loginsh && hp && *hp)
577c478bd9Sstevel@tonic-gate 		cp = hp;
587c478bd9Sstevel@tonic-gate 	else {
597c478bd9Sstevel@tonic-gate 		cp = getwd_(path);
607c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
617c478bd9Sstevel@tonic-gate 			printf("Warning: cannot determine current directory\n");
627c478bd9Sstevel@tonic-gate 			cp = S_DOT;
637c478bd9Sstevel@tonic-gate 		}
647c478bd9Sstevel@tonic-gate 	}
65*65b0c20eSnakanon 	dp = (struct directory *)xcalloc(sizeof (struct directory), 1);
667c478bd9Sstevel@tonic-gate 	dp->di_name = savestr(cp);
677c478bd9Sstevel@tonic-gate 	dp->di_count = 0;
687c478bd9Sstevel@tonic-gate 	dhead.di_next = dhead.di_prev = dp;
697c478bd9Sstevel@tonic-gate 	dp->di_next = dp->di_prev = &dhead;
707c478bd9Sstevel@tonic-gate 	printd = 0;
717c478bd9Sstevel@tonic-gate 	dnewcwd(dp);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * dodirs - list all directories in directory loop
767c478bd9Sstevel@tonic-gate  */
776c02b4a4Smuffin void
dodirs(tchar ** v)786c02b4a4Smuffin dodirs(tchar **v)
797c478bd9Sstevel@tonic-gate {
806c02b4a4Smuffin 	struct directory *dp;
817c478bd9Sstevel@tonic-gate 	bool lflag;
827c478bd9Sstevel@tonic-gate 	tchar *hp = value(S_home);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #ifdef TRACE
857c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dodirs()\n");
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 	if (*hp == '\0')
887c478bd9Sstevel@tonic-gate 		hp = NOSTR;
897c478bd9Sstevel@tonic-gate 	if (*++v != NOSTR)
907c478bd9Sstevel@tonic-gate 		if (eq(*v, S_MINl /* "-l" */) && *++v == NOSTR)
917c478bd9Sstevel@tonic-gate 			lflag = 1;
927c478bd9Sstevel@tonic-gate 		else
937c478bd9Sstevel@tonic-gate 			error("Usage: dirs [ -l ]");
947c478bd9Sstevel@tonic-gate 	else
957c478bd9Sstevel@tonic-gate 		lflag = 0;
967c478bd9Sstevel@tonic-gate 	dp = dcwd;
977c478bd9Sstevel@tonic-gate 	do {
987c478bd9Sstevel@tonic-gate 		if (dp == &dhead)
997c478bd9Sstevel@tonic-gate 			continue;
1007c478bd9Sstevel@tonic-gate 		if (!lflag && hp != NOSTR) {
1017c478bd9Sstevel@tonic-gate 			dtildepr(hp, dp->di_name);
1027c478bd9Sstevel@tonic-gate 		} else
1037c478bd9Sstevel@tonic-gate 			printf("%t", dp->di_name);
1047c478bd9Sstevel@tonic-gate 		printf(" ");
1057c478bd9Sstevel@tonic-gate 	} while ((dp = dp->di_prev) != dcwd);
1067c478bd9Sstevel@tonic-gate 	printf("\n");
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1096c02b4a4Smuffin void
dtildepr(tchar * home,tchar * dir)1106c02b4a4Smuffin dtildepr(tchar *home, tchar *dir)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #ifdef TRACE
1147c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dtildepr()\n");
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 	if (!eq(home, S_SLASH /* "/" */) && prefix(home, dir))
1177c478bd9Sstevel@tonic-gate 		printf("~%t", dir + strlen_(home));
1187c478bd9Sstevel@tonic-gate 	else
1197c478bd9Sstevel@tonic-gate 		printf("%t", dir);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * dochngd - implement chdir command.
1247c478bd9Sstevel@tonic-gate  */
1256c02b4a4Smuffin void
dochngd(tchar ** v)1266c02b4a4Smuffin dochngd(tchar **v)
1277c478bd9Sstevel@tonic-gate {
1286c02b4a4Smuffin 	tchar *cp;
1296c02b4a4Smuffin 	struct directory *dp;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #ifdef TRACE
1327c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dochngd()\n");
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate 	printd = 0;
1357c478bd9Sstevel@tonic-gate 	if (*++v == NOSTR) {
1367c478bd9Sstevel@tonic-gate 		if ((cp = value(S_home)) == NOSTR || *cp == 0)
1377c478bd9Sstevel@tonic-gate 			bferr("No home directory");
1387c478bd9Sstevel@tonic-gate 		if (chdir_(cp) < 0)
1397c478bd9Sstevel@tonic-gate 			bferr("Can't change to home directory");
1407c478bd9Sstevel@tonic-gate 		cp = savestr(cp);
1417c478bd9Sstevel@tonic-gate 	} else if ((dp = dfind(*v)) != 0) {
1427c478bd9Sstevel@tonic-gate 		printd = 1;
1437c478bd9Sstevel@tonic-gate 		if (chdir_(dp->di_name) < 0)
1447c478bd9Sstevel@tonic-gate 			Perror(dp->di_name);
1457c478bd9Sstevel@tonic-gate 		dcwd->di_prev->di_next = dcwd->di_next;
1467c478bd9Sstevel@tonic-gate 		dcwd->di_next->di_prev = dcwd->di_prev;
1477c478bd9Sstevel@tonic-gate 		goto flushcwd;
1487c478bd9Sstevel@tonic-gate 	} else
1497c478bd9Sstevel@tonic-gate 		cp = dfollow(*v);
150*65b0c20eSnakanon 	dp = (struct directory *)xcalloc(sizeof (struct directory), 1);
1517c478bd9Sstevel@tonic-gate 	dp->di_name = cp;
1527c478bd9Sstevel@tonic-gate 	dp->di_count = 0;
1537c478bd9Sstevel@tonic-gate 	dp->di_next = dcwd->di_next;
1547c478bd9Sstevel@tonic-gate 	dp->di_prev = dcwd->di_prev;
1557c478bd9Sstevel@tonic-gate 	dp->di_prev->di_next = dp;
1567c478bd9Sstevel@tonic-gate 	dp->di_next->di_prev = dp;
1577c478bd9Sstevel@tonic-gate flushcwd:
1587c478bd9Sstevel@tonic-gate 	dfree(dcwd);
1597c478bd9Sstevel@tonic-gate 	dnewcwd(dp);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * dfollow - change to arg directory; fall back on cdpath if not valid
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate tchar *
dfollow(tchar * cp)1666c02b4a4Smuffin dfollow(tchar *cp)
1677c478bd9Sstevel@tonic-gate {
1686c02b4a4Smuffin 	tchar *dp;
1697c478bd9Sstevel@tonic-gate 	struct varent *c;
1707c478bd9Sstevel@tonic-gate 	int cdhashval, cdhashval1;
1717c478bd9Sstevel@tonic-gate 	int index;
1727c478bd9Sstevel@tonic-gate 	int slash; /* slashes in the argument */
1737c478bd9Sstevel@tonic-gate 	tchar *fullpath;
1747c478bd9Sstevel@tonic-gate 	tchar *slashcp; /* cp string prepended with a slash */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #ifdef TRACE
1777c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dfollow()\n");
1787c478bd9Sstevel@tonic-gate #endif
1797c478bd9Sstevel@tonic-gate 	cp = globone(cp);
1807c478bd9Sstevel@tonic-gate 	if (chdir_(cp) >= 0)
1817c478bd9Sstevel@tonic-gate 		goto gotcha;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * If the directory argument has a slash in it,
1857c478bd9Sstevel@tonic-gate 	 * for example, directory/directory, then can't
1867c478bd9Sstevel@tonic-gate 	 * find that in the cache table.
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	slash = any('/', cp);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * Try interpreting wrt successive components of cdpath.
1927c478bd9Sstevel@tonic-gate 	 * cdpath caching is turned off or directory argument
1937c478bd9Sstevel@tonic-gate 	 * has a slash in it.
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	if (cp[0] != '/'
1967c478bd9Sstevel@tonic-gate 	    && !prefix(S_DOTSLA /* "./" */, cp)
1977c478bd9Sstevel@tonic-gate 	    && !prefix(S_DOTDOTSLA /* "../" */, cp)
1987c478bd9Sstevel@tonic-gate 	    && (c = adrof(S_cdpath))
1997c478bd9Sstevel@tonic-gate 	    && (!havhash2 || slash)) {
2007c478bd9Sstevel@tonic-gate 		tchar **cdp;
2016c02b4a4Smuffin 		tchar *p;
2027c478bd9Sstevel@tonic-gate 		tchar buf[MAXPATHLEN];
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		for (cdp = c->vec; *cdp; cdp++) {
2057c478bd9Sstevel@tonic-gate 			for (dp = buf, p = *cdp; *dp++ = *p++; )
2067c478bd9Sstevel@tonic-gate 				;
2077c478bd9Sstevel@tonic-gate 			dp[-1] = '/';
2087c478bd9Sstevel@tonic-gate 			for (p = cp; *dp++ = *p++; )
2097c478bd9Sstevel@tonic-gate 				;
2107c478bd9Sstevel@tonic-gate 			if (chdir_(buf) >= 0) {
2117c478bd9Sstevel@tonic-gate 				printd = 1;
2127c478bd9Sstevel@tonic-gate 				xfree(cp);
2137c478bd9Sstevel@tonic-gate 				cp = savestr(buf);
2147c478bd9Sstevel@tonic-gate 				goto gotcha;
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* cdpath caching turned on */
2207c478bd9Sstevel@tonic-gate 	if (cp[0] != '/'
2217c478bd9Sstevel@tonic-gate 	    && !prefix(S_DOTSLA /* "./" */, cp)
2227c478bd9Sstevel@tonic-gate 	    && !prefix(S_DOTDOTSLA /* "../" */, cp)
2237c478bd9Sstevel@tonic-gate 	    && (c = adrof(S_cdpath))
2247c478bd9Sstevel@tonic-gate 	    && havhash2 && !slash) {
2257c478bd9Sstevel@tonic-gate 		tchar **pv;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		/* If no cdpath or no paths in cdpath, leave */
2287c478bd9Sstevel@tonic-gate 		if (c == 0 || c->vec[0] == 0)
2297c478bd9Sstevel@tonic-gate 			pv = justabs;
2307c478bd9Sstevel@tonic-gate 		else
2317c478bd9Sstevel@tonic-gate 			pv = c->vec;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		slashcp = strspl(S_SLASH, cp);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		cdhashval = hashname(cp);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		/* index points to next path component to test */
2387c478bd9Sstevel@tonic-gate 		index = 0;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		/*
2417c478bd9Sstevel@tonic-gate 		 * Look at each path in cdpath until get a match.
2427c478bd9Sstevel@tonic-gate 		 * Only look at those path beginning with a slash
2437c478bd9Sstevel@tonic-gate 		 */
2447c478bd9Sstevel@tonic-gate 		do {
2457c478bd9Sstevel@tonic-gate 			/* only check cache for absolute pathnames */
2467c478bd9Sstevel@tonic-gate 			if (pv[0][0] == '/') {
2477c478bd9Sstevel@tonic-gate 				cdhashval1 = hash(cdhashval, index);
2487c478bd9Sstevel@tonic-gate 				if (bit(xhash2, cdhashval1)) {
2497c478bd9Sstevel@tonic-gate 					/*
2507c478bd9Sstevel@tonic-gate 					 * concatenate found path with
2517c478bd9Sstevel@tonic-gate 					 * arg directory
2527c478bd9Sstevel@tonic-gate 					 */
2537c478bd9Sstevel@tonic-gate 					fullpath = strspl(*pv, slashcp);
2547c478bd9Sstevel@tonic-gate 					if (chdir_(fullpath) >= 0) {
2557c478bd9Sstevel@tonic-gate 						printd = 1;
2567c478bd9Sstevel@tonic-gate 						xfree(cp);
2577c478bd9Sstevel@tonic-gate 						cp = savestr(fullpath);
2587c478bd9Sstevel@tonic-gate 						xfree(slashcp);
2597c478bd9Sstevel@tonic-gate 						xfree(fullpath);
2607c478bd9Sstevel@tonic-gate 						goto gotcha;
2617c478bd9Sstevel@tonic-gate 					}
2627c478bd9Sstevel@tonic-gate 				}
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 			/*
2657c478bd9Sstevel@tonic-gate 			 * relative pathnames are not cached, and must be
2667c478bd9Sstevel@tonic-gate 			 * checked manually
2677c478bd9Sstevel@tonic-gate 			 */
2687c478bd9Sstevel@tonic-gate 			else {
2696c02b4a4Smuffin 				tchar *p;
2707c478bd9Sstevel@tonic-gate 				tchar buf[MAXPATHLEN];
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 				for (dp = buf, p = *pv; *dp++ = *p++; )
2737c478bd9Sstevel@tonic-gate 					;
2747c478bd9Sstevel@tonic-gate 				dp[-1] = '/';
2757c478bd9Sstevel@tonic-gate 				for (p = cp; *dp++ = *p++; )
2767c478bd9Sstevel@tonic-gate 					;
2777c478bd9Sstevel@tonic-gate 				if (chdir_(buf) >= 0) {
2787c478bd9Sstevel@tonic-gate 					printd = 1;
2797c478bd9Sstevel@tonic-gate 					xfree(cp);
2807c478bd9Sstevel@tonic-gate 					cp = savestr(buf);
2817c478bd9Sstevel@tonic-gate 					xfree(slashcp);
2827c478bd9Sstevel@tonic-gate 					goto gotcha;
2837c478bd9Sstevel@tonic-gate 				}
2847c478bd9Sstevel@tonic-gate 			}
2857c478bd9Sstevel@tonic-gate 			pv++;
2867c478bd9Sstevel@tonic-gate 			index++;
2877c478bd9Sstevel@tonic-gate 		} while (*pv);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Try dereferencing the variable named by the argument.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	dp = value(cp);
2947c478bd9Sstevel@tonic-gate 	if ((dp[0] == '/' || dp[0] == '.') && chdir_(dp) >= 0) {
2957c478bd9Sstevel@tonic-gate 		xfree(cp);
2967c478bd9Sstevel@tonic-gate 		cp = savestr(dp);
2977c478bd9Sstevel@tonic-gate 		printd = 1;
2987c478bd9Sstevel@tonic-gate 		goto gotcha;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	xfree(cp);			/* XXX, use after free */
3017c478bd9Sstevel@tonic-gate 	Perror(cp);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate gotcha:
3047c478bd9Sstevel@tonic-gate 	if (*cp != '/') {
3056c02b4a4Smuffin 		tchar *p, *q;
3067c478bd9Sstevel@tonic-gate 		int cwdlen;
3077c478bd9Sstevel@tonic-gate 		int len;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * All in the name of efficiency?
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		if ((cwdlen = (strlen_(dcwd->di_name))) == 1) {
3147c478bd9Sstevel@tonic-gate 			if (*dcwd->di_name == '/') /* root */
3157c478bd9Sstevel@tonic-gate 				cwdlen = 0;
3167c478bd9Sstevel@tonic-gate 			else
3177c478bd9Sstevel@tonic-gate 			{
3187c478bd9Sstevel@tonic-gate 				/*
3197c478bd9Sstevel@tonic-gate 				 * if we are here, when the shell started
3207c478bd9Sstevel@tonic-gate 				 * it was unable to getwd(), lets try it again
3217c478bd9Sstevel@tonic-gate 				 */
3227c478bd9Sstevel@tonic-gate 				tchar path[MAXPATHLEN];
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 				p = getwd_(path);
3257c478bd9Sstevel@tonic-gate 				if (p == NULL)
3267c478bd9Sstevel@tonic-gate 					error("cannot determine current directory");
3277c478bd9Sstevel@tonic-gate 				else
3287c478bd9Sstevel@tonic-gate 				{
3297c478bd9Sstevel@tonic-gate 					xfree(dcwd->di_name);
3307c478bd9Sstevel@tonic-gate 					dcwd->di_name = savestr(p);
3317c478bd9Sstevel@tonic-gate 					xfree(cp);
3327c478bd9Sstevel@tonic-gate 					cp = savestr(p);
3337c478bd9Sstevel@tonic-gate 					return dcanon(cp, cp);
3347c478bd9Sstevel@tonic-gate 				}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			}
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		/*
3397c478bd9Sstevel@tonic-gate 		 *
3407c478bd9Sstevel@tonic-gate 		 * for (p = cp; *p++;)
3417c478bd9Sstevel@tonic-gate 		 * 	;
3427c478bd9Sstevel@tonic-gate 		 * dp = (tchar *)xalloc((unsigned) (cwdlen + (p - cp) + 1)*sizeof (tchar))
3437c478bd9Sstevel@tonic-gate 		 */
3447c478bd9Sstevel@tonic-gate 		len = strlen_(cp);
3457c478bd9Sstevel@tonic-gate 		dp = (tchar *)xalloc((unsigned)(cwdlen + len + 2) * sizeof (tchar));
3467c478bd9Sstevel@tonic-gate 		for (p = dp, q = dcwd->di_name; *p++ = *q++; )
3477c478bd9Sstevel@tonic-gate 			;
3487c478bd9Sstevel@tonic-gate 		if (cwdlen)
3497c478bd9Sstevel@tonic-gate 			p[-1] = '/';
3507c478bd9Sstevel@tonic-gate 		else
3517c478bd9Sstevel@tonic-gate 			p--;			/* don't add a / after root */
3527c478bd9Sstevel@tonic-gate 		for (q = cp; *p++ = *q++; )
3537c478bd9Sstevel@tonic-gate 			;
3547c478bd9Sstevel@tonic-gate 		xfree(cp);
3557c478bd9Sstevel@tonic-gate 		cp = dp;
3567c478bd9Sstevel@tonic-gate 		dp += cwdlen;
3577c478bd9Sstevel@tonic-gate 	} else
3587c478bd9Sstevel@tonic-gate 		dp = cp;
3597c478bd9Sstevel@tonic-gate 	return dcanon(cp, dp);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * dopushd - push new directory onto directory stack.
3647c478bd9Sstevel@tonic-gate  *	with no arguments exchange top and second.
3657c478bd9Sstevel@tonic-gate  *	with numeric argument (+n) bring it to top.
3667c478bd9Sstevel@tonic-gate  */
3676c02b4a4Smuffin void
dopushd(tchar ** v)3686c02b4a4Smuffin dopushd(tchar **v)
3697c478bd9Sstevel@tonic-gate {
3706c02b4a4Smuffin 	struct directory *dp;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate #ifdef TRACE
3737c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dopushd()\n");
3747c478bd9Sstevel@tonic-gate #endif
3757c478bd9Sstevel@tonic-gate 	printd = 1;
3767c478bd9Sstevel@tonic-gate 	if (*++v == NOSTR) {
3777c478bd9Sstevel@tonic-gate 		if ((dp = dcwd->di_prev) == &dhead)
3787c478bd9Sstevel@tonic-gate 			dp = dhead.di_prev;
3797c478bd9Sstevel@tonic-gate 		if (dp == dcwd)
3807c478bd9Sstevel@tonic-gate 			bferr("No other directory");
3817c478bd9Sstevel@tonic-gate 		if (chdir_(dp->di_name) < 0)
3827c478bd9Sstevel@tonic-gate 			Perror(dp->di_name);
3837c478bd9Sstevel@tonic-gate 		dp->di_prev->di_next = dp->di_next;
3847c478bd9Sstevel@tonic-gate 		dp->di_next->di_prev = dp->di_prev;
3857c478bd9Sstevel@tonic-gate 		dp->di_next = dcwd->di_next;
3867c478bd9Sstevel@tonic-gate 		dp->di_prev = dcwd;
3877c478bd9Sstevel@tonic-gate 		dcwd->di_next->di_prev = dp;
3887c478bd9Sstevel@tonic-gate 		dcwd->di_next = dp;
3897c478bd9Sstevel@tonic-gate 	} else if (dp = dfind(*v)) {
3907c478bd9Sstevel@tonic-gate 		if (chdir_(dp->di_name) < 0)
3917c478bd9Sstevel@tonic-gate 			Perror(dp->di_name);
3927c478bd9Sstevel@tonic-gate 	} else {
3936c02b4a4Smuffin 		tchar *cp;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		cp = dfollow(*v);
396*65b0c20eSnakanon 		dp = (struct directory *)xcalloc(sizeof (struct directory), 1);
3977c478bd9Sstevel@tonic-gate 		dp->di_name = cp;
3987c478bd9Sstevel@tonic-gate 		dp->di_count = 0;
3997c478bd9Sstevel@tonic-gate 		dp->di_prev = dcwd;
4007c478bd9Sstevel@tonic-gate 		dp->di_next = dcwd->di_next;
4017c478bd9Sstevel@tonic-gate 		dcwd->di_next = dp;
4027c478bd9Sstevel@tonic-gate 		dp->di_next->di_prev = dp;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	dnewcwd(dp);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * dfind - find a directory if specified by numeric (+n) argument
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate struct directory *
dfind(tchar * cp)4116c02b4a4Smuffin dfind(tchar *cp)
4127c478bd9Sstevel@tonic-gate {
4136c02b4a4Smuffin 	struct directory *dp;
4146c02b4a4Smuffin 	int i;
4156c02b4a4Smuffin 	tchar *ep;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate #ifdef TRACE
4187c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dfind()\n");
4197c478bd9Sstevel@tonic-gate #endif
4207c478bd9Sstevel@tonic-gate 	if (*cp++ != '+')
4217c478bd9Sstevel@tonic-gate 		return (0);
4227c478bd9Sstevel@tonic-gate 	for (ep = cp; digit(*ep); ep++)
4237c478bd9Sstevel@tonic-gate 		continue;
4247c478bd9Sstevel@tonic-gate 	if (*ep)
4257c478bd9Sstevel@tonic-gate 		return (0);
4267c478bd9Sstevel@tonic-gate 	i = getn(cp);
4277c478bd9Sstevel@tonic-gate 	if (i <= 0)
4287c478bd9Sstevel@tonic-gate 		return (0);
4297c478bd9Sstevel@tonic-gate 	for (dp = dcwd; i != 0; i--) {
4307c478bd9Sstevel@tonic-gate 		if ((dp = dp->di_prev) == &dhead)
4317c478bd9Sstevel@tonic-gate 			dp = dp->di_prev;
4327c478bd9Sstevel@tonic-gate 		if (dp == dcwd)
4337c478bd9Sstevel@tonic-gate 			bferr("Directory stack not that deep");
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	return (dp);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * dopopd - pop a directory out of the directory stack
4407c478bd9Sstevel@tonic-gate  *	with a numeric argument just discard it.
4417c478bd9Sstevel@tonic-gate  */
4426c02b4a4Smuffin void
dopopd(tchar ** v)4436c02b4a4Smuffin dopopd(tchar **v)
4447c478bd9Sstevel@tonic-gate {
4456c02b4a4Smuffin 	struct directory *dp, *p;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate #ifdef TRACE
4487c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dopopd()\n");
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate 	printd = 1;
4517c478bd9Sstevel@tonic-gate 	if (*++v == NOSTR)
4527c478bd9Sstevel@tonic-gate 		dp = dcwd;
4537c478bd9Sstevel@tonic-gate 	else if ((dp = dfind(*v)) == 0)
4547c478bd9Sstevel@tonic-gate 		bferr("Invalid argument");
4557c478bd9Sstevel@tonic-gate 	if (dp->di_prev == &dhead && dp->di_next == &dhead)
4567c478bd9Sstevel@tonic-gate 		bferr("Directory stack empty");
4577c478bd9Sstevel@tonic-gate 	if (dp == dcwd) {
4587c478bd9Sstevel@tonic-gate 		if ((p = dp->di_prev) == &dhead)
4597c478bd9Sstevel@tonic-gate 			p = dhead.di_prev;
4607c478bd9Sstevel@tonic-gate 		if (chdir_(p->di_name) < 0)
4617c478bd9Sstevel@tonic-gate 			Perror(p->di_name);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 	dp->di_prev->di_next = dp->di_next;
4647c478bd9Sstevel@tonic-gate 	dp->di_next->di_prev = dp->di_prev;
4657c478bd9Sstevel@tonic-gate 	if (dp == dcwd)
4667c478bd9Sstevel@tonic-gate 		dnewcwd(p);
4677c478bd9Sstevel@tonic-gate 	else
4687c478bd9Sstevel@tonic-gate 		dodirs(fakev);
4697c478bd9Sstevel@tonic-gate 	dfree(dp);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * dfree - free the directory (or keep it if it still has ref count)
4747c478bd9Sstevel@tonic-gate  */
4756c02b4a4Smuffin void
dfree(struct directory * dp)4766c02b4a4Smuffin dfree(struct directory *dp)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate #ifdef TRACE
4807c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dfree()\n");
4817c478bd9Sstevel@tonic-gate #endif
4827c478bd9Sstevel@tonic-gate 	if (dp->di_count != 0)
4837c478bd9Sstevel@tonic-gate 		dp->di_next = dp->di_prev = 0;
4847c478bd9Sstevel@tonic-gate 	else
4857c478bd9Sstevel@tonic-gate 		xfree(dp->di_name), xfree((tchar *)dp);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
4907c478bd9Sstevel@tonic-gate  *	We are of course assuming that the file system is standardly
4917c478bd9Sstevel@tonic-gate  *	constructed (always have ..'s, directories have links).
4927c478bd9Sstevel@tonic-gate  *
4937c478bd9Sstevel@tonic-gate  *	If the hardpaths shell variable is set, resolve the
4947c478bd9Sstevel@tonic-gate  *	resulting pathname to contain no symbolic link components.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate tchar *
dcanon(tchar * cp,tchar * p)4976c02b4a4Smuffin dcanon(tchar *cp, tchar *p)
4987c478bd9Sstevel@tonic-gate {
4996c02b4a4Smuffin 	tchar *sp;	/* rightmost component currently under
5007c478bd9Sstevel@tonic-gate 				consideration */
5016c02b4a4Smuffin 	tchar *p1,	/* general purpose */
5027c478bd9Sstevel@tonic-gate 	    *p2;
5037c478bd9Sstevel@tonic-gate 	bool slash, dotdot, hardpaths;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate #ifdef TRACE
5067c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dcannon()\n");
5077c478bd9Sstevel@tonic-gate #endif
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (*cp != '/')
5107c478bd9Sstevel@tonic-gate 		abort();
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (hardpaths = (adrof(S_hardpaths) != NULL)) {
5137c478bd9Sstevel@tonic-gate 		/*
5147c478bd9Sstevel@tonic-gate 		 * Be paranoid: don't trust the initial prefix
5157c478bd9Sstevel@tonic-gate 		 * to be symlink-free.
5167c478bd9Sstevel@tonic-gate 		 */
5177c478bd9Sstevel@tonic-gate 		p = cp;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * Loop invariant: cp points to the overall path start,
5227c478bd9Sstevel@tonic-gate 	 * p to its as yet uncanonicalized trailing suffix.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	while (*p) {			/* for each component */
5257c478bd9Sstevel@tonic-gate 		sp = p;			/* save slash address */
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		while (*++p == '/')	/* flush extra slashes */
5287c478bd9Sstevel@tonic-gate 			;
5297c478bd9Sstevel@tonic-gate 		if (p != ++sp)
5307c478bd9Sstevel@tonic-gate 			for (p1 = sp, p2 = p; *p1++ = *p2++; )
5317c478bd9Sstevel@tonic-gate 				;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		p = sp;			/* save start of component */
5347c478bd9Sstevel@tonic-gate 		slash = 0;
5357c478bd9Sstevel@tonic-gate 		if (*p)
5367c478bd9Sstevel@tonic-gate 			while (*++p)	/* find next slash or end of path */
5377c478bd9Sstevel@tonic-gate 				if (*p == '/') {
5387c478bd9Sstevel@tonic-gate 					slash = 1;
5397c478bd9Sstevel@tonic-gate 					*p = '\0';
5407c478bd9Sstevel@tonic-gate 					break;
5417c478bd9Sstevel@tonic-gate 				}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		if (*sp == '\0') {
5447c478bd9Sstevel@tonic-gate 			/* component is null */
5457c478bd9Sstevel@tonic-gate 			if (--sp == cp)	/* if path is one tchar (i.e. /) */
5467c478bd9Sstevel@tonic-gate 				break;
5477c478bd9Sstevel@tonic-gate 			else
5487c478bd9Sstevel@tonic-gate 				*sp = '\0';
5497c478bd9Sstevel@tonic-gate 			continue;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		if (sp[0] == '.' && sp[1] == '\0') {
5537c478bd9Sstevel@tonic-gate 			/* Squeeze out component consisting of "." */
5547c478bd9Sstevel@tonic-gate 			if (slash) {
5557c478bd9Sstevel@tonic-gate 				for (p1 = sp, p2 = p + 1; *p1++ = *p2++; )
5567c478bd9Sstevel@tonic-gate 					;
5577c478bd9Sstevel@tonic-gate 				p = --sp;
5587c478bd9Sstevel@tonic-gate 			} else if (--sp != cp)
5597c478bd9Sstevel@tonic-gate 				*sp = '\0';
5607c478bd9Sstevel@tonic-gate 			continue;
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * At this point we have a path of the form "x/yz",
5657c478bd9Sstevel@tonic-gate 		 * where "x" is null or rooted at "/", "y" is a single
5667c478bd9Sstevel@tonic-gate 		 * component, and "z" is possibly null.  The pointer cp
5677c478bd9Sstevel@tonic-gate 		 * points to the start of "x", sp to the start of "y",
5687c478bd9Sstevel@tonic-gate 		 * and p to the beginning of "z", which has been forced
5697c478bd9Sstevel@tonic-gate 		 * to a null.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 		/*
5727c478bd9Sstevel@tonic-gate 		 * Process symbolic link component.  Provided that either
5737c478bd9Sstevel@tonic-gate 		 * the hardpaths shell variable is set or "y" is really
5747c478bd9Sstevel@tonic-gate 		 * ".." we replace the symlink with its contents.  The
5757c478bd9Sstevel@tonic-gate 		 * second condition for replacement is necessary to make
5767c478bd9Sstevel@tonic-gate 		 * the command "cd x/.." produce the same results as the
5777c478bd9Sstevel@tonic-gate 		 * sequence "cd x; cd ..".
5787c478bd9Sstevel@tonic-gate 		 *
5797c478bd9Sstevel@tonic-gate 		 * Note that the two conditions correspond to different
5807c478bd9Sstevel@tonic-gate 		 * potential symlinks.  When hardpaths is set, we must
5817c478bd9Sstevel@tonic-gate 		 * check "x/y"; otherwise, when "y" is known to be "..",
5827c478bd9Sstevel@tonic-gate 		 * we check "x".
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		dotdot = sp[0] == '.' && sp[1] == '.' && sp[2] == '\0';
5857c478bd9Sstevel@tonic-gate 		if (hardpaths || dotdot) {
5867c478bd9Sstevel@tonic-gate 			tchar link[MAXPATHLEN];
5877c478bd9Sstevel@tonic-gate 			int cc;
5887c478bd9Sstevel@tonic-gate 			tchar *newcp;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			/*
5917c478bd9Sstevel@tonic-gate 			 * Isolate the end of the component that is to
5927c478bd9Sstevel@tonic-gate 			 * be checked for symlink-hood.
5937c478bd9Sstevel@tonic-gate 			 */
5947c478bd9Sstevel@tonic-gate 			sp--;
5957c478bd9Sstevel@tonic-gate 			if (! hardpaths)
5967c478bd9Sstevel@tonic-gate 				*sp = '\0';
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 			/*
5997c478bd9Sstevel@tonic-gate 			 * See whether the component is really a symlink by
6007c478bd9Sstevel@tonic-gate 			 * trying to read it.  If the read succeeds, it is.
6017c478bd9Sstevel@tonic-gate 			 */
6027c478bd9Sstevel@tonic-gate 			if ((hardpaths || sp > cp) &&
6037c478bd9Sstevel@tonic-gate 			    (cc = readlink_(cp, link, MAXPATHLEN)) >= 0) {
6047c478bd9Sstevel@tonic-gate 				/*
6057c478bd9Sstevel@tonic-gate 				 * readlink_ put null, so we don't need this.
6067c478bd9Sstevel@tonic-gate 				 */
6077c478bd9Sstevel@tonic-gate 				/* link[cc] = '\0'; */
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 				/* Restore path. */
6107c478bd9Sstevel@tonic-gate 				if (slash)
6117c478bd9Sstevel@tonic-gate 					*p = '/';
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 				/*
6147c478bd9Sstevel@tonic-gate 				 * Point p at the start of the trailing
6157c478bd9Sstevel@tonic-gate 				 * path following the symlink component.
6167c478bd9Sstevel@tonic-gate 				 * It's already there is hardpaths is set.
6177c478bd9Sstevel@tonic-gate 				 */
6187c478bd9Sstevel@tonic-gate 				if (! hardpaths) {
6197c478bd9Sstevel@tonic-gate 					/* Restore path as well. */
6207c478bd9Sstevel@tonic-gate 					*(p = sp) = '/';
6217c478bd9Sstevel@tonic-gate 				}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 				/*
6247c478bd9Sstevel@tonic-gate 				 * Find length of p.
6257c478bd9Sstevel@tonic-gate 				 */
6267c478bd9Sstevel@tonic-gate 				for (p1 = p; *p1++; )
6277c478bd9Sstevel@tonic-gate 					;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 				if (*link != '/') {
6307c478bd9Sstevel@tonic-gate 					/*
6317c478bd9Sstevel@tonic-gate 					 * Relative path: replace the symlink
6327c478bd9Sstevel@tonic-gate 					 * component with its value.  First,
6337c478bd9Sstevel@tonic-gate 					 * set sp to point to the slash at
6347c478bd9Sstevel@tonic-gate 					 * its beginning.  If hardpaths is
6357c478bd9Sstevel@tonic-gate 					 * set, this is already the case.
6367c478bd9Sstevel@tonic-gate 					 */
6377c478bd9Sstevel@tonic-gate 					if (! hardpaths) {
6387c478bd9Sstevel@tonic-gate 						while (*--sp != '/')
6397c478bd9Sstevel@tonic-gate 							;
6407c478bd9Sstevel@tonic-gate 					}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 					/*
6437c478bd9Sstevel@tonic-gate 					 * Terminate the leading part of the
6447c478bd9Sstevel@tonic-gate 					 * path, including trailing slash.
6457c478bd9Sstevel@tonic-gate 					 */
6467c478bd9Sstevel@tonic-gate 					sp++;
6477c478bd9Sstevel@tonic-gate 					*sp = '\0';
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 					/*
6507c478bd9Sstevel@tonic-gate 					 * New length is: "x/" + link + "z"
6517c478bd9Sstevel@tonic-gate 					 */
6527c478bd9Sstevel@tonic-gate 					p1 = newcp = (tchar *)xalloc((unsigned)
6537c478bd9Sstevel@tonic-gate 						((sp - cp) + cc + (p1 - p)) * sizeof (tchar));
6547c478bd9Sstevel@tonic-gate 					/*
6557c478bd9Sstevel@tonic-gate 					 * Copy new path into newcp
6567c478bd9Sstevel@tonic-gate 					 */
6577c478bd9Sstevel@tonic-gate 					for (p2 = cp; *p1++ = *p2++; )
6587c478bd9Sstevel@tonic-gate 						;
6597c478bd9Sstevel@tonic-gate 					for (p1--, p2 = link; *p1++ = *p2++; )
6607c478bd9Sstevel@tonic-gate 						;
6617c478bd9Sstevel@tonic-gate 					for (p1--, p2 = p; *p1++ = *p2++; )
6627c478bd9Sstevel@tonic-gate 						;
6637c478bd9Sstevel@tonic-gate 					/*
6647c478bd9Sstevel@tonic-gate 					 * Restart canonicalization at
6657c478bd9Sstevel@tonic-gate 					 * expanded "/y".
6667c478bd9Sstevel@tonic-gate 					 */
6677c478bd9Sstevel@tonic-gate 					p = sp - cp - 1 + newcp;
6687c478bd9Sstevel@tonic-gate 				} else {
6697c478bd9Sstevel@tonic-gate 					/*
6707c478bd9Sstevel@tonic-gate 					 * New length is: link + "z"
6717c478bd9Sstevel@tonic-gate 					 */
6727c478bd9Sstevel@tonic-gate 					p1 = newcp = (tchar *)xalloc((unsigned)
6737c478bd9Sstevel@tonic-gate 						(cc + (p1 - p))*sizeof (tchar));
6747c478bd9Sstevel@tonic-gate 					/*
6757c478bd9Sstevel@tonic-gate 					 * Copy new path into newcp
6767c478bd9Sstevel@tonic-gate 					 */
6777c478bd9Sstevel@tonic-gate 					for (p2 = link; *p1++ = *p2++; )
6787c478bd9Sstevel@tonic-gate 						;
6797c478bd9Sstevel@tonic-gate 					for (p1--, p2 = p; *p1++ = *p2++; )
6807c478bd9Sstevel@tonic-gate 						;
6817c478bd9Sstevel@tonic-gate 					/*
6827c478bd9Sstevel@tonic-gate 					 * Restart canonicalization at beginning
6837c478bd9Sstevel@tonic-gate 					 */
6847c478bd9Sstevel@tonic-gate 					p = newcp;
6857c478bd9Sstevel@tonic-gate 				}
6867c478bd9Sstevel@tonic-gate 				xfree(cp);
6877c478bd9Sstevel@tonic-gate 				cp = newcp;
6887c478bd9Sstevel@tonic-gate 				continue;	/* canonicalize the link */
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			/* The component wasn't a symlink after all. */
6927c478bd9Sstevel@tonic-gate 			if (! hardpaths)
6937c478bd9Sstevel@tonic-gate 				*sp = '/';
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		if (dotdot) {
6977c478bd9Sstevel@tonic-gate 			if (sp != cp)
6987c478bd9Sstevel@tonic-gate 				while (*--sp != '/')
6997c478bd9Sstevel@tonic-gate 					;
7007c478bd9Sstevel@tonic-gate 			if (slash) {
7017c478bd9Sstevel@tonic-gate 				for (p1 = sp + 1, p2 = p + 1; *p1++ = *p2++; )
7027c478bd9Sstevel@tonic-gate 					;
7037c478bd9Sstevel@tonic-gate 				p = sp;
7047c478bd9Sstevel@tonic-gate 			} else if (cp == sp)
7057c478bd9Sstevel@tonic-gate 				*++sp = '\0';
7067c478bd9Sstevel@tonic-gate 			else
7077c478bd9Sstevel@tonic-gate 				*sp = '\0';
7087c478bd9Sstevel@tonic-gate 			continue;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		if (slash)
7127c478bd9Sstevel@tonic-gate 			*p = '/';
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	return cp;
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * dnewcwd - make a new directory in the loop the current one
7197c478bd9Sstevel@tonic-gate  *	and export its name to the PWD environment variable.
7207c478bd9Sstevel@tonic-gate  */
7216c02b4a4Smuffin void
dnewcwd(struct directory * dp)7226c02b4a4Smuffin dnewcwd(struct directory *dp)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate #ifdef TRACE
7267c478bd9Sstevel@tonic-gate 	tprintf("TRACE- dnewcwd()\n");
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 	dcwd = dp;
7297c478bd9Sstevel@tonic-gate #ifdef notdef
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * If we have a fast version of getwd available
7327c478bd9Sstevel@tonic-gate 	 * and hardpaths is set, it would be reasonable
7337c478bd9Sstevel@tonic-gate 	 * here to verify that dcwd->di_name really does
7347c478bd9Sstevel@tonic-gate 	 * name the current directory.  Later...
7357c478bd9Sstevel@tonic-gate 	 */
7366c02b4a4Smuffin #endif /* notdef */
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	didchdir = 1;
7397c478bd9Sstevel@tonic-gate 	set(S_cwd, savestr(dcwd->di_name));
7407c478bd9Sstevel@tonic-gate 	didchdir = 0;
7417c478bd9Sstevel@tonic-gate 	local_setenv(S_PWD, dcwd->di_name);
7427c478bd9Sstevel@tonic-gate 	if (printd)
7437c478bd9Sstevel@tonic-gate 		dodirs(fakev);
7447c478bd9Sstevel@tonic-gate }
745