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
7 * Copyright (c) 1987 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, see the file
12 #include "libpurple/internal.h"
21 static char *get_localvarfile
__P((void));
22 static char *get_varval
__P((char *fn
, char *val
));
23 static int varline
__P((char *bfr
, char *var
));
25 char *ZGetVariable(var
)
30 if ((varfile
= get_localvarfile()) == NULL
)
33 ret
= get_varval(varfile
, var
);
39 varfile
= g_strdup("C:\\zephyr\\zephyr.var");
41 varfile
= g_build_filename(PURPLE_SYSCONFDIR
, "zephyr.vars", NULL
);
43 ret
= get_varval(varfile
, var
);
49 Code_t
ZSetVariable(var
, value
)
55 char *varfile
, *varfilebackup
, varbfr
[512];
59 if ((varfile
= get_localvarfile()) == NULL
)
60 return (ZERR_INTERNAL
);
62 varfilebackup
= g_strconcat(varfile
, ".backup", NULL
);
64 if (!(fpout
= fopen(varfilebackup
, "w"))) {
66 g_free(varfilebackup
);
69 if ((fpin
= fopen(varfile
, "r")) != NULL
) {
70 while (fgets(varbfr
, sizeof varbfr
, fpin
) != (char *) 0) {
71 if (varbfr
[strlen(varbfr
)-1] < ' ')
72 varbfr
[strlen(varbfr
)-1] = '\0';
73 if (varline(varbfr
, var
)) {
74 fprintf(fpout
, "%s = %s\n", var
, value
);
78 fprintf(fpout
, "%s\n", varbfr
);
80 (void) fclose(fpin
); /* don't care about errs on input */
83 fprintf(fpout
, "%s = %s\n", var
, value
);
84 if (fclose(fpout
) == EOF
) {
85 g_free(varfilebackup
);
87 return(EIO
); /* can't rely on errno */
89 if (rename(varfilebackup
, varfile
)) {
90 g_free(varfilebackup
);
94 g_free(varfilebackup
);
99 Code_t
ZUnsetVariable(var
)
103 char *varfile
, *varfilebackup
, varbfr
[512];
105 if ((varfile
= get_localvarfile()) == NULL
)
106 return (ZERR_INTERNAL
);
108 varfilebackup
= g_strconcat(varfile
, ".backup", NULL
);
110 if (!(fpout
= fopen(varfilebackup
, "w"))) {
112 g_free(varfilebackup
);
115 if ((fpin
= fopen(varfile
, "r")) != NULL
) {
116 while (fgets(varbfr
, sizeof varbfr
, fpin
) != (char *) 0) {
117 if (varbfr
[strlen(varbfr
)-1] < ' ')
118 varbfr
[strlen(varbfr
)-1] = '\0';
119 if (!varline(varbfr
, var
))
120 fprintf(fpout
, "%s\n", varbfr
);
122 (void) fclose(fpin
); /* don't care about read close errs */
124 if (fclose(fpout
) == EOF
) {
125 g_free(varfilebackup
);
127 return(EIO
); /* errno isn't reliable */
129 if (rename(varfilebackup
, varfile
)) {
130 g_free(varfilebackup
);
134 g_free(varfilebackup
);
139 static char *get_localvarfile(void)
144 base
= purple_home_dir();
146 base
= getenv("HOME");
148 base
= getenv("HOMEPATH");
154 if (!(pwd
= getpwuid((int) getuid()))) {
155 fprintf(stderr
, "Zephyr internal failure: Can't find your entry in /etc/passwd\n");
162 return g_strconcat(base
, "/.zephyr.vars", NULL
);
165 static char *get_varval(fn
, var
)
170 static char varbfr
[512];
177 while (fgets(varbfr
, sizeof varbfr
, fp
) != (char *) 0) {
178 if (varbfr
[strlen(varbfr
)-1] < ' ')
179 varbfr
[strlen(varbfr
)-1] = '\0';
180 if (!(i
= varline(varbfr
, var
)))
182 (void) fclose(fp
); /* open read-only, don't care */
185 (void) fclose(fp
); /* open read-only, don't care */
189 /* If the variable in the line bfr[] is the same as var, return index to
190 the variable value, else return 0. */
191 static int varline(bfr
, var
)
198 if (!bfr
[0] || bfr
[0] == '#') /* comment or null line */
202 while (*cp
&& !isspace(*cp
) && (*cp
!= '='))
206 #define max(a,b) ((a > b) ? (a) : (b))
209 if (g_ascii_strncasecmp(bfr
, var
, max(strlen(var
), (gsize
)(cp
- bfr
))))
210 return(0); /* var is not the var in
213 cp
= strchr(bfr
, '=');
217 while (*cp
&& isspace(*cp
)) /* space up to variable value */
220 return (cp
- bfr
); /* return index */