1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZGetVariable, ZSetVariable, and ZUnsetVariable
5 * Created by: Robert French
9 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
15 static char rcsid_ZVariables_c[] = "$Id$";
23 static int get_localvarfile(char *bfr);
24 static char *get_varval(char *fn, char *val);
25 static int varline(char *bfr, char *var);
28 ZGetVariable(char *var)
30 char varfile[128], *ret;
32 if (get_localvarfile(varfile))
35 if ((ret = get_varval(varfile, var)) != ZERR_NONE)
38 sprintf(varfile, "%s/zephyr/zephyr.vars", SYSCONFDIR);
39 return (get_varval(varfile, var));
43 ZSetVariable(char *var, char *value)
47 char varfile[128], varfilebackup[128], varbfr[512];
51 if (get_localvarfile(varfile))
52 return (ZERR_INTERNAL);
54 (void) strcpy(varfilebackup, varfile);
55 (void) strcat(varfilebackup, ".backup");
57 if (!(fpout = fopen(varfilebackup, "w")))
59 if ((fpin = fopen(varfile, "r")) != NULL) {
60 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) {
61 if (varbfr[strlen(varbfr)-1] < ' ')
62 varbfr[strlen(varbfr)-1] = '\0';
63 if (varline(varbfr, var)) {
64 fprintf(fpout, "%s = %s\n", var, value);
68 fprintf(fpout, "%s\n", varbfr);
70 (void) fclose(fpin); /* don't care about errs on input */
73 fprintf(fpout, "%s = %s\n", var, value);
74 if (fclose(fpout) == EOF)
75 return(EIO); /* can't rely on errno */
76 if (rename(varfilebackup, varfile))
82 ZUnsetVariable(char *var)
85 char varfile[128], varfilebackup[128], varbfr[512];
87 if (get_localvarfile(varfile))
88 return (ZERR_INTERNAL);
90 (void) strcpy(varfilebackup, varfile);
91 (void) strcat(varfilebackup, ".backup");
93 if (!(fpout = fopen(varfilebackup, "w")))
95 if ((fpin = fopen(varfile, "r")) != NULL) {
96 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) {
97 if (varbfr[strlen(varbfr)-1] < ' ')
98 varbfr[strlen(varbfr)-1] = '\0';
99 if (!varline(varbfr, var))
100 fprintf(fpout, "%s\n", varbfr);
102 (void) fclose(fpin); /* don't care about read close errs */
104 if (fclose(fpout) == EOF)
105 return(EIO); /* errno isn't reliable */
106 if (rename(varfilebackup, varfile))
112 get_localvarfile(char *bfr)
117 envptr = getenv("ZEPHYR_VARS");
119 (void) strcpy(bfr, envptr);
121 envptr = getenv("HOME");
123 (void) strcpy(bfr, envptr);
125 if (!(pwd = getpwuid((int) getuid()))) {
126 fprintf(stderr, "Zephyr internal failure: Can't determine your home directory.\n");
129 (void) strcpy(bfr, pwd->pw_dir);
132 (void) strcat(bfr, "/");
133 (void) strcat(bfr, ".zephyr.vars");
139 get_varval(char *fn, char *var)
142 static char varbfr[512];
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)))
154 (void) fclose(fp); /* open read-only, don't care */
157 (void) fclose(fp); /* open read-only, don't care */
161 /* If the variable in the line bfr[] is the same as var, return index to
162 the variable value, else return 0. */
164 varline(char *bfr, char *var)
169 if (!bfr[0] || bfr[0] == '#') /* comment or null line */
173 while (*cp && !isspace(*cp) && (*cp != '='))
176 #define max(a,b) ((a > b) ? (a) : (b))
178 if (strncasecmp(bfr, var, max(strlen(var),cp - bfr)))
179 return(0); /* var is not the var in
182 cp = strchr(bfr, '=');
186 while (*cp && isspace(*cp)) /* space up to variable value */
189 return (cp - bfr); /* return index */