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