Mercurial > pidgin
annotate src/protocols/zephyr/ZVariables.c @ 9260:947876140943
[gaim-migrate @ 10059]
Patch by Stu Tomlinson to add gaim_conversation_has_focus() and
gaim_conv_window_has_focus(), and the (optional) UI ops.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Fri, 11 Jun 2004 00:08:29 +0000 |
parents | 43d6c08d7e96 |
children | 0f7452b1f777 |
rev | line source |
---|---|
2086 | 1 /* This file is part of the Project Athena Zephyr Notification System. |
2 * It contains source for the ZGetVariable, ZSetVariable, and ZUnsetVariable | |
3 * functions. | |
4 * | |
5 * Created by: Robert French | |
6 * | |
7 * $Source$ | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
4272
diff
changeset
|
8 * $Author: chipx86 $ |
2086 | 9 * |
10 * Copyright (c) 1987 by the Massachusetts Institute of Technology. | |
11 * For copying and distribution information, see the file | |
12 * "mit-copyright.h". | |
13 */ | |
14 /* $Header$ */ | |
15 | |
16 #ifndef lint | |
17 static char rcsid_ZVariables_c[] = "$Header$"; | |
18 #endif | |
19 | |
8792
43d6c08d7e96
[gaim-migrate @ 9554]
Christian Hammond <chipx86@chipx86.com>
parents:
5872
diff
changeset
|
20 #include "internal.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
4272
diff
changeset
|
21 #include "util.h" |
2086 | 22 |
23 #include <ctype.h> | |
24 #include <pwd.h> | |
25 | |
26 static int get_localvarfile __P((char *bfr)); | |
27 static char *get_varval __P((char *fn, char *val)); | |
28 static int varline __P((char *bfr, char *var)); | |
29 | |
30 char *ZGetVariable(var) | |
31 char *var; | |
32 { | |
33 char varfile[128], *ret; | |
34 char *get_varval(); | |
35 | |
36 if (get_localvarfile(varfile)) | |
37 return ((char *)0); | |
38 | |
39 if ((ret = get_varval(varfile, var)) != ZERR_NONE) | |
40 return (ret); | |
41 | |
42 sprintf(varfile, "%s/zephyr.vars", CONFDIR); | |
43 return (get_varval(varfile, var)); | |
44 } | |
45 | |
46 Code_t ZSetVariable(var, value) | |
47 char *var; | |
48 char *value; | |
49 { | |
50 int written; | |
51 FILE *fpin, *fpout; | |
52 char varfile[128], varfilebackup[128], varbfr[512]; | |
53 | |
54 written = 0; | |
55 | |
56 if (get_localvarfile(varfile)) | |
57 return (ZERR_INTERNAL); | |
58 | |
59 (void) strcpy(varfilebackup, varfile); | |
60 (void) strcat(varfilebackup, ".backup"); | |
61 | |
62 if (!(fpout = fopen(varfilebackup, "w"))) | |
63 return (errno); | |
64 if ((fpin = fopen(varfile, "r")) != NULL) { | |
65 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) { | |
66 if (varbfr[strlen(varbfr)-1] < ' ') | |
67 varbfr[strlen(varbfr)-1] = '\0'; | |
68 if (varline(varbfr, var)) { | |
69 fprintf(fpout, "%s = %s\n", var, value); | |
70 written = 1; | |
71 } | |
72 else | |
73 fprintf(fpout, "%s\n", varbfr); | |
74 } | |
75 (void) fclose(fpin); /* don't care about errs on input */ | |
76 } | |
77 if (!written) | |
78 fprintf(fpout, "%s = %s\n", var, value); | |
79 if (fclose(fpout) == EOF) | |
80 return(EIO); /* can't rely on errno */ | |
81 if (rename(varfilebackup, varfile)) | |
82 return (errno); | |
83 return (ZERR_NONE); | |
84 } | |
85 | |
86 Code_t ZUnsetVariable(var) | |
87 char *var; | |
88 { | |
89 FILE *fpin, *fpout; | |
90 char varfile[128], varfilebackup[128], varbfr[512]; | |
91 | |
92 if (get_localvarfile(varfile)) | |
93 return (ZERR_INTERNAL); | |
94 | |
95 (void) strcpy(varfilebackup, varfile); | |
96 (void) strcat(varfilebackup, ".backup"); | |
97 | |
98 if (!(fpout = fopen(varfilebackup, "w"))) | |
99 return (errno); | |
100 if ((fpin = fopen(varfile, "r")) != NULL) { | |
101 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) { | |
102 if (varbfr[strlen(varbfr)-1] < ' ') | |
103 varbfr[strlen(varbfr)-1] = '\0'; | |
104 if (!varline(varbfr, var)) | |
105 fprintf(fpout, "%s\n", varbfr); | |
106 } | |
107 (void) fclose(fpin); /* don't care about read close errs */ | |
108 } | |
109 if (fclose(fpout) == EOF) | |
110 return(EIO); /* errno isn't reliable */ | |
111 if (rename(varfilebackup, varfile)) | |
112 return (errno); | |
113 return (ZERR_NONE); | |
114 } | |
115 | |
116 static int get_localvarfile(bfr) | |
117 char *bfr; | |
118 { | |
4272 | 119 const char *envptr; |
2086 | 120 struct passwd *pwd; |
121 | |
4272 | 122 envptr = gaim_home_dir(); |
2086 | 123 if (envptr) |
124 (void) strcpy(bfr, envptr); | |
125 else { | |
126 if (!(pwd = getpwuid((int) getuid()))) { | |
127 fprintf(stderr, "Zephyr internal failure: Can't find your entry in /etc/passwd\n"); | |
128 return (1); | |
129 } | |
130 (void) strcpy(bfr, pwd->pw_dir); | |
131 } | |
132 | |
133 (void) strcat(bfr, "/"); | |
134 (void) strcat(bfr, ".zephyr.vars"); | |
135 return (0); | |
136 } | |
137 | |
138 static char *get_varval(fn, var) | |
139 char *fn; | |
140 char *var; | |
141 { | |
142 FILE *fp; | |
143 static char varbfr[512]; | |
144 int i; | |
145 | |
146 fp = fopen(fn, "r"); | |
147 if (!fp) | |
148 return ((char *)0); | |
149 | |
150 while (fgets(varbfr, sizeof varbfr, fp) != (char *) 0) { | |
151 if (varbfr[strlen(varbfr)-1] < ' ') | |
152 varbfr[strlen(varbfr)-1] = '\0'; | |
153 if (!(i = varline(varbfr, var))) | |
154 continue; | |
155 (void) fclose(fp); /* open read-only, don't care */ | |
156 return (varbfr+i); | |
157 } | |
158 (void) fclose(fp); /* open read-only, don't care */ | |
159 return ((char *)0); | |
160 } | |
161 | |
162 /* If the variable in the line bfr[] is the same as var, return index to | |
163 the variable value, else return 0. */ | |
164 static int varline(bfr, var) | |
165 char *bfr; | |
166 char *var; | |
167 { | |
168 register char *cp; | |
169 | |
170 | |
171 if (!bfr[0] || bfr[0] == '#') /* comment or null line */ | |
172 return (0); | |
173 | |
174 cp = bfr; | |
175 while (*cp && !isspace(*cp) && (*cp != '=')) | |
176 cp++; | |
177 | |
178 #define max(a,b) ((a > b) ? (a) : (b)) | |
179 | |
180 if (strncasecmp(bfr, var, max(strlen(var),cp - bfr))) | |
181 return(0); /* var is not the var in | |
182 bfr ==> no match */ | |
183 | |
184 cp = strchr(bfr, '='); | |
185 if (!cp) | |
186 return(0); | |
187 cp++; | |
188 while (*cp && isspace(*cp)) /* space up to variable value */ | |
189 cp++; | |
190 | |
191 return (cp - bfr); /* return index */ | |
192 } |