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