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