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