| 245 | ****************************************************************************/ |
| 246 | |
| 247 | static bool find_variable(const char *configpath, const char *varname, |
| 248 | char **varvalue) |
| 249 | { |
| 250 | FILE *stream; |
| 251 | char *tmpname; |
| 252 | char *tmpvalue; |
| 253 | char *ptr; |
| 254 | |
| 255 | stream = fopen(configpath, "r"); |
| 256 | if (!stream) |
| 257 | { |
| 258 | fprintf(stderr, "ERROR: failed to open %s for reading: %s\n", |
| 259 | configpath, strerror(errno)); |
| 260 | exit(EXIT_FAILURE); |
| 261 | } |
| 262 | |
| 263 | /* Loop until the entire file has been parsed. */ |
| 264 | |
| 265 | do |
| 266 | { |
| 267 | /* Read the next line from the file */ |
| 268 | |
| 269 | ptr = read_line(stream); |
| 270 | if (ptr) |
| 271 | { |
| 272 | /* Parse the line into a variable and a value field */ |
| 273 | |
| 274 | tmpname = NULL; |
| 275 | tmpvalue = NULL; |
| 276 | parse_line(ptr, &tmpname, &tmpvalue); |
| 277 | |
| 278 | /* Make sure that both a variable name and value name were found. */ |
| 279 | |
| 280 | if (tmpname == NULL || tmpvalue == NULL) |
| 281 | { |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | /* Check if this the variable name and value we are looking for */ |
| 286 | |
| 287 | if (strcmp(varname, tmpname) == 0) |
| 288 | { |
| 289 | /* Yes.. return the value of the variable */ |
| 290 | |
| 291 | *varvalue = tmpvalue; |
| 292 | fclose(stream); |
| 293 | return true; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | while (ptr); |
| 298 | |
| 299 | /* Return failure */ |
| 300 | |
| 301 | fclose(stream); |
| 302 | return false; |
| 303 | } |
| 304 | |