2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/info_file.c
46 #endif /* HAVE_CONFIG_H */
52 /* forward declarations */
53 int file_init_or_mtime(mnt_map *m, char *map, time_t *tp);
54 int file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *));
55 int file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
59 file_read_line(char *buf, int size, FILE *fp)
64 while (fgets(buf, size, fp)) {
65 int len = strlen(buf);
67 if (len > 1 && buf[len - 2] == '\\' &&
68 buf[len - 1] == '\n') {
75 * Skip leading white space on next line
77 while ((ch = getc(fp)) != EOF &&
78 isascii((unsigned char)ch) && isspace((unsigned char)ch)) ;
79 (void) ungetc(ch, fp);
84 } while (size > 0 && !feof(fp) && !ferror(fp));
91 * Try to locate a key in a file
94 file_search_or_reload(mnt_map *m,
99 void (*fn) (mnt_map *m, char *, char *))
101 char key_val[INFO_MAX_LINE_LEN];
105 while (file_read_line(key_val, sizeof(key_val), fp)) {
109 int len = strlen(key_val);
113 * Make sure we got the whole line
115 if (key_val[len - 1] != '\n') {
116 plog(XLOG_WARNING, "line %d in \"%s\" is too long", line_no, map);
119 key_val[len - 1] = '\0';
125 hash = strchr(key_val, '#');
132 for (kp = key_val; *kp && isascii((unsigned char)*kp) && isspace((unsigned char)*kp); kp++) ;
143 for (cp = kp; *cp && (!isascii((unsigned char)*cp) || !isspace((unsigned char)*cp)); cp++) ;
146 * Check whether key matches
151 if (fn || (*key == *kp && STREQ(key, kp))) {
152 while (*cp && isascii((unsigned char)*cp) && isspace((unsigned char)*cp))
156 * Return a copy of the data
159 /* if m->cfm == NULL, not using amd.conf file */
160 if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX))
161 dc = sun_entry2amd(kp, cp);
165 (*fn) (m, xstrdup(kp), dc);
168 dlog("%s returns %s", key, dc);
173 plog(XLOG_USER, "%s: line %d has no value field", map, line_no);
179 * If the last read didn't get a whole line then
180 * throw away the remainder before continuing...
183 while (fgets(key_val, sizeof(key_val), fp) &&
184 !strchr(key_val, '\n')) ;
189 return fn ? 0 : ENOENT;
194 file_open(char *map, time_t *tp)
196 FILE *mapf = fopen(map, "r");
200 if (fstat(fileno(mapf), &stb) < 0)
201 *tp = clocktime(NULL);
210 file_init_or_mtime(mnt_map *m, char *map, time_t *tp)
212 FILE *mapf = file_open(map, tp);
223 file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *))
225 FILE *mapf = file_open(map, (time_t *) NULL);
228 int error = file_search_or_reload(m, mapf, map, NULL, NULL, fn);
237 file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
240 FILE *mapf = file_open(map, &t);
248 error = file_search_or_reload(m, mapf, map, key, pval, NULL);