Mercurial > pidgin.yaz
annotate src/protocols/zephyr/ZVariables.c @ 13033:69b3d5cbd2b1
[gaim-migrate @ 15389]
Kill gaim_date() and gaim_date_full(). The former isn't used and the latter is used only twice. This makes the buddy pounce pop-ups and debug log headers contain localized dates, fixing part of SF Bug #1325915. Thanks to Bleeter for discovering that gaim_date() isn't used.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Wed, 25 Jan 2006 02:58:54 +0000 |
parents | 64895571248f |
children |
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 * Copyright (c) 1987 by the Massachusetts Institute of Technology. | |
8 * For copying and distribution information, see the file | |
9 * "mit-copyright.h". | |
10 */ | |
11 | |
8792
43d6c08d7e96
[gaim-migrate @ 9554]
Christian Hammond <chipx86@chipx86.com>
parents:
5872
diff
changeset
|
12 #include "internal.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
4272
diff
changeset
|
13 #include "util.h" |
2086 | 14 |
15 #include <ctype.h> | |
10867 | 16 #ifndef WIN32 |
2086 | 17 #include <pwd.h> |
10867 | 18 #endif |
2086 | 19 |
20 static int get_localvarfile __P((char *bfr)); | |
21 static char *get_varval __P((char *fn, char *val)); | |
22 static int varline __P((char *bfr, char *var)); | |
23 | |
24 char *ZGetVariable(var) | |
25 char *var; | |
26 { | |
27 char varfile[128], *ret; | |
28 | |
29 if (get_localvarfile(varfile)) | |
30 return ((char *)0); | |
31 | |
32 if ((ret = get_varval(varfile, var)) != ZERR_NONE) | |
33 return (ret); | |
34 | |
10867 | 35 #ifdef WIN32 |
36 sprintf(varfile, "C:\\zephyr\\zephyr.var"); | |
37 #else | |
2086 | 38 sprintf(varfile, "%s/zephyr.vars", CONFDIR); |
10867 | 39 #endif |
2086 | 40 return (get_varval(varfile, var)); |
41 } | |
42 | |
43 Code_t ZSetVariable(var, value) | |
44 char *var; | |
45 char *value; | |
46 { | |
47 int written; | |
48 FILE *fpin, *fpout; | |
49 char varfile[128], varfilebackup[128], varbfr[512]; | |
50 | |
51 written = 0; | |
52 | |
53 if (get_localvarfile(varfile)) | |
54 return (ZERR_INTERNAL); | |
55 | |
56 (void) strcpy(varfilebackup, varfile); | |
57 (void) strcat(varfilebackup, ".backup"); | |
58 | |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
59 if (!(fpout = fopen(varfilebackup, "w"))) |
2086 | 60 return (errno); |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
61 if ((fpin = fopen(varfile, "r")) != NULL) { |
2086 | 62 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) { |
63 if (varbfr[strlen(varbfr)-1] < ' ') | |
64 varbfr[strlen(varbfr)-1] = '\0'; | |
65 if (varline(varbfr, var)) { | |
66 fprintf(fpout, "%s = %s\n", var, value); | |
67 written = 1; | |
68 } | |
69 else | |
70 fprintf(fpout, "%s\n", varbfr); | |
71 } | |
72 (void) fclose(fpin); /* don't care about errs on input */ | |
73 } | |
74 if (!written) | |
75 fprintf(fpout, "%s = %s\n", var, value); | |
76 if (fclose(fpout) == EOF) | |
77 return(EIO); /* can't rely on errno */ | |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
78 if (rename(varfilebackup, varfile)) |
2086 | 79 return (errno); |
80 return (ZERR_NONE); | |
81 } | |
82 | |
83 Code_t ZUnsetVariable(var) | |
84 char *var; | |
85 { | |
86 FILE *fpin, *fpout; | |
87 char varfile[128], varfilebackup[128], varbfr[512]; | |
88 | |
89 if (get_localvarfile(varfile)) | |
90 return (ZERR_INTERNAL); | |
91 | |
92 (void) strcpy(varfilebackup, varfile); | |
93 (void) strcat(varfilebackup, ".backup"); | |
94 | |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
95 if (!(fpout = fopen(varfilebackup, "w"))) |
2086 | 96 return (errno); |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
97 if ((fpin = fopen(varfile, "r")) != NULL) { |
2086 | 98 while (fgets(varbfr, sizeof varbfr, fpin) != (char *) 0) { |
99 if (varbfr[strlen(varbfr)-1] < ' ') | |
100 varbfr[strlen(varbfr)-1] = '\0'; | |
101 if (!varline(varbfr, var)) | |
102 fprintf(fpout, "%s\n", varbfr); | |
103 } | |
104 (void) fclose(fpin); /* don't care about read close errs */ | |
105 } | |
106 if (fclose(fpout) == EOF) | |
107 return(EIO); /* errno isn't reliable */ | |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
108 if (rename(varfilebackup, varfile)) |
2086 | 109 return (errno); |
110 return (ZERR_NONE); | |
111 } | |
112 | |
113 static int get_localvarfile(bfr) | |
114 char *bfr; | |
115 { | |
4272 | 116 const char *envptr; |
10867 | 117 #ifndef WIN32 |
2086 | 118 struct passwd *pwd; |
4272 | 119 envptr = gaim_home_dir(); |
10867 | 120 #else |
121 envptr = getenv("HOME"); | |
122 if (!envptr) | |
123 envptr = getenv("HOMEPATH"); | |
124 if (!envptr) | |
125 envptr = "C:\\"; | |
126 #endif | |
2086 | 127 if (envptr) |
128 (void) strcpy(bfr, envptr); | |
129 else { | |
10867 | 130 #ifndef WIN32 |
2086 | 131 if (!(pwd = getpwuid((int) getuid()))) { |
132 fprintf(stderr, "Zephyr internal failure: Can't find your entry in /etc/passwd\n"); | |
133 return (1); | |
134 } | |
135 (void) strcpy(bfr, pwd->pw_dir); | |
10867 | 136 #endif |
2086 | 137 } |
138 | |
139 (void) strcat(bfr, "/"); | |
140 (void) strcat(bfr, ".zephyr.vars"); | |
141 return (0); | |
142 } | |
143 | |
144 static char *get_varval(fn, var) | |
145 char *fn; | |
146 char *var; | |
147 { | |
148 FILE *fp; | |
149 static char varbfr[512]; | |
150 int i; | |
151 | |
10592
d72fffd1b1ad
[gaim-migrate @ 11998]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10589
diff
changeset
|
152 fp = fopen(fn, "r"); |
2086 | 153 if (!fp) |
154 return ((char *)0); | |
155 | |
156 while (fgets(varbfr, sizeof varbfr, fp) != (char *) 0) { | |
157 if (varbfr[strlen(varbfr)-1] < ' ') | |
158 varbfr[strlen(varbfr)-1] = '\0'; | |
159 if (!(i = varline(varbfr, var))) | |
160 continue; | |
161 (void) fclose(fp); /* open read-only, don't care */ | |
162 return (varbfr+i); | |
163 } | |
164 (void) fclose(fp); /* open read-only, don't care */ | |
165 return ((char *)0); | |
166 } | |
167 | |
168 /* If the variable in the line bfr[] is the same as var, return index to | |
169 the variable value, else return 0. */ | |
170 static int varline(bfr, var) | |
171 char *bfr; | |
172 char *var; | |
173 { | |
174 register char *cp; | |
175 | |
176 | |
177 if (!bfr[0] || bfr[0] == '#') /* comment or null line */ | |
178 return (0); | |
179 | |
180 cp = bfr; | |
181 while (*cp && !isspace(*cp) && (*cp != '=')) | |
182 cp++; | |
183 | |
10867 | 184 #ifndef WIN32 |
2086 | 185 #define max(a,b) ((a > b) ? (a) : (b)) |
10867 | 186 #endif |
2086 | 187 |
188 if (strncasecmp(bfr, var, max(strlen(var),cp - bfr))) | |
189 return(0); /* var is not the var in | |
190 bfr ==> no match */ | |
191 | |
192 cp = strchr(bfr, '='); | |
193 if (!cp) | |
194 return(0); | |
195 cp++; | |
196 while (*cp && isspace(*cp)) /* space up to variable value */ | |
197 cp++; | |
198 | |
199 return (cp - bfr); /* return index */ | |
200 } |