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