Deleted Added
full compact
var.c (200943) var.c (200956)
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>
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 200943 2009-12-24 15:14:22Z jilles $");
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
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(char *);
126STATIC int varequal(char *, char *);
127STATIC int localevar(char *);
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
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(char *name, char *val, int flags)
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
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(char *name, char *val, int flags)
218setvar(const char *name, const char *val, int flags)
219{
219{
220 char *p, *q;
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 }
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 p = nameeq = ckmalloc(len);
249 q = name;
250 while (--namelen >= 0)
251 *p++ = *q++;
252 *p++ = '=';
253 *p = '\0';
248 nameeq = ckmalloc(len);
249 memcpy(nameeq, name, namelen);
250 nameeq[namelen] = '=';
254 if (val)
251 if (val)
255 scopy(val, p);
252 scopy(val, nameeq + namelen + 1);
253 else
254 nameeq[namelen + 1] = '\0';
256 setvareq(nameeq, flags);
257}
258
259STATIC int
255 setvareq(nameeq, flags);
256}
257
258STATIC int
260localevar(char *s)
259localevar(const char *s)
261{
262 static char *lnames[7] = {
263 "ALL", "COLLATE", "CTYPE", "MONETARY",
264 "NUMERIC", "TIME", NULL
265 };
266 char **ss;
267
268 if (*s != 'L')

--- 9 unchanged lines hidden (view full) ---

278}
279
280
281/*
282 * Sets/unsets an environment variable from a pointer that may actually be a
283 * pointer into environ where the string should not be manipulated.
284 */
285static void
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
286change_env(char *s, int set)
285change_env(const char *s, int set)
287{
288 char *eqp;
289 char *ss;
290
291 ss = savestr(s);
292 if ((eqp = strchr(ss, '=')) != NULL)
293 *eqp = '\0';
294 if (set && eqp != NULL)

--- 89 unchanged lines hidden (view full) ---

384
385
386
387/*
388 * Find the value of a variable. Returns NULL if not set.
389 */
390
391char *
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 *
392lookupvar(char *name)
391lookupvar(const char *name)
393{
394 struct var *v;
395
396 for (v = *hashvar(name) ; v ; v = v->next) {
397 if (varequal(v->text, name)) {
398 if (v->flags & VUNSET)
399 return NULL;
400 return strchr(v->text, '=') + 1;

--- 6 unchanged lines hidden (view full) ---

407
408/*
409 * Search the environment of a builtin command. If the second argument
410 * is nonzero, return the value of a variable even if it hasn't been
411 * exported.
412 */
413
414char *
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 *
415bltinlookup(char *name, int doall)
414bltinlookup(const char *name, int doall)
416{
417 struct strlist *sp;
418 struct var *v;
419
420 for (sp = cmdenviron ; sp ; sp = sp->next) {
421 if (varequal(sp->text, name))
422 return strchr(sp->text, '=') + 1;
423 }

--- 367 unchanged lines hidden (view full) ---

791}
792
793
794/*
795 * Unset the specified variable.
796 */
797
798int
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
799unsetvar(char *s)
798unsetvar(const char *s)
800{
801 struct var **vpp;
802 struct var *vp;
803
804 vpp = hashvar(s);
805 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
806 if (varequal(vp->text, s)) {
807 if (vp->flags & VREADONLY)

--- 23 unchanged lines hidden (view full) ---

831
832
833
834/*
835 * Find the appropriate entry in the hash table from the name.
836 */
837
838STATIC struct var **
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 **
839hashvar(char *p)
838hashvar(const char *p)
840{
841 unsigned int hashval;
842
843 hashval = ((unsigned char) *p) << 4;
844 while (*p && *p != '=')
845 hashval += (unsigned char) *p++;
846 return &vartab[hashval % VTABSIZE];
847}
848
849
850
851/*
852 * Returns true if the two strings specify the same varable. The first
853 * variable name is terminated by '='; the second may be terminated by
854 * either '=' or '\0'.
855 */
856
857STATIC int
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
858varequal(char *p, char *q)
857varequal(const char *p, const char *q)
859{
860 while (*p == *q++) {
861 if (*p++ == '=')
862 return 1;
863 }
864 if (*p == '=' && *(q - 1) == '\0')
865 return 1;
866 return 0;
867}
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}