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.7 (Berkeley) 02/12/92"; 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 == NULL || bp + len1 + len2 + 2 >= endp) { 78 buflen += MAX(len1 + len2 + 2, 1024); 79 if ((pathbuf = realloc(pathbuf, buflen)) == NULL) 80 enomem(); 81 endp = (bp = pathbuf) + buflen; 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"))) { 101 (void)fprintf(stderr, "%s: no configuration file %s.\n", 102 progname, _PATH_MANCONF); 103 exit(1); 104 } 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)) { 119 if (!index(line, '\n')) { 120 (void)fprintf(stderr, "%s: config line too long.\n", 121 progname); 122 exit(1); 123 } 124 p = strtok(line, " \t\n"); 125 #define WHATDB "_whatdb" 126 if (!p || *p == '#' || strcmp(p, WHATDB)) 127 continue; 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))) 133 enomem(); 134 } 135 } 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); 143 } 144 145 char ** 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; 185 { 186 register char *p; 187 char line[MAXLINE]; 188 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)); 208 exit(1); 209 } 210