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