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