config.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 *
  17. ws(char **buf)
  18. {
  19. char *b = *buf;
  20. char *p;
  21. /* eat ws */
  22. while (*b &&
  23. (*b == ' ' ||
  24. *b == '\n' ||
  25. *b == '\t')) b++;
  26. p = b;
  27. /* find the end */
  28. while (*p &&
  29. !(*p == ' ' ||
  30. *p == '\n' ||
  31. *p == '\t')) p++;
  32. *p = 0;
  33. *buf = p+1;
  34. return b;
  35. }
  36. char **
  37. cfgread(FILE *fp)
  38. {
  39. char *ebuf;
  40. char *p;
  41. int i;
  42. int j;
  43. if (!fp) {
  44. errno = EIO;
  45. return (void *)0;
  46. }
  47. while (fgets(cfgbuf, sizeof(cfgbuf), fp)) {
  48. /* ship comment lines */
  49. if (cfgbuf[0] == '#') continue;
  50. ebuf = cfgbuf + strlen(cfgbuf);
  51. p = cfgbuf;
  52. for (i = 0; i < 16 && p < ebuf; i++) {
  53. args[i] = ws(&p);
  54. }
  55. args[i] = (void *)0;
  56. /* return if we found something */
  57. if (strlen(args[0])) return args;
  58. }
  59. return (void *)0;
  60. }
  61. char **
  62. cfgfind(FILE *fp, char *var)
  63. {
  64. char **ret;
  65. char search[80];
  66. if (!fp || !var) {
  67. errno = EIO;
  68. return (void *)0;
  69. }
  70. strncpy(search, var, sizeof(search));
  71. fseek(fp, 0, SEEK_SET);
  72. while (ret = cfgread(fp)) {
  73. if (!strcmp(ret[0], search)) return ret;
  74. }
  75. return (void *)0;
  76. }