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