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