[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / zephyr / ZVariables.c
blob1939ccb057ed3ae668e0554a18b7b532f2fdd053
1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZGetVariable, ZSetVariable, and ZUnsetVariable
3 * functions.
5 * Created by: Robert French
7 * $Source$
8 * $Author: warmenhoven $
10 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, see the file
12 * "mit-copyright.h".
14 /* $Header$ */
16 #ifndef lint
17 static char rcsid_ZVariables_c[] = "$Header$";
18 #endif
20 #include <internal.h>
22 #include <ctype.h>
23 #include <pwd.h>
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));
29 char *ZGetVariable(var)
30 char *var;
32 char varfile[128], *ret;
33 char *get_varval();
35 if (get_localvarfile(varfile))
36 return ((char *)0);
38 if ((ret = get_varval(varfile, var)) != ZERR_NONE)
39 return (ret);
41 sprintf(varfile, "%s/zephyr.vars", CONFDIR);
42 return (get_varval(varfile, var));
45 Code_t ZSetVariable(var, value)
46 char *var;
47 char *value;
49 int written;
50 FILE *fpin, *fpout;
51 char varfile[128], varfilebackup[128], varbfr[512];
53 written = 0;
55 if (get_localvarfile(varfile))
56 return (ZERR_INTERNAL);
58 (void) strcpy(varfilebackup, varfile);
59 (void) strcat(varfilebackup, ".backup");
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;
71 else
72 fprintf(fpout, "%s\n", varbfr);
74 (void) fclose(fpin); /* don't care about errs on input */
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);
85 Code_t ZUnsetVariable(var)
86 char *var;
88 FILE *fpin, *fpout;
89 char varfile[128], varfilebackup[128], varbfr[512];
91 if (get_localvarfile(varfile))
92 return (ZERR_INTERNAL);
94 (void) strcpy(varfilebackup, varfile);
95 (void) strcat(varfilebackup, ".backup");
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);
106 (void) fclose(fpin); /* don't care about read close errs */
108 if (fclose(fpout) == EOF)
109 return(EIO); /* errno isn't reliable */
110 if (rename(varfilebackup, varfile))
111 return (errno);
112 return (ZERR_NONE);
115 static int get_localvarfile(bfr)
116 char *bfr;
118 char *envptr;
119 struct passwd *pwd;
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);
129 (void) strcpy(bfr, pwd->pw_dir);
132 (void) strcat(bfr, "/");
133 (void) strcat(bfr, ".zephyr.vars");
134 return (0);
137 static char *get_varval(fn, var)
138 char *fn;
139 char *var;
141 FILE *fp;
142 static char varbfr[512];
143 int i;
145 fp = fopen(fn, "r");
146 if (!fp)
147 return ((char *)0);
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);
157 (void) fclose(fp); /* open read-only, don't care */
158 return ((char *)0);
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;
167 register char *cp;
170 if (!bfr[0] || bfr[0] == '#') /* comment or null line */
171 return (0);
173 cp = bfr;
174 while (*cp && !isspace(*cp) && (*cp != '='))
175 cp++;
177 #define max(a,b) ((a > b) ? (a) : (b))
179 if (strncasecmp(bfr, var, max(strlen(var),cp - bfr)))
180 return(0); /* var is not the var in
181 bfr ==> no match */
183 cp = strchr(bfr, '=');
184 if (!cp)
185 return(0);
186 cp++;
187 while (*cp && isspace(*cp)) /* space up to variable value */
188 cp++;
190 return (cp - bfr); /* return index */