VirtualBox

source: kBuild/trunk/src/ash-messup/var.c@ 881

Last change on this file since 881 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

  • Property svn:eol-style set to native
File size: 19.1 KB
Line 
1/* $NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $");
43#endif
44#endif /* not lint */
45
46#include <unistd.h>
47#include <stdlib.h>
48#include <strings.h>
49#ifndef __sun__
50#include <paths.h>
51#else
52#define _PATH_DEFPATH "/usr/bin:/usr/sbin"
53#include <iso/limits_iso.h>
54#endif
55
56#ifdef PC_OS2_LIBPATHS
57#define INCL_BASE
58#include <os2.h>
59
60#ifndef LIBPATHSTRICT
61#define LIBPATHSTRICT 3
62#endif
63
64extern APIRET
65#ifdef APIENTRY
66 APIENTRY
67#endif
68 DosQueryHeaderInfo(HMODULE hmod, ULONG ulIndex, PVOID pvBuffer, ULONG cbBuffer, ULONG ulSubFunction);
69#define QHINF_EXEINFO 1 /* NE exeinfo. */
70#define QHINF_READRSRCTBL 2 /* Reads from the resource table. */
71#define QHINF_READFILE 3 /* Reads from the executable file. */
72#define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
73#define QHINF_LIBPATH 5 /* Gets the entire libpath. */
74#define QHINF_FIXENTRY 6 /* NE only */
75#define QHINF_STE 7 /* NE only */
76#define QHINF_MAPSEL 8 /* NE only */
77
78#endif
79
80
81/*
82 * Shell variables.
83 */
84
85#include "shell.h"
86#include "output.h"
87#include "expand.h"
88#include "nodes.h" /* for other headers */
89#include "eval.h" /* defines cmdenviron */
90#include "exec.h"
91#include "syntax.h"
92#include "options.h"
93#include "mail.h"
94#include "var.h"
95#include "memalloc.h"
96#include "error.h"
97#include "mystring.h"
98#include "parser.h"
99#include "show.h"
100#ifndef SMALL
101#include "myhistedit.h"
102#endif
103
104#ifdef SMALL
105#define VTABSIZE 39
106#else
107#define VTABSIZE 517
108#endif
109
110
111struct varinit {
112 struct var *var;
113 int flags;
114 const char *text;
115 void (*func)(const char *);
116};
117
118
119#if ATTY
120struct var vatty;
121#endif
122#ifndef SMALL
123struct var vhistsize;
124struct var vterm;
125#endif
126struct var vifs;
127struct var vmail;
128struct var vmpath;
129struct var vpath;
130#ifdef _MSC_VER
131struct var vpath2;
132#endif
133struct var vps1;
134struct var vps2;
135struct var vps4;
136struct var vvers;
137struct var voptind;
138
139#ifdef PC_OS2_LIBPATHS
140static struct var libpath_vars[4];
141static const char *libpath_envs[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
142#endif
143
144const struct varinit varinit[] = {
145#if ATTY
146 { &vatty, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=",
147 NULL },
148#endif
149#ifndef SMALL
150 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
151 sethistsize },
152#endif
153 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
154 NULL },
155 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
156 NULL },
157 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
158 NULL },
159 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
160 changepath },
161#ifdef _MSC_VER
162 { &vpath2, VSTRFIXED|VTEXTFIXED, "Path=",
163 changepath },
164#endif
165 /*
166 * vps1 depends on uid
167 */
168 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
169 NULL },
170 { &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ",
171 NULL },
172#ifndef SMALL
173 { &vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
174 setterm },
175#endif
176 { &voptind, VSTRFIXED|VTEXTFIXED|VNOFUNC, "OPTIND=1",
177 getoptsreset },
178 { NULL, 0, NULL,
179 NULL }
180};
181
182struct var *vartab[VTABSIZE];
183
184STATIC int strequal(const char *, const char *);
185STATIC struct var *find_var(const char *, struct var ***, int *);
186
187/*
188 * Initialize the varable symbol tables and import the environment
189 */
190
191#ifdef mkinit
192INCLUDE "var.h"
193MKINIT char **environ;
194INIT {
195 char **envp;
196
197 initvar();
198 for (envp = environ ; *envp ; envp++) {
199 if (strchr(*envp, '=')) {
200 setvareq(*envp, VEXPORT|VTEXTFIXED);
201 }
202 }
203}
204#endif
205
206
207/*
208 * This routine initializes the builtin variables. It is called when the
209 * shell is initialized and again when a shell procedure is spawned.
210 */
211
212void
213initvar(void)
214{
215 const struct varinit *ip;
216 struct var *vp;
217 struct var **vpp;
218
219#ifdef PC_OS2_LIBPATHS
220 char *psz = ckmalloc(2048);
221 int rc;
222 int i;
223 for (i = 0; i < 4; i++)
224 {
225 libpath_vars[i].flags = VSTRFIXED | VOS2LIBPATH;
226 libpath_vars[i].func = NULL;
227
228 if (i > 0)
229 rc = DosQueryExtLIBPATH(psz, i);
230 else
231 {
232 rc = DosQueryHeaderInfo(NULLHANDLE, 0, psz, 2048, QHINF_LIBPATH);
233 libpath_vars[i].flags |= VREADONLY;
234 }
235 if (!rc && *psz)
236 {
237 int cch1 = strlen(libpath_envs[i]);
238 int cch2 = strlen(psz) + 1;
239 libpath_vars[i].text = ckmalloc(cch1 + cch2);
240 memcpy(libpath_vars[i].text, libpath_envs[i], cch1);
241 memcpy(libpath_vars[i].text + cch1, psz, cch2);
242 }
243 else
244 {
245 libpath_vars[i].flags |= VUNSET | VTEXTFIXED;
246 libpath_vars[i].text = (char*)libpath_envs[i];
247 }
248 if (find_var(libpath_vars[i].text, &vpp, &libpath_vars[i].name_len) != NULL)
249 continue;
250 libpath_vars[i].next = *vpp;
251 *vpp = &libpath_vars[i];
252 }
253 free(psz);
254#endif
255
256 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
257 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
258 continue;
259 vp->next = *vpp;
260 *vpp = vp;
261 vp->text = strdup(ip->text);
262 vp->flags = ip->flags;
263 vp->func = ip->func;
264 }
265 /*
266 * PS1 depends on uid
267 */
268 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
269 vps1.next = *vpp;
270 *vpp = &vps1;
271 vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
272 vps1.flags = VSTRFIXED|VTEXTFIXED;
273 }
274}
275
276/*
277 * Safe version of setvar, returns 1 on success 0 on failure.
278 */
279
280int
281setvarsafe(const char *name, const char *val, int flags)
282{
283 struct jmploc jmploc;
284 struct jmploc *volatile savehandler = handler;
285 int err = 0;
286#ifdef __GNUC__
287 (void) &err;
288#endif
289
290 if (setjmp(jmploc.loc))
291 err = 1;
292 else {
293 handler = &jmploc;
294 setvar(name, val, flags);
295 }
296 handler = savehandler;
297 return err;
298}
299
300/*
301 * Set the value of a variable. The flags argument is ored with the
302 * flags of the variable. If val is NULL, the variable is unset.
303 */
304
305void
306setvar(const char *name, const char *val, int flags)
307{
308 const char *p;
309 const char *q;
310 char *d;
311 int len;
312 int namelen;
313 char *nameeq;
314 int isbad;
315
316 isbad = 0;
317 p = name;
318 if (! is_name(*p))
319 isbad = 1;
320 p++;
321 for (;;) {
322 if (! is_in_name(*p)) {
323 if (*p == '\0' || *p == '=')
324 break;
325 isbad = 1;
326 }
327 p++;
328 }
329 namelen = p - name;
330 if (isbad)
331 error("%.*s: bad variable name", namelen, name);
332 len = namelen + 2; /* 2 is space for '=' and '\0' */
333 if (val == NULL) {
334 flags |= VUNSET;
335 } else {
336 len += strlen(val);
337 }
338 d = nameeq = ckmalloc(len);
339 q = name;
340 while (--namelen >= 0)
341 *d++ = *q++;
342 *d++ = '=';
343 *d = '\0';
344 if (val)
345 scopy(val, d);
346 setvareq(nameeq, flags);
347}
348
349
350
351/*
352 * Same as setvar except that the variable and value are passed in
353 * the first argument as name=value. Since the first argument will
354 * be actually stored in the table, it should not be a string that
355 * will go away.
356 */
357
358void
359setvareq(char *s, int flags)
360{
361 struct var *vp, **vpp;
362 int nlen;
363
364 if (aflag)
365 flags |= VEXPORT;
366 vp = find_var(s, &vpp, &nlen);
367 if (vp != NULL) {
368 if (vp->flags & VREADONLY)
369 error("%.*s: is read only", vp->name_len, s);
370 if (flags & VNOSET)
371 return;
372 INTOFF;
373
374 if (vp->func && (flags & VNOFUNC) == 0)
375 (*vp->func)(s + vp->name_len + 1);
376
377 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
378 ckfree(vp->text);
379
380 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
381 vp->flags |= flags & ~VNOFUNC;
382 vp->text = s;
383#ifdef PC_OS2_LIBPATHS
384 if ((vp->flags & VOS2LIBPATH) && (vp->flags & VEXPORT))
385 vp->flags &= ~VEXPORT;
386#endif
387
388 /*
389 * We could roll this to a function, to handle it as
390 * a regular variable function callback, but why bother?
391 */
392 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
393 chkmail(1);
394 INTON;
395 return;
396 }
397 /* not found */
398 if (flags & VNOSET)
399 return;
400 vp = ckmalloc(sizeof (*vp));
401 vp->flags = flags & ~VNOFUNC;
402 vp->text = s;
403 vp->name_len = nlen;
404 vp->next = *vpp;
405 vp->func = NULL;
406 *vpp = vp;
407}
408
409
410
411/*
412 * Process a linked list of variable assignments.
413 */
414
415void
416listsetvar(struct strlist *list, int flags)
417{
418 struct strlist *lp;
419
420 INTOFF;
421 for (lp = list ; lp ; lp = lp->next) {
422 setvareq(savestr(lp->text), flags);
423 }
424 INTON;
425}
426
427void
428listmklocal(struct strlist *list, int flags)
429{
430 struct strlist *lp;
431
432 for (lp = list ; lp ; lp = lp->next)
433 mklocal(lp->text, flags);
434}
435
436
437/*
438 * Find the value of a variable. Returns NULL if not set.
439 */
440
441char *
442lookupvar(const char *name)
443{
444 struct var *v;
445
446 v = find_var(name, NULL, NULL);
447 if (v == NULL || v->flags & VUNSET)
448 return NULL;
449 return v->text + v->name_len + 1;
450}
451
452
453
454/*
455 * Search the environment of a builtin command. If the second argument
456 * is nonzero, return the value of a variable even if it hasn't been
457 * exported.
458 */
459
460char *
461bltinlookup(const char *name, int doall)
462{
463 struct strlist *sp;
464 struct var *v;
465
466 for (sp = cmdenviron ; sp ; sp = sp->next) {
467 if (strequal(sp->text, name))
468 return strchr(sp->text, '=') + 1;
469 }
470
471 v = find_var(name, NULL, NULL);
472
473 if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
474 return NULL;
475 return v->text + v->name_len + 1;
476}
477
478
479
480/*
481 * Generate a list of exported variables. This routine is used to construct
482 * the third argument to execve when executing a program.
483 */
484
485char **
486environment(void)
487{
488 int nenv;
489 struct var **vpp;
490 struct var *vp;
491 char **env;
492 char **ep;
493
494 nenv = 0;
495 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
496 for (vp = *vpp ; vp ; vp = vp->next)
497 if (vp->flags & VEXPORT)
498 nenv++;
499 }
500 ep = env = stalloc((nenv + 1) * sizeof *env);
501 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
502 for (vp = *vpp ; vp ; vp = vp->next)
503 if (vp->flags & VEXPORT)
504 *ep++ = vp->text;
505 }
506 *ep = NULL;
507
508#ifdef PC_OS2_LIBPATHS
509 /*
510 * Set the libpaths now as this is exec() time.
511 */
512 for (nenv = 0; nenv < 3; nenv++)
513 DosSetExtLIBPATH(strchr(libpath_vars[nenv].text, '=') + 1, nenv);
514#endif
515
516 return env;
517}
518
519
520/*
521 * Called when a shell procedure is invoked to clear out nonexported
522 * variables. It is also necessary to reallocate variables of with
523 * VSTACK set since these are currently allocated on the stack.
524 */
525
526#ifdef mkinit
527void shprocvar(void);
528
529SHELLPROC {
530 shprocvar();
531}
532#endif
533
534void
535shprocvar(void)
536{
537 struct var **vpp;
538 struct var *vp, **prev;
539
540 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
541 for (prev = vpp ; (vp = *prev) != NULL ; ) {
542 if ((vp->flags & VEXPORT) == 0) {
543 *prev = vp->next;
544 if ((vp->flags & VTEXTFIXED) == 0)
545 ckfree(vp->text);
546 if ((vp->flags & VSTRFIXED) == 0)
547 ckfree(vp);
548 } else {
549 if (vp->flags & VSTACK) {
550 vp->text = savestr(vp->text);
551 vp->flags &=~ VSTACK;
552 }
553 prev = &vp->next;
554 }
555 }
556 }
557 initvar();
558}
559
560
561
562/*
563 * Command to list all variables which are set. Currently this command
564 * is invoked from the set command when the set command is called without
565 * any variables.
566 */
567
568void
569print_quoted(const char *p)
570{
571 const char *q;
572
573 if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
574 out1fmt("%s", p);
575 return;
576 }
577 while (*p) {
578 if (*p == '\'') {
579 out1fmt("\\'");
580 p++;
581 continue;
582 }
583 q = index(p, '\'');
584 if (!q) {
585 out1fmt("'%s'", p );
586 return;
587 }
588 out1fmt("'%.*s'", (int)(q - p), p );
589 p = q;
590 }
591}
592
593static int
594sort_var(const void *v_v1, const void *v_v2)
595{
596 const struct var * const *v1 = v_v1;
597 const struct var * const *v2 = v_v2;
598
599 /* XXX Will anyone notice we include the '=' of the shorter name? */
600 return strcoll((*v1)->text, (*v2)->text);
601}
602
603/*
604 * POSIX requires that 'set' (but not export or readonly) output the
605 * variables in lexicographic order - by the locale's collating order (sigh).
606 * Maybe we could keep them in an ordered balanced binary tree
607 * instead of hashed lists.
608 * For now just roll 'em through qsort for printing...
609 */
610
611int
612showvars(const char *name, int flag, int show_value)
613{
614 struct var **vpp;
615 struct var *vp;
616 const char *p;
617
618 static struct var **list; /* static in case we are interrupted */
619 static int list_len;
620 int count = 0;
621
622 if (!list) {
623 list_len = 32;
624 list = ckmalloc(list_len * sizeof *list);
625 }
626
627 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
628 for (vp = *vpp ; vp ; vp = vp->next) {
629 if (flag && !(vp->flags & flag))
630 continue;
631 if (vp->flags & VUNSET && !(show_value & 2))
632 continue;
633 if (count >= list_len) {
634 list = ckrealloc(list,
635 (list_len << 1) * sizeof *list);
636 list_len <<= 1;
637 }
638 list[count++] = vp;
639 }
640 }
641
642 qsort(list, count, sizeof *list, sort_var);
643
644 for (vpp = list; count--; vpp++) {
645 vp = *vpp;
646 if (name)
647 out1fmt("%s ", name);
648 for (p = vp->text ; *p != '=' ; p++)
649 out1c(*p);
650 if (!(vp->flags & VUNSET) && show_value) {
651 out1fmt("=");
652 print_quoted(++p);
653 }
654 out1c('\n');
655 }
656 return 0;
657}
658
659
660
661/*
662 * The export and readonly commands.
663 */
664
665int
666exportcmd(int argc, char **argv)
667{
668 struct var *vp;
669 char *name;
670 const char *p;
671 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
672 int pflag;
673
674 pflag = nextopt("p") == 'p' ? 3 : 0;
675 if (argc <= 1 || pflag) {
676 showvars( pflag ? argv[0] : 0, flag, pflag );
677 return 0;
678 }
679
680 while ((name = *argptr++) != NULL) {
681 if ((p = strchr(name, '=')) != NULL) {
682 p++;
683 } else {
684 vp = find_var(name, NULL, NULL);
685 if (vp != NULL) {
686 vp->flags |= flag;
687 continue;
688 }
689 }
690 setvar(name, p, flag);
691 }
692 return 0;
693}
694
695
696/*
697 * The "local" command.
698 */
699
700int
701localcmd(int argc, char **argv)
702{
703 char *name;
704
705 if (! in_function())
706 error("Not in a function");
707 while ((name = *argptr++) != NULL) {
708 mklocal(name, 0);
709 }
710 return 0;
711}
712
713
714/*
715 * Make a variable a local variable. When a variable is made local, it's
716 * value and flags are saved in a localvar structure. The saved values
717 * will be restored when the shell function returns. We handle the name
718 * "-" as a special case.
719 */
720
721void
722mklocal(const char *name, int flags)
723{
724 struct localvar *lvp;
725 struct var **vpp;
726 struct var *vp;
727
728 INTOFF;
729 lvp = ckmalloc(sizeof (struct localvar));
730 if (name[0] == '-' && name[1] == '\0') {
731 char *p;
732 p = ckmalloc(sizeof_optlist);
733 lvp->text = memcpy(p, optlist, sizeof_optlist);
734 vp = NULL;
735 } else {
736 vp = find_var(name, &vpp, NULL);
737 if (vp == NULL) {
738 if (strchr(name, '='))
739 setvareq(savestr(name), VSTRFIXED|flags);
740 else
741 setvar(name, NULL, VSTRFIXED|flags);
742 vp = *vpp; /* the new variable */
743 lvp->text = NULL;
744 lvp->flags = VUNSET;
745 } else {
746 lvp->text = vp->text;
747 lvp->flags = vp->flags;
748 vp->flags |= VSTRFIXED|VTEXTFIXED;
749 if (name[vp->name_len] == '=')
750 setvareq(savestr(name), flags);
751 }
752 }
753 lvp->vp = vp;
754 lvp->next = localvars;
755 localvars = lvp;
756 INTON;
757}
758
759
760/*
761 * Called after a function returns.
762 */
763
764void
765poplocalvars(void)
766{
767 struct localvar *lvp;
768 struct var *vp;
769
770 while ((lvp = localvars) != NULL) {
771 localvars = lvp->next;
772 vp = lvp->vp;
773 TRACE(("poplocalvar %s", vp ? vp->text : "-"));
774 if (vp == NULL) { /* $- saved */
775 memcpy(optlist, lvp->text, sizeof_optlist);
776 ckfree(lvp->text);
777 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
778 (void)unsetvar(vp->text, 0);
779 } else {
780 if (vp->func && (vp->flags & VNOFUNC) == 0)
781 (*vp->func)(lvp->text + vp->name_len + 1);
782 if ((vp->flags & VTEXTFIXED) == 0)
783 ckfree(vp->text);
784 vp->flags = lvp->flags;
785 vp->text = lvp->text;
786 }
787 ckfree(lvp);
788 }
789}
790
791
792int
793setvarcmd(int argc, char **argv)
794{
795 if (argc <= 2)
796 return unsetcmd(argc, argv);
797 else if (argc == 3)
798 setvar(argv[1], argv[2], 0);
799 else
800 error("List assignment not implemented");
801 return 0;
802}
803
804
805/*
806 * The unset builtin command. We unset the function before we unset the
807 * variable to allow a function to be unset when there is a readonly variable
808 * with the same name.
809 */
810
811int
812unsetcmd(int argc, char **argv)
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("evf")) != '\0') {
821 if (i == 'f')
822 flg_func = 1;
823 else
824 flg_var = i;
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, flg_var == 'e');
834 }
835 return ret;
836}
837
838
839/*
840 * Unset the specified variable.
841 */
842
843int
844unsetvar(const char *s, int unexport)
845{
846 struct var **vpp;
847 struct var *vp;
848
849 vp = find_var(s, &vpp, NULL);
850 if (vp == NULL)
851 return 1;
852
853 if (vp->flags & VREADONLY)
854 return (1);
855
856 INTOFF;
857 if (unexport) {
858 vp->flags &= ~VEXPORT;
859 } else {
860 if (vp->text[vp->name_len + 1] != '\0')
861 setvar(s, nullstr, 0);
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 }
871 INTON;
872 return 0;
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
882STATIC int
883strequal(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
901STATIC struct var *
902find_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}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette