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
8 * $Author: warmenhoven $
10 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, see the file
17 static char rcsid_ZVariables_c
[] = "$Header$";
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
)
32 char varfile
[128], *ret
;
35 if (get_localvarfile(varfile
))
38 if ((ret
= get_varval(varfile
, var
)) != ZERR_NONE
)
41 sprintf(varfile
, "%s/zephyr.vars", CONFDIR
);
42 return (get_varval(varfile
, var
));
45 Code_t
ZSetVariable(var
, value
)
51 char varfile
[128], varfilebackup
[128], varbfr
[512];
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")))
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
);
72 fprintf(fpout
, "%s\n", varbfr
);
74 (void) fclose(fpin
); /* don't care about errs on input */
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
))
85 Code_t
ZUnsetVariable(var
)
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")))
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
))
115 static int get_localvarfile(bfr
)
121 envptr
= getenv("HOME");
123 (void) strcpy(bfr
, envptr
);
125 if (!(pwd
= getpwuid((int) getuid()))) {
126 fprintf(stderr
, "Zephyr internal failure: Can't find your entry in /etc/passwd\n");
129 (void) strcpy(bfr
, pwd
->pw_dir
);
132 (void) strcat(bfr
, "/");
133 (void) strcat(bfr
, ".zephyr.vars");
137 static char *get_varval(fn
, 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. */
163 static int varline(bfr
, var
)
170 if (!bfr
[0] || bfr
[0] == '#') /* comment or null line */
174 while (*cp
&& !isspace(*cp
) && (*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
183 cp
= strchr(bfr
, '=');
187 while (*cp
&& isspace(*cp
)) /* space up to variable value */
190 return (cp
- bfr
); /* return index */