xref: /csrg-svn/usr.sbin/amd/fsinfo/fsinfo.c (revision 47530)
147485Spendry /*
247485Spendry  * $Id: fsinfo.c,v 5.2.1.2 90/12/21 16:46:47 jsp Alpha $
347485Spendry  *
447485Spendry  * Copyright (c) 1989 Jan-Simon Pendry
547485Spendry  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
647485Spendry  * Copyright (c) 1989 The Regents of the University of California.
747485Spendry  * All rights reserved.
847485Spendry  *
947485Spendry  * This code is derived from software contributed to Berkeley by
1047485Spendry  * Jan-Simon Pendry at Imperial College, London.
1147485Spendry  *
12*47530Spendry  * %sccs.include.redist.c%
1347485Spendry  *
14*47530Spendry  *	@(#)fsinfo.c	5.2 (Berkeley) 03/17/91
1547485Spendry  */
1647485Spendry 
1747485Spendry /*
1847485Spendry  * fsinfo
1947485Spendry  */
2047485Spendry 
2147485Spendry #include "../fsinfo/fsinfo.h"
2247485Spendry #include "fsi_gram.h"
2347485Spendry #include <pwd.h>
2447485Spendry 
2547485Spendry qelem *list_of_hosts;
2647485Spendry qelem *list_of_automounts;
2747485Spendry dict *dict_of_volnames;
2847485Spendry dict *dict_of_hosts;
2947485Spendry char *autodir = "/a";
3047485Spendry char hostname[MAXHOSTNAMELEN+1];
3147485Spendry char *username;
3247485Spendry int file_io_errors;
3347485Spendry int parse_errors;
3447485Spendry int errors;
3547485Spendry int verbose;
3647485Spendry char idvbuf[1024];
3747485Spendry 
3847485Spendry char **g_argv;
3947485Spendry char *progname;
4047485Spendry 
4147485Spendry /*
4247485Spendry  * Output file prefixes
4347485Spendry  */
4447485Spendry char *exportfs_pref;
4547485Spendry char *fstab_pref;
4647485Spendry char *dumpset_pref;
4747485Spendry char *mount_pref;
4847485Spendry char *bootparams_pref;
4947485Spendry 
5047485Spendry /*
5147485Spendry  * Argument cracking...
5247485Spendry  */
5347485Spendry static void get_args(c, v)
5447485Spendry int c;
5547485Spendry char *v[];
5647485Spendry {
5747485Spendry 	extern char *optarg;
5847485Spendry 	extern int optind;
5947485Spendry 	int ch;
6047485Spendry 	int usage = 0;
6147485Spendry 	char *iptr = idvbuf;
6247485Spendry 
6347485Spendry 	/*
6447485Spendry 	 * Determine program name
6547485Spendry 	 */
6647485Spendry 	if (v[0]) {
6747485Spendry 		progname = strrchr(v[0], '/');
6847485Spendry 		if (progname && progname[1])
6947485Spendry 			progname++;
7047485Spendry 		else
7147485Spendry 			progname = v[0];
7247485Spendry 	}
7347485Spendry 	if (!progname)
7447485Spendry 		progname = "fsinfo";
7547485Spendry 
7647485Spendry 	while ((ch = getopt(c, v, "a:b:d:e:f:h:m:D:U:I:qv")) != EOF)
7747485Spendry 	switch (ch) {
7847485Spendry 	case 'a':
7947485Spendry 		autodir = optarg;
8047485Spendry 		break;
8147485Spendry 	case 'b':
8247485Spendry 		if (bootparams_pref)
8347485Spendry 			fatal("-b option specified twice");
8447485Spendry 		bootparams_pref = optarg;
8547485Spendry 		break;
8647485Spendry 	case 'd':
8747485Spendry 		if (dumpset_pref)
8847485Spendry 			fatal("-d option specified twice");
8947485Spendry 		dumpset_pref = optarg;
9047485Spendry 		break;
9147485Spendry 	case 'h':
9247485Spendry 		strncpy(hostname, optarg, sizeof(hostname)-1);
9347485Spendry 		break;
9447485Spendry 	case 'e':
9547485Spendry 		if (exportfs_pref)
9647485Spendry 			fatal("-e option specified twice");
9747485Spendry 		exportfs_pref = optarg;
9847485Spendry 		break;
9947485Spendry 	case 'f':
10047485Spendry 		if (fstab_pref)
10147485Spendry 			fatal("-f option specified twice");
10247485Spendry 		fstab_pref = optarg;
10347485Spendry 		break;
10447485Spendry 	case 'm':
10547485Spendry 		if (mount_pref)
10647485Spendry 			fatal("-m option specified twice");
10747485Spendry 		mount_pref = optarg;
10847485Spendry 		break;
10947485Spendry 	case 'q':
11047485Spendry 		verbose = -1;
11147485Spendry 		break;
11247485Spendry 	case 'v':
11347485Spendry 		verbose = 1;
11447485Spendry 		break;
11547485Spendry 	case 'I': case 'D': case 'U':
11647485Spendry 		sprintf(iptr, "-%c%s ", ch, optarg);
11747485Spendry 		iptr += strlen(iptr);
11847485Spendry 		break;
11947485Spendry 	default:
12047485Spendry 		usage++;
12147485Spendry 		break;
12247485Spendry 	}
12347485Spendry 
12447485Spendry 	if (c != optind) {
12547485Spendry 		g_argv = v + optind - 1;
12647485Spendry 		if (yywrap())
12747485Spendry 			fatal("Cannot read any input files");
12847485Spendry 	} else {
12947485Spendry 		usage++;
13047485Spendry 	}
13147485Spendry 
13247485Spendry 	if (usage) {
13347485Spendry 		fprintf(stderr,
13447485Spendry "\
13547485Spendry Usage: %s [-v] [-a autodir] [-h hostname] [-b bootparams] [-d dumpsets]\n\
13647485Spendry \t[-e exports] [-f fstabs] [-m automounts]\n\
13747485Spendry \t[-I dir] [-D|-U string[=string]] config ...\n", progname);
13847485Spendry 		exit(1);
13947485Spendry 	}
14047485Spendry 
14147485Spendry 
14247485Spendry 	if (g_argv[0])
14347485Spendry 		log("g_argv[0] = %s", g_argv[0]);
14447485Spendry 	else
14547485Spendry 		log("g_argv[0] = (nil)");
14647485Spendry }
14747485Spendry 
14847485Spendry /*
14947485Spendry  * Determine username of caller
15047485Spendry  */
15147485Spendry static char *find_username()
15247485Spendry {
15347485Spendry 	extern char *getlogin();
15447485Spendry 	extern char *getenv();
15547485Spendry 	char *u = getlogin();
15647485Spendry 	if (!u) {
15747485Spendry 		struct passwd *pw = getpwuid(getuid());
15847485Spendry 		if (pw)
15947485Spendry 			u = pw->pw_name;
16047485Spendry 	}
16147485Spendry 	if (!u)
16247485Spendry 		u = getenv("USER");
16347485Spendry 	if (!u)
16447485Spendry 		u = getenv("LOGNAME");
16547485Spendry 	if (!u)
16647485Spendry 		u = "root";
16747485Spendry 
16847485Spendry 	return strdup(u);
16947485Spendry }
17047485Spendry 
17147485Spendry /*
17247485Spendry  * MAIN
17347485Spendry  */
17447485Spendry main(argc, argv)
17547485Spendry int argc;
17647485Spendry char *argv[];
17747485Spendry {
17847485Spendry 	/*
17947485Spendry 	 * Process arguments
18047485Spendry 	 */
18147485Spendry 	get_args(argc, argv);
18247485Spendry 
18347485Spendry 	/*
18447485Spendry 	 * If no hostname given then use the local name
18547485Spendry 	 */
18647485Spendry 	if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) {
18747485Spendry 		perror("gethostname");
18847485Spendry 		exit(1);
18947485Spendry 	}
19047485Spendry 
19147485Spendry 	/*
19247485Spendry 	 * Get the username
19347485Spendry 	 */
19447485Spendry 	username = find_username();
19547485Spendry 
19647485Spendry 	/*
19747485Spendry 	 * New hosts and automounts
19847485Spendry 	 */
19947485Spendry 	list_of_hosts = new_que();
20047485Spendry 	list_of_automounts = new_que();
20147485Spendry 
20247485Spendry 	/*
20347485Spendry 	 * New dictionaries
20447485Spendry 	 */
20547485Spendry 	dict_of_volnames = new_dict();
20647485Spendry 	dict_of_hosts = new_dict();
20747485Spendry 
20847485Spendry 	/*
20947485Spendry 	 * Parse input
21047485Spendry 	 */
21147485Spendry 	show_area_being_processed("read config", 11);
21247485Spendry 	if (yyparse())
21347485Spendry 		errors = 1;
21447485Spendry 	errors += file_io_errors + parse_errors;
21547485Spendry 
21647485Spendry 	if (errors == 0) {
21747485Spendry 		/*
21847485Spendry 		 * Do semantic analysis of input
21947485Spendry 		 */
22047485Spendry 		analyze_hosts(list_of_hosts);
22147485Spendry 		analyze_automounts(list_of_automounts);
22247485Spendry 	}
22347485Spendry 
22447485Spendry 	/*
22547485Spendry 	 * Give up if errors
22647485Spendry 	 */
22747485Spendry 	if (errors == 0) {
22847485Spendry 		/*
22947485Spendry 		 * Output data files
23047485Spendry 		 */
23147485Spendry 
23247485Spendry 		write_atab(list_of_automounts);
23347485Spendry 		write_bootparams(list_of_hosts);
23447485Spendry 		write_dumpset(list_of_hosts);
23547485Spendry 		write_exportfs(list_of_hosts);
23647485Spendry 		write_fstab(list_of_hosts);
23747485Spendry 	}
23847485Spendry 
23947485Spendry 	col_cleanup(1);
24047485Spendry 
24147485Spendry 	exit(errors);
24247485Spendry }
243