var.c revision 208755
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/var.c 208755 2010-06-02 19:16:58Z jilles $");
40
41#include <unistd.h>
42#include <stdlib.h>
43#include <paths.h>
44
45/*
46 * Shell variables.
47 */
48
49#include <locale.h>
50
51#include "shell.h"
52#include "output.h"
53#include "expand.h"
54#include "nodes.h"	/* for other headers */
55#include "eval.h"	/* defines cmdenviron */
56#include "exec.h"
57#include "syntax.h"
58#include "options.h"
59#include "mail.h"
60#include "var.h"
61#include "memalloc.h"
62#include "error.h"
63#include "mystring.h"
64#include "parser.h"
65#ifndef NO_HISTORY
66#include "myhistedit.h"
67#endif
68
69
70#define VTABSIZE 39
71
72
73struct varinit {
74	struct var *var;
75	int flags;
76	const char *text;
77	void (*func)(const char *);
78};
79
80
81#ifndef NO_HISTORY
82struct var vhistsize;
83struct var vterm;
84#endif
85struct var vifs;
86struct var vmail;
87struct var vmpath;
88struct var vpath;
89struct var vppid;
90struct var vps1;
91struct var vps2;
92struct var vps4;
93struct var vvers;
94STATIC struct var voptind;
95
96STATIC const struct varinit varinit[] = {
97#ifndef NO_HISTORY
98	{ &vhistsize,	VUNSET,				"HISTSIZE=",
99	  sethistsize },
100#endif
101	{ &vifs,	0,				"IFS= \t\n",
102	  NULL },
103	{ &vmail,	VUNSET,				"MAIL=",
104	  NULL },
105	{ &vmpath,	VUNSET,				"MAILPATH=",
106	  NULL },
107	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
108	  changepath },
109	{ &vppid,	VUNSET,				"PPID=",
110	  NULL },
111	/*
112	 * vps1 depends on uid
113	 */
114	{ &vps2,	0,				"PS2=> ",
115	  NULL },
116	{ &vps4,	0,				"PS4=+ ",
117	  NULL },
118#ifndef NO_HISTORY
119	{ &vterm,	VUNSET,				"TERM=",
120	  setterm },
121#endif
122	{ &voptind,	0,				"OPTIND=1",
123	  getoptsreset },
124	{ NULL,	0,				NULL,
125	  NULL }
126};
127
128STATIC struct var *vartab[VTABSIZE];
129
130STATIC const char *const locale_names[7] = {
131	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
132	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
133};
134STATIC const int locale_categories[7] = {
135	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
136};
137
138STATIC struct var **hashvar(const char *);
139STATIC int varequal(const char *, const char *);
140STATIC int localevar(const char *);
141
142/*
143 * Initialize the variable symbol tables and import the environment.
144 */
145
146#ifdef mkinit
147INCLUDE "var.h"
148MKINIT char **environ;
149INIT {
150	char **envp;
151
152	initvar();
153	for (envp = environ ; *envp ; envp++) {
154		if (strchr(*envp, '=')) {
155			setvareq(*envp, VEXPORT|VTEXTFIXED);
156		}
157	}
158}
159#endif
160
161
162/*
163 * This routine initializes the builtin variables.  It is called when the
164 * shell is initialized and again when a shell procedure is spawned.
165 */
166
167void
168initvar(void)
169{
170	char ppid[20];
171	const struct varinit *ip;
172	struct var *vp;
173	struct var **vpp;
174
175	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
176		if ((vp->flags & VEXPORT) == 0) {
177			vpp = hashvar(ip->text);
178			vp->next = *vpp;
179			*vpp = vp;
180			vp->text = __DECONST(char *, ip->text);
181			vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
182			vp->func = ip->func;
183		}
184	}
185	/*
186	 * PS1 depends on uid
187	 */
188	if ((vps1.flags & VEXPORT) == 0) {
189		vpp = hashvar("PS1=");
190		vps1.next = *vpp;
191		*vpp = &vps1;
192		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
193		vps1.flags = VSTRFIXED|VTEXTFIXED;
194	}
195	if ((vppid.flags & VEXPORT) == 0) {
196		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
197		setvarsafe("PPID", ppid, 0);
198	}
199}
200
201/*
202 * Safe version of setvar, returns 1 on success 0 on failure.
203 */
204
205int
206setvarsafe(const char *name, const char *val, int flags)
207{
208	struct jmploc jmploc;
209	struct jmploc *const savehandler = handler;
210	int err = 0;
211	int inton;
212
213	inton = is_int_on();
214	if (setjmp(jmploc.loc))
215		err = 1;
216	else {
217		handler = &jmploc;
218		setvar(name, val, flags);
219	}
220	handler = savehandler;
221	SETINTON(inton);
222	return err;
223}
224
225/*
226 * Set the value of a variable.  The flags argument is stored with the
227 * flags of the variable.  If val is NULL, the variable is unset.
228 */
229
230void
231setvar(const char *name, const char *val, int flags)
232{
233	const char *p;
234	int len;
235	int namelen;
236	char *nameeq;
237	int isbad;
238
239	isbad = 0;
240	p = name;
241	if (! is_name(*p))
242		isbad = 1;
243	p++;
244	for (;;) {
245		if (! is_in_name(*p)) {
246			if (*p == '\0' || *p == '=')
247				break;
248			isbad = 1;
249		}
250		p++;
251	}
252	namelen = p - name;
253	if (isbad)
254		error("%.*s: bad variable name", namelen, name);
255	len = namelen + 2;		/* 2 is space for '=' and '\0' */
256	if (val == NULL) {
257		flags |= VUNSET;
258	} else {
259		len += strlen(val);
260	}
261	nameeq = ckmalloc(len);
262	memcpy(nameeq, name, namelen);
263	nameeq[namelen] = '=';
264	if (val)
265		scopy(val, nameeq + namelen + 1);
266	else
267		nameeq[namelen + 1] = '\0';
268	setvareq(nameeq, flags);
269}
270
271STATIC int
272localevar(const char *s)
273{
274	const char *const *ss;
275
276	if (*s != 'L')
277		return 0;
278	if (varequal(s + 1, "ANG"))
279		return 1;
280	if (strncmp(s + 1, "C_", 2) != 0)
281		return 0;
282	if (varequal(s + 3, "ALL"))
283		return 1;
284	for (ss = locale_names; *ss ; ss++)
285		if (varequal(s + 3, *ss + 3))
286			return 1;
287	return 0;
288}
289
290
291/*
292 * Sets/unsets an environment variable from a pointer that may actually be a
293 * pointer into environ where the string should not be manipulated.
294 */
295static void
296change_env(const char *s, int set)
297{
298	char *eqp;
299	char *ss;
300
301	ss = savestr(s);
302	if ((eqp = strchr(ss, '=')) != NULL)
303		*eqp = '\0';
304	if (set && eqp != NULL)
305		(void) setenv(ss, eqp + 1, 1);
306	else
307		(void) unsetenv(ss);
308	ckfree(ss);
309
310	return;
311}
312
313
314/*
315 * Same as setvar except that the variable and value are passed in
316 * the first argument as name=value.  Since the first argument will
317 * be actually stored in the table, it should not be a string that
318 * will go away.
319 */
320
321void
322setvareq(char *s, int flags)
323{
324	struct var *vp, **vpp;
325	int len;
326
327	if (aflag)
328		flags |= VEXPORT;
329	vpp = hashvar(s);
330	for (vp = *vpp ; vp ; vp = vp->next) {
331		if (varequal(s, vp->text)) {
332			if (vp->flags & VREADONLY) {
333				len = strchr(s, '=') - s;
334				error("%.*s: is read only", len, s);
335			}
336			INTOFF;
337
338			if (vp->func && (flags & VNOFUNC) == 0)
339				(*vp->func)(strchr(s, '=') + 1);
340
341			if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
342				ckfree(vp->text);
343
344			vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
345			vp->flags |= flags;
346			vp->text = s;
347
348			/*
349			 * We could roll this to a function, to handle it as
350			 * a regular variable function callback, but why bother?
351			 *
352			 * Note: this assumes iflag is not set to 1 initially.
353			 * As part of init(), this is called before arguments
354			 * are looked at.
355			 */
356			if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
357			    iflag == 1)
358				chkmail(1);
359			if ((vp->flags & VEXPORT) && localevar(s)) {
360				change_env(s, 1);
361				(void) setlocale(LC_ALL, "");
362			}
363			INTON;
364			return;
365		}
366	}
367	/* not found */
368	vp = ckmalloc(sizeof (*vp));
369	vp->flags = flags;
370	vp->text = s;
371	vp->next = *vpp;
372	vp->func = NULL;
373	INTOFF;
374	*vpp = vp;
375	if ((vp->flags & VEXPORT) && localevar(s)) {
376		change_env(s, 1);
377		(void) setlocale(LC_ALL, "");
378	}
379	INTON;
380}
381
382
383
384/*
385 * Process a linked list of variable assignments.
386 */
387
388void
389listsetvar(struct strlist *list)
390{
391	struct strlist *lp;
392
393	INTOFF;
394	for (lp = list ; lp ; lp = lp->next) {
395		setvareq(savestr(lp->text), 0);
396	}
397	INTON;
398}
399
400
401
402/*
403 * Find the value of a variable.  Returns NULL if not set.
404 */
405
406char *
407lookupvar(const char *name)
408{
409	struct var *v;
410
411	for (v = *hashvar(name) ; v ; v = v->next) {
412		if (varequal(v->text, name)) {
413			if (v->flags & VUNSET)
414				return NULL;
415			return strchr(v->text, '=') + 1;
416		}
417	}
418	return NULL;
419}
420
421
422
423/*
424 * Search the environment of a builtin command.  If the second argument
425 * is nonzero, return the value of a variable even if it hasn't been
426 * exported.
427 */
428
429char *
430bltinlookup(const char *name, int doall)
431{
432	struct strlist *sp;
433	struct var *v;
434
435	for (sp = cmdenviron ; sp ; sp = sp->next) {
436		if (varequal(sp->text, name))
437			return strchr(sp->text, '=') + 1;
438	}
439	for (v = *hashvar(name) ; v ; v = v->next) {
440		if (varequal(v->text, name)) {
441			if ((v->flags & VUNSET)
442			 || (!doall && (v->flags & VEXPORT) == 0))
443				return NULL;
444			return strchr(v->text, '=') + 1;
445		}
446	}
447	return NULL;
448}
449
450
451/*
452 * Set up locale for a builtin (LANG/LC_* assignments).
453 */
454void
455bltinsetlocale(void)
456{
457	struct strlist *lp;
458	int act = 0;
459	char *loc, *locdef;
460	int i;
461
462	for (lp = cmdenviron ; lp ; lp = lp->next) {
463		if (localevar(lp->text)) {
464			act = 1;
465			break;
466		}
467	}
468	if (!act)
469		return;
470	loc = bltinlookup("LC_ALL", 0);
471	INTOFF;
472	if (loc != NULL) {
473		setlocale(LC_ALL, loc);
474		INTON;
475		return;
476	}
477	locdef = bltinlookup("LANG", 0);
478	for (i = 0; locale_names[i] != NULL; i++) {
479		loc = bltinlookup(locale_names[i], 0);
480		if (loc == NULL)
481			loc = locdef;
482		if (loc != NULL)
483			setlocale(locale_categories[i], loc);
484	}
485	INTON;
486}
487
488/*
489 * Undo the effect of bltinlocaleset().
490 */
491void
492bltinunsetlocale(void)
493{
494	struct strlist *lp;
495
496	INTOFF;
497	for (lp = cmdenviron ; lp ; lp = lp->next) {
498		if (localevar(lp->text)) {
499			setlocale(LC_ALL, "");
500			return;
501		}
502	}
503	INTON;
504}
505
506
507/*
508 * Generate a list of exported variables.  This routine is used to construct
509 * the third argument to execve when executing a program.
510 */
511
512char **
513environment(void)
514{
515	int nenv;
516	struct var **vpp;
517	struct var *vp;
518	char **env, **ep;
519
520	nenv = 0;
521	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
522		for (vp = *vpp ; vp ; vp = vp->next)
523			if (vp->flags & VEXPORT)
524				nenv++;
525	}
526	ep = env = stalloc((nenv + 1) * sizeof *env);
527	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
528		for (vp = *vpp ; vp ; vp = vp->next)
529			if (vp->flags & VEXPORT)
530				*ep++ = vp->text;
531	}
532	*ep = NULL;
533	return env;
534}
535
536
537/*
538 * Called when a shell procedure is invoked to clear out nonexported
539 * variables.  It is also necessary to reallocate variables of with
540 * VSTACK set since these are currently allocated on the stack.
541 */
542
543MKINIT void shprocvar(void);
544
545#ifdef mkinit
546SHELLPROC {
547	shprocvar();
548}
549#endif
550
551void
552shprocvar(void)
553{
554	struct var **vpp;
555	struct var *vp, **prev;
556
557	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
558		for (prev = vpp ; (vp = *prev) != NULL ; ) {
559			if ((vp->flags & VEXPORT) == 0) {
560				*prev = vp->next;
561				if ((vp->flags & VTEXTFIXED) == 0)
562					ckfree(vp->text);
563				if ((vp->flags & VSTRFIXED) == 0)
564					ckfree(vp);
565			} else {
566				if (vp->flags & VSTACK) {
567					vp->text = savestr(vp->text);
568					vp->flags &=~ VSTACK;
569				}
570				prev = &vp->next;
571			}
572		}
573	}
574	initvar();
575}
576
577
578static int
579var_compare(const void *a, const void *b)
580{
581	const char *const *sa, *const *sb;
582
583	sa = a;
584	sb = b;
585	/*
586	 * This compares two var=value strings which creates a different
587	 * order from what you would probably expect.  POSIX is somewhat
588	 * ambiguous on what should be sorted exactly.
589	 */
590	return strcoll(*sa, *sb);
591}
592
593
594/*
595 * Command to list all variables which are set.  Currently this command
596 * is invoked from the set command when the set command is called without
597 * any variables.
598 */
599
600int
601showvarscmd(int argc __unused, char **argv __unused)
602{
603	struct var **vpp;
604	struct var *vp;
605	const char *s;
606	const char **vars;
607	int i, n;
608
609	/*
610	 * POSIX requires us to sort the variables.
611	 */
612	n = 0;
613	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
614		for (vp = *vpp; vp; vp = vp->next) {
615			if (!(vp->flags & VUNSET))
616				n++;
617		}
618	}
619
620	INTON;
621	vars = ckmalloc(n * sizeof(*vars));
622	i = 0;
623	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
624		for (vp = *vpp; vp; vp = vp->next) {
625			if (!(vp->flags & VUNSET))
626				vars[i++] = vp->text;
627		}
628	}
629
630	qsort(vars, n, sizeof(*vars), var_compare);
631	for (i = 0; i < n; i++) {
632		for (s = vars[i]; *s != '='; s++)
633			out1c(*s);
634		out1c('=');
635		out1qstr(s + 1);
636		out1c('\n');
637	}
638	ckfree(vars);
639	INTOFF;
640
641	return 0;
642}
643
644
645
646/*
647 * The export and readonly commands.
648 */
649
650int
651exportcmd(int argc, char **argv)
652{
653	struct var **vpp;
654	struct var *vp;
655	char *name;
656	char *p;
657	char *cmdname;
658	int ch, values;
659	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
660
661	cmdname = argv[0];
662	optreset = optind = 1;
663	opterr = 0;
664	values = 0;
665	while ((ch = getopt(argc, argv, "p")) != -1) {
666		switch (ch) {
667		case 'p':
668			values = 1;
669			break;
670		case '?':
671		default:
672			error("unknown option: -%c", optopt);
673		}
674	}
675	argc -= optind;
676	argv += optind;
677
678	if (values && argc != 0)
679		error("-p requires no arguments");
680	if (argc != 0) {
681		while ((name = *argv++) != NULL) {
682			if ((p = strchr(name, '=')) != NULL) {
683				p++;
684			} else {
685				vpp = hashvar(name);
686				for (vp = *vpp ; vp ; vp = vp->next) {
687					if (varequal(vp->text, name)) {
688
689						vp->flags |= flag;
690						if ((vp->flags & VEXPORT) && localevar(vp->text)) {
691							change_env(vp->text, 1);
692							(void) setlocale(LC_ALL, "");
693						}
694						goto found;
695					}
696				}
697			}
698			setvar(name, p, flag);
699found:;
700		}
701	} else {
702		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
703			for (vp = *vpp ; vp ; vp = vp->next) {
704				if (vp->flags & flag) {
705					if (values) {
706						out1str(cmdname);
707						out1c(' ');
708					}
709					for (p = vp->text ; *p != '=' ; p++)
710						out1c(*p);
711					if (values && !(vp->flags & VUNSET)) {
712						out1c('=');
713						out1qstr(p + 1);
714					}
715					out1c('\n');
716				}
717			}
718		}
719	}
720	return 0;
721}
722
723
724/*
725 * The "local" command.
726 */
727
728int
729localcmd(int argc __unused, char **argv __unused)
730{
731	char *name;
732
733	if (! in_function())
734		error("Not in a function");
735	while ((name = *argptr++) != NULL) {
736		mklocal(name);
737	}
738	return 0;
739}
740
741
742/*
743 * Make a variable a local variable.  When a variable is made local, it's
744 * value and flags are saved in a localvar structure.  The saved values
745 * will be restored when the shell function returns.  We handle the name
746 * "-" as a special case.
747 */
748
749void
750mklocal(char *name)
751{
752	struct localvar *lvp;
753	struct var **vpp;
754	struct var *vp;
755
756	INTOFF;
757	lvp = ckmalloc(sizeof (struct localvar));
758	if (name[0] == '-' && name[1] == '\0') {
759		lvp->text = ckmalloc(sizeof optlist);
760		memcpy(lvp->text, optlist, sizeof optlist);
761		vp = NULL;
762	} else {
763		vpp = hashvar(name);
764		for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
765		if (vp == NULL) {
766			if (strchr(name, '='))
767				setvareq(savestr(name), VSTRFIXED);
768			else
769				setvar(name, NULL, VSTRFIXED);
770			vp = *vpp;	/* the new variable */
771			lvp->text = NULL;
772			lvp->flags = VUNSET;
773		} else {
774			lvp->text = vp->text;
775			lvp->flags = vp->flags;
776			vp->flags |= VSTRFIXED|VTEXTFIXED;
777			if (strchr(name, '='))
778				setvareq(savestr(name), 0);
779		}
780	}
781	lvp->vp = vp;
782	lvp->next = localvars;
783	localvars = lvp;
784	INTON;
785}
786
787
788/*
789 * Called after a function returns.
790 */
791
792void
793poplocalvars(void)
794{
795	struct localvar *lvp;
796	struct var *vp;
797
798	while ((lvp = localvars) != NULL) {
799		localvars = lvp->next;
800		vp = lvp->vp;
801		if (vp == NULL) {	/* $- saved */
802			memcpy(optlist, lvp->text, sizeof optlist);
803			ckfree(lvp->text);
804		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
805			(void)unsetvar(vp->text);
806		} else {
807			if ((vp->flags & VTEXTFIXED) == 0)
808				ckfree(vp->text);
809			vp->flags = lvp->flags;
810			vp->text = lvp->text;
811		}
812		ckfree(lvp);
813	}
814}
815
816
817int
818setvarcmd(int argc, char **argv)
819{
820	if (argc <= 2)
821		return unsetcmd(argc, argv);
822	else if (argc == 3)
823		setvar(argv[1], argv[2], 0);
824	else
825		error("List assignment not implemented");
826	return 0;
827}
828
829
830/*
831 * The unset builtin command.  We unset the function before we unset the
832 * variable to allow a function to be unset when there is a readonly variable
833 * with the same name.
834 */
835
836int
837unsetcmd(int argc __unused, char **argv __unused)
838{
839	char **ap;
840	int i;
841	int flg_func = 0;
842	int flg_var = 0;
843	int ret = 0;
844
845	while ((i = nextopt("vf")) != '\0') {
846		if (i == 'f')
847			flg_func = 1;
848		else
849			flg_var = 1;
850	}
851	if (flg_func == 0 && flg_var == 0)
852		flg_var = 1;
853
854	for (ap = argptr; *ap ; ap++) {
855		if (flg_func)
856			ret |= unsetfunc(*ap);
857		if (flg_var)
858			ret |= unsetvar(*ap);
859	}
860	return ret;
861}
862
863
864/*
865 * Unset the specified variable.
866 */
867
868int
869unsetvar(const char *s)
870{
871	struct var **vpp;
872	struct var *vp;
873
874	vpp = hashvar(s);
875	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
876		if (varequal(vp->text, s)) {
877			if (vp->flags & VREADONLY)
878				return (1);
879			INTOFF;
880			if (*(strchr(vp->text, '=') + 1) != '\0')
881				setvar(s, nullstr, 0);
882			if ((vp->flags & VEXPORT) && localevar(vp->text)) {
883				change_env(s, 0);
884				setlocale(LC_ALL, "");
885			}
886			vp->flags &= ~VEXPORT;
887			vp->flags |= VUNSET;
888			if ((vp->flags & VSTRFIXED) == 0) {
889				if ((vp->flags & VTEXTFIXED) == 0)
890					ckfree(vp->text);
891				*vpp = vp->next;
892				ckfree(vp);
893			}
894			INTON;
895			return (0);
896		}
897	}
898
899	return (0);
900}
901
902
903
904/*
905 * Find the appropriate entry in the hash table from the name.
906 */
907
908STATIC struct var **
909hashvar(const char *p)
910{
911	unsigned int hashval;
912
913	hashval = ((unsigned char) *p) << 4;
914	while (*p && *p != '=')
915		hashval += (unsigned char) *p++;
916	return &vartab[hashval % VTABSIZE];
917}
918
919
920
921/*
922 * Returns true if the two strings specify the same varable.  The first
923 * variable name is terminated by '='; the second may be terminated by
924 * either '=' or '\0'.
925 */
926
927STATIC int
928varequal(const char *p, const char *q)
929{
930	while (*p == *q++) {
931		if (*p++ == '=')
932			return 1;
933	}
934	if (*p == '=' && *(q - 1) == '\0')
935		return 1;
936	return 0;
937}
938