xref: /illumos-gate/usr/src/cmd/sh/name.c (revision 39e7390a)
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
5*39e7390aSna  * Common Development and Distribution License (the "License").
6*39e7390aSna  * 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 /*
23*39e7390aSna  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * UNIX shell
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate #include	<stropts.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate extern BOOL	chkid();
397c478bd9Sstevel@tonic-gate extern unsigned char	*simple();
407c478bd9Sstevel@tonic-gate extern int	mailchk;
417c478bd9Sstevel@tonic-gate 
42965005c8Schin static void	setname(unsigned char *, int);
437c478bd9Sstevel@tonic-gate static void	set_builtins_path();
447c478bd9Sstevel@tonic-gate static int	patheq();
45965005c8Schin static void	namwalk(struct namnod *);
46965005c8Schin static void	dolocale();
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate struct namnod ps2nod =
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
517c478bd9Sstevel@tonic-gate 	&acctnod,
527c478bd9Sstevel@tonic-gate 	(unsigned char *)ps2name
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate struct namnod cdpnod =
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
577c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
587c478bd9Sstevel@tonic-gate 	(unsigned char *)cdpname
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate struct namnod pathnod =
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	&mailpnod,
637c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
647c478bd9Sstevel@tonic-gate 	(unsigned char *)pathname
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate struct namnod ifsnod =
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	&homenod,
697c478bd9Sstevel@tonic-gate 	&mailnod,
707c478bd9Sstevel@tonic-gate 	(unsigned char *)ifsname
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate struct namnod ps1nod =
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	&pathnod,
757c478bd9Sstevel@tonic-gate 	&ps2nod,
767c478bd9Sstevel@tonic-gate 	(unsigned char *)ps1name
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate struct namnod homenod =
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	&cdpnod,
817c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
827c478bd9Sstevel@tonic-gate 	(unsigned char *)homename
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate struct namnod mailnod =
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
877c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
887c478bd9Sstevel@tonic-gate 	(unsigned char *)mailname
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate struct namnod mchknod =
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	&ifsnod,
937c478bd9Sstevel@tonic-gate 	&ps1nod,
947c478bd9Sstevel@tonic-gate 	(unsigned char *)mchkname
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate struct namnod acctnod =
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
997c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
1007c478bd9Sstevel@tonic-gate 	(unsigned char *)acctname
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate struct namnod mailpnod =
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
1057c478bd9Sstevel@tonic-gate 	(struct namnod *)NIL,
1067c478bd9Sstevel@tonic-gate 	(unsigned char *)mailpname
1077c478bd9Sstevel@tonic-gate };
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct namnod *namep = &mchknod;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /* ========	variable and string handling	======== */
1137c478bd9Sstevel@tonic-gate 
114965005c8Schin int
115965005c8Schin syslook(unsigned char *w, struct sysnod syswds[], int n)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	int	low;
1187c478bd9Sstevel@tonic-gate 	int	high;
1197c478bd9Sstevel@tonic-gate 	int	mid;
120965005c8Schin 	int	cond;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (w == 0 || *w == 0)
1237c478bd9Sstevel@tonic-gate 		return(0);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	low = 0;
1267c478bd9Sstevel@tonic-gate 	high = n - 1;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	while (low <= high)
1297c478bd9Sstevel@tonic-gate 	{
1307c478bd9Sstevel@tonic-gate 		mid = (low + high) / 2;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if ((cond = cf(w, syswds[mid].sysnam)) < 0)
1337c478bd9Sstevel@tonic-gate 			high = mid - 1;
1347c478bd9Sstevel@tonic-gate 		else if (cond > 0)
1357c478bd9Sstevel@tonic-gate 			low = mid + 1;
1367c478bd9Sstevel@tonic-gate 		else
1377c478bd9Sstevel@tonic-gate 			return(syswds[mid].sysval);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	return(0);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
142965005c8Schin void
143965005c8Schin setlist(struct argnod *arg, int xp)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	if (flags & exportflg)
1467c478bd9Sstevel@tonic-gate 		xp |= N_EXPORT;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	while (arg)
1497c478bd9Sstevel@tonic-gate 	{
150965005c8Schin 		unsigned char *s = mactrim(arg->argval);
1517c478bd9Sstevel@tonic-gate 		setname(s, xp);
1527c478bd9Sstevel@tonic-gate 		arg = arg->argnxt;
1537c478bd9Sstevel@tonic-gate 		if (flags & execpr)
1547c478bd9Sstevel@tonic-gate 		{
1557c478bd9Sstevel@tonic-gate 			prs(s);
1567c478bd9Sstevel@tonic-gate 			if (arg)
1577c478bd9Sstevel@tonic-gate 				blank();
1587c478bd9Sstevel@tonic-gate 			else
1597c478bd9Sstevel@tonic-gate 				newline();
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
164965005c8Schin static void
165965005c8Schin setname(unsigned char *argi, int xp)	/* does parameter assignments */
1667c478bd9Sstevel@tonic-gate {
167965005c8Schin 	unsigned char *argscan = argi;
168965005c8Schin 	struct namnod *n;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (letter(*argscan))
1717c478bd9Sstevel@tonic-gate 	{
1727c478bd9Sstevel@tonic-gate 		while (alphanum(*argscan))
1737c478bd9Sstevel@tonic-gate 			argscan++;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (*argscan == '=')
1767c478bd9Sstevel@tonic-gate 		{
1777c478bd9Sstevel@tonic-gate 			*argscan = 0;	/* make name a cohesive string */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 			n = lookup(argi);
1807c478bd9Sstevel@tonic-gate 			*argscan++ = '=';
1817c478bd9Sstevel@tonic-gate 			attrib(n, xp);
1827c478bd9Sstevel@tonic-gate 			if (xp & N_ENVNAM)
1837c478bd9Sstevel@tonic-gate 			{
1847c478bd9Sstevel@tonic-gate 				n->namenv = n->namval = argscan;
1857c478bd9Sstevel@tonic-gate 				if (n == &pathnod)
1867c478bd9Sstevel@tonic-gate 					set_builtins_path();
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 			else
1897c478bd9Sstevel@tonic-gate 				assign(n, argscan);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 			dolocale(n->namid);
1927c478bd9Sstevel@tonic-gate 			return;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
197965005c8Schin void
198965005c8Schin replace(unsigned char **a, unsigned char *v)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	free(*a);
2017c478bd9Sstevel@tonic-gate 	*a = make(v);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
204965005c8Schin void
205965005c8Schin dfault(struct namnod *n, unsigned char	*v)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	if (n->namval == 0)
2087c478bd9Sstevel@tonic-gate 		assign(n, v);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
211965005c8Schin void
212965005c8Schin assign(struct namnod *n, unsigned char *v)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	if (n->namflg & N_RDONLY)
2157c478bd9Sstevel@tonic-gate 		failed(n->namid, wtfailed);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate #ifndef RES
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	else if (flags & rshflg)
2207c478bd9Sstevel@tonic-gate 	{
2217c478bd9Sstevel@tonic-gate 		if (n == &pathnod || eq(n->namid,"SHELL"))
2227c478bd9Sstevel@tonic-gate 			failed(n->namid, restricted);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate #endif
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	else if (n->namflg & N_FUNCTN)
2277c478bd9Sstevel@tonic-gate 	{
2287c478bd9Sstevel@tonic-gate 		func_unhash(n->namid);
2297c478bd9Sstevel@tonic-gate 		freefunc(n);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		n->namenv = 0;
2327c478bd9Sstevel@tonic-gate 		n->namflg = N_DEFAULT;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (n == &mchknod)
2367c478bd9Sstevel@tonic-gate 	{
2377c478bd9Sstevel@tonic-gate 		mailchk = stoi(v);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	replace(&n->namval, v);
2417c478bd9Sstevel@tonic-gate 	attrib(n, N_ENVCHG);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (n == &pathnod)
2447c478bd9Sstevel@tonic-gate 	{
2457c478bd9Sstevel@tonic-gate 		zaphash();
2467c478bd9Sstevel@tonic-gate 		set_dotpath();
2477c478bd9Sstevel@tonic-gate 		set_builtins_path();
2487c478bd9Sstevel@tonic-gate 		return;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (flags & prompt)
2527c478bd9Sstevel@tonic-gate 	{
2537c478bd9Sstevel@tonic-gate 		if ((n == &mailpnod) || (n == &mailnod && mailpnod.namflg == N_DEFAULT))
2547c478bd9Sstevel@tonic-gate 			setmail(n->namval);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static void
2597c478bd9Sstevel@tonic-gate set_builtins_path()
2607c478bd9Sstevel@tonic-gate {
261965005c8Schin 	unsigned char *path;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate         ucb_builtins = 0;
2647c478bd9Sstevel@tonic-gate         path = getpath("");
2657c478bd9Sstevel@tonic-gate         while (path && *path)
2667c478bd9Sstevel@tonic-gate         {
2677c478bd9Sstevel@tonic-gate                 if (patheq(path, "/usr/ucb"))
2687c478bd9Sstevel@tonic-gate                 {
2697c478bd9Sstevel@tonic-gate                         ucb_builtins++;
2707c478bd9Sstevel@tonic-gate                         break;
2717c478bd9Sstevel@tonic-gate                 }
2727c478bd9Sstevel@tonic-gate                 else if (patheq(path, "/usr/bin"))
2737c478bd9Sstevel@tonic-gate                         break;
2747c478bd9Sstevel@tonic-gate                 else if (patheq(path, "/bin"))
2757c478bd9Sstevel@tonic-gate                         break;
2767c478bd9Sstevel@tonic-gate                 else if (patheq(path, "/usr/5bin"))
2777c478bd9Sstevel@tonic-gate                         break;
2787c478bd9Sstevel@tonic-gate                 path = nextpath(path);
2797c478bd9Sstevel@tonic-gate         }
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static int
283965005c8Schin patheq(unsigned char *component, char *dir)
2847c478bd9Sstevel@tonic-gate {
285965005c8Schin 	unsigned char   c;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate         for (;;)
2887c478bd9Sstevel@tonic-gate         {
2897c478bd9Sstevel@tonic-gate                 c = *component++;
2907c478bd9Sstevel@tonic-gate                 if (c == COLON)
2917c478bd9Sstevel@tonic-gate                         c = '\0';       /* end of component of path */
292965005c8Schin 		if (c != *dir++)
293965005c8Schin 			return (0);
2947c478bd9Sstevel@tonic-gate                 if (c == '\0')
2957c478bd9Sstevel@tonic-gate                         return(1);
2967c478bd9Sstevel@tonic-gate         }
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
299965005c8Schin int
300965005c8Schin readvar(unsigned char **names)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	struct fileblk	fb;
303965005c8Schin 	struct fileblk *f = &fb;
3047c478bd9Sstevel@tonic-gate 	unsigned char	c[MULTI_BYTE_MAX+1];
305965005c8Schin 	int	rc = 0;
3067c478bd9Sstevel@tonic-gate 	struct namnod *n = lookup(*names++);	/* done now to avoid storage mess */
3077c478bd9Sstevel@tonic-gate 	unsigned char	*rel = (unsigned char *)relstak();
3087c478bd9Sstevel@tonic-gate 	unsigned char *oldstak;
309965005c8Schin 	unsigned char *pc, *rest;
3107c478bd9Sstevel@tonic-gate 	int		d;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	push(f);
3137c478bd9Sstevel@tonic-gate 	initf(dup(0));
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * If stdin is a pipe then this lseek(2) will fail with ESPIPE, so
3177c478bd9Sstevel@tonic-gate 	 * the read buffer size is set to 1 because we will not be able
3187c478bd9Sstevel@tonic-gate 	 * lseek(2) back towards the beginning of the file, so we have
3197c478bd9Sstevel@tonic-gate 	 * to read a byte at a time instead
3207c478bd9Sstevel@tonic-gate 	 *
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	if (lseek(0, (off_t)0, SEEK_CUR) == -1)
3237c478bd9Sstevel@tonic-gate 		f->fsiz = 1;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 * If stdin is a socket then this isastream(3C) will return 1, so
3277c478bd9Sstevel@tonic-gate 	 * the read buffer size is set to 1 because we will not be able
3287c478bd9Sstevel@tonic-gate 	 * lseek(2) back towards the beginning of the file, so we have
3297c478bd9Sstevel@tonic-gate 	 * to read a byte at a time instead
3307c478bd9Sstevel@tonic-gate 	 *
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	if (isastream(0) == 1)
3337c478bd9Sstevel@tonic-gate 		f->fsiz = 1;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * strip leading IFS characters
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	for (;;)
3397c478bd9Sstevel@tonic-gate 	{
3407c478bd9Sstevel@tonic-gate 		d = nextwc();
3417c478bd9Sstevel@tonic-gate 		if(eolchar(d))
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 		rest = readw(d);
3447c478bd9Sstevel@tonic-gate 		pc = c;
3457c478bd9Sstevel@tonic-gate 		while(*pc++ = *rest++);
3467c478bd9Sstevel@tonic-gate 		if(!anys(c, ifsnod.namval))
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	oldstak = curstak();
3517c478bd9Sstevel@tonic-gate 	for (;;)
3527c478bd9Sstevel@tonic-gate 	{
3537c478bd9Sstevel@tonic-gate 		if ((*names && anys(c, ifsnod.namval)) || eolchar(d))
3547c478bd9Sstevel@tonic-gate 		{
3557c478bd9Sstevel@tonic-gate 			if (staktop >= brkend)
3567c478bd9Sstevel@tonic-gate 				growstak(staktop);
3577c478bd9Sstevel@tonic-gate 			zerostak();
3587c478bd9Sstevel@tonic-gate 			assign(n, absstak(rel));
3597c478bd9Sstevel@tonic-gate 			setstak(rel);
3607c478bd9Sstevel@tonic-gate 			if (*names)
3617c478bd9Sstevel@tonic-gate 				n = lookup(*names++);
3627c478bd9Sstevel@tonic-gate 			else
3637c478bd9Sstevel@tonic-gate 				n = 0;
3647c478bd9Sstevel@tonic-gate 			if (eolchar(d))
3657c478bd9Sstevel@tonic-gate 			{
3667c478bd9Sstevel@tonic-gate 				break;
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 			else		/* strip imbedded IFS characters */
3697c478bd9Sstevel@tonic-gate 				while(1) {
3707c478bd9Sstevel@tonic-gate 					d = nextwc();
3717c478bd9Sstevel@tonic-gate 					if(eolchar(d))
3727c478bd9Sstevel@tonic-gate 						break;
3737c478bd9Sstevel@tonic-gate 					rest = readw(d);
3747c478bd9Sstevel@tonic-gate 					pc = c;
3757c478bd9Sstevel@tonic-gate 					while(*pc++ = *rest++);
3767c478bd9Sstevel@tonic-gate 					if(!anys(c, ifsnod.namval))
3777c478bd9Sstevel@tonic-gate 						break;
3787c478bd9Sstevel@tonic-gate 				}
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		else
3817c478bd9Sstevel@tonic-gate 		{
3827c478bd9Sstevel@tonic-gate 			if(d == '\\') {
3837c478bd9Sstevel@tonic-gate 				d = readwc();
3847c478bd9Sstevel@tonic-gate 				rest = readw(d);
3857c478bd9Sstevel@tonic-gate 				while(d = *rest++) {
3867c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
3877c478bd9Sstevel@tonic-gate 						growstak(staktop);
3887c478bd9Sstevel@tonic-gate 					pushstak(d);
3897c478bd9Sstevel@tonic-gate 				}
3907c478bd9Sstevel@tonic-gate 				oldstak = staktop;
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			else
3937c478bd9Sstevel@tonic-gate 			{
3947c478bd9Sstevel@tonic-gate 				pc = c;
3957c478bd9Sstevel@tonic-gate 				while(d = *pc++) {
3967c478bd9Sstevel@tonic-gate 					if (staktop >= brkend)
3977c478bd9Sstevel@tonic-gate 						growstak(staktop);
3987c478bd9Sstevel@tonic-gate 					pushstak(d);
3997c478bd9Sstevel@tonic-gate 				}
4007c478bd9Sstevel@tonic-gate 				if(!anys(c, ifsnod.namval))
4017c478bd9Sstevel@tonic-gate 					oldstak = staktop;
4027c478bd9Sstevel@tonic-gate 			}
4037c478bd9Sstevel@tonic-gate 			d = nextwc();
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 			if (eolchar(d))
4067c478bd9Sstevel@tonic-gate 				staktop = oldstak;
4077c478bd9Sstevel@tonic-gate 			else
4087c478bd9Sstevel@tonic-gate 			{
4097c478bd9Sstevel@tonic-gate 				rest = readw(d);
4107c478bd9Sstevel@tonic-gate 				pc = c;
4117c478bd9Sstevel@tonic-gate 				while(*pc++ = *rest++);
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 	while (n)
4167c478bd9Sstevel@tonic-gate 	{
417965005c8Schin 		assign(n, (unsigned char *)nullstr);
4187c478bd9Sstevel@tonic-gate 		if (*names)
4197c478bd9Sstevel@tonic-gate 			n = lookup(*names++);
4207c478bd9Sstevel@tonic-gate 		else
4217c478bd9Sstevel@tonic-gate 			n = 0;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if (eof)
4257c478bd9Sstevel@tonic-gate 		rc = 1;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (isastream(0) != 1)
4287c478bd9Sstevel@tonic-gate 		/*
4297c478bd9Sstevel@tonic-gate 		 * If we are reading on a stream do not attempt to
4307c478bd9Sstevel@tonic-gate 		 * lseek(2) back towards the start because this is
4317c478bd9Sstevel@tonic-gate 		 * logically meaningless, but there is nothing in
4327c478bd9Sstevel@tonic-gate 		 * the standards to pervent the stream implementation
4337c478bd9Sstevel@tonic-gate 		 * from attempting it and breaking our code here
4347c478bd9Sstevel@tonic-gate 		 *
4357c478bd9Sstevel@tonic-gate 		 */
4367c478bd9Sstevel@tonic-gate 		lseek(0, (off_t)(f->nxtoff - f->endoff), SEEK_CUR);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	pop();
4397c478bd9Sstevel@tonic-gate 	return(rc);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
442965005c8Schin void
443965005c8Schin assnum(unsigned char **p, long i)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	int j = ltos(i);
4467c478bd9Sstevel@tonic-gate 	replace(p, &numbuf[j]);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate unsigned char *
4507c478bd9Sstevel@tonic-gate make(v)
4517c478bd9Sstevel@tonic-gate unsigned char	*v;
4527c478bd9Sstevel@tonic-gate {
453965005c8Schin 	unsigned char	*p;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (v)
4567c478bd9Sstevel@tonic-gate 	{
4577c478bd9Sstevel@tonic-gate 		movstr(v, p = (unsigned char *)alloc(length(v)));
4587c478bd9Sstevel@tonic-gate 		return(p);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	else
4617c478bd9Sstevel@tonic-gate 		return(0);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate struct namnod *
466965005c8Schin lookup(unsigned char *nam)
4677c478bd9Sstevel@tonic-gate {
468965005c8Schin 	struct namnod *nscan = namep;
469965005c8Schin 	struct namnod **prev;
4707c478bd9Sstevel@tonic-gate 	int		LR;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (!chkid(nam))
4737c478bd9Sstevel@tonic-gate 		failed(nam, notid);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	while (nscan)
4767c478bd9Sstevel@tonic-gate 	{
4777c478bd9Sstevel@tonic-gate 		if ((LR = cf(nam, nscan->namid)) == 0)
4787c478bd9Sstevel@tonic-gate 			return(nscan);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		else if (LR < 0)
4817c478bd9Sstevel@tonic-gate 			prev = &(nscan->namlft);
4827c478bd9Sstevel@tonic-gate 		else
4837c478bd9Sstevel@tonic-gate 			prev = &(nscan->namrgt);
4847c478bd9Sstevel@tonic-gate 		nscan = *prev;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * add name node
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 	nscan = (struct namnod *)alloc(sizeof *nscan);
4907c478bd9Sstevel@tonic-gate 	nscan->namlft = nscan->namrgt = (struct namnod *)NIL;
4917c478bd9Sstevel@tonic-gate 	nscan->namid = make(nam);
4927c478bd9Sstevel@tonic-gate 	nscan->namval = 0;
4937c478bd9Sstevel@tonic-gate 	nscan->namflg = N_DEFAULT;
4947c478bd9Sstevel@tonic-gate 	nscan->namenv = 0;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	return(*prev = nscan);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate BOOL
5007c478bd9Sstevel@tonic-gate chkid(nam)
5017c478bd9Sstevel@tonic-gate unsigned char	*nam;
5027c478bd9Sstevel@tonic-gate {
503965005c8Schin 	unsigned char *cp = nam;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (!letter(*cp))
5067c478bd9Sstevel@tonic-gate 		return(FALSE);
5077c478bd9Sstevel@tonic-gate 	else
5087c478bd9Sstevel@tonic-gate 	{
5097c478bd9Sstevel@tonic-gate 		while (*++cp)
5107c478bd9Sstevel@tonic-gate 		{
5117c478bd9Sstevel@tonic-gate 			if (!alphanum(*cp))
5127c478bd9Sstevel@tonic-gate 				return(FALSE);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	return(TRUE);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
518965005c8Schin static void (*namfn)();
519965005c8Schin 
520965005c8Schin void
521965005c8Schin namscan(void (*fn)())
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	namfn = fn;
5247c478bd9Sstevel@tonic-gate 	namwalk(namep);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate static void
528965005c8Schin namwalk(struct namnod *np)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	if (np)
5317c478bd9Sstevel@tonic-gate 	{
5327c478bd9Sstevel@tonic-gate 		namwalk(np->namlft);
5337c478bd9Sstevel@tonic-gate 		(*namfn)(np);
5347c478bd9Sstevel@tonic-gate 		namwalk(np->namrgt);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
538965005c8Schin void
539965005c8Schin printnam(struct namnod *n)
5407c478bd9Sstevel@tonic-gate {
541965005c8Schin 	unsigned char	*s;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	sigchk();
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (n->namflg & N_FUNCTN)
5467c478bd9Sstevel@tonic-gate 	{
5477c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5487c478bd9Sstevel@tonic-gate 		prs_buff("(){\n");
5497c478bd9Sstevel@tonic-gate 		prf(n->namenv);
5507c478bd9Sstevel@tonic-gate 		prs_buff("\n}\n");
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	else if (s = n->namval)
5537c478bd9Sstevel@tonic-gate 	{
5547c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5557c478bd9Sstevel@tonic-gate 		prc_buff('=');
5567c478bd9Sstevel@tonic-gate 		prs_buff(s);
5577c478bd9Sstevel@tonic-gate 		prc_buff(NL);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static int namec;
5627c478bd9Sstevel@tonic-gate 
563965005c8Schin void
564965005c8Schin printro(struct namnod *n)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	if (n->namflg & N_RDONLY)
5677c478bd9Sstevel@tonic-gate 	{
568*39e7390aSna 		prs_buff(_gettext(readonly));
5697c478bd9Sstevel@tonic-gate 		prc_buff(SPACE);
5707c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5717c478bd9Sstevel@tonic-gate 		prc_buff(NL);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
575965005c8Schin void
576965005c8Schin printexp(struct namnod *n)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	if (n->namflg & N_EXPORT)
5797c478bd9Sstevel@tonic-gate 	{
580*39e7390aSna 		prs_buff(_gettext(export));
5817c478bd9Sstevel@tonic-gate 		prc_buff(SPACE);
5827c478bd9Sstevel@tonic-gate 		prs_buff(n->namid);
5837c478bd9Sstevel@tonic-gate 		prc_buff(NL);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
587965005c8Schin void
588965005c8Schin setup_env(void)
5897c478bd9Sstevel@tonic-gate {
590965005c8Schin 	unsigned char **e = environ;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	while (*e)
5937c478bd9Sstevel@tonic-gate 		setname(*e++, N_ENVNAM);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static unsigned char **argnam;
5987c478bd9Sstevel@tonic-gate 
599965005c8Schin static void
600965005c8Schin countnam(struct namnod *n)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	if (n->namval)
6037c478bd9Sstevel@tonic-gate 		namec++;
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
606965005c8Schin static void
607965005c8Schin pushnam(struct namnod *n)
6087c478bd9Sstevel@tonic-gate {
609965005c8Schin 	int 	flg = n->namflg;
610965005c8Schin 	unsigned char	*p;
611965005c8Schin 	unsigned char	*namval;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (((flg & N_ENVCHG) && (flg & N_EXPORT)) || (flg & N_FUNCTN))
6147c478bd9Sstevel@tonic-gate 		namval = n->namval;
6157c478bd9Sstevel@tonic-gate 	else {
6167c478bd9Sstevel@tonic-gate 		/* Discard Local variable in child process */
6177c478bd9Sstevel@tonic-gate 		if (!(flg & ~N_ENVCHG)) {
6187c478bd9Sstevel@tonic-gate 			n->namflg = 0;
6197c478bd9Sstevel@tonic-gate 			n->namenv = 0;
6207c478bd9Sstevel@tonic-gate 			if (n->namval) {
6217c478bd9Sstevel@tonic-gate 				/* Release for re-use */
6227c478bd9Sstevel@tonic-gate 				free(n->namval);
6237c478bd9Sstevel@tonic-gate 				n->namval = (unsigned char *)NIL;
6247c478bd9Sstevel@tonic-gate 			}
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		namval = n->namenv;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (namval)
6307c478bd9Sstevel@tonic-gate 	{
6317c478bd9Sstevel@tonic-gate 		p = movstrstak(n->namid, staktop);
6327c478bd9Sstevel@tonic-gate 		p = movstrstak("=", p);
6337c478bd9Sstevel@tonic-gate 		p = movstrstak(namval, p);
6347c478bd9Sstevel@tonic-gate 		*argnam++ = getstak(p + 1 - (unsigned char *)(stakbot));
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate unsigned char **
6397c478bd9Sstevel@tonic-gate local_setenv()
6407c478bd9Sstevel@tonic-gate {
641965005c8Schin 	unsigned char	**er;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	namec = 0;
6447c478bd9Sstevel@tonic-gate 	namscan(countnam);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	argnam = er = (unsigned char **)getstak(namec * BYTESPERWORD + BYTESPERWORD);
6477c478bd9Sstevel@tonic-gate 	namscan(pushnam);
6487c478bd9Sstevel@tonic-gate 	*argnam++ = 0;
6497c478bd9Sstevel@tonic-gate 	return(er);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate struct namnod *
6537c478bd9Sstevel@tonic-gate findnam(nam)
654965005c8Schin 	unsigned char	*nam;
6557c478bd9Sstevel@tonic-gate {
656965005c8Schin 	struct namnod	*nscan = namep;
6577c478bd9Sstevel@tonic-gate 	int		LR;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (!chkid(nam))
6607c478bd9Sstevel@tonic-gate 		return(0);
6617c478bd9Sstevel@tonic-gate 	while (nscan)
6627c478bd9Sstevel@tonic-gate 	{
6637c478bd9Sstevel@tonic-gate 		if ((LR = cf(nam, nscan->namid)) == 0)
6647c478bd9Sstevel@tonic-gate 			return(nscan);
6657c478bd9Sstevel@tonic-gate 		else if (LR < 0)
6667c478bd9Sstevel@tonic-gate 			nscan = nscan->namlft;
6677c478bd9Sstevel@tonic-gate 		else
6687c478bd9Sstevel@tonic-gate 			nscan = nscan->namrgt;
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	return(0);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
673965005c8Schin void
674965005c8Schin unset_name(unsigned char 	*name)
6757c478bd9Sstevel@tonic-gate {
676965005c8Schin 	struct namnod	*n;
677965005c8Schin 	unsigned char 	call_dolocale = 0;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (n = findnam(name))
6807c478bd9Sstevel@tonic-gate 	{
6817c478bd9Sstevel@tonic-gate 		if (n->namflg & N_RDONLY)
6827c478bd9Sstevel@tonic-gate 			failed(name, wtfailed);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		if (n == &pathnod ||
6857c478bd9Sstevel@tonic-gate 		    n == &ifsnod ||
6867c478bd9Sstevel@tonic-gate 		    n == &ps1nod ||
6877c478bd9Sstevel@tonic-gate 		    n == &ps2nod ||
6887c478bd9Sstevel@tonic-gate 		    n == &mchknod)
6897c478bd9Sstevel@tonic-gate 		{
6907c478bd9Sstevel@tonic-gate 			failed(name, badunset);
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate #ifndef RES
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		if ((flags & rshflg) && eq(name, "SHELL"))
6967c478bd9Sstevel@tonic-gate 			failed(name, restricted);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate #endif
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		if (n->namflg & N_FUNCTN)
7017c478bd9Sstevel@tonic-gate 		{
7027c478bd9Sstevel@tonic-gate 			func_unhash(name);
7037c478bd9Sstevel@tonic-gate 			freefunc(n);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 		else
7067c478bd9Sstevel@tonic-gate 		{
7077c478bd9Sstevel@tonic-gate 			call_dolocale++;
7087c478bd9Sstevel@tonic-gate 			free(n->namval);
7097c478bd9Sstevel@tonic-gate 			free(n->namenv);
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		n->namval = n->namenv = 0;
7137c478bd9Sstevel@tonic-gate 		n->namflg = N_DEFAULT;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if (call_dolocale)
7167c478bd9Sstevel@tonic-gate 			dolocale(name);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		if (flags & prompt)
7197c478bd9Sstevel@tonic-gate 		{
7207c478bd9Sstevel@tonic-gate 			if (n == &mailpnod)
7217c478bd9Sstevel@tonic-gate 				setmail(mailnod.namval);
7227c478bd9Sstevel@tonic-gate 			else if (n == &mailnod && mailpnod.namflg == N_DEFAULT)
7237c478bd9Sstevel@tonic-gate 				setmail(0);
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate /*
7297c478bd9Sstevel@tonic-gate  * The environment variables which affect locale.
7307c478bd9Sstevel@tonic-gate  * Note: if all names in this list do not begin with 'L',
7317c478bd9Sstevel@tonic-gate  * you MUST modify dolocale().  Also, be sure that the
7327c478bd9Sstevel@tonic-gate  * fake_env has the same number of elements as localevar.
7337c478bd9Sstevel@tonic-gate  */
7347c478bd9Sstevel@tonic-gate static char *localevar[] = {
7357c478bd9Sstevel@tonic-gate 	"LC_ALL",
7367c478bd9Sstevel@tonic-gate 	"LC_CTYPE",
7377c478bd9Sstevel@tonic-gate 	"LC_MESSAGES",
7387c478bd9Sstevel@tonic-gate 	"LANG",
7397c478bd9Sstevel@tonic-gate 	0
7407c478bd9Sstevel@tonic-gate };
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate static char *fake_env[] = {
7437c478bd9Sstevel@tonic-gate 	0,
7447c478bd9Sstevel@tonic-gate 	0,
7457c478bd9Sstevel@tonic-gate 	0,
7467c478bd9Sstevel@tonic-gate 	0,
7477c478bd9Sstevel@tonic-gate 	0
7487c478bd9Sstevel@tonic-gate };
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate /*
7517c478bd9Sstevel@tonic-gate  * If name is one of several special variables which affect the locale,
7527c478bd9Sstevel@tonic-gate  * do a setlocale().
7537c478bd9Sstevel@tonic-gate  */
7547c478bd9Sstevel@tonic-gate static void
7557c478bd9Sstevel@tonic-gate dolocale(nm)
7567c478bd9Sstevel@tonic-gate 	char *nm;
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	char **real_env;
7597c478bd9Sstevel@tonic-gate 	struct namnod *n;
7607c478bd9Sstevel@tonic-gate 	int lv, fe;
7617c478bd9Sstevel@tonic-gate 	int i;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * Take advantage of fact that names of these vars all start
7657c478bd9Sstevel@tonic-gate 	 * with 'L' to avoid unnecessary work.
7667c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	if ((*nm != 'L') || !localedir_exists ||
7697c478bd9Sstevel@tonic-gate 	    (!(eq(nm, "LC_ALL") || eq(nm, "LC_CTYPE") ||
7707c478bd9Sstevel@tonic-gate 	    eq(nm, "LANG") || eq(nm, "LC_MESSAGES"))))
7717c478bd9Sstevel@tonic-gate 		return;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * setlocale() has all the smarts built into it, but
7757c478bd9Sstevel@tonic-gate 	 * it works by examining the environment.  Unfortunately,
7767c478bd9Sstevel@tonic-gate 	 * when you set an environment variable, the shell does
7777c478bd9Sstevel@tonic-gate 	 * not modify its own environment; it just remembers that the
7787c478bd9Sstevel@tonic-gate 	 * variable needs to be exported to any children.  We hack around
7797c478bd9Sstevel@tonic-gate 	 * this by consing up a fake environment for the use of setlocale()
7807c478bd9Sstevel@tonic-gate 	 * and substituting it for the real env before calling setlocale().
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * Build the fake environment.
7857c478bd9Sstevel@tonic-gate 	 * Look up the value of each of the special environment
7867c478bd9Sstevel@tonic-gate 	 * variables, and put their value into the fake environment,
7877c478bd9Sstevel@tonic-gate 	 * if they are exported.
7887c478bd9Sstevel@tonic-gate 	 */
7897c478bd9Sstevel@tonic-gate 	for (lv = 0, fe = 0; localevar[lv]; lv++) {
7907c478bd9Sstevel@tonic-gate 		if ((n = findnam(localevar[lv]))) {
791965005c8Schin 			char *p, *q;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 			if (!n->namval)
7947c478bd9Sstevel@tonic-gate 				continue;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 			fake_env[fe++] = p = alloc(length(localevar[lv])
7977c478bd9Sstevel@tonic-gate 					       + length(n->namval) + 2);
7987c478bd9Sstevel@tonic-gate 			/* copy name */
7997c478bd9Sstevel@tonic-gate 			q = localevar[lv];
8007c478bd9Sstevel@tonic-gate 			while (*q)
8017c478bd9Sstevel@tonic-gate 				*p++ = *q++;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 			*p++ = '=';
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 			/* copy value */
8067c478bd9Sstevel@tonic-gate 			q = (char*)(n->namval);
8077c478bd9Sstevel@tonic-gate 			while (*q)
8087c478bd9Sstevel@tonic-gate 				*p++ = *q++;
8097c478bd9Sstevel@tonic-gate 			*p++ = '\0';
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	fake_env[fe] = (char *)0;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * Switch fake env for real and call setlocale().
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	real_env = (char **)environ;
8187c478bd9Sstevel@tonic-gate 	environ = (unsigned char **)fake_env;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (setlocale(LC_ALL, "") == NULL)
821*39e7390aSna 		prs(_gettext(badlocale));
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * Switch back and tear down the fake env.
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 	environ = (unsigned char **)real_env;
8277c478bd9Sstevel@tonic-gate 	for (i = 0; i < fe; i++) {
8287c478bd9Sstevel@tonic-gate 		free(fake_env[i]);
8297c478bd9Sstevel@tonic-gate 		fake_env[i] = (char *)0;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate }
832