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