Minor adjustments
[yaz-moved-to-github.git] / util / readconf.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: readconf.c,v $
7  * Revision 1.1  1995-11-01 13:55:06  quinn
8  * Minor adjustments
9  *
10  * Revision 1.2  1995/10/30  13:54:27  quinn
11  * iRemoved fclose().
12  *
13  * Revision 1.1  1995/10/10  16:28:18  quinn
14  * Initial revision
15  *
16  *
17  */
18
19 #include <stdio.h>
20 #include <ctype.h>
21
22 #include <log.h>
23
24 int readconf_line(FILE *f, char *line, int len, char *argv[], int num)
25 {
26     char *p;
27     int argc;
28
29     while ((p = fgets(line, len, f)))
30     {
31         while (*p && isspace(*p))
32             p++;
33         if (*p && *p != '#')
34             break;
35     }
36     if (!p)
37         return 0;
38
39     for (argc = 0; *p ; argc++)
40     {
41         if (*p == '#')  /* trailing comment */
42             break;
43         argv[argc] = p;
44         while (*p && !isspace(*p))
45             p++;
46         if (*p)
47         {
48             *(p++) = '\0';
49             while (*p && isspace(*p))
50                 p++;
51         }
52     }
53     return argc;
54 }
55
56 /*
57  * Read lines of a configuration file.
58  */
59 int readconf(char *name, void *private,
60     int (*fun)(char *name, void *private, int argc, char *argv[]))
61 {
62     FILE *f;
63     char line[512], *m_argv[50];
64     int m_argc;
65
66     if (!(f = fopen(name, "r")))
67     {
68         logf(LOG_WARN|LOG_ERRNO, "readconf: %s", name);
69         return -1;
70     }
71     for (;;)
72     {
73         int res;
74
75         if (!(m_argc = readconf_line(f, line, 512, m_argv, 50)))
76         {
77             fclose(f);
78             return 0;
79         }
80
81         if ((res = (*fun)(name, private, m_argc, m_argv)))
82         {
83             fclose(f);
84             return res;
85         }
86     }
87 }