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.58 2011/05/08 16:15:50 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 /* 518 * Generate a list of exported variables. This routine is used to construct 519 * the third argument to execve when executing a program. 520 */ 521 522 char ** 523 environment(void) 524 { 525 int nenv; 526 struct var **vpp; 527 struct var *vp; 528 char **env, **ep; 529 530 nenv = 0; 531 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 532 for (vp = *vpp ; vp ; vp = vp->next) 533 if (vp->flags & VEXPORT) 534 nenv++; 535 } 536 ep = env = stalloc((nenv + 1) * sizeof *env); 537 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 538 for (vp = *vpp ; vp ; vp = vp->next) 539 if (vp->flags & VEXPORT) 540 *ep++ = vp->text; 541 } 542 *ep = NULL; 543 return env; 544 } 545 546 547 static int 548 var_compare(const void *a, const void *b) 549 { 550 const char *const *sa, *const *sb; 551 552 sa = a; 553 sb = b; 554 /* 555 * This compares two var=value strings which creates a different 556 * order from what you would probably expect. POSIX is somewhat 557 * ambiguous on what should be sorted exactly. 558 */ 559 return strcoll(*sa, *sb); 560 } 561 562 563 /* 564 * Command to list all variables which are set. This is invoked from the 565 * set command when it is called without any options or operands. 566 */ 567 568 int 569 showvarscmd(int argc __unused, char **argv __unused) 570 { 571 struct var **vpp; 572 struct var *vp; 573 const char *s; 574 const char **vars; 575 int i, n; 576 577 /* 578 * POSIX requires us to sort the variables. 579 */ 580 n = 0; 581 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 582 for (vp = *vpp; vp; vp = vp->next) { 583 if (!(vp->flags & VUNSET)) 584 n++; 585 } 586 } 587 588 INTON; 589 vars = ckmalloc(n * sizeof(*vars)); 590 i = 0; 591 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 592 for (vp = *vpp; vp; vp = vp->next) { 593 if (!(vp->flags & VUNSET)) 594 vars[i++] = vp->text; 595 } 596 } 597 598 qsort(vars, n, sizeof(*vars), var_compare); 599 for (i = 0; i < n; i++) { 600 s = strchr(vars[i], '='); 601 s++; 602 outbin(vars[i], s - vars[i], out1); 603 out1qstr(s); 604 out1c('\n'); 605 } 606 ckfree(vars); 607 INTOFF; 608 609 return 0; 610 } 611 612 613 614 /* 615 * The export and readonly commands. 616 */ 617 618 int 619 exportcmd(int argc, char **argv) 620 { 621 struct var **vpp; 622 struct var *vp; 623 char *name; 624 char *p; 625 char *cmdname; 626 int ch, values; 627 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 628 629 cmdname = argv[0]; 630 optreset = optind = 1; 631 opterr = 0; 632 values = 0; 633 while ((ch = getopt(argc, argv, "p")) != -1) { 634 switch (ch) { 635 case 'p': 636 values = 1; 637 break; 638 case '?': 639 default: 640 error("unknown option: -%c", optopt); 641 } 642 } 643 argc -= optind; 644 argv += optind; 645 646 if (values && argc != 0) 647 error("-p requires no arguments"); 648 if (argc != 0) { 649 while ((name = *argv++) != NULL) { 650 if ((p = strchr(name, '=')) != NULL) { 651 p++; 652 } else { 653 vp = find_var(name, NULL, NULL); 654 if (vp != NULL) { 655 vp->flags |= flag; 656 if ((vp->flags & VEXPORT) && localevar(vp->text)) { 657 change_env(vp->text, 1); 658 setlocale(LC_ALL, ""); 659 updatecharset(); 660 } 661 continue; 662 } 663 } 664 setvar(name, p, flag); 665 } 666 } else { 667 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 668 for (vp = *vpp ; vp ; vp = vp->next) { 669 if (vp->flags & flag) { 670 if (values) { 671 out1str(cmdname); 672 out1c(' '); 673 } 674 p = strchr(vp->text, '='); 675 if (values && !(vp->flags & VUNSET)) { 676 p++; 677 outbin(vp->text, p - vp->text, 678 out1); 679 out1qstr(p); 680 } else 681 outbin(vp->text, p - vp->text, 682 out1); 683 out1c('\n'); 684 } 685 } 686 } 687 } 688 return 0; 689 } 690 691 692 /* 693 * The "local" command. 694 */ 695 696 int 697 localcmd(int argc __unused, char **argv __unused) 698 { 699 char *name; 700 701 if (! in_function()) 702 error("Not in a function"); 703 while ((name = *argptr++) != NULL) { 704 mklocal(name); 705 } 706 return 0; 707 } 708 709 710 /* 711 * Make a variable a local variable. When a variable is made local, it's 712 * value and flags are saved in a localvar structure. The saved values 713 * will be restored when the shell function returns. We handle the name 714 * "-" as a special case. 715 */ 716 717 void 718 mklocal(char *name) 719 { 720 struct localvar *lvp; 721 struct var **vpp; 722 struct var *vp; 723 724 INTOFF; 725 lvp = ckmalloc(sizeof (struct localvar)); 726 if (name[0] == '-' && name[1] == '\0') { 727 lvp->text = ckmalloc(sizeof optlist); 728 memcpy(lvp->text, optlist, sizeof optlist); 729 vp = NULL; 730 } else { 731 vp = find_var(name, &vpp, NULL); 732 if (vp == NULL) { 733 if (strchr(name, '=')) 734 setvareq(savestr(name), VSTRFIXED); 735 else 736 setvar(name, NULL, VSTRFIXED); 737 vp = *vpp; /* the new variable */ 738 lvp->text = NULL; 739 lvp->flags = VUNSET; 740 } else { 741 lvp->text = vp->text; 742 lvp->flags = vp->flags; 743 vp->flags |= VSTRFIXED|VTEXTFIXED; 744 if (name[vp->name_len] == '=') 745 setvareq(savestr(name), 0); 746 } 747 } 748 lvp->vp = vp; 749 lvp->next = localvars; 750 localvars = lvp; 751 INTON; 752 } 753 754 755 /* 756 * Called after a function returns. 757 */ 758 759 void 760 poplocalvars(void) 761 { 762 struct localvar *lvp; 763 struct var *vp; 764 765 while ((lvp = localvars) != NULL) { 766 localvars = lvp->next; 767 vp = lvp->vp; 768 if (vp == NULL) { /* $- saved */ 769 memcpy(optlist, lvp->text, sizeof optlist); 770 ckfree(lvp->text); 771 optschanged(); 772 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 773 unsetvar(vp->text); 774 } else { 775 if ((vp->flags & VTEXTFIXED) == 0) 776 ckfree(vp->text); 777 vp->flags = lvp->flags; 778 vp->text = lvp->text; 779 } 780 ckfree(lvp); 781 } 782 } 783 784 785 int 786 setvarcmd(int argc, char **argv) 787 { 788 if (argc <= 2) 789 return unsetcmd(argc, argv); 790 else if (argc == 3) 791 setvar(argv[1], argv[2], 0); 792 else 793 error("too many arguments"); 794 return 0; 795 } 796 797 798 /* 799 * The unset builtin command. 800 */ 801 802 int 803 unsetcmd(int argc __unused, char **argv __unused) 804 { 805 char **ap; 806 int i; 807 int flg_func = 0; 808 int flg_var = 0; 809 int ret = 0; 810 811 while ((i = nextopt("vf")) != '\0') { 812 if (i == 'f') 813 flg_func = 1; 814 else 815 flg_var = 1; 816 } 817 if (flg_func == 0 && flg_var == 0) 818 flg_var = 1; 819 820 for (ap = argptr; *ap ; ap++) { 821 if (flg_func) 822 ret |= unsetfunc(*ap); 823 if (flg_var) 824 ret |= unsetvar(*ap); 825 } 826 return ret; 827 } 828 829 830 /* 831 * Unset the specified variable. 832 */ 833 834 int 835 unsetvar(const char *s) 836 { 837 struct var **vpp; 838 struct var *vp; 839 840 vp = find_var(s, &vpp, NULL); 841 if (vp == NULL) 842 return (0); 843 if (vp->flags & VREADONLY) 844 return (1); 845 INTOFF; 846 if (vp->text[vp->name_len + 1] != '\0') 847 setvar(s, nullstr, 0); 848 if ((vp->flags & VEXPORT) && localevar(vp->text)) { 849 change_env(__DECONST(char *, s), 0); 850 setlocale(LC_ALL, ""); 851 updatecharset(); 852 } 853 vp->flags &= ~VEXPORT; 854 vp->flags |= VUNSET; 855 if ((vp->flags & VSTRFIXED) == 0) { 856 if ((vp->flags & VTEXTFIXED) == 0) 857 ckfree(vp->text); 858 *vpp = vp->next; 859 ckfree(vp); 860 } 861 INTON; 862 return (0); 863 } 864 865 866 867 /* 868 * Returns true if the two strings specify the same varable. The first 869 * variable name is terminated by '='; the second may be terminated by 870 * either '=' or '\0'. 871 */ 872 873 static int 874 varequal(const char *p, const char *q) 875 { 876 while (*p == *q++) { 877 if (*p++ == '=') 878 return 1; 879 } 880 if (*p == '=' && *(q - 1) == '\0') 881 return 1; 882 return 0; 883 } 884 885 /* 886 * Search for a variable. 887 * 'name' may be terminated by '=' or a NUL. 888 * vppp is set to the pointer to vp, or the list head if vp isn't found 889 * lenp is set to the number of charactets in 'name' 890 */ 891 892 static struct var * 893 find_var(const char *name, struct var ***vppp, int *lenp) 894 { 895 unsigned int hashval; 896 int len; 897 struct var *vp, **vpp; 898 const char *p = name; 899 900 hashval = 0; 901 while (*p && *p != '=') 902 hashval = 2 * hashval + (unsigned char)*p++; 903 len = p - name; 904 905 if (lenp) 906 *lenp = len; 907 vpp = &vartab[hashval % VTABSIZE]; 908 if (vppp) 909 *vppp = vpp; 910 911 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 912 if (vp->name_len != len) 913 continue; 914 if (memcmp(vp->text, name, len) != 0) 915 continue; 916 if (vppp) 917 *vppp = vpp; 918 return vp; 919 } 920 return NULL; 921 } 922