2 * Copyright (c) 1997-2004 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * $Id: info_file.c,v 1.3.2.5 2004/01/06 03:15:16 ezk Exp $
51 #endif /* HAVE_CONFIG_H */
55 #define MAX_LINE_LEN 1500
57 /* forward declarations */
58 int file_init(mnt_map *m, char *map, time_t *tp);
59 int file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *));
60 int file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
61 int file_mtime(mnt_map *m, char *map, time_t *tp);
65 read_line(char *buf, int size, FILE *fp)
70 while (fgets(buf, size, fp)) {
71 int len = strlen(buf);
73 if (len > 1 && buf[len - 2] == '\\' &&
74 buf[len - 1] == '\n') {
81 * Skip leading white space on next line
83 while ((ch = getc(fp)) != EOF &&
84 isascii(ch) && isspace(ch)) ;
85 (void) ungetc(ch, fp);
90 } while (size > 0 && !feof(fp));
97 * Try to locate a key in a file
100 search_or_reload_file(FILE *fp, char *map, char *key, char **val, mnt_map *m, void (*fn) (mnt_map *m, char *, char *))
102 char key_val[MAX_LINE_LEN];
106 while (read_line(key_val, sizeof(key_val), fp)) {
110 int len = strlen(key_val);
114 * Make sure we got the whole line
116 if (key_val[len - 1] != '\n') {
117 plog(XLOG_WARNING, "line %d in \"%s\" is too long", line_no, map);
120 key_val[len - 1] = '\0';
126 hash = strchr(key_val, '#');
133 for (kp = key_val; *kp && isascii(*kp) && isspace((int)*kp); kp++) ;
144 for (cp = kp; *cp && (!isascii(*cp) || !isspace((int)*cp)); cp++) ;
147 * Check whether key matches
152 if (fn || (*key == *kp && STREQ(key, kp))) {
153 while (*cp && isascii(*cp) && isspace((int)*cp))
157 * Return a copy of the data
159 char *dc = strdup(cp);
161 (*fn) (m, strdup(kp), dc);
165 dlog("%s returns %s", key, dc);
171 plog(XLOG_USER, "%s: line %d has no value field", map, line_no);
177 * If the last read didn't get a whole line then
178 * throw away the remainder before continuing...
181 while (fgets(key_val, sizeof(key_val), fp) &&
182 !strchr(key_val, '\n')) ;
187 return fn ? 0 : ENOENT;
192 file_open(char *map, time_t *tp)
194 FILE *mapf = fopen(map, "r");
198 if (fstat(fileno(mapf), &stb) < 0)
208 file_init(mnt_map *m, char *map, time_t *tp)
210 FILE *mapf = file_open(map, tp);
221 file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *))
223 FILE *mapf = file_open(map, (time_t *) 0);
226 int error = search_or_reload_file(mapf, map, 0, 0, m, fn);
235 file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
238 FILE *mapf = file_open(map, &t);
246 error = search_or_reload_file(mapf, map, key, pval, 0, 0);
256 file_mtime(mnt_map *m, char *map, time_t *tp)
258 FILE *mapf = file_open(map, tp);