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