Annotation of src/usr.bin/mail/vars.c, Revision 1.7
1.7 ! millert 1: /* $OpenBSD: vars.c,v 1.6 2001/01/16 05:36:09 millert Exp $ */
1.2 deraadt 2: /* $NetBSD: vars.c,v 1.4 1996/06/08 19:48:45 christos Exp $ */
3:
1.1 deraadt 4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * 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: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
1.2 deraadt 38: #if 0
1.7 ! millert 39: static const char sccsid[] = "@(#)vars.c 8.1 (Berkeley) 6/6/93";
1.2 deraadt 40: #else
1.7 ! millert 41: static const char rcsid[] = "$OpenBSD: vars.c,v 1.6 2001/01/16 05:36:09 millert Exp $";
1.2 deraadt 42: #endif
1.1 deraadt 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
1.7 ! millert 58: assign(char *name, char *value)
1.1 deraadt 59: {
1.5 millert 60: struct var *vp;
61: int h;
1.1 deraadt 62:
63: h = hash(name);
64: vp = lookup(name);
1.7 ! millert 65: if (vp == NULL) {
1.4 millert 66: vp = (struct var *)calloc(sizeof(*vp), 1);
1.1 deraadt 67: vp->v_name = vcopy(name);
68: vp->v_link = variables[h];
69: variables[h] = vp;
70: }
71: else
72: vfree(vp->v_value);
73: vp->v_value = vcopy(value);
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
1.7 ! millert 82: vfree(char *cp)
1.1 deraadt 83: {
1.7 ! millert 84:
1.1 deraadt 85: if (*cp)
1.4 millert 86: (void)free(cp);
1.1 deraadt 87: }
88:
89: /*
90: * Copy a variable value into permanent (ie, not collected after each
91: * command) space. Do not bother to alloc space for ""
92: */
93: char *
1.7 ! millert 94: vcopy(char *str)
1.1 deraadt 95: {
96: char *new;
97:
98: if (*str == '\0')
1.3 millert 99: return("");
1.7 ! millert 100: if ((new = strdup(str)) == NULL)
1.5 millert 101: errx(1, "Out of memory");
1.3 millert 102: return(new);
1.1 deraadt 103: }
104:
105: /*
106: * Get the value of a variable and return it.
107: * Look in the environment if its not available locally.
108: */
109:
110: char *
1.7 ! millert 111: value(char *name)
1.1 deraadt 112: {
1.5 millert 113: struct var *vp;
1.6 millert 114: char *env;
1.1 deraadt 115:
1.7 ! millert 116: if ((vp = lookup(name)) != NULL)
1.6 millert 117: return(vp->v_value);
118: else if ((env = getenv(name)))
119: return(env);
120: /* not set, see if we can provide a default */
121: else if (strcmp(name, "SHELL") == 0)
122: return(_PATH_CSHELL);
123: else if (strcmp(name, "LISTER") == 0)
124: return(_PATH_LS);
125: else if (strcmp(name, "PAGER") == 0)
126: return(_PATH_MORE);
127: else
128: return(NULL);
1.1 deraadt 129: }
130:
131: /*
132: * Locate a variable and return its variable
133: * node.
134: */
135: struct var *
1.7 ! millert 136: lookup(char *name)
1.1 deraadt 137: {
1.5 millert 138: struct var *vp;
1.1 deraadt 139:
1.7 ! millert 140: for (vp = variables[hash(name)]; vp != NULL; vp = vp->v_link)
1.1 deraadt 141: if (*vp->v_name == *name && equal(vp->v_name, name))
142: return(vp);
1.7 ! millert 143: return(NULL);
1.1 deraadt 144: }
145:
146: /*
147: * Locate a group name and return it.
148: */
149: struct grouphead *
1.7 ! millert 150: findgroup(char *name)
1.1 deraadt 151: {
1.5 millert 152: struct grouphead *gh;
1.1 deraadt 153:
1.7 ! millert 154: for (gh = groups[hash(name)]; gh != NULL; gh = gh->g_link)
1.1 deraadt 155: if (*gh->g_name == *name && equal(gh->g_name, name))
156: return(gh);
1.7 ! millert 157: return(NULL);
1.1 deraadt 158: }
159:
160: /*
161: * Print a group out on stdout
162: */
163: void
1.7 ! millert 164: printgroup(char *name)
1.1 deraadt 165: {
1.5 millert 166: struct grouphead *gh;
167: struct group *gp;
1.1 deraadt 168:
1.7 ! millert 169: if ((gh = findgroup(name)) == NULL) {
1.1 deraadt 170: printf("\"%s\": not a group\n", name);
171: return;
172: }
173: printf("%s\t", gh->g_name);
1.7 ! millert 174: for (gp = gh->g_list; gp != NULL; gp = gp->ge_link)
1.1 deraadt 175: printf(" %s", gp->ge_name);
176: putchar('\n');
177: }
178:
179: /*
180: * Hash the passed string and return an index into
181: * the variable or group hash table.
182: */
183: int
1.7 ! millert 184: hash(char *name)
1.1 deraadt 185: {
1.5 millert 186: int h = 0;
1.1 deraadt 187:
188: while (*name) {
189: h <<= 2;
190: h += *name++;
191: }
192: if (h < 0 && (h = -h) < 0)
193: h = 0;
1.3 millert 194: return(h % HSHSIZE);
1.1 deraadt 195: }