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