xref: /dragonfly/bin/sh/var.c (revision 010baf5e)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#)var.c	8.3 (Berkeley) 5/4/95
37  * $FreeBSD: src/bin/sh/var.c,v 1.60 2011/05/15 22:09:27 jilles Exp $
38  */
39 
40 #include <unistd.h>
41 #include <stdlib.h>
42 
43 /*
44  * Shell variables.
45  */
46 
47 #include <locale.h>
48 #include <langinfo.h>
49 #include <paths.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 int varequal(const char *, const char *);
139 static struct var *find_var(const char *, struct var ***, int *);
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 (find_var(ip->text, &vpp, &vp->name_len) != NULL)
177 			continue;
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 	 * PS1 depends on uid
186 	 */
187 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
188 		vps1.next = *vpp;
189 		*vpp = &vps1;
190 		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
191 		vps1.flags = VSTRFIXED|VTEXTFIXED;
192 	}
193 	if ((vppid.flags & VEXPORT) == 0) {
194 		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
195 		setvarsafe("PPID", ppid, 0);
196 	}
197 }
198 
199 /*
200  * Safe version of setvar, returns 1 on success 0 on failure.
201  */
202 
203 int
204 setvarsafe(const char *name, const char *val, int flags)
205 {
206 	struct jmploc jmploc;
207 	struct jmploc *const savehandler = handler;
208 	int err = 0;
209 	int inton;
210 
211 	inton = is_int_on();
212 	if (setjmp(jmploc.loc))
213 		err = 1;
214 	else {
215 		handler = &jmploc;
216 		setvar(name, val, flags);
217 	}
218 	handler = savehandler;
219 	SETINTON(inton);
220 	return err;
221 }
222 
223 /*
224  * Set the value of a variable.  The flags argument is stored with the
225  * flags of the variable.  If val is NULL, the variable is unset.
226  */
227 
228 void
229 setvar(const char *name, const char *val, int flags)
230 {
231 	const char *p;
232 	int len;
233 	int namelen;
234 	char *nameeq;
235 	int isbad;
236 
237 	isbad = 0;
238 	p = name;
239 	if (!is_name(*p))
240 		isbad = 1;
241 	p++;
242 	for (;;) {
243 		if (!is_in_name(*p)) {
244 			if (*p == '\0' || *p == '=')
245 				break;
246 			isbad = 1;
247 		}
248 		p++;
249 	}
250 	namelen = p - name;
251 	if (isbad)
252 		error("%.*s: bad variable name", namelen, name);
253 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
254 	if (val == NULL) {
255 		flags |= VUNSET;
256 	} else {
257 		len += strlen(val);
258 	}
259 	nameeq = ckmalloc(len);
260 	memcpy(nameeq, name, namelen);
261 	nameeq[namelen] = '=';
262 	if (val)
263 		scopy(val, nameeq + namelen + 1);
264 	else
265 		nameeq[namelen + 1] = '\0';
266 	setvareq(nameeq, flags);
267 }
268 
269 static int
270 localevar(const char *s)
271 {
272 	const char *const *ss;
273 
274 	if (*s != 'L')
275 		return 0;
276 	if (varequal(s + 1, "ANG"))
277 		return 1;
278 	if (strncmp(s + 1, "C_", 2) != 0)
279 		return 0;
280 	if (varequal(s + 3, "ALL"))
281 		return 1;
282 	for (ss = locale_names; *ss ; ss++)
283 		if (varequal(s + 3, *ss + 3))
284 			return 1;
285 	return 0;
286 }
287 
288 /*
289  * Sets/unsets an environment variable from a pointer that may actually be a
290  * pointer into environ where the string should not be manipulated.
291  */
292 static void
293 change_env(const char *s, int set)
294 {
295 	char *eqp;
296 	char *ss;
297 
298 	ss = savestr(s);
299 	if ((eqp = strchr(ss, '=')) != NULL)
300 		*eqp = '\0';
301 	if (set && eqp != NULL) {
302 		if (setenv(ss, eqp + 1, 1) != 0)
303 			error("setenv: cannot set %s=%s", ss, eqp + 1);
304 	} else
305 		unsetenv(ss);
306 	ckfree(ss);
307 }
308 
309 
310 /*
311  * Same as setvar except that the variable and value are passed in
312  * the first argument as name=value.  Since the first argument will
313  * be actually stored in the table, it should not be a string that
314  * will go away.
315  */
316 
317 void
318 setvareq(char *s, int flags)
319 {
320 	struct var *vp, **vpp;
321 	int nlen;
322 
323 	if (aflag)
324 		flags |= VEXPORT;
325 	vp = find_var(s, &vpp, &nlen);
326 	if (vp != NULL) {
327 		if (vp->flags & VREADONLY)
328 			error("%.*s: is read only", vp->name_len, s);
329 		if (flags & VNOSET)
330 			return;
331 		INTOFF;
332 
333 		if (vp->func && (flags & VNOFUNC) == 0)
334 			(*vp->func)(s + vp->name_len + 1);
335 
336 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
337 			ckfree(vp->text);
338 
339 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
340 		vp->flags |= flags;
341 		vp->text = s;
342 
343 		/*
344 		 * We could roll this to a function, to handle it as
345 		 * a regular variable function callback, but why bother?
346 		 *
347 		 * Note: this assumes iflag is not set to 1 initially.
348 		 * As part of init(), this is called before arguments
349 		 * are looked at.
350 		 */
351 		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
352 		    iflag == 1)
353 			chkmail(1);
354 		if ((vp->flags & VEXPORT) && localevar(s)) {
355 			change_env(s, 1);
356 			setlocale(LC_ALL, "");
357 			updatecharset();
358 		}
359 		INTON;
360 		return;
361 	}
362 	/* not found */
363 	if (flags & VNOSET)
364 		return;
365 	vp = ckmalloc(sizeof (*vp));
366 	vp->flags = flags;
367 	vp->text = s;
368 	vp->name_len = nlen;
369 	vp->next = *vpp;
370 	vp->func = NULL;
371 	INTOFF;
372 	*vpp = vp;
373 	if ((vp->flags & VEXPORT) && localevar(s)) {
374 		change_env(s, 1);
375 		setlocale(LC_ALL, "");
376 		updatecharset();
377 	}
378 	INTON;
379 }
380 
381 
382 
383 /*
384  * Process a linked list of variable assignments.
385  */
386 
387 void
388 listsetvar(struct strlist *list, int flags)
389 {
390 	struct strlist *lp;
391 
392 	INTOFF;
393 	for (lp = list ; lp ; lp = lp->next) {
394 		setvareq(savestr(lp->text), flags);
395 	}
396 	INTON;
397 }
398 
399 
400 
401 /*
402  * Find the value of a variable.  Returns NULL if not set.
403  */
404 
405 char *
406 lookupvar(const char *name)
407 {
408 	struct var *v;
409 
410 	v = find_var(name, NULL, NULL);
411 	if (v == NULL || v->flags & VUNSET)
412 		return NULL;
413 	return v->text + v->name_len + 1;
414 }
415 
416 
417 
418 /*
419  * Search the environment of a builtin command.  If the second argument
420  * is nonzero, return the value of a variable even if it hasn't been
421  * exported.
422  */
423 
424 char *
425 bltinlookup(const char *name, int doall)
426 {
427 	struct strlist *sp;
428 	struct var *v;
429 	char *result;
430 
431 	result = NULL;
432 	for (sp = cmdenviron ; sp ; sp = sp->next) {
433 		if (varequal(sp->text, name))
434 			result = strchr(sp->text, '=') + 1;
435 	}
436 	if (result != NULL)
437 		return result;
438 
439 	v = find_var(name, NULL, NULL);
440 	if (v == NULL || v->flags & VUNSET ||
441 	    (!doall && (v->flags & VEXPORT) == 0))
442 		return NULL;
443 	return v->text + v->name_len + 1;
444 }
445 
446 
447 /*
448  * Set up locale for a builtin (LANG/LC_* assignments).
449  */
450 void
451 bltinsetlocale(void)
452 {
453 	struct strlist *lp;
454 	int act = 0;
455 	char *loc, *locdef;
456 	int i;
457 
458 	for (lp = cmdenviron ; lp ; lp = lp->next) {
459 		if (localevar(lp->text)) {
460 			act = 1;
461 			break;
462 		}
463 	}
464 	if (!act)
465 		return;
466 	loc = bltinlookup("LC_ALL", 0);
467 	INTOFF;
468 	if (loc != NULL) {
469 		setlocale(LC_ALL, loc);
470 		INTON;
471 		updatecharset();
472 		return;
473 	}
474 	locdef = bltinlookup("LANG", 0);
475 	for (i = 0; locale_names[i] != NULL; i++) {
476 		loc = bltinlookup(locale_names[i], 0);
477 		if (loc == NULL)
478 			loc = locdef;
479 		if (loc != NULL)
480 			setlocale(locale_categories[i], loc);
481 	}
482 	INTON;
483 	updatecharset();
484 }
485 
486 /*
487  * Undo the effect of bltinlocaleset().
488  */
489 void
490 bltinunsetlocale(void)
491 {
492 	struct strlist *lp;
493 
494 	INTOFF;
495 	for (lp = cmdenviron ; lp ; lp = lp->next) {
496 		if (localevar(lp->text)) {
497 			setlocale(LC_ALL, "");
498 			updatecharset();
499 			return;
500 		}
501 	}
502 	INTON;
503 }
504 
505 /*
506  * Update the localeisutf8 flag.
507  */
508 void
509 updatecharset(void)
510 {
511 	char *charset;
512 
513 	charset = nl_langinfo(CODESET);
514 	localeisutf8 = !strcmp(charset, "UTF-8");
515 }
516 
517 void
518 initcharset(void)
519 {
520 	updatecharset();
521 	initial_localeisutf8 = localeisutf8;
522 }
523 
524 /*
525  * Generate a list of exported variables.  This routine is used to construct
526  * the third argument to execve when executing a program.
527  */
528 
529 char **
530 environment(void)
531 {
532 	int nenv;
533 	struct var **vpp;
534 	struct var *vp;
535 	char **env, **ep;
536 
537 	nenv = 0;
538 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
539 		for (vp = *vpp ; vp ; vp = vp->next)
540 			if (vp->flags & VEXPORT)
541 				nenv++;
542 	}
543 	ep = env = stalloc((nenv + 1) * sizeof *env);
544 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
545 		for (vp = *vpp ; vp ; vp = vp->next)
546 			if (vp->flags & VEXPORT)
547 				*ep++ = vp->text;
548 	}
549 	*ep = NULL;
550 	return env;
551 }
552 
553 
554 static int
555 var_compare(const void *a, const void *b)
556 {
557 	const char *const *sa, *const *sb;
558 
559 	sa = a;
560 	sb = b;
561 	/*
562 	 * This compares two var=value strings which creates a different
563 	 * order from what you would probably expect.  POSIX is somewhat
564 	 * ambiguous on what should be sorted exactly.
565 	 */
566 	return strcoll(*sa, *sb);
567 }
568 
569 
570 /*
571  * Command to list all variables which are set.  This is invoked from the
572  * set command when it is called without any options or operands.
573  */
574 
575 int
576 showvarscmd(int argc __unused, char **argv __unused)
577 {
578 	struct var **vpp;
579 	struct var *vp;
580 	const char *s;
581 	const char **vars;
582 	int i, n;
583 
584 	/*
585 	 * POSIX requires us to sort the variables.
586 	 */
587 	n = 0;
588 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
589 		for (vp = *vpp; vp; vp = vp->next) {
590 			if (!(vp->flags & VUNSET))
591 				n++;
592 		}
593 	}
594 
595 	INTON;
596 	vars = ckmalloc(n * sizeof(*vars));
597 	i = 0;
598 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
599 		for (vp = *vpp; vp; vp = vp->next) {
600 			if (!(vp->flags & VUNSET))
601 				vars[i++] = vp->text;
602 		}
603 	}
604 
605 	qsort(vars, n, sizeof(*vars), var_compare);
606 	for (i = 0; i < n; i++) {
607 		s = strchr(vars[i], '=');
608 		s++;
609 		outbin(vars[i], s - vars[i], out1);
610 		out1qstr(s);
611 		out1c('\n');
612 	}
613 	ckfree(vars);
614 	INTOFF;
615 
616 	return 0;
617 }
618 
619 
620 
621 /*
622  * The export and readonly commands.
623  */
624 
625 int
626 exportcmd(int argc, char **argv)
627 {
628 	struct var **vpp;
629 	struct var *vp;
630 	char *name;
631 	char *p;
632 	char *cmdname;
633 	int ch, values;
634 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
635 
636 	cmdname = argv[0];
637 	optreset = optind = 1;
638 	opterr = 0;
639 	values = 0;
640 	while ((ch = getopt(argc, argv, "p")) != -1) {
641 		switch (ch) {
642 		case 'p':
643 			values = 1;
644 			break;
645 		case '?':
646 		default:
647 			error("unknown option: -%c", optopt);
648 		}
649 	}
650 	argc -= optind;
651 	argv += optind;
652 
653 	if (values && argc != 0)
654 		error("-p requires no arguments");
655 	if (argc != 0) {
656 		while ((name = *argv++) != NULL) {
657 			if ((p = strchr(name, '=')) != NULL) {
658 				p++;
659 			} else {
660 				vp = find_var(name, NULL, NULL);
661 				if (vp != NULL) {
662 					vp->flags |= flag;
663 					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
664 						change_env(vp->text, 1);
665 						setlocale(LC_ALL, "");
666 						updatecharset();
667 					}
668 					continue;
669 				}
670 			}
671 			setvar(name, p, flag);
672 		}
673 	} else {
674 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
675 			for (vp = *vpp ; vp ; vp = vp->next) {
676 				if (vp->flags & flag) {
677 					if (values) {
678 						out1str(cmdname);
679 						out1c(' ');
680 					}
681 					if (values && !(vp->flags & VUNSET)) {
682 						outbin(vp->text,
683 						    vp->name_len + 1, out1);
684 						out1qstr(vp->text +
685 						    vp->name_len + 1);
686 					} else
687 						outbin(vp->text, vp->name_len,
688 						    out1);
689 					out1c('\n');
690 				}
691 			}
692 		}
693 	}
694 	return 0;
695 }
696 
697 
698 /*
699  * The "local" command.
700  */
701 
702 int
703 localcmd(int argc __unused, char **argv __unused)
704 {
705 	char *name;
706 
707 	if (! in_function())
708 		error("Not in a function");
709 	while ((name = *argptr++) != NULL) {
710 		mklocal(name);
711 	}
712 	return 0;
713 }
714 
715 
716 /*
717  * Make a variable a local variable.  When a variable is made local, it's
718  * value and flags are saved in a localvar structure.  The saved values
719  * will be restored when the shell function returns.  We handle the name
720  * "-" as a special case.
721  */
722 
723 void
724 mklocal(char *name)
725 {
726 	struct localvar *lvp;
727 	struct var **vpp;
728 	struct var *vp;
729 
730 	INTOFF;
731 	lvp = ckmalloc(sizeof (struct localvar));
732 	if (name[0] == '-' && name[1] == '\0') {
733 		lvp->text = ckmalloc(sizeof optlist);
734 		memcpy(lvp->text, optlist, sizeof optlist);
735 		vp = NULL;
736 	} else {
737 		vp = find_var(name, &vpp, NULL);
738 		if (vp == NULL) {
739 			if (strchr(name, '='))
740 				setvareq(savestr(name), VSTRFIXED);
741 			else
742 				setvar(name, NULL, VSTRFIXED);
743 			vp = *vpp;	/* the new variable */
744 			lvp->text = NULL;
745 			lvp->flags = VUNSET;
746 		} else {
747 			lvp->text = vp->text;
748 			lvp->flags = vp->flags;
749 			vp->flags |= VSTRFIXED|VTEXTFIXED;
750 			if (name[vp->name_len] == '=')
751 				setvareq(savestr(name), 0);
752 		}
753 	}
754 	lvp->vp = vp;
755 	lvp->next = localvars;
756 	localvars = lvp;
757 	INTON;
758 }
759 
760 
761 /*
762  * Called after a function returns.
763  */
764 
765 void
766 poplocalvars(void)
767 {
768 	struct localvar *lvp;
769 	struct var *vp;
770 
771 	while ((lvp = localvars) != NULL) {
772 		localvars = lvp->next;
773 		vp = lvp->vp;
774 		if (vp == NULL) {	/* $- saved */
775 			memcpy(optlist, lvp->text, sizeof optlist);
776 			ckfree(lvp->text);
777 			optschanged();
778 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
779 			unsetvar(vp->text);
780 		} else {
781 			if ((vp->flags & VTEXTFIXED) == 0)
782 				ckfree(vp->text);
783 			vp->flags = lvp->flags;
784 			vp->text = lvp->text;
785 		}
786 		ckfree(lvp);
787 	}
788 }
789 
790 
791 int
792 setvarcmd(int argc, char **argv)
793 {
794 	if (argc <= 2)
795 		return unsetcmd(argc, argv);
796 	else if (argc == 3)
797 		setvar(argv[1], argv[2], 0);
798 	else
799 		error("too many arguments");
800 	return 0;
801 }
802 
803 
804 /*
805  * The unset builtin command.
806  */
807 
808 int
809 unsetcmd(int argc __unused, char **argv __unused)
810 {
811 	char **ap;
812 	int i;
813 	int flg_func = 0;
814 	int flg_var = 0;
815 	int ret = 0;
816 
817 	while ((i = nextopt("vf")) != '\0') {
818 		if (i == 'f')
819 			flg_func = 1;
820 		else
821 			flg_var = 1;
822 	}
823 	if (flg_func == 0 && flg_var == 0)
824 		flg_var = 1;
825 
826 	for (ap = argptr; *ap ; ap++) {
827 		if (flg_func)
828 			ret |= unsetfunc(*ap);
829 		if (flg_var)
830 			ret |= unsetvar(*ap);
831 	}
832 	return ret;
833 }
834 
835 
836 /*
837  * Unset the specified variable.
838  */
839 
840 int
841 unsetvar(const char *s)
842 {
843 	struct var **vpp;
844 	struct var *vp;
845 
846 	vp = find_var(s, &vpp, NULL);
847 	if (vp == NULL)
848 		return (0);
849 	if (vp->flags & VREADONLY)
850 		return (1);
851 	INTOFF;
852 	if (vp->text[vp->name_len + 1] != '\0')
853 		setvar(s, nullstr, 0);
854 	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
855 		change_env(__DECONST(char *, s), 0);
856 		setlocale(LC_ALL, "");
857 		updatecharset();
858 	}
859 	vp->flags &= ~VEXPORT;
860 	vp->flags |= VUNSET;
861 	if ((vp->flags & VSTRFIXED) == 0) {
862 		if ((vp->flags & VTEXTFIXED) == 0)
863 			ckfree(vp->text);
864 		*vpp = vp->next;
865 		ckfree(vp);
866 	}
867 	INTON;
868 	return (0);
869 }
870 
871 
872 
873 /*
874  * Returns true if the two strings specify the same variable.  The first
875  * variable name is terminated by '='; the second may be terminated by
876  * either '=' or '\0'.
877  */
878 
879 static int
880 varequal(const char *p, const char *q)
881 {
882 	while (*p == *q++) {
883 		if (*p++ == '=')
884 			return 1;
885 	}
886 	if (*p == '=' && *(q - 1) == '\0')
887 		return 1;
888 	return 0;
889 }
890 
891 /*
892  * Search for a variable.
893  * 'name' may be terminated by '=' or a NUL.
894  * vppp is set to the pointer to vp, or the list head if vp isn't found
895  * lenp is set to the number of characters in 'name'
896  */
897 
898 static struct var *
899 find_var(const char *name, struct var ***vppp, int *lenp)
900 {
901 	unsigned int hashval;
902 	int len;
903 	struct var *vp, **vpp;
904 	const char *p = name;
905 
906 	hashval = 0;
907 	while (*p && *p != '=')
908 		hashval = 2 * hashval + (unsigned char)*p++;
909 	len = p - name;
910 
911 	if (lenp)
912 		*lenp = len;
913 	vpp = &vartab[hashval % VTABSIZE];
914 	if (vppp)
915 		*vppp = vpp;
916 
917 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
918 		if (vp->name_len != len)
919 			continue;
920 		if (memcmp(vp->text, name, len) != 0)
921 			continue;
922 		if (vppp)
923 			*vppp = vpp;
924 		return vp;
925 	}
926 	return NULL;
927 }
928