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