1 /* $NetBSD: info_file.c,v 1.1.1.2 2009/03/20 20:26:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1997-2009 Erez Zadok 5 * Copyright (c) 1990 Jan-Simon Pendry 6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 7 * Copyright (c) 1990 The Regents of the University of California. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Jan-Simon Pendry at Imperial College, London. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgment: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * 42 * File: am-utils/amd/info_file.c 43 * 44 */ 45 46 /* 47 * Get info from file 48 */ 49 50 #ifdef HAVE_CONFIG_H 51 # include <config.h> 52 #endif /* HAVE_CONFIG_H */ 53 #include <am_defs.h> 54 #include <amd.h> 55 #include <sun_map.h> 56 57 58 /* forward declarations */ 59 int file_init_or_mtime(mnt_map *m, char *map, time_t *tp); 60 int file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *)); 61 int file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp); 62 63 64 int 65 file_read_line(char *buf, int size, FILE *fp) 66 { 67 int done = 0; 68 69 do { 70 while (fgets(buf, size, fp)) { 71 int len = strlen(buf); 72 done += len; 73 if (len > 1 && buf[len - 2] == '\\' && 74 buf[len - 1] == '\n') { 75 int ch; 76 buf += len - 2; 77 size -= len - 2; 78 *buf = '\n'; 79 buf[1] = '\0'; 80 /* 81 * Skip leading white space on next line 82 */ 83 while ((ch = getc(fp)) != EOF && 84 isascii((unsigned char)ch) && isspace((unsigned char)ch)) ; 85 (void) ungetc(ch, fp); 86 } else { 87 return done; 88 } 89 } 90 } while (size > 0 && !feof(fp) && !ferror(fp)); 91 92 return done; 93 } 94 95 96 /* 97 * Try to locate a key in a file 98 */ 99 static int 100 file_search_or_reload(mnt_map *m, 101 FILE *fp, 102 char *map, 103 char *key, 104 char **val, 105 void (*fn) (mnt_map *m, char *, char *)) 106 { 107 char key_val[INFO_MAX_LINE_LEN]; 108 int chuck = 0; 109 int line_no = 0; 110 111 while (file_read_line(key_val, sizeof(key_val), fp)) { 112 char *kp; 113 char *cp; 114 char *hash; 115 int len = strlen(key_val); 116 line_no++; 117 118 /* 119 * Make sure we got the whole line 120 */ 121 if (key_val[len - 1] != '\n') { 122 plog(XLOG_WARNING, "line %d in \"%s\" is too long", line_no, map); 123 chuck = 1; 124 } else { 125 key_val[len - 1] = '\0'; 126 } 127 128 /* 129 * Strip comments 130 */ 131 hash = strchr(key_val, '#'); 132 if (hash) 133 *hash = '\0'; 134 135 /* 136 * Find start of key 137 */ 138 for (kp = key_val; *kp && isascii((unsigned char)*kp) && isspace((unsigned char)*kp); kp++) ; 139 140 /* 141 * Ignore blank lines 142 */ 143 if (!*kp) 144 goto again; 145 146 /* 147 * Find end of key 148 */ 149 for (cp = kp; *cp && (!isascii((unsigned char)*cp) || !isspace((unsigned char)*cp)); cp++) ; 150 151 /* 152 * Check whether key matches 153 */ 154 if (*cp) 155 *cp++ = '\0'; 156 157 if (fn || (*key == *kp && STREQ(key, kp))) { 158 while (*cp && isascii((unsigned char)*cp) && isspace((unsigned char)*cp)) 159 cp++; 160 if (*cp) { 161 /* 162 * Return a copy of the data 163 */ 164 char *dc; 165 /* if m->cfm == NULL, not using amd.conf file */ 166 if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX)) 167 dc = sun_entry2amd(kp, cp); 168 else 169 dc = strdup(cp); 170 if (fn) { 171 (*fn) (m, strdup(kp), dc); 172 } else { 173 *val = dc; 174 dlog("%s returns %s", key, dc); 175 } 176 if (!fn) 177 return 0; 178 } else { 179 plog(XLOG_USER, "%s: line %d has no value field", map, line_no); 180 } 181 } 182 183 again: 184 /* 185 * If the last read didn't get a whole line then 186 * throw away the remainder before continuing... 187 */ 188 if (chuck) { 189 while (fgets(key_val, sizeof(key_val), fp) && 190 !strchr(key_val, '\n')) ; 191 chuck = 0; 192 } 193 } 194 195 return fn ? 0 : ENOENT; 196 } 197 198 199 static FILE * 200 file_open(char *map, time_t *tp) 201 { 202 FILE *mapf = fopen(map, "r"); 203 204 if (mapf && tp) { 205 struct stat stb; 206 if (fstat(fileno(mapf), &stb) < 0) 207 *tp = clocktime(NULL); 208 else 209 *tp = stb.st_mtime; 210 } 211 return mapf; 212 } 213 214 215 int 216 file_init_or_mtime(mnt_map *m, char *map, time_t *tp) 217 { 218 FILE *mapf = file_open(map, tp); 219 220 if (mapf) { 221 fclose(mapf); 222 return 0; 223 } 224 return errno; 225 } 226 227 228 int 229 file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *)) 230 { 231 FILE *mapf = file_open(map, (time_t *) NULL); 232 233 if (mapf) { 234 int error = file_search_or_reload(m, mapf, map, NULL, NULL, fn); 235 (void) fclose(mapf); 236 return error; 237 } 238 return errno; 239 } 240 241 242 int 243 file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp) 244 { 245 time_t t; 246 FILE *mapf = file_open(map, &t); 247 248 if (mapf) { 249 int error; 250 if (*tp < t) { 251 *tp = t; 252 error = -1; 253 } else { 254 error = file_search_or_reload(m, mapf, map, key, pval, NULL); 255 } 256 (void) fclose(mapf); 257 return error; 258 } 259 return errno; 260 } 261