1700
|
1 /* This file is part of the Project Athena Zephyr Notification System.
|
|
2 * It contains source for the ZGetVariable, ZSetVariable, and ZUnsetVariable
|
|
3 * functions.
|
|
4 *
|
|
5 * Created by: Robert French
|
|
6 *
|
|
7 * $Source$
|
|
8 * $Author: warmenhoven $
|
|
9 *
|
|
10 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
|
|
11 * For copying and distribution information, see the file
|
|
12 * "mit-copyright.h".
|
|
13 */
|
|
14 /* $Header$ */
|
|
15
|
|
16 #ifndef lint
|
|
17 static char rcsid_ZVariables_c[] = "$Header$";
|
|
18 #endif
|
|
19
|
|
20 #include <internal.h>
|
|
21
|
|
22 #include <ctype.h>
|
|
23 #include <pwd.h>
|
|
24
|
|
25 static int get_localvarfile __P((char *bfr));
|
|
26 static char *get_varval __P((char *fn, char *val));
|
|
27 static int varline __P((char *bfr, char *var));
|
|
28
|
|
29 char *ZGetVariable(var)
|
|
30 char *var;
|
|
31 {
|
|
32 char varfile[128], *ret;
|
|
33 char *get_varval();
|
|
34
|
|
35 if (get_localvarfile(varfile))
|
|
36 return ((char *)0);
|
|
37
|
|
38 if ((ret = get_varval(varfile, var)) != ZERR_NONE)
|
|
39 return (ret);
|
|
40
|
|
41 sprintf(varfile, "%s/zephyr.vars", CONFDIR);
|
|
42 return (get_varval(varfile, var));
|
|
43 }
|
|
44
|
|
45 Code_t ZSetVariable(var, value)
|
|
46 char *var;
|
|
47 char *value;
|
|
48 {
|
|
49 int written;
|
|
50 FILE *fpin, *fpout;
|
|
51 char varfile[128], varfilebackup[128], varbfr[512];
|
|
52
|
|
53 written = 0;
|
|
54
|
|
55 if (get_localvarfile(varfile))
|
|
56 return (ZERR_INTERNAL);
|
|
57
|
|
58 (void) strcpy(varfilebackup, varfile);
|
|
59 (void) strcat(varfilebackup, ".backup");
|
|
60
|
|
61 if (!(fpout = fopen(varfilebackup, "w")))
|
|
62 return (errno);
|
|
63 if ((fpin = fopen(varfile, "r")) != NULL) {
|
|
64 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) {
|
|
65 if (varbfr[strlen(varbfr)-1] < ' ')
|
|
66 varbfr[strlen(varbfr)-1] = '\0';
|
|
67 if (varline(varbfr, var)) {
|
|
68 fprintf(fpout, "%s = %s\n", var, value);
|
|
69 written = 1;
|
|
70 }
|
|
71 else
|
|
72 fprintf(fpout, "%s\n", varbfr);
|
|
73 }
|
|
74 (void) fclose(fpin); /* don't care about errs on input */
|
|
75 }
|
|
76 if (!written)
|
|
77 fprintf(fpout, "%s = %s\n", var, value);
|
|
78 if (fclose(fpout) == EOF)
|
|
79 return(EIO); /* can't rely on errno */
|
|
80 if (rename(varfilebackup, varfile))
|
|
81 return (errno);
|
|
82 return (ZERR_NONE);
|
|
83 }
|
|
84
|
|
85 Code_t ZUnsetVariable(var)
|
|
86 char *var;
|
|
87 {
|
|
88 FILE *fpin, *fpout;
|
|
89 char varfile[128], varfilebackup[128], varbfr[512];
|
|
90
|
|
91 if (get_localvarfile(varfile))
|
|
92 return (ZERR_INTERNAL);
|
|
93
|
|
94 (void) strcpy(varfilebackup, varfile);
|
|
95 (void) strcat(varfilebackup, ".backup");
|
|
96
|
|
97 if (!(fpout = fopen(varfilebackup, "w")))
|
|
98 return (errno);
|
|
99 if ((fpin = fopen(varfile, "r")) != NULL) {
|
|
100 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) {
|
|
101 if (varbfr[strlen(varbfr)-1] < ' ')
|
|
102 varbfr[strlen(varbfr)-1] = '\0';
|
|
103 if (!varline(varbfr, var))
|
|
104 fprintf(fpout, "%s\n", varbfr);
|
|
105 }
|
|
106 (void) fclose(fpin); /* don't care about read close errs */
|
|
107 }
|
|
108 if (fclose(fpout) == EOF)
|
|
109 return(EIO); /* errno isn't reliable */
|
|
110 if (rename(varfilebackup, varfile))
|
|
111 return (errno);
|
|
112 return (ZERR_NONE);
|
|
113 }
|
|
114
|
|
115 static int get_localvarfile(bfr)
|
|
116 char *bfr;
|
|
117 {
|
|
118 char *envptr;
|
|
119 struct passwd *pwd;
|
|
120
|
|
121 envptr = getenv("HOME");
|
|
122 if (envptr)
|
|
123 (void) strcpy(bfr, envptr);
|
|
124 else {
|
|
125 if (!(pwd = getpwuid((int) getuid()))) {
|
|
126 fprintf(stderr, "Zephyr internal failure: Can't find your entry in /etc/passwd\n");
|
|
127 return (1);
|
|
128 }
|
|
129 (void) strcpy(bfr, pwd->pw_dir);
|
|
130 }
|
|
131
|
|
132 (void) strcat(bfr, "/");
|
|
133 (void) strcat(bfr, ".zephyr.vars");
|
|
134 return (0);
|
|
135 }
|
|
136
|
|
137 static char *get_varval(fn, var)
|
|
138 char *fn;
|
|
139 char *var;
|
|
140 {
|
|
141 FILE *fp;
|
|
142 static char varbfr[512];
|
|
143 int i;
|
|
144
|
|
145 fp = fopen(fn, "r");
|
|
146 if (!fp)
|
|
147 return ((char *)0);
|
|
148
|
|
149 while (fgets(varbfr, sizeof varbfr, fp) != (char *) 0) {
|
|
150 if (varbfr[strlen(varbfr)-1] < ' ')
|
|
151 varbfr[strlen(varbfr)-1] = '\0';
|
|
152 if (!(i = varline(varbfr, var)))
|
|
153 continue;
|
|
154 (void) fclose(fp); /* open read-only, don't care */
|
|
155 return (varbfr+i);
|
|
156 }
|
|
157 (void) fclose(fp); /* open read-only, don't care */
|
|
158 return ((char *)0);
|
|
159 }
|
|
160
|
|
161 /* If the variable in the line bfr[] is the same as var, return index to
|
|
162 the variable value, else return 0. */
|
|
163 static int varline(bfr, var)
|
|
164 char *bfr;
|
|
165 char *var;
|
|
166 {
|
|
167 register char *cp;
|
|
168
|
|
169
|
|
170 if (!bfr[0] || bfr[0] == '#') /* comment or null line */
|
|
171 return (0);
|
|
172
|
|
173 cp = bfr;
|
|
174 while (*cp && !isspace(*cp) && (*cp != '='))
|
|
175 cp++;
|
|
176
|
|
177 #define max(a,b) ((a > b) ? (a) : (b))
|
|
178
|
|
179 if (strncasecmp(bfr, var, max(strlen(var),cp - bfr)))
|
|
180 return(0); /* var is not the var in
|
|
181 bfr ==> no match */
|
|
182
|
|
183 cp = strchr(bfr, '=');
|
|
184 if (!cp)
|
|
185 return(0);
|
|
186 cp++;
|
|
187 while (*cp && isspace(*cp)) /* space up to variable value */
|
|
188 cp++;
|
|
189
|
|
190 return (cp - bfr); /* return index */
|
|
191 }
|