xref: /illumos-gate/usr/src/cmd/sh/name.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
539e7390aSna  * Common Development and Distribution License (the "License").
639e7390aSna  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23d6b882a9SNobutomo Nakano  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27965005c8Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28965005c8Schin /*	  All Rights Reserved  	*/
29965005c8Schin 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * UNIX shell
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	"defs.h"
357c478bd9Sstevel@tonic-gate #include	<stropts.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate extern BOOL	chkid();
387c478bd9Sstevel@tonic-gate extern unsigned char	*simple();
397c478bd9Sstevel@tonic-gate extern int	mailchk;
407c478bd9Sstevel@tonic-gate 
41965005c8Schin static void	setname(unsigned char *, int);
427c478bd9Sstevel@tonic-gate static void	set_builtins_path();
437c478bd9Sstevel@tonic-gate static int	patheq();
44965005c8Schin static void	namwalk(struct namnod *);
45965005c8Schin static void	dolocale();
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate struct namnod ps2nod =
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
507c478bd9Sstevel@tonic-gate 	&acctnod,
517c478bd9Sstevel@tonic-gate 	(unsigned char *)ps2name
527c478bd9Sstevel@tonic-gate };
53*55fea89dSDan Cross struct namnod cdpnod =
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
567c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
577c478bd9Sstevel@tonic-gate 	(unsigned char *)cdpname
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate struct namnod pathnod =
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	&mailpnod,
627c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
637c478bd9Sstevel@tonic-gate 	(unsigned char *)pathname
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate struct namnod ifsnod =
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	&homenod,
687c478bd9Sstevel@tonic-gate 	&mailnod,
697c478bd9Sstevel@tonic-gate 	(unsigned char *)ifsname
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate struct namnod ps1nod =
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	&pathnod,
747c478bd9Sstevel@tonic-gate 	&ps2nod,
757c478bd9Sstevel@tonic-gate 	(unsigned char *)ps1name
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate struct namnod homenod =
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	&cdpnod,
807c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
817c478bd9Sstevel@tonic-gate 	(unsigned char *)homename
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate struct namnod mailnod =
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
867c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
877c478bd9Sstevel@tonic-gate 	(unsigned char *)mailname
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate struct namnod mchknod =
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	&ifsnod,
927c478bd9Sstevel@tonic-gate 	&ps1nod,
937c478bd9Sstevel@tonic-gate 	(unsigned char *)mchkname
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate struct namnod acctnod =
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
987c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
997c478bd9Sstevel@tonic-gate 	(unsigned char *)acctname
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate struct namnod mailpnod =
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
1047c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
1057c478bd9Sstevel@tonic-gate 	(unsigned char *)mailpname
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate struct namnod *namep = &mchknod;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* ========	variable and string handling	======== */
1127c478bd9Sstevel@tonic-gate 
113965005c8Schin int
syslook(unsigned char * w,struct sysnod syswds[],int n)114965005c8Schin syslook(unsigned char *w, struct sysnod syswds[], int n)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	int	low;
1177c478bd9Sstevel@tonic-gate 	int	high;
1187c478bd9Sstevel@tonic-gate 	int	mid;
119965005c8Schin 	int	cond;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (w == 0 || *w == 0)
1227c478bd9Sstevel@tonic-gate 		return(0);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	low = 0;
1257c478bd9Sstevel@tonic-gate 	high = n - 1;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	while (low <= high)
1287c478bd9Sstevel@tonic-gate 	{
1297c478bd9Sstevel@tonic-gate 		mid = (low + high) / 2;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		if ((cond = cf(w, syswds[mid].sysnam)) < 0)
1327c478bd9Sstevel@tonic-gate 			high = mid - 1;
1337c478bd9Sstevel@tonic-gate 		else if (cond > 0)
1347c478bd9Sstevel@tonic-gate 			low = mid + 1;
1357c478bd9Sstevel@tonic-gate 		else
1367c478bd9Sstevel@tonic-gate 			return(syswds[mid].sysval);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	return(0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
141965005c8Schin void
setlist(struct argnod * arg,int xp)142965005c8Schin setlist(struct argnod *arg, int xp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	if (flags & exportflg)
1457c478bd9Sstevel@tonic-gate 		xp |= N_EXPORT;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	while (arg)
1487c478bd9Sstevel@tonic-gate 	{
149965005c8Schin 		unsigned char *s = mactrim(arg->argval);
1507c478bd9Sstevel@tonic-gate 		setname(s, xp);
1517c478bd9Sstevel@tonic-gate 		arg = arg->argnxt;
1527c478bd9Sstevel@tonic-gate 		if (flags & execpr)
1537c478bd9Sstevel@tonic-gate 		{
1547c478bd9Sstevel@tonic-gate 			prs(s);
1557c478bd9Sstevel@tonic-gate 			if (arg)
1567c478bd9Sstevel@tonic-gate 				blank();
1577c478bd9Sstevel@tonic-gate 			else
1587c478bd9Sstevel@tonic-gate 				newline();
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
163965005c8Schin static void
setname(unsigned char * argi,int xp)164965005c8Schin setname(unsigned char *argi, int xp)	/* does parameter assignments */
1657c478bd9Sstevel@tonic-gate {
166965005c8Schin 	unsigned char *argscan = argi;
167965005c8Schin 	struct namnod *n;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (letter(*argscan))
1707c478bd9Sstevel@tonic-gate 	{
1717c478bd9Sstevel@tonic-gate 		while (alphanum(*argscan))
1727c478bd9Sstevel@tonic-gate 			argscan++;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		if (*argscan == '=')
1757c478bd9Sstevel@tonic-gate 		{
1767c478bd9Sstevel@tonic-gate 			*argscan = 0;	/* make name a cohesive string */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			n = lookup(argi);
1797c478bd9Sstevel@tonic-gate 			*argscan++ = '=';
1807c478bd9Sstevel@tonic-gate 			attrib(n, xp);
1817c478bd9Sstevel@tonic-gate 			if (xp & N_ENVNAM)
1827c478bd9Sstevel@tonic-gate 			{
1837c478bd9Sstevel@tonic-gate 				n->namenv = n->namval = argscan;
1847c478bd9Sstevel@tonic-gate 				if (n == &pathnod)
1857c478bd9Sstevel@tonic-gate 					set_builtins_path();
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 			else
1887c478bd9Sstevel@tonic-gate 				assign(n, argscan);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 			dolocale(n->namid);
1917c478bd9Sstevel@tonic-gate 			return;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
196965005c8Schin void
replace(unsigned char ** a,unsigned char * v)197965005c8Schin replace(unsigned char **a, unsigned char *v)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	free(*a);
2007c478bd9Sstevel@tonic-gate 	*a = make(v);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
203965005c8Schin void
dfault(struct namnod * n,unsigned char * v)204965005c8Schin dfault(struct namnod *n, unsigned char	*v)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	if (n->namval == 0)
2077c478bd9Sstevel@tonic-gate 		assign(n, v);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
210965005c8Schin void
assign(struct namnod * n,unsigned char * v)211965005c8Schin assign(struct namnod *n, unsigned char *v)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	if (n->namflg & N_RDONLY)
2147c478bd9Sstevel@tonic-gate 		failed(n->namid, wtfailed);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #ifndef RES
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	else if (flags & rshflg)
2197c478bd9Sstevel@tonic-gate 	{
2207c478bd9Sstevel@tonic-gate 		if (n == &pathnod || eq(n->namid,"SHELL"))
2217c478bd9Sstevel@tonic-gate 			failed(n->namid, restricted);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate #endif
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	else if (n->namflg & N_FUNCTN)
2267c478bd9Sstevel@tonic-gate 	{
2277c478bd9Sstevel@tonic-gate 		func_unhash(n->namid);
2287c478bd9Sstevel@tonic-gate 		freefunc(n);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		n->namenv = 0;
2317c478bd9Sstevel@tonic-gate 		n->namflg = N_DEFAULT;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (n == &mchknod)
2357c478bd9Sstevel@tonic-gate 	{
2367c478bd9Sstevel@tonic-gate 		mailchk = stoi(v);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	replace(&n->namval, v);
2407c478bd9Sstevel@tonic-gate 	attrib(n, N_ENVCHG);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (n == &pathnod)
2437c478bd9Sstevel@tonic-gate 	{
2447c478bd9Sstevel@tonic-gate 		zaphash();
2457c478bd9Sstevel@tonic-gate 		set_dotpath();
2467c478bd9Sstevel@tonic-gate 		set_builtins_path();
2477c478bd9Sstevel@tonic-gate 		return;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (flags & prompt)
2517c478bd9Sstevel@tonic-gate 	{
2527c478bd9Sstevel@tonic-gate 		if ((n == &mailpnod) || (n == &mailnod && mailpnod.namflg == N_DEFAULT))
2537c478bd9Sstevel@tonic-gate 			setmail(n->namval);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate static void
set_builtins_path()2587c478bd9Sstevel@tonic-gate set_builtins_path()
2597c478bd9Sstevel@tonic-gate {
260965005c8Schin 	unsigned char *path;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate         ucb_builtins = 0;
2637c478bd9Sstevel@tonic-gate         path = getpath("");
2647c478bd9Sstevel@tonic-gate         while (path && *path)
2657c478bd9Sstevel@tonic-gate         {
2667c478bd9Sstevel@tonic-gate                 if (patheq(path, "/usr/ucb"))
2677c478bd9Sstevel@tonic-gate                 {
2687c478bd9Sstevel@tonic-gate                         ucb_builtins++;
2697c478bd9Sstevel@tonic-gate                         break;
2707c478bd9Sstevel@tonic-gate                 }
2717c478bd9Sstevel@tonic-gate                 else if (patheq(path, "/usr/bin"))
2727c478bd9Sstevel@tonic-gate                         break;
2737c478bd9Sstevel@tonic-gate                 else if (patheq(path, "/bin"))
2747c478bd9Sstevel@tonic-gate                         break;
2757c478bd9Sstevel@tonic-gate                 else if (patheq(path, "/usr/5bin"))
2767c478bd9Sstevel@tonic-gate                         break;
2777c478bd9Sstevel@tonic-gate                 path = nextpath(path);
2787c478bd9Sstevel@tonic-gate         }
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static int
patheq(unsigned char * component,char * dir)282965005c8Schin patheq(unsigned char *component, char *dir)
2837c478bd9Sstevel@tonic-gate {
284965005c8Schin 	unsigned char   c;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate         for (;;)
2877c478bd9Sstevel@tonic-gate         {
2887c478bd9Sstevel@tonic-gate                 c = *component++;
2897c478bd9Sstevel@tonic-gate                 if (c == COLON)
2907c478bd9Sstevel@tonic-gate                         c = '\0';       /* end of component of path */
291965005c8Schin 		if (c != *dir++)
292965005c8Schin 			return (0);
2937c478bd9Sstevel@tonic-gate                 if (c == '\0')
2947c478bd9Sstevel@tonic-gate                         return(1);
2957c478bd9Sstevel@tonic-gate         }
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
298965005c8Schin int
readvar(unsigned char ** names)299965005c8Schin readvar(unsigned char **names)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	struct fileblk	fb;
302965005c8Schin 	struct fileblk *f = &fb;
3037c478bd9Sstevel@tonic-gate 	unsigned char	c[MULTI_BYTE_MAX+1];
304965005c8Schin 	int	rc = 0;
3057c478bd9Sstevel@tonic-gate 	struct namnod *n = lookup(*names++);	/* done now to avoid storage mess */
3067c478bd9Sstevel@tonic-gate 	unsigned char	*rel = (unsigned char *)relstak();
3077c478bd9Sstevel@tonic-gate 	unsigned char *oldstak;
308965005c8Schin 	unsigned char *pc, *rest;
3097c478bd9Sstevel@tonic-gate 	int		d;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	push(f);
3127c478bd9Sstevel@tonic-gate 	initf(dup(0));
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * If stdin is a pipe then this lseek(2) will fail with ESPIPE, so
3167c478bd9Sstevel@tonic-gate 	 * the read buffer size is set to 1 because we will not be able
3177c478bd9Sstevel@tonic-gate 	 * lseek(2) back towards the beginning of the file, so we have
3187c478bd9Sstevel@tonic-gate 	 * to read a byte at a time instead
3197c478bd9Sstevel@tonic-gate 	 *
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	if (lseek(0, (off_t)0, SEEK_CUR) == -1)
3227c478bd9Sstevel@tonic-gate 		f->fsiz = 1;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * If stdin is a socket then this isastream(3C) will return 1, so
3267c478bd9Sstevel@tonic-gate 	 * the read buffer size is set to 1 because we will not be able
3277c478bd9Sstevel@tonic-gate 	 * lseek(2) back towards the beginning of the file, so we have
3287c478bd9Sstevel@tonic-gate 	 * to read a byte at a time instead
3297c478bd9Sstevel@tonic-gate 	 *
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	if (isastream(0) == 1)
3327c478bd9Sstevel@tonic-gate 		f->fsiz = 1;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * strip leading IFS characters
3367c478bd9Sstevel@tonic-gate 	 */
337*55fea89dSDan Cross 	for (;;)
3387c478bd9Sstevel@tonic-gate 	{
3397c478bd9Sstevel@tonic-gate 		d = nextwc();
3407c478bd9Sstevel@tonic-gate 		if(eolchar(d))
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		rest = readw(d);
3437c478bd9Sstevel@tonic-gate 		pc = c;
3447c478bd9Sstevel@tonic-gate 		while(*pc++ = *rest++);
3457c478bd9Sstevel@tonic-gate 		if(!anys(c, ifsnod.namval))
3467c478bd9Sstevel@tonic-gate 			break;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	oldstak = curstak();
3507c478bd9Sstevel@tonic-gate 	for (;;)
3517c478bd9Sstevel@tonic-gate 	{
3527c478bd9Sstevel@tonic-gate 		if ((*names && anys(c, ifsnod.namval)) || eolchar(d))
3537c478bd9Sstevel@tonic-gate 		{
3547c478bd9Sstevel@tonic-gate 			if (staktop >= brkend)
3557c478bd9Sstevel@tonic-gate 				growstak(staktop);
3567c478bd9Sstevel@tonic-gate 			zerostak();
3577c478bd9Sstevel@tonic-gate 			assign(n, absstak(rel));
3587c478bd9Sstevel@tonic-gate 			setstak(rel);
3597c478bd9Sstevel@tonic-gate 			if (*names)
3607c478bd9Sstevel@tonic-gate 				n = lookup(*names++);
3617c478bd9Sstevel@tonic-gate 			else
3627c478bd9Sstevel@tonic-gate 				n = 0;
3637c478bd9Sstevel@tonic-gate 			if (eolchar(d))
3647c478bd9Sstevel@tonic-gate 			{
3657c478bd9Sstevel@tonic-gate 				break;
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 			else		/* strip imbedded IFS characters */
3687c478bd9Sstevel@tonic-gate 				while(1) {
3697c478bd9Sstevel@tonic-gate 					d = nextwc();
3707c478bd9Sstevel@tonic-gate 					if(eolchar(d))
3717c478bd9Sstevel@tonic-gate 						break;
3727c478bd9Sstevel@tonic-gate 					rest = readw(d);
3737c478bd9Sstevel@tonic-gate 					pc = c;
3747c478bd9Sstevel@tonic-gate 					while(*pc++ = *rest++);
3757c478bd9Sstevel@tonic-gate 					if(!anys(c, ifsnod.namval))
3767c478bd9Sstevel@tonic-gate 						break;
3777c478bd9Sstevel@tonic-gate 				}
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 		else
3807c478bd9Sstevel@tonic-gate 		{
3817c478bd9Sstevel@tonic-gate 			if(d == '\\') {
3827c478bd9Sstevel@tonic-gate 				d = readwc();
3837c478bd9Sstevel@tonic-gate 				rest = readw(d);
3847c478bd9Sstevel@tonic-gate 				while(d = *rest++) {
3857c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
3867c478bd9Sstevel@tonic-gate 						growstak(staktop);
3877c478bd9Sstevel@tonic-gate 					pushstak(d);
3887c478bd9Sstevel@tonic-gate 				}
3897c478bd9Sstevel@tonic-gate 				oldstak = staktop;
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 			else
3927c478bd9Sstevel@tonic-gate 			{
3937c478bd9Sstevel@tonic-gate 				pc = c;
3947c478bd9Sstevel@tonic-gate 				while(d = *pc++) {
3957c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
396*55fea89dSDan Cross 						growstak(staktop);
3977c478bd9Sstevel@tonic-gate 					pushstak(d);
3987c478bd9Sstevel@tonic-gate 				}
3997c478bd9Sstevel@tonic-gate 				if(!anys(c, ifsnod.namval))
4007c478bd9Sstevel@tonic-gate 					oldstak = staktop;
4017c478bd9Sstevel@tonic-gate 			}
4027c478bd9Sstevel@tonic-gate 			d = nextwc();
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 			if (eolchar(d))
4057c478bd9Sstevel@tonic-gate 				staktop = oldstak;
406*55fea89dSDan Cross 			else
4077c478bd9Sstevel@tonic-gate 			{
4087c478bd9Sstevel@tonic-gate 				rest = readw(d);
4097c478bd9Sstevel@tonic-gate 				pc = c;
4107c478bd9Sstevel@tonic-gate 				while(*pc++ = *rest++);
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	while (n)
4157c478bd9Sstevel@tonic-gate 	{
416965005c8Schin 		assign(n, (unsigned char *)nullstr);
4177c478bd9Sstevel@tonic-gate 		if (*names)
4187c478bd9Sstevel@tonic-gate 			n = lookup(*names++);
4197c478bd9Sstevel@tonic-gate 		else
4207c478bd9Sstevel@tonic-gate 			n = 0;
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (eof)
4247c478bd9Sstevel@tonic-gate 		rc = 1;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (isastream(0) != 1)
4277c478bd9Sstevel@tonic-gate 		/*
4287c478bd9Sstevel@tonic-gate 		 * If we are reading on a stream do not attempt to
4297c478bd9Sstevel@tonic-gate 		 * lseek(2) back towards the start because this is
4307c478bd9Sstevel@tonic-gate 		 * logically meaningless, but there is nothing in
4317c478bd9Sstevel@tonic-gate 		 * the standards to pervent the stream implementation
4327c478bd9Sstevel@tonic-gate 		 * from attempting it and breaking our code here
4337c478bd9Sstevel@tonic-gate 		 *
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		lseek(0, (off_t)(f->nxtoff - f->endoff), SEEK_CUR);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	pop();
4387c478bd9Sstevel@tonic-gate 	return(rc);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
441965005c8Schin void
assnum(unsigned char ** p,long i)442965005c8Schin assnum(unsigned char **p, long i)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int j = ltos(i);
4457c478bd9Sstevel@tonic-gate 	replace(p, &numbuf[j]);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate unsigned char *
make(v)4497c478bd9Sstevel@tonic-gate make(v)
4507c478bd9Sstevel@tonic-gate unsigned char	*v;
4517c478bd9Sstevel@tonic-gate {
452965005c8Schin 	unsigned char	*p;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (v)
4557c478bd9Sstevel@tonic-gate 	{
4567c478bd9Sstevel@tonic-gate 		movstr(v, p = (unsigned char *)alloc(length(v)));
4577c478bd9Sstevel@tonic-gate 		return(p);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	else
4607c478bd9Sstevel@tonic-gate 		return(0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate struct namnod *
lookup(unsigned char * nam)465965005c8Schin lookup(unsigned char *nam)
4667c478bd9Sstevel@tonic-gate {
467965005c8Schin 	struct namnod *nscan = namep;
468965005c8Schin 	struct namnod **prev;
4697c478bd9Sstevel@tonic-gate 	int		LR;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (!chkid(nam))
4727c478bd9Sstevel@tonic-gate 		failed(nam, notid);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	while (nscan)
4757c478bd9Sstevel@tonic-gate 	{
4767c478bd9Sstevel@tonic-gate 		if ((LR = cf(nam, nscan->namid)) == 0)
4777c478bd9Sstevel@tonic-gate 			return(nscan);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		else if (LR < 0)
4807c478bd9Sstevel@tonic-gate 			prev = &(nscan->namlft);
4817c478bd9Sstevel@tonic-gate 		else
4827c478bd9Sstevel@tonic-gate 			prev = &(nscan->namrgt);
4837c478bd9Sstevel@tonic-gate 		nscan = *prev;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * add name node
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	nscan = (struct namnod *)alloc(sizeof *nscan);
4897c478bd9Sstevel@tonic-gate 	nscan->namlft = nscan->namrgt = (struct namnod *)NIL;
4907c478bd9Sstevel@tonic-gate 	nscan->namid = make(nam);
4917c478bd9Sstevel@tonic-gate 	nscan->namval = 0;
4927c478bd9Sstevel@tonic-gate 	nscan->namflg = N_DEFAULT;
4937c478bd9Sstevel@tonic-gate 	nscan->namenv = 0;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	return(*prev = nscan);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate BOOL
chkid(nam)4997c478bd9Sstevel@tonic-gate chkid(nam)
5007c478bd9Sstevel@tonic-gate unsigned char	*nam;
5017c478bd9Sstevel@tonic-gate {
502965005c8Schin 	unsigned char *cp = nam;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (!letter(*cp))
5057c478bd9Sstevel@tonic-gate 		return(FALSE);
5067c478bd9Sstevel@tonic-gate 	else
5077c478bd9Sstevel@tonic-gate 	{
5087c478bd9Sstevel@tonic-gate 		while (*++cp)
5097c478bd9Sstevel@tonic-gate 		{
5107c478bd9Sstevel@tonic-gate 			if (!alphanum(*cp))
5117c478bd9Sstevel@tonic-gate 				return(FALSE);
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 	return(TRUE);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
517965005c8Schin static void (*namfn)();
518965005c8Schin 
519965005c8Schin void
namscan(void (* fn)())520965005c8Schin namscan(void (*fn)())
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	namfn = fn;
5237c478bd9Sstevel@tonic-gate 	namwalk(namep);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static void
namwalk(struct namnod * np)527965005c8Schin namwalk(struct namnod *np)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	if (np)
5307c478bd9Sstevel@tonic-gate 	{
5317c478bd9Sstevel@tonic-gate 		namwalk(np->namlft);
5327c478bd9Sstevel@tonic-gate 		(*namfn)(np);
5337c478bd9Sstevel@tonic-gate 		namwalk(np->namrgt);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
537965005c8Schin void
printnam(struct namnod * n)538965005c8Schin printnam(struct namnod *n)
5397c478bd9Sstevel@tonic-gate {
540965005c8Schin 	unsigned char	*s;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	sigchk();
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (n->namflg & N_FUNCTN)
5457c478bd9Sstevel@tonic-gate 	{
546d6b882a9SNobutomo Nakano 		struct fndnod *f = fndptr(n->namenv);
547d6b882a9SNobutomo Nakano 
5487c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5497c478bd9Sstevel@tonic-gate 		prs_buff("(){\n");
550d6b882a9SNobutomo Nakano 		if (f != NULL)
551d6b882a9SNobutomo Nakano 			prf(f->fndval);
5527c478bd9Sstevel@tonic-gate 		prs_buff("\n}\n");
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 	else if (s = n->namval)
5557c478bd9Sstevel@tonic-gate 	{
5567c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5577c478bd9Sstevel@tonic-gate 		prc_buff('=');
5587c478bd9Sstevel@tonic-gate 		prs_buff(s);
5597c478bd9Sstevel@tonic-gate 		prc_buff(NL);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static int namec;
5647c478bd9Sstevel@tonic-gate 
565965005c8Schin void
printro(struct namnod * n)566965005c8Schin printro(struct namnod *n)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	if (n->namflg & N_RDONLY)
5697c478bd9Sstevel@tonic-gate 	{
57039e7390aSna 		prs_buff(_gettext(readonly));
5717c478bd9Sstevel@tonic-gate 		prc_buff(SPACE);
5727c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5737c478bd9Sstevel@tonic-gate 		prc_buff(NL);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
577965005c8Schin void
printexp(struct namnod * n)578965005c8Schin printexp(struct namnod *n)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	if (n->namflg & N_EXPORT)
5817c478bd9Sstevel@tonic-gate 	{
58239e7390aSna 		prs_buff(_gettext(export));
5837c478bd9Sstevel@tonic-gate 		prc_buff(SPACE);
5847c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5857c478bd9Sstevel@tonic-gate 		prc_buff(NL);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
589965005c8Schin void
setup_env(void)590965005c8Schin setup_env(void)
5917c478bd9Sstevel@tonic-gate {
592965005c8Schin 	unsigned char **e = environ;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	while (*e)
5957c478bd9Sstevel@tonic-gate 		setname(*e++, N_ENVNAM);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate static unsigned char **argnam;
6007c478bd9Sstevel@tonic-gate 
601965005c8Schin static void
countnam(struct namnod * n)602965005c8Schin countnam(struct namnod *n)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	if (n->namval)
6057c478bd9Sstevel@tonic-gate 		namec++;
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
608965005c8Schin static void
pushnam(struct namnod * n)609965005c8Schin pushnam(struct namnod *n)
6107c478bd9Sstevel@tonic-gate {
611965005c8Schin 	int 	flg = n->namflg;
612965005c8Schin 	unsigned char	*p;
613965005c8Schin 	unsigned char	*namval;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (((flg & N_ENVCHG) && (flg & N_EXPORT)) || (flg & N_FUNCTN))
6167c478bd9Sstevel@tonic-gate 		namval = n->namval;
6177c478bd9Sstevel@tonic-gate 	else {
6187c478bd9Sstevel@tonic-gate 		/* Discard Local variable in child process */
6197c478bd9Sstevel@tonic-gate 		if (!(flg & ~N_ENVCHG)) {
6207c478bd9Sstevel@tonic-gate 			n->namflg = 0;
6217c478bd9Sstevel@tonic-gate 			n->namenv = 0;
6227c478bd9Sstevel@tonic-gate 			if (n->namval) {
6237c478bd9Sstevel@tonic-gate 				/* Release for re-use */
6247c478bd9Sstevel@tonic-gate 				free(n->namval);
6257c478bd9Sstevel@tonic-gate 				n->namval = (unsigned char *)NIL;
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		namval = n->namenv;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (namval)
6327c478bd9Sstevel@tonic-gate 	{
6337c478bd9Sstevel@tonic-gate 		p = movstrstak(n->namid, staktop);
6347c478bd9Sstevel@tonic-gate 		p = movstrstak("=", p);
6357c478bd9Sstevel@tonic-gate 		p = movstrstak(namval, p);
6367c478bd9Sstevel@tonic-gate 		*argnam++ = getstak(p + 1 - (unsigned char *)(stakbot));
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate unsigned char **
local_setenv()6417c478bd9Sstevel@tonic-gate local_setenv()
6427c478bd9Sstevel@tonic-gate {
643965005c8Schin 	unsigned char	**er;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	namec = 0;
6467c478bd9Sstevel@tonic-gate 	namscan(countnam);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	argnam = er = (unsigned char **)getstak(namec * BYTESPERWORD + BYTESPERWORD);
6497c478bd9Sstevel@tonic-gate 	namscan(pushnam);
6507c478bd9Sstevel@tonic-gate 	*argnam++ = 0;
6517c478bd9Sstevel@tonic-gate 	return(er);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate struct namnod *
findnam(nam)6557c478bd9Sstevel@tonic-gate findnam(nam)
656965005c8Schin 	unsigned char	*nam;
6577c478bd9Sstevel@tonic-gate {
658965005c8Schin 	struct namnod	*nscan = namep;
6597c478bd9Sstevel@tonic-gate 	int		LR;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (!chkid(nam))
6627c478bd9Sstevel@tonic-gate 		return(0);
6637c478bd9Sstevel@tonic-gate 	while (nscan)
6647c478bd9Sstevel@tonic-gate 	{
6657c478bd9Sstevel@tonic-gate 		if ((LR = cf(nam, nscan->namid)) == 0)
6667c478bd9Sstevel@tonic-gate 			return(nscan);
6677c478bd9Sstevel@tonic-gate 		else if (LR < 0)
6687c478bd9Sstevel@tonic-gate 			nscan = nscan->namlft;
6697c478bd9Sstevel@tonic-gate 		else
6707c478bd9Sstevel@tonic-gate 			nscan = nscan->namrgt;
6717c478bd9Sstevel@tonic-gate 	}
672*55fea89dSDan Cross 	return(0);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
675965005c8Schin void
unset_name(unsigned char * name)676965005c8Schin unset_name(unsigned char 	*name)
6777c478bd9Sstevel@tonic-gate {
678965005c8Schin 	struct namnod	*n;
679965005c8Schin 	unsigned char 	call_dolocale = 0;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (n = findnam(name))
6827c478bd9Sstevel@tonic-gate 	{
6837c478bd9Sstevel@tonic-gate 		if (n->namflg & N_RDONLY)
6847c478bd9Sstevel@tonic-gate 			failed(name, wtfailed);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		if (n == &pathnod ||
6877c478bd9Sstevel@tonic-gate 		    n == &ifsnod ||
6887c478bd9Sstevel@tonic-gate 		    n == &ps1nod ||
6897c478bd9Sstevel@tonic-gate 		    n == &ps2nod ||
6907c478bd9Sstevel@tonic-gate 		    n == &mchknod)
6917c478bd9Sstevel@tonic-gate 		{
6927c478bd9Sstevel@tonic-gate 			failed(name, badunset);
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate #ifndef RES
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		if ((flags & rshflg) && eq(name, "SHELL"))
6987c478bd9Sstevel@tonic-gate 			failed(name, restricted);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate #endif
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if (n->namflg & N_FUNCTN)
7037c478bd9Sstevel@tonic-gate 		{
7047c478bd9Sstevel@tonic-gate 			func_unhash(name);
7057c478bd9Sstevel@tonic-gate 			freefunc(n);
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		else
7087c478bd9Sstevel@tonic-gate 		{
7097c478bd9Sstevel@tonic-gate 			call_dolocale++;
7107c478bd9Sstevel@tonic-gate 			free(n->namval);
7117c478bd9Sstevel@tonic-gate 			free(n->namenv);
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		n->namval = n->namenv = 0;
7157c478bd9Sstevel@tonic-gate 		n->namflg = N_DEFAULT;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		if (call_dolocale)
7187c478bd9Sstevel@tonic-gate 			dolocale(name);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		if (flags & prompt)
7217c478bd9Sstevel@tonic-gate 		{
7227c478bd9Sstevel@tonic-gate 			if (n == &mailpnod)
7237c478bd9Sstevel@tonic-gate 				setmail(mailnod.namval);
7247c478bd9Sstevel@tonic-gate 			else if (n == &mailnod && mailpnod.namflg == N_DEFAULT)
7257c478bd9Sstevel@tonic-gate 				setmail(0);
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate  * The environment variables which affect locale.
7327c478bd9Sstevel@tonic-gate  * Note: if all names in this list do not begin with 'L',
7337c478bd9Sstevel@tonic-gate  * you MUST modify dolocale().  Also, be sure that the
7347c478bd9Sstevel@tonic-gate  * fake_env has the same number of elements as localevar.
7357c478bd9Sstevel@tonic-gate  */
7367c478bd9Sstevel@tonic-gate static char *localevar[] = {
7377c478bd9Sstevel@tonic-gate 	"LC_ALL",
7387c478bd9Sstevel@tonic-gate 	"LC_CTYPE",
7397c478bd9Sstevel@tonic-gate 	"LC_MESSAGES",
7407c478bd9Sstevel@tonic-gate 	"LANG",
7417c478bd9Sstevel@tonic-gate 	0
7427c478bd9Sstevel@tonic-gate };
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate static char *fake_env[] = {
7457c478bd9Sstevel@tonic-gate 	0,
7467c478bd9Sstevel@tonic-gate 	0,
7477c478bd9Sstevel@tonic-gate 	0,
7487c478bd9Sstevel@tonic-gate 	0,
7497c478bd9Sstevel@tonic-gate 	0
7507c478bd9Sstevel@tonic-gate };
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * If name is one of several special variables which affect the locale,
7547c478bd9Sstevel@tonic-gate  * do a setlocale().
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate static void
dolocale(nm)7577c478bd9Sstevel@tonic-gate dolocale(nm)
7587c478bd9Sstevel@tonic-gate 	char *nm;
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	char **real_env;
7617c478bd9Sstevel@tonic-gate 	struct namnod *n;
7627c478bd9Sstevel@tonic-gate 	int lv, fe;
7637c478bd9Sstevel@tonic-gate 	int i;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/*
766*55fea89dSDan Cross 	 * Take advantage of fact that names of these vars all start
7677c478bd9Sstevel@tonic-gate 	 * with 'L' to avoid unnecessary work.
7687c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
7697c478bd9Sstevel@tonic-gate 	 */
7707c478bd9Sstevel@tonic-gate 	if ((*nm != 'L') || !localedir_exists ||
7717c478bd9Sstevel@tonic-gate 	    (!(eq(nm, "LC_ALL") || eq(nm, "LC_CTYPE") ||
7727c478bd9Sstevel@tonic-gate 	    eq(nm, "LANG") || eq(nm, "LC_MESSAGES"))))
7737c478bd9Sstevel@tonic-gate 		return;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * setlocale() has all the smarts built into it, but
7777c478bd9Sstevel@tonic-gate 	 * it works by examining the environment.  Unfortunately,
7787c478bd9Sstevel@tonic-gate 	 * when you set an environment variable, the shell does
779*55fea89dSDan Cross 	 * not modify its own environment; it just remembers that the
780*55fea89dSDan Cross 	 * variable needs to be exported to any children.  We hack around
781*55fea89dSDan Cross 	 * this by consing up a fake environment for the use of setlocale()
7827c478bd9Sstevel@tonic-gate 	 * and substituting it for the real env before calling setlocale().
7837c478bd9Sstevel@tonic-gate 	 */
784*55fea89dSDan Cross 
7857c478bd9Sstevel@tonic-gate 	/*
7867c478bd9Sstevel@tonic-gate 	 * Build the fake environment.
7877c478bd9Sstevel@tonic-gate 	 * Look up the value of each of the special environment
7887c478bd9Sstevel@tonic-gate 	 * variables, and put their value into the fake environment,
7897c478bd9Sstevel@tonic-gate 	 * if they are exported.
7907c478bd9Sstevel@tonic-gate 	 */
7917c478bd9Sstevel@tonic-gate 	for (lv = 0, fe = 0; localevar[lv]; lv++) {
7927c478bd9Sstevel@tonic-gate 		if ((n = findnam(localevar[lv]))) {
793965005c8Schin 			char *p, *q;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 			if (!n->namval)
7967c478bd9Sstevel@tonic-gate 				continue;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 			fake_env[fe++] = p = alloc(length(localevar[lv])
7997c478bd9Sstevel@tonic-gate 					       + length(n->namval) + 2);
8007c478bd9Sstevel@tonic-gate 			/* copy name */
8017c478bd9Sstevel@tonic-gate 			q = localevar[lv];
8027c478bd9Sstevel@tonic-gate 			while (*q)
8037c478bd9Sstevel@tonic-gate 				*p++ = *q++;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 			*p++ = '=';
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			/* copy value */
8087c478bd9Sstevel@tonic-gate 			q = (char*)(n->namval);
8097c478bd9Sstevel@tonic-gate 			while (*q)
8107c478bd9Sstevel@tonic-gate 				*p++ = *q++;
8117c478bd9Sstevel@tonic-gate 			*p++ = '\0';
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 	fake_env[fe] = (char *)0;
815*55fea89dSDan Cross 
8167c478bd9Sstevel@tonic-gate 	/*
8177c478bd9Sstevel@tonic-gate 	 * Switch fake env for real and call setlocale().
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	real_env = (char **)environ;
8207c478bd9Sstevel@tonic-gate 	environ = (unsigned char **)fake_env;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if (setlocale(LC_ALL, "") == NULL)
82339e7390aSna 		prs(_gettext(badlocale));
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * Switch back and tear down the fake env.
8277c478bd9Sstevel@tonic-gate 	 */
8287c478bd9Sstevel@tonic-gate 	environ = (unsigned char **)real_env;
8297c478bd9Sstevel@tonic-gate 	for (i = 0; i < fe; i++) {
8307c478bd9Sstevel@tonic-gate 		free(fake_env[i]);
8317c478bd9Sstevel@tonic-gate 		fake_env[i] = (char *)0;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate }
834