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