1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)config.c 5.6 (Berkeley) 03/01/91"; 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 (!index(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; 73 static char *bp, *endp; 74 register size_t len2; 75 76 len2 = add2 ? strlen(add2) : 0; 77 if (!bp || bp + len1 + len2 + 2 >= endp) { 78 if (!(pathbuf = realloc(pathbuf, buflen += 1024))) 79 enomem(); 80 if (!bp) 81 bp = pathbuf; 82 endp = pathbuf + buflen; 83 } 84 bcopy(add1, bp, len1); 85 bp += len1; 86 if (len2) { 87 bcopy(add2, bp, len2); 88 bp += len2; 89 } 90 *bp++ = ':'; 91 *bp = '\0'; 92 } 93 94 static 95 openconfig() 96 { 97 if (cfp) { 98 rewind(cfp); 99 return; 100 } 101 if (!(cfp = fopen(_PATH_MANCONF, "r"))) { 102 (void)fprintf(stderr, "%s: no configuration file %s.\n", 103 progname, _PATH_MANCONF); 104 exit(1); 105 } 106 } 107 108 char ** 109 getdb() 110 { 111 register char *p; 112 int cnt, num; 113 char **ar, line[MAXLINE]; 114 115 ar = NULL; 116 num = 0; 117 cnt = -1; 118 openconfig(); 119 while (fgets(line, sizeof(line), cfp)) { 120 if (!index(line, '\n')) { 121 (void)fprintf(stderr, "%s: config line too long.\n", 122 progname); 123 exit(1); 124 } 125 p = strtok(line, " \t\n"); 126 #define WHATDB "_whatdb" 127 if (!p || *p == '#' || strcmp(p, WHATDB)) 128 continue; 129 while (p = strtok((char *)NULL, " \t\n")) { 130 if (cnt == num - 1 && 131 !(ar = realloc(ar, (num += 30) * sizeof(char **)))) 132 enomem(); 133 if (!(ar[++cnt] = strdup(p))) 134 enomem(); 135 } 136 } 137 if (ar) { 138 if (cnt == num - 1 && 139 !(ar = realloc(ar, ++num * sizeof(char **)))) 140 enomem(); 141 ar[++cnt] = NULL; 142 } 143 return(ar); 144 } 145 146 char ** 147 getorder() 148 { 149 register char *p; 150 int cnt, num; 151 char **ar, line[MAXLINE]; 152 153 ar = NULL; 154 num = 0; 155 cnt = -1; 156 openconfig(); 157 while (fgets(line, sizeof(line), cfp)) { 158 if (!index(line, '\n')) { 159 (void)fprintf(stderr, "%s: config line too long.\n", 160 progname); 161 exit(1); 162 } 163 p = strtok(line, " \t\n"); 164 #define SUBDIR "_subdir" 165 if (!p || *p == '#' || strcmp(p, SUBDIR)) 166 continue; 167 while (p = strtok((char *)NULL, " \t\n")) { 168 if (cnt == num - 1 && 169 !(ar = realloc(ar, (num += 30) * sizeof(char **)))) 170 enomem(); 171 if (!(ar[++cnt] = strdup(p))) 172 enomem(); 173 } 174 } 175 if (ar) { 176 if (cnt == num - 1 && 177 !(ar = realloc(ar, ++num * sizeof(char **)))) 178 enomem(); 179 ar[++cnt] = NULL; 180 } 181 return(ar); 182 } 183 184 getsection(sect) 185 char *sect; 186 { 187 register char *p; 188 char line[MAXLINE]; 189 190 openconfig(); 191 while (fgets(line, sizeof(line), cfp)) { 192 if (!index(line, '\n')) { 193 (void)fprintf(stderr, "%s: config line too long.\n", 194 progname); 195 exit(1); 196 } 197 p = strtok(line, " \t\n"); 198 if (!p || *p == '#') 199 continue; 200 if (!strcmp(p, sect)) 201 return(1); 202 } 203 return(0); 204 } 205 206 enomem() 207 { 208 (void)fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM)); 209 exit(1); 210 } 211