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.
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
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
42 * File: am-utils/amd/info_file.c
52 #endif /* HAVE_CONFIG_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
);
65 file_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((unsigned char)ch
) && isspace((unsigned char)ch
)) ;
85 (void) ungetc(ch
, fp
);
90 } while (size
> 0 && !feof(fp
) && !ferror(fp
));
97 * Try to locate a key in a file
100 file_search_or_reload(mnt_map
*m
,
105 void (*fn
) (mnt_map
*m
, char *, char *))
107 char key_val
[INFO_MAX_LINE_LEN
];
111 while (file_read_line(key_val
, sizeof(key_val
), fp
)) {
115 int len
= strlen(key_val
);
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
);
125 key_val
[len
- 1] = '\0';
131 hash
= strchr(key_val
, '#');
138 for (kp
= key_val
; *kp
&& isascii((unsigned char)*kp
) && isspace((unsigned char)*kp
); kp
++) ;
149 for (cp
= kp
; *cp
&& (!isascii((unsigned char)*cp
) || !isspace((unsigned char)*cp
)); cp
++) ;
152 * Check whether key matches
157 if (fn
|| (*key
== *kp
&& STREQ(key
, kp
))) {
158 while (*cp
&& isascii((unsigned char)*cp
) && isspace((unsigned char)*cp
))
162 * Return a copy of the data
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
);
171 (*fn
) (m
, strdup(kp
), dc
);
174 dlog("%s returns %s", key
, dc
);
179 plog(XLOG_USER
, "%s: line %d has no value field", map
, line_no
);
185 * If the last read didn't get a whole line then
186 * throw away the remainder before continuing...
189 while (fgets(key_val
, sizeof(key_val
), fp
) &&
190 !strchr(key_val
, '\n')) ;
195 return fn
? 0 : ENOENT
;
200 file_open(char *map
, time_t *tp
)
202 FILE *mapf
= fopen(map
, "r");
206 if (fstat(fileno(mapf
), &stb
) < 0)
207 *tp
= clocktime(NULL
);
216 file_init_or_mtime(mnt_map
*m
, char *map
, time_t *tp
)
218 FILE *mapf
= file_open(map
, tp
);
229 file_reload(mnt_map
*m
, char *map
, void (*fn
) (mnt_map
*, char *, char *))
231 FILE *mapf
= file_open(map
, (time_t *) NULL
);
234 int error
= file_search_or_reload(m
, mapf
, map
, NULL
, NULL
, fn
);
243 file_search(mnt_map
*m
, char *map
, char *key
, char **pval
, time_t *tp
)
246 FILE *mapf
= file_open(map
, &t
);
254 error
= file_search_or_reload(m
, mapf
, map
, key
, pval
, NULL
);