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