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