2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * File: am-utils/fsinfo/fsi_util.c
39 #endif /* HAVE_CONFIG_H */
44 /* static variables */
45 static int show_range = 10;
47 static int total_shown = 0;
48 static int total_mmm = 8;
69 if (total_mmm != -show_range + 1) {
75 xsnprintf(n, sizeof(n), "%d", total_shown);
81 total_mmm = -show_range;
103 * Lots of ways of reporting errors...
106 error(char *fmt, ...)
112 fprintf(stderr, "%s: Error, ", progname);
113 vfprintf(stderr, fmt, ap);
121 lerror(ioloc *l, char *fmt, ...)
127 fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
128 vfprintf(stderr, fmt, ap);
136 lwarning(ioloc *l, char *fmt, ...)
142 fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
143 vfprintf(stderr, fmt, ap);
150 fatal(char *fmt, ...)
156 fprintf(stderr, "%s: Fatal, ", progname);
157 vfprintf(stderr, fmt, ap);
168 fsi_log(char *fmt, ...)
175 fprintf(stdout, "%s: ", progname);
176 vfprintf(stdout, fmt, ap);
184 info_hdr(FILE *ef, char *info)
186 fprintf(ef, "# *** NOTE: This file contains %s info\n", info);
191 gen_hdr(FILE *ef, char *hn)
193 fprintf(ef, "# *** NOTE: Only for use on %s\n", hn);
198 make_banner(FILE *fp)
200 time_t t = time((time_t *) NULL);
201 char *cp = ctime(&t);
205 # *** This file was automatically generated -- DO NOT EDIT HERE ***\n\
206 # \"%s\" run by %s@%s on %s\
209 progname, username, hostname, cp);
220 if (total_mmm > show_range) {
222 } else if (total_mmm == 0) {
232 show_area_being_processed(char *area, int n)
234 static char *last_area = NULL;
245 if (!last_area || !STREQ(area, last_area)) {
249 total_mmm = show_range + 1;
251 (void) col_output(strlen(area) + 2);
252 fprintf(stdout, "[%s", area);
266 * Open a file with the given prefix and name
269 pref_open(char *pref, char *hn, void (*hdr) (FILE *, char *), char *arg)
274 xsnprintf(p, sizeof(p), "%s%s", pref, hn);
275 fsi_log("Writing %s info for %s to %s", pref, hn, p);
281 error("can't open %s for writing", p);
291 return fclose(fp) == 0;
296 * Determine where Amd would automount the host/volname pair
299 compute_automount_point(char *buf, size_t l, host *hp, char *vn)
301 xsnprintf(buf, l, "%s/%s%s", autodir, hp->h_lochost, vn);
306 * Data constructors..
309 new_automount(char *name)
311 automount *ap = CALLOC(struct automount);
313 ap->a_ioloc = current_location();
315 ap->a_volname = NULL;
318 show_new("automount");
324 new_auto_tree(char *def, qelem *ap)
326 auto_tree *tp = CALLOC(struct auto_tree);
328 tp->t_ioloc = current_location();
329 tp->t_defaults = def;
331 show_new("auto_tree");
339 host *hp = CALLOC(struct host);
341 hp->h_ioloc = current_location();
349 set_host(host *hp, int k, char *v)
353 if (hp->h_mask & m) {
354 fsi_error("host field \"%s\" already set", host_strings[k]);
362 char *p = xstrdup(v);
363 dict_ent *de = dict_locate(dict_of_hosts, v);
366 fsi_error("duplicate host %s!", v);
368 dict_add(dict_of_hosts, v, (char *) hp);
370 domain_strip(p, hostname);
371 if (strchr(p, '.') != 0)
380 qelem *vq = (qelem *) v;
386 q = hp->h_config = new_que();
387 ins_que(vq, q->q_back);
393 qelem *vq = (qelem *) v;
399 q = hp->h_ether = new_que();
400 ins_que(vq, q->q_back);
426 ether_if *ep = CALLOC(struct ether_if);
429 ep->e_ioloc = current_location();
430 show_new("ether_if");
436 set_ether_if(ether_if *ep, int k, char *v)
440 if (ep->e_mask & m) {
441 fsi_error("netif field \"%s\" already set", ether_if_strings[k]);
449 ep->e_inaddr.s_addr = inet_addr(v);
450 if ((int) ep->e_inaddr.s_addr == (int) INADDR_NONE)
451 fsi_error("malformed IP dotted quad: %s", v);
459 if ((sscanf(v, "0x%lx", &nm) == 1 || sscanf(v, "%lx", &nm) == 1) && nm != 0)
460 ep->e_netmask = htonl(nm);
462 fsi_error("malformed netmask: %s", v);
479 set_disk_fs(disk_fs *dp, int k, char *v)
483 if (dp->d_mask & m) {
484 fsi_error("fs field \"%s\" already set", disk_fs_strings[k]);
508 dp->d_passno = atoi(v);
513 dp->d_freq = atoi(v);
518 dp->d_mount = &((fsi_mount *) v)->m_q;
531 disk_fs *dp = CALLOC(struct disk_fs);
533 dp->d_ioloc = current_location();
540 set_mount(fsi_mount *mp, int k, char *v)
544 if (mp->m_mask & m) {
545 fsi_error("mount tree field \"%s\" already set", mount_strings[k]);
553 dict_add(dict_of_volnames, v, (char *) mp);
575 fsi_mount *fp = CALLOC(struct fsi_mount);
577 fp->m_ioloc = current_location();
584 set_fsmount(fsmount *fp, int k, char *v)
588 if (fp->f_mask & m) {
589 fsi_error("mount field \"%s\" already set", fsmount_strings[k]);
629 fsmount *fp = CALLOC(struct fsmount);
631 fp->f_ioloc = current_location();
640 q->q_forw = q->q_back = q;
647 qelem *q = CALLOC(qelem);
655 ins_que(qelem *elem, qelem *pred)