version 1.6, 2001/01/16 05:36:09 |
version 1.7, 2001/11/21 15:26:39 |
|
|
|
|
#ifndef lint |
#ifndef lint |
#if 0 |
#if 0 |
static char sccsid[] = "@(#)vars.c 8.1 (Berkeley) 6/6/93"; |
static const char sccsid[] = "@(#)vars.c 8.1 (Berkeley) 6/6/93"; |
#else |
#else |
static char rcsid[] = "$OpenBSD$"; |
static const char rcsid[] = "$OpenBSD$"; |
#endif |
#endif |
#endif /* not lint */ |
#endif /* not lint */ |
|
|
|
|
* Assign a value to a variable. |
* Assign a value to a variable. |
*/ |
*/ |
void |
void |
assign(name, value) |
assign(char *name, char *value) |
char name[], value[]; |
|
{ |
{ |
struct var *vp; |
struct var *vp; |
int h; |
int h; |
|
|
h = hash(name); |
h = hash(name); |
vp = lookup(name); |
vp = lookup(name); |
if (vp == NOVAR) { |
if (vp == NULL) { |
vp = (struct var *)calloc(sizeof(*vp), 1); |
vp = (struct var *)calloc(sizeof(*vp), 1); |
vp->v_name = vcopy(name); |
vp->v_name = vcopy(name); |
vp->v_link = variables[h]; |
vp->v_link = variables[h]; |
|
|
* Thus, we cannot free same! |
* Thus, we cannot free same! |
*/ |
*/ |
void |
void |
vfree(cp) |
vfree(char *cp) |
char *cp; |
|
{ |
{ |
|
|
if (*cp) |
if (*cp) |
(void)free(cp); |
(void)free(cp); |
} |
} |
|
|
* Copy a variable value into permanent (ie, not collected after each |
* Copy a variable value into permanent (ie, not collected after each |
* command) space. Do not bother to alloc space for "" |
* command) space. Do not bother to alloc space for "" |
*/ |
*/ |
|
|
char * |
char * |
vcopy(str) |
vcopy(char *str) |
char str[]; |
|
{ |
{ |
char *new; |
char *new; |
unsigned len; |
|
|
|
if (*str == '\0') |
if (*str == '\0') |
return(""); |
return(""); |
len = strlen(str) + 1; |
if ((new = strdup(str)) == NULL) |
if ((new = (char *)malloc(len)) == NULL) |
|
errx(1, "Out of memory"); |
errx(1, "Out of memory"); |
(void)memcpy(new, str, len); |
|
return(new); |
return(new); |
} |
} |
|
|
|
|
*/ |
*/ |
|
|
char * |
char * |
value(name) |
value(char *name) |
char name[]; |
|
{ |
{ |
struct var *vp; |
struct var *vp; |
char *env; |
char *env; |
|
|
if ((vp = lookup(name)) != NOVAR) |
if ((vp = lookup(name)) != NULL) |
return(vp->v_value); |
return(vp->v_value); |
else if ((env = getenv(name))) |
else if ((env = getenv(name))) |
return(env); |
return(env); |
|
|
* Locate a variable and return its variable |
* Locate a variable and return its variable |
* node. |
* node. |
*/ |
*/ |
|
|
struct var * |
struct var * |
lookup(name) |
lookup(char *name) |
char name[]; |
|
{ |
{ |
struct var *vp; |
struct var *vp; |
|
|
for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link) |
for (vp = variables[hash(name)]; vp != NULL; vp = vp->v_link) |
if (*vp->v_name == *name && equal(vp->v_name, name)) |
if (*vp->v_name == *name && equal(vp->v_name, name)) |
return(vp); |
return(vp); |
return(NOVAR); |
return(NULL); |
} |
} |
|
|
/* |
/* |
* Locate a group name and return it. |
* Locate a group name and return it. |
*/ |
*/ |
|
|
struct grouphead * |
struct grouphead * |
findgroup(name) |
findgroup(char *name) |
char name[]; |
|
{ |
{ |
struct grouphead *gh; |
struct grouphead *gh; |
|
|
for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link) |
for (gh = groups[hash(name)]; gh != NULL; gh = gh->g_link) |
if (*gh->g_name == *name && equal(gh->g_name, name)) |
if (*gh->g_name == *name && equal(gh->g_name, name)) |
return(gh); |
return(gh); |
return(NOGRP); |
return(NULL); |
} |
} |
|
|
/* |
/* |
* Print a group out on stdout |
* Print a group out on stdout |
*/ |
*/ |
void |
void |
printgroup(name) |
printgroup(char *name) |
char name[]; |
|
{ |
{ |
struct grouphead *gh; |
struct grouphead *gh; |
struct group *gp; |
struct group *gp; |
|
|
if ((gh = findgroup(name)) == NOGRP) { |
if ((gh = findgroup(name)) == NULL) { |
printf("\"%s\": not a group\n", name); |
printf("\"%s\": not a group\n", name); |
return; |
return; |
} |
} |
printf("%s\t", gh->g_name); |
printf("%s\t", gh->g_name); |
for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) |
for (gp = gh->g_list; gp != NULL; gp = gp->ge_link) |
printf(" %s", gp->ge_name); |
printf(" %s", gp->ge_name); |
putchar('\n'); |
putchar('\n'); |
} |
} |
|
|
* the variable or group hash table. |
* the variable or group hash table. |
*/ |
*/ |
int |
int |
hash(name) |
hash(char *name) |
char *name; |
|
{ |
{ |
int h = 0; |
int h = 0; |
|
|