usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / bin / sh / var.c
blobb2d5595f99e19f3dea2a25a9f836ef94bbc87ff1
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
38 #endif
39 #endif /* not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/var.c 342740 2019-01-03 20:22:35Z jilles $");
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <paths.h>
48 * Shell variables.
51 #include <locale.h>
52 #include <langinfo.h>
54 #include "shell.h"
55 #include "output.h"
56 #include "expand.h"
57 #include "nodes.h" /* for other headers */
58 #include "eval.h" /* defines cmdenviron */
59 #include "exec.h"
60 #include "syntax.h"
61 #include "options.h"
62 #include "mail.h"
63 #include "var.h"
64 #include "memalloc.h"
65 #include "error.h"
66 #include "mystring.h"
67 #include "parser.h"
68 #include "builtins.h"
69 #ifndef NO_HISTORY
70 #include "myhistedit.h"
71 #endif
74 #ifndef VTABSIZE
75 #define VTABSIZE 39
76 #endif
79 struct varinit {
80 struct var *var;
81 int flags;
82 const char *text;
83 void (*func)(const char *);
87 #ifndef NO_HISTORY
88 struct var vhistsize;
89 struct var vterm;
90 #endif
91 struct var vifs;
92 struct var vmail;
93 struct var vmpath;
94 struct var vpath;
95 struct var vps1;
96 struct var vps2;
97 struct var vps4;
98 static struct var voptind;
99 struct var vdisvfork;
101 struct localvar *localvars;
102 int forcelocal;
104 static const struct varinit varinit[] = {
105 #ifndef NO_HISTORY
106 { &vhistsize, VUNSET, "HISTSIZE=",
107 sethistsize },
108 #endif
109 { &vifs, 0, "IFS= \t\n",
110 NULL },
111 { &vmail, VUNSET, "MAIL=",
112 NULL },
113 { &vmpath, VUNSET, "MAILPATH=",
114 NULL },
115 { &vpath, 0, "PATH=" _PATH_DEFPATH,
116 changepath },
118 * vps1 depends on uid
120 { &vps2, 0, "PS2=> ",
121 NULL },
122 { &vps4, 0, "PS4=+ ",
123 NULL },
124 #ifndef NO_HISTORY
125 { &vterm, VUNSET, "TERM=",
126 setterm },
127 #endif
128 { &voptind, 0, "OPTIND=1",
129 getoptsreset },
130 { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
131 NULL },
132 { NULL, 0, NULL,
133 NULL }
136 static struct var *vartab[VTABSIZE];
138 static const char *const locale_names[7] = {
139 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
140 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
142 static const int locale_categories[7] = {
143 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
146 static int varequal(const char *, const char *);
147 static struct var *find_var(const char *, struct var ***, int *);
148 static int localevar(const char *);
149 static void setvareq_const(const char *s, int flags);
151 extern char **environ;
154 * This routine initializes the builtin variables and imports the environment.
155 * It is called when the shell is initialized.
158 void
159 initvar(void)
161 char ppid[20];
162 const struct varinit *ip;
163 struct var *vp;
164 struct var **vpp;
165 char **envp;
167 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
168 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
169 continue;
170 vp->next = *vpp;
171 *vpp = vp;
172 vp->text = __DECONST(char *, ip->text);
173 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
174 vp->func = ip->func;
177 * PS1 depends on uid
179 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
180 vps1.next = *vpp;
181 *vpp = &vps1;
182 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
183 vps1.flags = VSTRFIXED|VTEXTFIXED;
185 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
186 setvarsafe("PPID", ppid, 0);
187 for (envp = environ ; *envp ; envp++) {
188 if (strchr(*envp, '=')) {
189 setvareq(*envp, VEXPORT|VTEXTFIXED);
192 setvareq_const("OPTIND=1", 0);
193 setvareq_const("IFS= \t\n", 0);
197 * Safe version of setvar, returns 1 on success 0 on failure.
201 setvarsafe(const char *name, const char *val, int flags)
203 struct jmploc jmploc;
204 struct jmploc *const savehandler = handler;
205 int err = 0;
206 int inton;
208 inton = is_int_on();
209 if (setjmp(jmploc.loc))
210 err = 1;
211 else {
212 handler = &jmploc;
213 setvar(name, val, flags);
215 handler = savehandler;
216 SETINTON(inton);
217 return err;
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.
225 void
226 setvar(const char *name, const char *val, int flags)
228 const char *p;
229 size_t len;
230 size_t namelen;
231 size_t vallen;
232 char *nameeq;
233 int isbad;
235 isbad = 0;
236 p = name;
237 if (! is_name(*p))
238 isbad = 1;
239 p++;
240 for (;;) {
241 if (! is_in_name(*p)) {
242 if (*p == '\0' || *p == '=')
243 break;
244 isbad = 1;
246 p++;
248 namelen = p - name;
249 if (isbad)
250 error("%.*s: bad variable name", (int)namelen, name);
251 len = namelen + 2; /* 2 is space for '=' and '\0' */
252 if (val == NULL) {
253 flags |= VUNSET;
254 vallen = 0;
255 } else {
256 vallen = strlen(val);
257 len += vallen;
259 INTOFF;
260 nameeq = ckmalloc(len);
261 memcpy(nameeq, name, namelen);
262 nameeq[namelen] = '=';
263 if (val)
264 memcpy(nameeq + namelen + 1, val, vallen + 1);
265 else
266 nameeq[namelen + 1] = '\0';
267 setvareq(nameeq, flags);
268 INTON;
271 static int
272 localevar(const char *s)
274 const char *const *ss;
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;
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.
295 static void
296 change_env(const char *s, int set)
298 char *eqp;
299 char *ss;
301 INTOFF;
302 ss = savestr(s);
303 if ((eqp = strchr(ss, '=')) != NULL)
304 *eqp = '\0';
305 if (set && eqp != NULL)
306 (void) setenv(ss, eqp + 1, 1);
307 else
308 (void) unsetenv(ss);
309 ckfree(ss);
310 INTON;
312 return;
317 * Same as setvar except that the variable and value are passed in
318 * the first argument as name=value. Since the first argument will
319 * be actually stored in the table, it should not be a string that
320 * will go away.
323 void
324 setvareq(char *s, int flags)
326 struct var *vp, **vpp;
327 int nlen;
329 if (aflag)
330 flags |= VEXPORT;
331 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
332 mklocal(s);
333 vp = find_var(s, &vpp, &nlen);
334 if (vp != NULL) {
335 if (vp->flags & VREADONLY) {
336 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
337 ckfree(s);
338 error("%.*s: is read only", vp->name_len, vp->text);
340 if (flags & VNOSET) {
341 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
342 ckfree(s);
343 return;
345 INTOFF;
347 if (vp->func && (flags & VNOFUNC) == 0)
348 (*vp->func)(s + vp->name_len + 1);
350 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
351 ckfree(vp->text);
353 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
354 vp->flags |= flags;
355 vp->text = s;
358 * We could roll this to a function, to handle it as
359 * a regular variable function callback, but why bother?
361 * Note: this assumes iflag is not set to 1 initially.
362 * As part of initvar(), this is called before arguments
363 * are looked at.
365 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
366 iflag == 1)
367 chkmail(1);
368 if ((vp->flags & VEXPORT) && localevar(s)) {
369 change_env(s, 1);
370 (void) setlocale(LC_ALL, "");
371 updatecharset();
373 INTON;
374 return;
376 /* not found */
377 if (flags & VNOSET) {
378 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
379 ckfree(s);
380 return;
382 INTOFF;
383 vp = ckmalloc(sizeof (*vp));
384 vp->flags = flags;
385 vp->text = s;
386 vp->name_len = nlen;
387 vp->next = *vpp;
388 vp->func = NULL;
389 *vpp = vp;
390 if ((vp->flags & VEXPORT) && localevar(s)) {
391 change_env(s, 1);
392 (void) setlocale(LC_ALL, "");
393 updatecharset();
395 INTON;
399 static void
400 setvareq_const(const char *s, int flags)
402 setvareq(__DECONST(char *, s), flags | VTEXTFIXED);
407 * Process a linked list of variable assignments.
410 void
411 listsetvar(struct arglist *list, int flags)
413 int i;
415 INTOFF;
416 for (i = 0; i < list->count; i++)
417 setvareq(savestr(list->args[i]), flags);
418 INTON;
424 * Find the value of a variable. Returns NULL if not set.
427 char *
428 lookupvar(const char *name)
430 struct var *v;
432 v = find_var(name, NULL, NULL);
433 if (v == NULL || v->flags & VUNSET)
434 return NULL;
435 return v->text + v->name_len + 1;
441 * Search the environment of a builtin command. If the second argument
442 * is nonzero, return the value of a variable even if it hasn't been
443 * exported.
446 char *
447 bltinlookup(const char *name, int doall)
449 struct var *v;
450 char *result;
451 int i;
453 result = NULL;
454 if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
455 if (varequal(cmdenviron->args[i], name))
456 result = strchr(cmdenviron->args[i], '=') + 1;
458 if (result != NULL)
459 return result;
461 v = find_var(name, NULL, NULL);
462 if (v == NULL || v->flags & VUNSET ||
463 (!doall && (v->flags & VEXPORT) == 0))
464 return NULL;
465 return v->text + v->name_len + 1;
470 * Set up locale for a builtin (LANG/LC_* assignments).
472 void
473 bltinsetlocale(void)
475 int act = 0;
476 char *loc, *locdef;
477 int i;
479 if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
480 if (localevar(cmdenviron->args[i])) {
481 act = 1;
482 break;
485 if (!act)
486 return;
487 loc = bltinlookup("LC_ALL", 0);
488 INTOFF;
489 if (loc != NULL) {
490 setlocale(LC_ALL, loc);
491 INTON;
492 updatecharset();
493 return;
495 locdef = bltinlookup("LANG", 0);
496 for (i = 0; locale_names[i] != NULL; i++) {
497 loc = bltinlookup(locale_names[i], 0);
498 if (loc == NULL)
499 loc = locdef;
500 if (loc != NULL)
501 setlocale(locale_categories[i], loc);
503 INTON;
504 updatecharset();
508 * Undo the effect of bltinlocaleset().
510 void
511 bltinunsetlocale(void)
513 int i;
515 INTOFF;
516 if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
517 if (localevar(cmdenviron->args[i])) {
518 setlocale(LC_ALL, "");
519 updatecharset();
520 break;
523 INTON;
527 * Update the localeisutf8 flag.
529 void
530 updatecharset(void)
532 char *charset;
534 charset = nl_langinfo(CODESET);
535 localeisutf8 = !strcmp(charset, "UTF-8");
538 void
539 initcharset(void)
541 updatecharset();
542 initial_localeisutf8 = localeisutf8;
546 * Generate a list of exported variables. This routine is used to construct
547 * the third argument to execve when executing a program.
550 char **
551 environment(void)
553 int nenv;
554 struct var **vpp;
555 struct var *vp;
556 char **env, **ep;
558 nenv = 0;
559 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
560 for (vp = *vpp ; vp ; vp = vp->next)
561 if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
562 nenv++;
564 ep = env = stalloc((nenv + 1) * sizeof *env);
565 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
566 for (vp = *vpp ; vp ; vp = vp->next)
567 if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
568 *ep++ = vp->text;
570 *ep = NULL;
571 return env;
575 static int
576 var_compare(const void *a, const void *b)
578 const char *const *sa, *const *sb;
580 sa = a;
581 sb = b;
583 * This compares two var=value strings which creates a different
584 * order from what you would probably expect. POSIX is somewhat
585 * ambiguous on what should be sorted exactly.
587 return strcoll(*sa, *sb);
592 * Command to list all variables which are set. This is invoked from the
593 * set command when it is called without any options or operands.
597 showvarscmd(int argc __unused, char **argv __unused)
599 struct var **vpp;
600 struct var *vp;
601 const char *s;
602 const char **vars;
603 int i, n;
606 * POSIX requires us to sort the variables.
608 n = 0;
609 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
610 for (vp = *vpp; vp; vp = vp->next) {
611 if (!(vp->flags & VUNSET))
612 n++;
616 INTOFF;
617 vars = ckmalloc(n * sizeof(*vars));
618 i = 0;
619 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
620 for (vp = *vpp; vp; vp = vp->next) {
621 if (!(vp->flags & VUNSET))
622 vars[i++] = vp->text;
626 qsort(vars, n, sizeof(*vars), var_compare);
627 for (i = 0; i < n; i++) {
629 * Skip improper variable names so the output remains usable as
630 * shell input.
632 if (!isassignment(vars[i]))
633 continue;
634 s = strchr(vars[i], '=');
635 s++;
636 outbin(vars[i], s - vars[i], out1);
637 out1qstr(s);
638 out1c('\n');
640 ckfree(vars);
641 INTON;
643 return 0;
649 * The export and readonly commands.
653 exportcmd(int argc __unused, char **argv)
655 struct var **vpp;
656 struct var *vp;
657 char **ap;
658 char *name;
659 char *p;
660 char *cmdname;
661 int ch, values;
662 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
664 cmdname = argv[0];
665 values = 0;
666 while ((ch = nextopt("p")) != '\0') {
667 switch (ch) {
668 case 'p':
669 values = 1;
670 break;
674 if (values && *argptr != NULL)
675 error("-p requires no arguments");
676 if (*argptr != NULL) {
677 for (ap = argptr; (name = *ap) != NULL; ap++) {
678 if ((p = strchr(name, '=')) != NULL) {
679 p++;
680 } else {
681 vp = find_var(name, NULL, NULL);
682 if (vp != NULL) {
683 vp->flags |= flag;
684 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
685 change_env(vp->text, 1);
686 (void) setlocale(LC_ALL, "");
687 updatecharset();
689 continue;
692 setvar(name, p, flag);
694 } else {
695 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
696 for (vp = *vpp ; vp ; vp = vp->next) {
697 if (vp->flags & flag) {
698 if (values) {
700 * Skip improper variable names
701 * so the output remains usable
702 * as shell input.
704 if (!isassignment(vp->text))
705 continue;
706 out1str(cmdname);
707 out1c(' ');
709 if (values && !(vp->flags & VUNSET)) {
710 outbin(vp->text,
711 vp->name_len + 1, out1);
712 out1qstr(vp->text +
713 vp->name_len + 1);
714 } else
715 outbin(vp->text, vp->name_len,
716 out1);
717 out1c('\n');
722 return 0;
727 * The "local" command.
731 localcmd(int argc __unused, char **argv __unused)
733 char *name;
735 nextopt("");
736 if (! in_function())
737 error("Not in a function");
738 while ((name = *argptr++) != NULL) {
739 mklocal(name);
741 return 0;
746 * Make a variable a local variable. When a variable is made local, it's
747 * value and flags are saved in a localvar structure. The saved values
748 * will be restored when the shell function returns. We handle the name
749 * "-" as a special case.
752 void
753 mklocal(char *name)
755 struct localvar *lvp;
756 struct var **vpp;
757 struct var *vp;
759 INTOFF;
760 lvp = ckmalloc(sizeof (struct localvar));
761 if (name[0] == '-' && name[1] == '\0') {
762 lvp->text = ckmalloc(sizeof optval);
763 memcpy(lvp->text, optval, sizeof optval);
764 vp = NULL;
765 } else {
766 vp = find_var(name, &vpp, NULL);
767 if (vp == NULL) {
768 if (strchr(name, '='))
769 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
770 else
771 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
772 vp = *vpp; /* the new variable */
773 lvp->text = NULL;
774 lvp->flags = VUNSET;
775 } else {
776 lvp->text = vp->text;
777 lvp->flags = vp->flags;
778 vp->flags |= VSTRFIXED|VTEXTFIXED;
779 if (name[vp->name_len] == '=')
780 setvareq(savestr(name), VNOLOCAL);
783 lvp->vp = vp;
784 lvp->next = localvars;
785 localvars = lvp;
786 INTON;
791 * Called after a function returns.
794 void
795 poplocalvars(void)
797 struct localvar *lvp;
798 struct var *vp;
799 int islocalevar;
801 INTOFF;
802 while ((lvp = localvars) != NULL) {
803 localvars = lvp->next;
804 vp = lvp->vp;
805 if (vp == NULL) { /* $- saved */
806 memcpy(optval, lvp->text, sizeof optval);
807 ckfree(lvp->text);
808 optschanged();
809 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
810 vp->flags &= ~VREADONLY;
811 (void)unsetvar(vp->text);
812 } else {
813 islocalevar = (vp->flags | lvp->flags) & VEXPORT &&
814 localevar(lvp->text);
815 if ((vp->flags & VTEXTFIXED) == 0)
816 ckfree(vp->text);
817 vp->flags = lvp->flags;
818 vp->text = lvp->text;
819 if (vp->func)
820 (*vp->func)(vp->text + vp->name_len + 1);
821 if (islocalevar) {
822 change_env(vp->text, vp->flags & VEXPORT &&
823 (vp->flags & VUNSET) == 0);
824 setlocale(LC_ALL, "");
825 updatecharset();
828 ckfree(lvp);
830 INTON;
835 setvarcmd(int argc, char **argv)
837 if (argc <= 2)
838 return unsetcmd(argc, argv);
839 else if (argc == 3)
840 setvar(argv[1], argv[2], 0);
841 else
842 error("too many arguments");
843 return 0;
848 * The unset builtin command.
852 unsetcmd(int argc __unused, char **argv __unused)
854 char **ap;
855 int i;
856 int flg_func = 0;
857 int flg_var = 0;
858 int ret = 0;
860 while ((i = nextopt("vf")) != '\0') {
861 if (i == 'f')
862 flg_func = 1;
863 else
864 flg_var = 1;
866 if (flg_func == 0 && flg_var == 0)
867 flg_var = 1;
869 INTOFF;
870 for (ap = argptr; *ap ; ap++) {
871 if (flg_func)
872 ret |= unsetfunc(*ap);
873 if (flg_var)
874 ret |= unsetvar(*ap);
876 INTON;
877 return ret;
882 * Unset the specified variable.
883 * Called with interrupts off.
887 unsetvar(const char *s)
889 struct var **vpp;
890 struct var *vp;
892 vp = find_var(s, &vpp, NULL);
893 if (vp == NULL)
894 return (0);
895 if (vp->flags & VREADONLY)
896 return (1);
897 if (vp->text[vp->name_len + 1] != '\0')
898 setvar(s, "", 0);
899 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
900 change_env(s, 0);
901 setlocale(LC_ALL, "");
902 updatecharset();
904 vp->flags &= ~VEXPORT;
905 vp->flags |= VUNSET;
906 if ((vp->flags & VSTRFIXED) == 0) {
907 if ((vp->flags & VTEXTFIXED) == 0)
908 ckfree(vp->text);
909 *vpp = vp->next;
910 ckfree(vp);
912 return (0);
918 * Returns true if the two strings specify the same variable. The first
919 * variable name is terminated by '='; the second may be terminated by
920 * either '=' or '\0'.
923 static int
924 varequal(const char *p, const char *q)
926 while (*p == *q++) {
927 if (*p++ == '=')
928 return 1;
930 if (*p == '=' && *(q - 1) == '\0')
931 return 1;
932 return 0;
936 * Search for a variable.
937 * 'name' may be terminated by '=' or a NUL.
938 * vppp is set to the pointer to vp, or the list head if vp isn't found
939 * lenp is set to the number of characters in 'name'
942 static struct var *
943 find_var(const char *name, struct var ***vppp, int *lenp)
945 unsigned int hashval;
946 int len;
947 struct var *vp, **vpp;
948 const char *p = name;
950 hashval = 0;
951 while (*p && *p != '=')
952 hashval = 2 * hashval + (unsigned char)*p++;
953 len = p - name;
955 if (lenp)
956 *lenp = len;
957 vpp = &vartab[hashval % VTABSIZE];
958 if (vppp)
959 *vppp = vpp;
961 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
962 if (vp->name_len != len)
963 continue;
964 if (memcmp(vp->text, name, len) != 0)
965 continue;
966 if (vppp)
967 *vppp = vpp;
968 return vp;
970 return NULL;