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