xref: /onnv-gate/usr/src/cmd/sh/pwd.c (revision 2256:a9ca1c675600)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2256Sna195498  * Common Development and Distribution License (the "License").
6*2256Sna195498  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21527Schin 
22527Schin /*
23*2256Sna195498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24527Schin  * Use is subject to license terms.
25527Schin  */
26527Schin 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
31527Schin #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	UNIX shell
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include	"mac.h"
370Sstevel@tonic-gate #include	<errno.h>
380Sstevel@tonic-gate #include	<sys/types.h>
390Sstevel@tonic-gate #include	<sys/stat.h>
400Sstevel@tonic-gate #include	<limits.h>
41*2256Sna195498 #include	"defs.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	DOT		'.'
440Sstevel@tonic-gate #define	NULL	0
450Sstevel@tonic-gate #define	SLASH	'/'
460Sstevel@tonic-gate #define PARTLY	2
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static void rmslash();
490Sstevel@tonic-gate #ifdef __STDC__
500Sstevel@tonic-gate extern const char	longpwd[];
510Sstevel@tonic-gate #else
520Sstevel@tonic-gate extern char	longpwd[];
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate extern char *getcwd();
550Sstevel@tonic-gate 
560Sstevel@tonic-gate unsigned char cwdname[PATH_MAX+1];
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int 	didpwd = FALSE;
590Sstevel@tonic-gate 
60527Schin void
cwd(unsigned char * dir)61527Schin cwd(unsigned char *dir)
620Sstevel@tonic-gate {
63527Schin 	unsigned char *pcwd;
64527Schin 	unsigned char *pdir;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/* First remove extra /'s */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	rmslash(dir);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/* Now remove any .'s */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	pdir = dir;
730Sstevel@tonic-gate 	if(*dir == SLASH)
740Sstevel@tonic-gate 		pdir++;
750Sstevel@tonic-gate 	while(*pdir) 			/* remove /./ by itself */
760Sstevel@tonic-gate 	{
770Sstevel@tonic-gate 		if((*pdir==DOT) && (*(pdir+1)==SLASH))
780Sstevel@tonic-gate 		{
790Sstevel@tonic-gate 			movstr(pdir+2, pdir);
800Sstevel@tonic-gate 			continue;
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		pdir++;
830Sstevel@tonic-gate 		while ((*pdir) && (*pdir != SLASH))
840Sstevel@tonic-gate 			pdir++;
850Sstevel@tonic-gate 		if (*pdir)
860Sstevel@tonic-gate 			pdir++;
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 	/* take care of trailing /. */
890Sstevel@tonic-gate 	if(*(--pdir)==DOT && pdir > dir && *(--pdir)==SLASH) {
900Sstevel@tonic-gate 		if(pdir > dir) {
910Sstevel@tonic-gate 			*pdir = NULL;
920Sstevel@tonic-gate 		} else {
930Sstevel@tonic-gate 			*(pdir+1) = NULL;
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* Remove extra /'s */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	rmslash(dir);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	/* Now that the dir is canonicalized, process it */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if(*dir==DOT && *(dir+1)==NULL)
1050Sstevel@tonic-gate 	{
1060Sstevel@tonic-gate 		return;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if(*dir==SLASH)
1110Sstevel@tonic-gate 	{
1120Sstevel@tonic-gate 		/* Absolute path */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		pcwd = cwdname;
1150Sstevel@tonic-gate 		*pcwd++ = *dir++;
1160Sstevel@tonic-gate 		didpwd = PARTLY;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	else
1190Sstevel@tonic-gate 	{
1200Sstevel@tonic-gate 		/* Relative path */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		if (didpwd == FALSE)
1230Sstevel@tonic-gate 			return;
1240Sstevel@tonic-gate 		didpwd = PARTLY;
1250Sstevel@tonic-gate 		pcwd = cwdname + length(cwdname) - 1;
1260Sstevel@tonic-gate 		if(pcwd != cwdname+1)
1270Sstevel@tonic-gate 			*pcwd++ = SLASH;
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	while(*dir)
1300Sstevel@tonic-gate 	{
1310Sstevel@tonic-gate 		if(*dir==DOT &&
1320Sstevel@tonic-gate 		   *(dir+1)==DOT &&
1330Sstevel@tonic-gate 		   (*(dir+2)==SLASH || *(dir+2)==NULL))
1340Sstevel@tonic-gate 		{
1350Sstevel@tonic-gate 			/* Parent directory, so backup one */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 			if( pcwd > cwdname+2 )
1380Sstevel@tonic-gate 				--pcwd;
1390Sstevel@tonic-gate 			while(*(--pcwd) != SLASH)
1400Sstevel@tonic-gate 				;
1410Sstevel@tonic-gate 			pcwd++;
1420Sstevel@tonic-gate 			dir += 2;
1430Sstevel@tonic-gate 			if(*dir==SLASH)
1440Sstevel@tonic-gate 			{
1450Sstevel@tonic-gate 				dir++;
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 			continue;
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 	 	if (pcwd >= &cwdname[PATH_MAX+1])
1500Sstevel@tonic-gate 		{
1510Sstevel@tonic-gate 			didpwd=FALSE;
1520Sstevel@tonic-gate 			return;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		*pcwd++ = *dir++;
1550Sstevel@tonic-gate 		while((*dir) && (*dir != SLASH))
1560Sstevel@tonic-gate 		{
1570Sstevel@tonic-gate 	 		if (pcwd >= &cwdname[PATH_MAX+1])
1580Sstevel@tonic-gate 			{
1590Sstevel@tonic-gate 				didpwd=FALSE;
1600Sstevel@tonic-gate 				return;
1610Sstevel@tonic-gate 			}
1620Sstevel@tonic-gate 			*pcwd++ = *dir++;
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 		if (*dir)
1650Sstevel@tonic-gate 		{
1660Sstevel@tonic-gate 	 		if (pcwd >= &cwdname[PATH_MAX+1])
1670Sstevel@tonic-gate 			{
1680Sstevel@tonic-gate 				didpwd=FALSE;
1690Sstevel@tonic-gate 				return;
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 			*pcwd++ = *dir++;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	if (pcwd >= &cwdname[PATH_MAX+1])
1750Sstevel@tonic-gate 	{
1760Sstevel@tonic-gate 		didpwd=FALSE;
1770Sstevel@tonic-gate 		return;
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 	*pcwd = NULL;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	--pcwd;
1820Sstevel@tonic-gate 	if(pcwd>cwdname && *pcwd==SLASH)
1830Sstevel@tonic-gate 	{
1840Sstevel@tonic-gate 		/* Remove trailing / */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		*pcwd = NULL;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	return;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate void
cwd2()1920Sstevel@tonic-gate cwd2()
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	struct stat stat1, stat2;
1950Sstevel@tonic-gate 	unsigned char *pcwd;
1960Sstevel@tonic-gate 	/* check if there are any symbolic links in pathname */
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if(didpwd == FALSE)
1990Sstevel@tonic-gate 		return;
2000Sstevel@tonic-gate 	pcwd = cwdname + 1;
2010Sstevel@tonic-gate 	if(didpwd == PARTLY) {
2020Sstevel@tonic-gate 		while (*pcwd)
2030Sstevel@tonic-gate 		{
2040Sstevel@tonic-gate 			char c;
2050Sstevel@tonic-gate 			while((c = *pcwd++) != SLASH && c != '\0');
2060Sstevel@tonic-gate 			*--pcwd = '\0';
2070Sstevel@tonic-gate 			if (lstat((char *)cwdname, &stat1) == -1
2080Sstevel@tonic-gate 		    	|| (stat1.st_mode & S_IFMT) == S_IFLNK) {
2090Sstevel@tonic-gate 				didpwd = FALSE;
2100Sstevel@tonic-gate 				*pcwd = c;
2110Sstevel@tonic-gate 				return;
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 			*pcwd = c;
2140Sstevel@tonic-gate 			if(c)
2150Sstevel@tonic-gate 				pcwd++;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		didpwd = TRUE;
2180Sstevel@tonic-gate 	} else
2190Sstevel@tonic-gate 		if (stat((char *)cwdname, &stat1) == -1) {
2200Sstevel@tonic-gate 			didpwd = FALSE;
2210Sstevel@tonic-gate 			return;
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * check if ino's and dev's match; pathname could
2250Sstevel@tonic-gate 	 * consist of symbolic links with ".."
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (stat(".", &stat2) == -1
2290Sstevel@tonic-gate 	    || stat1.st_dev != stat2.st_dev
2300Sstevel@tonic-gate 	    || stat1.st_ino != stat2.st_ino)
2310Sstevel@tonic-gate 		didpwd = FALSE;
2320Sstevel@tonic-gate 	return;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate unsigned char *
cwdget()2360Sstevel@tonic-gate cwdget()
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	cwd2();
2390Sstevel@tonic-gate 	if (didpwd == FALSE) {
240*2256Sna195498 		if (getcwd((char *)cwdname, PATH_MAX+1) == NULL)
2410Sstevel@tonic-gate 			*cwdname = 0;
2420Sstevel@tonic-gate 		didpwd = TRUE;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 	return (cwdname);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  *	Print the current working directory.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate 
251527Schin void
cwdprint(void)252527Schin cwdprint(void)
2530Sstevel@tonic-gate {
254527Schin 	unsigned char *cp;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	cwd2();
2570Sstevel@tonic-gate 	if (didpwd == FALSE) {
258*2256Sna195498 		if (getcwd((char *)cwdname, PATH_MAX+1) == NULL) {
259*2256Sna195498 			if (errno && errno != ERANGE)
260*2256Sna195498 				error(badpwd);
2610Sstevel@tonic-gate 			else
2620Sstevel@tonic-gate 				error(longpwd);
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 		didpwd = TRUE;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	for (cp = cwdname; *cp; cp++) {
2680Sstevel@tonic-gate 	  prc_buff(*cp);
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	prc_buff(NL);
2720Sstevel@tonic-gate 	return;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  *	This routine will remove repeated slashes from string.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static void
rmslash(string)2800Sstevel@tonic-gate rmslash(string)
2810Sstevel@tonic-gate 	unsigned char *string;
2820Sstevel@tonic-gate {
283527Schin 	unsigned char *pstring;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	pstring = string;
2860Sstevel@tonic-gate 	while(*pstring)
2870Sstevel@tonic-gate 	{
2880Sstevel@tonic-gate 		if(*pstring==SLASH && *(pstring+1)==SLASH)
2890Sstevel@tonic-gate 		{
2900Sstevel@tonic-gate 			/* Remove repeated SLASH's */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 			movstr(pstring+1, pstring);
2930Sstevel@tonic-gate 			continue;
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 		pstring++;
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	--pstring;
2990Sstevel@tonic-gate 	if(pstring>string && *pstring==SLASH)
3000Sstevel@tonic-gate 	{
3010Sstevel@tonic-gate 		/* Remove trailing / */
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		*pstring = NULL;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	return;
3060Sstevel@tonic-gate }
307