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