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