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