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