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