config.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* config.c: Config file reader.
  2. *
  3. * Copyright 1999 D. Jeff Dionne, <jeff@rt-control.com>
  4. *
  5. * This is free software, under the LGPL V2.0
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <cfgfile.h>
  11. /* This is a quick and dirty config file parser. It reads the file once for
  12. * each request, there is no cache. Each line must be less than 128bytes.
  13. */
  14. static char *args[16];
  15. static char cfgbuf[128];
  16. static char *ws(char **buf)
  17. {
  18. char *b = *buf;
  19. char *p;
  20. /* eat ws */
  21. while (*b && (*b == ' ' || *b == '\n' || *b == '\t'))
  22. b++;
  23. p = b;
  24. /* find the end */
  25. while (*p && !(*p == ' ' || *p == '\n' || *p == '\t'))
  26. p++;
  27. *p = 0;
  28. *buf = p + 1;
  29. return b;
  30. }
  31. char **cfgread(FILE * fp)
  32. {
  33. char *ebuf;
  34. char *p;
  35. int i;
  36. if (!fp) {
  37. __set_errno(EIO);
  38. return (void *) 0;
  39. }
  40. while (fgets(cfgbuf, sizeof(cfgbuf), fp)) {
  41. /* ship comment lines */
  42. if (cfgbuf[0] == '#')
  43. continue;
  44. ebuf = cfgbuf + strlen(cfgbuf);
  45. p = cfgbuf;
  46. for (i = 0; i < 16 && p < ebuf; i++) {
  47. args[i] = ws(&p);
  48. }
  49. args[i] = (void *) 0;
  50. /* return if we found something */
  51. if (strlen(args[0]))
  52. return args;
  53. }
  54. return (void *) 0;
  55. }
  56. char **cfgfind(FILE * fp, char *var)
  57. {
  58. char **ret;
  59. char search[80];
  60. if (!fp || !var) {
  61. __set_errno(EIO);
  62. return (void *) 0;
  63. }
  64. strncpy(search, var, sizeof(search));
  65. fseek(fp, 0, SEEK_SET);
  66. while ((ret = cfgread(fp))) {
  67. if (!strcmp(ret[0], search))
  68. return ret;
  69. }
  70. return (void *) 0;
  71. }