Mon, 22 Aug 2011 02:07:41 +0000
Don't use strlen() when all you're trying to do is check if the string
is empty
| 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 | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
9 | * "mit-copyright.h". |
| 2086 | 10 | */ |
| 11 | ||
|
18273
e61c53184c52
#include reorganizations to allow compiling with glib < 2.8 using the
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
15884
diff
changeset
|
12 | #include "libpurple/internal.h" |
|
8792
b0645c9dc276
[gaim-migrate @ 9554]
Christian Hammond <chipx86@chipx86.com>
parents:
5872
diff
changeset
|
13 | #include "internal.h" |
|
5872
754c63f29b77
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
4272
diff
changeset
|
14 | #include "util.h" |
| 2086 | 15 | |
| 16 | #include <ctype.h> | |
| 10867 | 17 | #ifndef WIN32 |
| 2086 | 18 | #include <pwd.h> |
| 10867 | 19 | #endif |
| 2086 | 20 | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
21 | static char *get_localvarfile __P((void)); |
| 2086 | 22 | static char *get_varval __P((char *fn, char *val)); |
| 23 | static int varline __P((char *bfr, char *var)); | |
| 24 | ||
| 25 | char *ZGetVariable(var) | |
| 26 | char *var; | |
| 27 | { | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
28 | char *varfile, *ret; |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
29 | |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
30 | if ((varfile = get_localvarfile()) == NULL) |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
31 | return ((char *)0); |
| 2086 | 32 | |
|
30617
d79eaf981448
Fix a minor memleak on initialization
Mark Doliner <markdoliner@pidgin.im>
parents:
28001
diff
changeset
|
33 | ret = get_varval(varfile, var); |
|
d79eaf981448
Fix a minor memleak on initialization
Mark Doliner <markdoliner@pidgin.im>
parents:
28001
diff
changeset
|
34 | g_free(varfile); |
|
d79eaf981448
Fix a minor memleak on initialization
Mark Doliner <markdoliner@pidgin.im>
parents:
28001
diff
changeset
|
35 | if (ret != ZERR_NONE) |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
36 | return ret; |
| 2086 | 37 | |
| 10867 | 38 | #ifdef WIN32 |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
39 | varfile = g_strdup("C:\\zephyr\\zephyr.var"); |
| 10867 | 40 | #else |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
41 | varfile = g_strdup_printf("%s/zephyr.vars", CONFDIR); |
| 10867 | 42 | #endif |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
43 | ret = get_varval(varfile, var); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
44 | g_free(varfile); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
45 | |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
46 | return ret; |
| 2086 | 47 | } |
| 48 | ||
| 49 | Code_t ZSetVariable(var, value) | |
| 50 | char *var; | |
| 51 | char *value; | |
| 52 | { | |
| 53 | int written; | |
| 54 | FILE *fpin, *fpout; | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
55 | char *varfile, *varfilebackup, varbfr[512]; |
| 2086 | 56 | |
| 57 | written = 0; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
58 | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
59 | if ((varfile = get_localvarfile()) == NULL) |
| 2086 | 60 | return (ZERR_INTERNAL); |
| 61 | ||
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
62 | varfilebackup = g_strconcat(varfile, ".backup", NULL); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
63 | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
64 | if (!(fpout = fopen(varfilebackup, "w"))) { |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
65 | g_free(varfile); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
66 | g_free(varfilebackup); |
| 2086 | 67 | return (errno); |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
68 | } |
|
10592
cd4c3a7d7bdf
[gaim-migrate @ 11998]
Daniel Atallah <datallah@pidgin.im>
parents:
10589
diff
changeset
|
69 | if ((fpin = fopen(varfile, "r")) != NULL) { |
| 2086 | 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); | |
| 75 | written = 1; | |
| 76 | } | |
| 77 | else | |
| 78 | fprintf(fpout, "%s\n", varbfr); | |
| 79 | } | |
| 80 | (void) fclose(fpin); /* don't care about errs on input */ | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
81 | } |
| 2086 | 82 | if (!written) |
| 83 | fprintf(fpout, "%s = %s\n", var, value); | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
84 | if (fclose(fpout) == EOF) { |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
85 | g_free(varfilebackup); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
86 | g_free(varfile); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
87 | return(EIO); /* can't rely on errno */ |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
88 | } |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
89 | if (rename(varfilebackup, varfile)) { |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
90 | g_free(varfilebackup); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
91 | g_free(varfile); |
| 2086 | 92 | return (errno); |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
93 | } |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
94 | g_free(varfilebackup); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
95 | g_free(varfile); |
| 2086 | 96 | return (ZERR_NONE); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
97 | } |
| 2086 | 98 | |
| 99 | Code_t ZUnsetVariable(var) | |
| 100 | char *var; | |
| 101 | { | |
| 102 | FILE *fpin, *fpout; | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
103 | char *varfile, *varfilebackup, varbfr[512]; |
| 2086 | 104 | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
105 | if ((varfile = get_localvarfile()) == NULL) |
| 2086 | 106 | return (ZERR_INTERNAL); |
| 107 | ||
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
108 | varfilebackup = g_strconcat(varfile, ".backup", NULL); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
109 | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
110 | if (!(fpout = fopen(varfilebackup, "w"))) { |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
111 | g_free(varfile); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
112 | g_free(varfilebackup); |
| 2086 | 113 | return (errno); |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
114 | } |
|
10592
cd4c3a7d7bdf
[gaim-migrate @ 11998]
Daniel Atallah <datallah@pidgin.im>
parents:
10589
diff
changeset
|
115 | if ((fpin = fopen(varfile, "r")) != NULL) { |
| 2086 | 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); | |
| 121 | } | |
| 122 | (void) fclose(fpin); /* don't care about read close errs */ | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
123 | } |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
124 | if (fclose(fpout) == EOF) { |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
125 | g_free(varfilebackup); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
126 | g_free(varfile); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
127 | return(EIO); /* errno isn't reliable */ |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
128 | } |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
129 | if (rename(varfilebackup, varfile)) { |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
130 | g_free(varfilebackup); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
131 | g_free(varfile); |
| 2086 | 132 | return (errno); |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
133 | } |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
134 | g_free(varfilebackup); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
135 | g_free(varfile); |
| 2086 | 136 | return (ZERR_NONE); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
137 | } |
| 2086 | 138 | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
139 | static char *get_localvarfile(void) |
| 2086 | 140 | { |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
141 | const char *base; |
| 10867 | 142 | #ifndef WIN32 |
| 2086 | 143 | struct passwd *pwd; |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
144 | base = purple_home_dir(); |
| 10867 | 145 | #else |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
146 | base = getenv("HOME"); |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
147 | if (!base) |
|
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
148 | base = getenv("HOMEPATH"); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
149 | if (!base) |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
150 | base = "C:\\"; |
| 10867 | 151 | #endif |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
152 | if (!base) { |
| 10867 | 153 | #ifndef WIN32 |
| 2086 | 154 | if (!(pwd = getpwuid((int) getuid()))) { |
| 155 | fprintf(stderr, "Zephyr internal failure: Can't find your entry in /etc/passwd\n"); | |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
156 | return NULL; |
| 2086 | 157 | } |
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
158 | base = pwd->pw_dir; |
| 10867 | 159 | #endif |
| 2086 | 160 | } |
| 161 | ||
|
28001
c6446d1742d3
Don't use a static buffer to hold a full filename, since it could possibly
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
26726
diff
changeset
|
162 | return g_strconcat(base, "/.zephyr.vars", NULL); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
163 | } |
|
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
164 | |
| 2086 | 165 | static char *get_varval(fn, var) |
| 166 | char *fn; | |
| 167 | char *var; | |
| 168 | { | |
| 169 | FILE *fp; | |
| 170 | static char varbfr[512]; | |
| 171 | int i; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
172 | |
|
10592
cd4c3a7d7bdf
[gaim-migrate @ 11998]
Daniel Atallah <datallah@pidgin.im>
parents:
10589
diff
changeset
|
173 | fp = fopen(fn, "r"); |
| 2086 | 174 | if (!fp) |
| 175 | return ((char *)0); | |
| 176 | ||
| 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))) | |
| 181 | continue; | |
| 182 | (void) fclose(fp); /* open read-only, don't care */ | |
| 183 | return (varbfr+i); | |
| 184 | } | |
| 185 | (void) fclose(fp); /* open read-only, don't care */ | |
| 186 | return ((char *)0); | |
| 187 | } | |
| 188 | ||
| 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) | |
| 192 | char *bfr; | |
| 193 | char *var; | |
| 194 | { | |
| 195 | register char *cp; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
196 | |
| 2086 | 197 | |
| 198 | if (!bfr[0] || bfr[0] == '#') /* comment or null line */ | |
| 199 | return (0); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
30617
diff
changeset
|
200 | |
| 2086 | 201 | cp = bfr; |
| 202 | while (*cp && !isspace(*cp) && (*cp != '=')) | |
| 203 | cp++; | |
| 204 | ||
| 10867 | 205 | #ifndef WIN32 |
| 2086 | 206 | #define max(a,b) ((a > b) ? (a) : (b)) |
| 10867 | 207 | #endif |
| 2086 | 208 | |
|
26726
b81bcec8f359
Updates for GTK+ 3.0. Remove some deprecated functions (someone should
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
18552
diff
changeset
|
209 | if (g_ascii_strncasecmp(bfr, var, max(strlen(var), cp - bfr))) |
| 2086 | 210 | return(0); /* var is not the var in |
| 211 | bfr ==> no match */ | |
| 212 | ||
| 213 | cp = strchr(bfr, '='); | |
| 214 | if (!cp) | |
| 215 | return(0); | |
| 216 | cp++; | |
| 217 | while (*cp && isspace(*cp)) /* space up to variable value */ | |
| 218 | cp++; | |
| 219 | ||
| 220 | return (cp - bfr); /* return index */ | |
| 221 | } |