config.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (!fp) {
  43. errno = EIO;
  44. return (void *)0;
  45. }
  46. while (fgets(cfgbuf, sizeof(cfgbuf), fp)) {
  47. /* ship comment lines */
  48. if (cfgbuf[0] == '#') continue;
  49. ebuf = cfgbuf + strlen(cfgbuf);
  50. p = cfgbuf;
  51. for (i = 0; i < 16 && p < ebuf; i++) {
  52. args[i] = ws(&p);
  53. }
  54. args[i] = (void *)0;
  55. /* return if we found something */
  56. if (strlen(args[0])) return args;
  57. }
  58. return (void *)0;
  59. }
  60. char **
  61. cfgfind(FILE *fp, char *var)
  62. {
  63. char **ret;
  64. char search[80];
  65. if (!fp || !var) {
  66. errno = EIO;
  67. return (void *)0;
  68. }
  69. strncpy(search, var, sizeof(search));
  70. fseek(fp, 0, SEEK_SET);
  71. while ((ret = cfgread(fp))) {
  72. if (!strcmp(ret[0], search)) return ret;
  73. }
  74. return (void *)0;
  75. }