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 --- 22 unchanged lines hidden (view full) --- 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 200956 2009-12-24 18:41:14Z jilles $"); |
40 41#include <unistd.h> 42#include <stdlib.h> 43#include <paths.h> 44 45/* 46 * Shell variables. 47 */ --- 69 unchanged lines hidden (view full) --- 117 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1", 118 getoptsreset }, 119 { NULL, 0, NULL, 120 NULL } 121}; 122 123STATIC struct var *vartab[VTABSIZE]; 124 |
125STATIC struct var **hashvar(const char *); 126STATIC int varequal(const char *, const char *); 127STATIC int localevar(const char *); |
128 129/* 130 * Initialize the variable symbol tables and import the environment. 131 */ 132 133#ifdef mkinit 134INCLUDE "var.h" 135INIT { --- 49 unchanged lines hidden (view full) --- 185 } 186} 187 188/* 189 * Safe version of setvar, returns 1 on success 0 on failure. 190 */ 191 192int |
193setvarsafe(const char *name, const char *val, int flags) |
194{ 195 struct jmploc jmploc; 196 struct jmploc *const savehandler = handler; 197 int err = 0; 198 int inton; 199 200 inton = is_int_on(); 201 if (setjmp(jmploc.loc)) --- 8 unchanged lines hidden (view full) --- 210} 211 212/* 213 * Set the value of a variable. The flags argument is stored with the 214 * flags of the variable. If val is NULL, the variable is unset. 215 */ 216 217void |
218setvar(const char *name, const char *val, int flags) |
219{ |
220 const char *p; |
221 int len; 222 int namelen; 223 char *nameeq; 224 int isbad; 225 226 isbad = 0; 227 p = name; 228 if (! is_name(*p)) --- 11 unchanged lines hidden (view full) --- 240 if (isbad) 241 error("%.*s: bad variable name", namelen, name); 242 len = namelen + 2; /* 2 is space for '=' and '\0' */ 243 if (val == NULL) { 244 flags |= VUNSET; 245 } else { 246 len += strlen(val); 247 } |
248 nameeq = ckmalloc(len); 249 memcpy(nameeq, name, namelen); 250 nameeq[namelen] = '='; |
251 if (val) |
252 scopy(val, nameeq + namelen + 1); 253 else 254 nameeq[namelen + 1] = '\0'; |
255 setvareq(nameeq, flags); 256} 257 258STATIC int |
259localevar(const char *s) |
260{ 261 static char *lnames[7] = { 262 "ALL", "COLLATE", "CTYPE", "MONETARY", 263 "NUMERIC", "TIME", NULL 264 }; 265 char **ss; 266 267 if (*s != 'L') --- 9 unchanged lines hidden (view full) --- 277} 278 279 280/* 281 * Sets/unsets an environment variable from a pointer that may actually be a 282 * pointer into environ where the string should not be manipulated. 283 */ 284static void |
285change_env(const char *s, int set) |
286{ 287 char *eqp; 288 char *ss; 289 290 ss = savestr(s); 291 if ((eqp = strchr(ss, '=')) != NULL) 292 *eqp = '\0'; 293 if (set && eqp != NULL) --- 89 unchanged lines hidden (view full) --- 383 384 385 386/* 387 * Find the value of a variable. Returns NULL if not set. 388 */ 389 390char * |
391lookupvar(const char *name) |
392{ 393 struct var *v; 394 395 for (v = *hashvar(name) ; v ; v = v->next) { 396 if (varequal(v->text, name)) { 397 if (v->flags & VUNSET) 398 return NULL; 399 return strchr(v->text, '=') + 1; --- 6 unchanged lines hidden (view full) --- 406 407/* 408 * Search the environment of a builtin command. If the second argument 409 * is nonzero, return the value of a variable even if it hasn't been 410 * exported. 411 */ 412 413char * |
414bltinlookup(const char *name, int doall) |
415{ 416 struct strlist *sp; 417 struct var *v; 418 419 for (sp = cmdenviron ; sp ; sp = sp->next) { 420 if (varequal(sp->text, name)) 421 return strchr(sp->text, '=') + 1; 422 } --- 367 unchanged lines hidden (view full) --- 790} 791 792 793/* 794 * Unset the specified variable. 795 */ 796 797int |
798unsetvar(const char *s) |
799{ 800 struct var **vpp; 801 struct var *vp; 802 803 vpp = hashvar(s); 804 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 805 if (varequal(vp->text, s)) { 806 if (vp->flags & VREADONLY) --- 23 unchanged lines hidden (view full) --- 830 831 832 833/* 834 * Find the appropriate entry in the hash table from the name. 835 */ 836 837STATIC struct var ** |
838hashvar(const char *p) |
839{ 840 unsigned int hashval; 841 842 hashval = ((unsigned char) *p) << 4; 843 while (*p && *p != '=') 844 hashval += (unsigned char) *p++; 845 return &vartab[hashval % VTABSIZE]; 846} 847 848 849 850/* 851 * Returns true if the two strings specify the same varable. The first 852 * variable name is terminated by '='; the second may be terminated by 853 * either '=' or '\0'. 854 */ 855 856STATIC int |
857varequal(const char *p, const char *q) |
858{ 859 while (*p == *q++) { 860 if (*p++ == '=') 861 return 1; 862 } 863 if (*p == '=' && *(q - 1) == '\0') 864 return 1; 865 return 0; 866} |