No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / am-utils / dist / amd / info_file.c
blob70cb9a90cc1effb5e871035a373453518c591798
1 /* $NetBSD$ */
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.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
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.
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.
42 * File: am-utils/amd/info_file.c
47 * Get info from file
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>
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);
64 int
65 file_read_line(char *buf, int size, FILE *fp)
67 int done = 0;
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';
81 * Skip leading white space on next line
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;
90 } while (size > 0 && !feof(fp) && !ferror(fp));
92 return done;
97 * Try to locate a key in a file
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 *))
107 char key_val[INFO_MAX_LINE_LEN];
108 int chuck = 0;
109 int line_no = 0;
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++;
119 * Make sure we got the whole line
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';
129 * Strip comments
131 hash = strchr(key_val, '#');
132 if (hash)
133 *hash = '\0';
136 * Find start of key
138 for (kp = key_val; *kp && isascii((unsigned char)*kp) && isspace((unsigned char)*kp); kp++) ;
141 * Ignore blank lines
143 if (!*kp)
144 goto again;
147 * Find end of key
149 for (cp = kp; *cp && (!isascii((unsigned char)*cp) || !isspace((unsigned char)*cp)); cp++) ;
152 * Check whether key matches
154 if (*cp)
155 *cp++ = '\0';
157 if (fn || (*key == *kp && STREQ(key, kp))) {
158 while (*cp && isascii((unsigned char)*cp) && isspace((unsigned char)*cp))
159 cp++;
160 if (*cp) {
162 * Return a copy of the data
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);
176 if (!fn)
177 return 0;
178 } else {
179 plog(XLOG_USER, "%s: line %d has no value field", map, line_no);
183 again:
185 * If the last read didn't get a whole line then
186 * throw away the remainder before continuing...
188 if (chuck) {
189 while (fgets(key_val, sizeof(key_val), fp) &&
190 !strchr(key_val, '\n')) ;
191 chuck = 0;
195 return fn ? 0 : ENOENT;
199 static FILE *
200 file_open(char *map, time_t *tp)
202 FILE *mapf = fopen(map, "r");
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;
211 return mapf;
216 file_init_or_mtime(mnt_map *m, char *map, time_t *tp)
218 FILE *mapf = file_open(map, tp);
220 if (mapf) {
221 fclose(mapf);
222 return 0;
224 return errno;
229 file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *))
231 FILE *mapf = file_open(map, (time_t *) NULL);
233 if (mapf) {
234 int error = file_search_or_reload(m, mapf, map, NULL, NULL, fn);
235 (void) fclose(mapf);
236 return error;
238 return errno;
243 file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
245 time_t t;
246 FILE *mapf = file_open(map, &t);
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);
256 (void) fclose(mapf);
257 return error;
259 return errno;