Home | History | Annotate | Line # | Download | only in mail
vars.c revision 1.9
      1 /*	$NetBSD: vars.c,v 1.9 2002/03/04 03:16:10 wiz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)vars.c	8.1 (Berkeley) 6/6/93";
     40 #else
     41 __RCSID("$NetBSD: vars.c,v 1.9 2002/03/04 03:16:10 wiz Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "rcv.h"
     46 #include "extern.h"
     47 
     48 /*
     49  * Mail -- a mail program
     50  *
     51  * Variable handling stuff.
     52  */
     53 
     54 /*
     55  * Assign a value to a variable.
     56  */
     57 void
     58 assign(char name[], char values[])
     59 {
     60 	struct var *vp;
     61 	int h;
     62 
     63 	h = hash(name);
     64 	vp = lookup(name);
     65 	if (vp == NULL) {
     66 		vp = (struct var *) calloc(sizeof *vp, 1);
     67 		vp->v_name = vcopy(name);
     68 		vp->v_link = variables[h];
     69 		variables[h] = vp;
     70 	}
     71 	else
     72                 v_free(vp->v_value);
     73 	vp->v_value = vcopy(values);
     74 }
     75 
     76 /*
     77  * Free up a variable string.  We do not bother to allocate
     78  * strings whose value is "" since they are expected to be frequent.
     79  * Thus, we cannot free same!
     80  */
     81 void
     82 v_free(char *cp)
     83 {
     84 	if (*cp)
     85 		free(cp);
     86 }
     87 
     88 /*
     89  * Copy a variable value into permanent (ie, not collected after each
     90  * command) space.  Do not bother to alloc space for ""
     91  */
     92 
     93 char *
     94 vcopy(char str[])
     95 {
     96 	char *new;
     97 	unsigned len;
     98 
     99 	if (*str == '\0')
    100 		return "";
    101 	len = strlen(str) + 1;
    102 	if ((new = malloc(len)) == NULL)
    103 		errx(1, "Out of memory");
    104 	memmove(new, str, (int) len);
    105 	return new;
    106 }
    107 
    108 /*
    109  * Get the value of a variable and return it.
    110  * Look in the environment if its not available locally.
    111  */
    112 
    113 char *
    114 value(char name[])
    115 {
    116 	struct var *vp;
    117 
    118 	if ((vp = lookup(name)) == NULL)
    119 		return(getenv(name));
    120 	return(vp->v_value);
    121 }
    122 
    123 /*
    124  * Locate a variable and return its variable
    125  * node.
    126  */
    127 
    128 struct var *
    129 lookup(char name[])
    130 {
    131 	struct var *vp;
    132 
    133 	for (vp = variables[hash(name)]; vp != NULL; vp = vp->v_link)
    134 		if (*vp->v_name == *name && equal(vp->v_name, name))
    135 			return(vp);
    136 	return(NULL);
    137 }
    138 
    139 /*
    140  * Locate a group name and return it.
    141  */
    142 
    143 struct grouphead *
    144 findgroup(char name[])
    145 {
    146 	struct grouphead *gh;
    147 
    148 	for (gh = groups[hash(name)]; gh != NULL; gh = gh->g_link)
    149 		if (*gh->g_name == *name && equal(gh->g_name, name))
    150 			return(gh);
    151 	return(NULL);
    152 }
    153 
    154 /*
    155  * Print a group out on stdout
    156  */
    157 void
    158 printgroup(char name[])
    159 {
    160 	struct grouphead *gh;
    161 	struct group *gp;
    162 
    163 	if ((gh = findgroup(name)) == NULL) {
    164 		printf("\"%s\": not a group\n", name);
    165 		return;
    166 	}
    167 	printf("%s\t", gh->g_name);
    168 	for (gp = gh->g_list; gp != NULL; gp = gp->ge_link)
    169 		printf(" %s", gp->ge_name);
    170 	putchar('\n');
    171 }
    172 
    173 /*
    174  * Hash the passed string and return an index into
    175  * the variable or group hash table.
    176  */
    177 int
    178 hash(char *name)
    179 {
    180 	int h = 0;
    181 
    182 	while (*name) {
    183 		h <<= 2;
    184 		h += *name++;
    185 	}
    186 	if (h < 0 && (h = -h) < 0)
    187 		h = 0;
    188 	return (h % HSHSIZE);
    189 }
    190