Commit | Line | Data |
---|---|---|
1ef31675 KB |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
f15db449 | 5 | * %sccs.include.redist.c% |
1ef31675 KB |
6 | */ |
7 | ||
8 | #ifndef lint | |
5db167c0 | 9 | static char sccsid[] = "@(#)config.c 5.5 (Berkeley) %G%"; |
1ef31675 KB |
10 | #endif /* not lint */ |
11 | ||
011519cd | 12 | #include <sys/param.h> |
1ef31675 KB |
13 | #include <stdio.h> |
14 | #include <errno.h> | |
15 | #include <string.h> | |
011519cd KB |
16 | #include <stdlib.h> |
17 | #include <pwd.h> | |
1ef31675 KB |
18 | #include "pathnames.h" |
19 | ||
011519cd KB |
20 | #define MAXLINE 1024 |
21 | ||
5db167c0 TH |
22 | extern char *progname; |
23 | char *pathbuf, **arorder; | |
1ef31675 | 24 | |
011519cd | 25 | static FILE *cfp; |
011519cd | 26 | |
1ef31675 | 27 | /* |
011519cd KB |
28 | * getpath -- |
29 | * read in the configuration file, calling a function with the line | |
30 | * from each matching section. | |
1ef31675 KB |
31 | */ |
32 | char * | |
011519cd KB |
33 | getpath(sects) |
34 | char **sects; | |
1ef31675 | 35 | { |
011519cd KB |
36 | register char **av, *p; |
37 | size_t len; | |
a201b3c4 | 38 | char line[MAXLINE]; |
011519cd | 39 | |
011519cd KB |
40 | openconfig(); |
41 | while (fgets(line, sizeof(line), cfp)) { | |
42 | if (!index(line, '\n')) { | |
43 | (void)fprintf(stderr, "%s: config line too long.\n", | |
44 | progname); | |
45 | exit(1); | |
46 | } | |
47 | p = strtok(line, " \t\n"); | |
48 | if (!p || *p == '#') | |
49 | continue; | |
50 | for (av = sects; *av; ++av) | |
51 | if (!strcmp(p, *av)) | |
52 | break; | |
53 | if (!*av) | |
54 | continue; | |
55 | while (p = strtok((char *)NULL, " \t\n")) { | |
56 | len = strlen(p); | |
57 | if (p[len - 1] == '/') | |
a201b3c4 TH |
58 | for (av = arorder; *av; ++av) |
59 | cadd(p, len, *av); | |
011519cd KB |
60 | else |
61 | cadd(p, len, (char *)NULL); | |
62 | } | |
63 | } | |
a201b3c4 | 64 | return(pathbuf); |
011519cd | 65 | } |
1ef31675 | 66 | |
011519cd KB |
67 | cadd(add1, len1, add2) |
68 | char *add1, *add2; | |
69 | register size_t len1; | |
70 | { | |
71 | static size_t buflen; | |
72 | static char *bp, *endp; | |
73 | register size_t len2; | |
74 | ||
75 | len2 = add2 ? strlen(add2) : 0; | |
76 | if (!bp || bp + len1 + len2 + 2 >= endp) { | |
a201b3c4 | 77 | if (!(pathbuf = realloc(pathbuf, buflen += 1024))) |
011519cd KB |
78 | enomem(); |
79 | if (!bp) | |
a201b3c4 TH |
80 | bp = pathbuf; |
81 | endp = pathbuf + buflen; | |
011519cd KB |
82 | } |
83 | bcopy(add1, bp, len1); | |
84 | bp += len1; | |
85 | if (len2) { | |
86 | bcopy(add2, bp, len2); | |
87 | bp += len2; | |
88 | } | |
89 | *bp++ = ':'; | |
90 | *bp = '\0'; | |
91 | } | |
92 | ||
93 | static | |
94 | openconfig() | |
95 | { | |
96 | if (cfp) { | |
97 | rewind(cfp); | |
98 | return; | |
99 | } | |
100 | if (!(cfp = fopen(_PATH_MANCONF, "r"))) { | |
1ef31675 KB |
101 | (void)fprintf(stderr, "%s: no configuration file %s.\n", |
102 | progname, _PATH_MANCONF); | |
103 | exit(1); | |
104 | } | |
011519cd KB |
105 | } |
106 | ||
107 | char ** | |
108 | getdb() | |
109 | { | |
110 | register char *p; | |
111 | int cnt, num; | |
112 | char **ar, line[MAXLINE]; | |
113 | ||
114 | ar = NULL; | |
115 | num = 0; | |
116 | cnt = -1; | |
117 | openconfig(); | |
118 | while (fgets(line, sizeof(line), cfp)) { | |
1ef31675 KB |
119 | if (!index(line, '\n')) { |
120 | (void)fprintf(stderr, "%s: config line too long.\n", | |
121 | progname); | |
122 | exit(1); | |
123 | } | |
011519cd KB |
124 | p = strtok(line, " \t\n"); |
125 | #define WHATDB "_whatdb" | |
126 | if (!p || *p == '#' || strcmp(p, WHATDB)) | |
1ef31675 | 127 | continue; |
011519cd KB |
128 | while (p = strtok((char *)NULL, " \t\n")) { |
129 | if (cnt == num - 1 && | |
130 | !(ar = realloc(ar, (num += 30) * sizeof(char **)))) | |
131 | enomem(); | |
132 | if (!(ar[++cnt] = strdup(p))) | |
1ef31675 | 133 | enomem(); |
1ef31675 | 134 | } |
1ef31675 | 135 | } |
011519cd KB |
136 | if (ar) { |
137 | if (cnt == num - 1 && | |
138 | !(ar = realloc(ar, ++num * sizeof(char **)))) | |
139 | enomem(); | |
140 | ar[++cnt] = NULL; | |
141 | } | |
142 | return(ar); | |
1ef31675 KB |
143 | } |
144 | ||
a201b3c4 | 145 | char ** |
011519cd KB |
146 | getorder() |
147 | { | |
148 | register char *p; | |
149 | int cnt, num; | |
150 | char **ar, line[MAXLINE]; | |
151 | ||
152 | ar = NULL; | |
153 | num = 0; | |
154 | cnt = -1; | |
155 | openconfig(); | |
156 | while (fgets(line, sizeof(line), cfp)) { | |
157 | if (!index(line, '\n')) { | |
158 | (void)fprintf(stderr, "%s: config line too long.\n", | |
159 | progname); | |
160 | exit(1); | |
161 | } | |
162 | p = strtok(line, " \t\n"); | |
163 | #define SUBDIR "_subdir" | |
164 | if (!p || *p == '#' || strcmp(p, SUBDIR)) | |
165 | continue; | |
166 | while (p = strtok((char *)NULL, " \t\n")) { | |
167 | if (cnt == num - 1 && | |
168 | !(ar = realloc(ar, (num += 30) * sizeof(char **)))) | |
169 | enomem(); | |
170 | if (!(ar[++cnt] = strdup(p))) | |
171 | enomem(); | |
172 | } | |
173 | } | |
174 | if (ar) { | |
175 | if (cnt == num - 1 && | |
176 | !(ar = realloc(ar, ++num * sizeof(char **)))) | |
177 | enomem(); | |
178 | ar[++cnt] = NULL; | |
179 | } | |
180 | return(ar); | |
181 | } | |
182 | ||
183 | getsection(sect) | |
184 | char *sect; | |
1ef31675 | 185 | { |
011519cd KB |
186 | register char *p; |
187 | char line[MAXLINE]; | |
1ef31675 | 188 | |
011519cd KB |
189 | openconfig(); |
190 | while (fgets(line, sizeof(line), cfp)) { | |
191 | if (!index(line, '\n')) { | |
192 | (void)fprintf(stderr, "%s: config line too long.\n", | |
193 | progname); | |
194 | exit(1); | |
195 | } | |
196 | p = strtok(line, " \t\n"); | |
197 | if (!p || *p == '#') | |
198 | continue; | |
199 | if (!strcmp(p, sect)) | |
200 | return(1); | |
201 | } | |
202 | return(0); | |
203 | } | |
204 | ||
205 | enomem() | |
206 | { | |
207 | (void)fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM)); | |
1ef31675 KB |
208 | exit(1); |
209 | } |