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