|
|
1.1 root 1: #
2:
3: #include "rcv.h"
4:
5: /*
6: * Mail -- a mail program
7: *
8: * Variable handling stuff.
9: */
10:
11: /*
12: * Assign a value to a variable.
13: */
14:
15: assign(name, value)
16: char name[], value[];
17: {
18: register struct var *vp;
19: register int h;
20:
21: h = hash(name);
22: vp = lookup(name);
23: if (vp == NOVAR) {
24: vp = (struct var *) calloc(sizeof *vp, 1);
25: vp->v_name = vcopy(name);
26: vp->v_link = variables[h];
27: variables[h] = vp;
28: }
29: else
30: vfree(vp->v_value);
31: vp->v_value = vcopy(value);
32: }
33:
34: /*
35: * Free up a variable string. We do not bother to allocate
36: * strings whose value is "" since they are expected to be frequent.
37: * Thus, we cannot free same!
38: */
39:
40: vfree(cp)
41: register char *cp;
42: {
43: if (!equal(cp, ""))
44: cfree(cp);
45: }
46:
47: /*
48: * Copy a variable value into permanent (ie, not collected after each
49: * command) space. Do not bother to alloc space for ""
50: */
51:
52: char *
53: vcopy(str)
54: char str[];
55: {
56: register char *top, *cp, *cp2;
57:
58: if (equal(str, ""))
59: return("");
60: top = calloc(strlen(str)+1, 1);
61: cp = top;
62: cp2 = str;
63: while (*cp++ = *cp2++)
64: ;
65: return(top);
66: }
67:
68: /*
69: * Get the value of a variable and return it.
70: * Look in the environment if its not available locally.
71: */
72:
73: char *
74: value(name)
75: char name[];
76: {
77: register struct var *vp;
78:
79: if ((vp = lookup(name)) == NOVAR)
80: return(getenv(name));
81: return(vp->v_value);
82: }
83:
84: /*
85: * Locate a variable and return its variable
86: * node.
87: */
88:
89: struct var *
90: lookup(name)
91: char name[];
92: {
93: register struct var *vp;
94: register int h;
95:
96: h = hash(name);
97: for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
98: if (equal(vp->v_name, name))
99: return(vp);
100: return(NOVAR);
101: }
102:
103: /*
104: * Locate a group name and return it.
105: */
106:
107: struct grouphead *
108: findgroup(name)
109: char name[];
110: {
111: register struct grouphead *gh;
112: register int h;
113:
114: h = hash(name);
115: for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
116: if (equal(gh->g_name, name))
117: return(gh);
118: return(NOGRP);
119: }
120:
121: /*
122: * Print a group out on stdout
123: */
124:
125: printgroup(name)
126: char name[];
127: {
128: register struct grouphead *gh;
129: register struct group *gp;
130:
131: if ((gh = findgroup(name)) == NOGRP) {
132: printf("\"%s\": not a group\n", name);
133: return;
134: }
135: printf("%s\t", gh->g_name);
136: for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
137: printf(" %s", gp->ge_name);
138: printf("\n");
139: }
140:
141: /*
142: * Hash the passed string and return an index into
143: * the variable or group hash table.
144: */
145:
146: hash(name)
147: char name[];
148: {
149: register int h;
150: register char *cp;
151:
152: for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
153: ;
154: h &= ~0100000;
155: return(h % HSHSIZE);
156: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.