Commit | Line | Data |
---|---|---|
761330fe DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
2ae9f53f | 7 | #ifndef lint |
828615a1 | 8 | static char *sccsid = "@(#)vars.c 5.3 (Berkeley) %G%"; |
761330fe | 9 | #endif not lint |
2e4ee99d KS |
10 | |
11 | #include "rcv.h" | |
12 | ||
13 | /* | |
14 | * Mail -- a mail program | |
15 | * | |
16 | * Variable handling stuff. | |
17 | */ | |
18 | ||
2e4ee99d KS |
19 | /* |
20 | * Assign a value to a variable. | |
21 | */ | |
22 | ||
23 | assign(name, value) | |
24 | char name[], value[]; | |
25 | { | |
26 | register struct var *vp; | |
27 | register int h; | |
28 | ||
29 | h = hash(name); | |
30 | vp = lookup(name); | |
31 | if (vp == NOVAR) { | |
32 | vp = (struct var *) calloc(sizeof *vp, 1); | |
33 | vp->v_name = vcopy(name); | |
34 | vp->v_link = variables[h]; | |
35 | variables[h] = vp; | |
36 | } | |
37 | else | |
38 | vfree(vp->v_value); | |
39 | vp->v_value = vcopy(value); | |
40 | } | |
41 | ||
42 | /* | |
43 | * Free up a variable string. We do not bother to allocate | |
44 | * strings whose value is "" since they are expected to be frequent. | |
45 | * Thus, we cannot free same! | |
46 | */ | |
47 | ||
48 | vfree(cp) | |
828615a1 | 49 | char *cp; |
2e4ee99d | 50 | { |
828615a1 EW |
51 | if (*cp) |
52 | free(cp); | |
2e4ee99d KS |
53 | } |
54 | ||
55 | /* | |
56 | * Copy a variable value into permanent (ie, not collected after each | |
57 | * command) space. Do not bother to alloc space for "" | |
58 | */ | |
59 | ||
60 | char * | |
61 | vcopy(str) | |
62 | char str[]; | |
63 | { | |
828615a1 EW |
64 | char *new; |
65 | unsigned len; | |
66 | ||
67 | if (*str == '\0') | |
68 | return ""; | |
69 | len = strlen(str) + 1; | |
70 | if ((new = malloc(len)) == NULL) | |
71 | panic("Out of memory"); | |
72 | bcopy(str, new, (int) len); | |
73 | return new; | |
2e4ee99d KS |
74 | } |
75 | ||
76 | /* | |
77 | * Get the value of a variable and return it. | |
78 | * Look in the environment if its not available locally. | |
79 | */ | |
80 | ||
81 | char * | |
82 | value(name) | |
83 | char name[]; | |
84 | { | |
85 | register struct var *vp; | |
86 | ||
87 | if ((vp = lookup(name)) == NOVAR) | |
88 | return(getenv(name)); | |
89 | return(vp->v_value); | |
90 | } | |
91 | ||
92 | /* | |
93 | * Locate a variable and return its variable | |
94 | * node. | |
95 | */ | |
96 | ||
97 | struct var * | |
98 | lookup(name) | |
828615a1 | 99 | register char name[]; |
2e4ee99d KS |
100 | { |
101 | register struct var *vp; | |
2e4ee99d | 102 | |
828615a1 EW |
103 | for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link) |
104 | if (*vp->v_name == *name && equal(vp->v_name, name)) | |
2e4ee99d KS |
105 | return(vp); |
106 | return(NOVAR); | |
107 | } | |
108 | ||
109 | /* | |
110 | * Locate a group name and return it. | |
111 | */ | |
112 | ||
113 | struct grouphead * | |
114 | findgroup(name) | |
828615a1 | 115 | register char name[]; |
2e4ee99d KS |
116 | { |
117 | register struct grouphead *gh; | |
2e4ee99d | 118 | |
828615a1 EW |
119 | for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link) |
120 | if (*gh->g_name == *name && equal(gh->g_name, name)) | |
2e4ee99d KS |
121 | return(gh); |
122 | return(NOGRP); | |
123 | } | |
124 | ||
125 | /* | |
126 | * Print a group out on stdout | |
127 | */ | |
128 | ||
129 | printgroup(name) | |
130 | char name[]; | |
131 | { | |
132 | register struct grouphead *gh; | |
133 | register struct group *gp; | |
134 | ||
135 | if ((gh = findgroup(name)) == NOGRP) { | |
136 | printf("\"%s\": not a group\n", name); | |
137 | return; | |
138 | } | |
139 | printf("%s\t", gh->g_name); | |
140 | for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) | |
141 | printf(" %s", gp->ge_name); | |
828615a1 | 142 | putchar('\n'); |
2e4ee99d KS |
143 | } |
144 | ||
145 | /* | |
146 | * Hash the passed string and return an index into | |
147 | * the variable or group hash table. | |
148 | */ | |
149 | ||
150 | hash(name) | |
828615a1 | 151 | register char *name; |
2e4ee99d | 152 | { |
828615a1 EW |
153 | register h = 0; |
154 | ||
155 | while (*name) { | |
156 | h <<= 2; | |
157 | h += *name++; | |
158 | } | |
159 | if (h < 0 && (h = -h) < 0) | |
46691008 | 160 | h = 0; |
828615a1 | 161 | return (h % HSHSIZE); |
2e4ee99d | 162 | } |