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