xref: /original-bsd/usr.sbin/amd/fsinfo/fsinfo.c (revision 95a66346)
1 /*
2  * $Id: fsinfo.c,v 5.2.1.2 90/12/21 16:46:47 jsp Alpha $
3  *
4  * Copyright (c) 1989 Jan-Simon Pendry
5  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
6  * Copyright (c) 1989 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry at Imperial College, London.
11  *
12  * %sccs.include.redist.c%
13  *
14  *	@(#)fsinfo.c	5.2 (Berkeley) 03/17/91
15  */
16 
17 /*
18  * fsinfo
19  */
20 
21 #include "../fsinfo/fsinfo.h"
22 #include "fsi_gram.h"
23 #include <pwd.h>
24 
25 qelem *list_of_hosts;
26 qelem *list_of_automounts;
27 dict *dict_of_volnames;
28 dict *dict_of_hosts;
29 char *autodir = "/a";
30 char hostname[MAXHOSTNAMELEN+1];
31 char *username;
32 int file_io_errors;
33 int parse_errors;
34 int errors;
35 int verbose;
36 char idvbuf[1024];
37 
38 char **g_argv;
39 char *progname;
40 
41 /*
42  * Output file prefixes
43  */
44 char *exportfs_pref;
45 char *fstab_pref;
46 char *dumpset_pref;
47 char *mount_pref;
48 char *bootparams_pref;
49 
50 /*
51  * Argument cracking...
52  */
53 static void get_args(c, v)
54 int c;
55 char *v[];
56 {
57 	extern char *optarg;
58 	extern int optind;
59 	int ch;
60 	int usage = 0;
61 	char *iptr = idvbuf;
62 
63 	/*
64 	 * Determine program name
65 	 */
66 	if (v[0]) {
67 		progname = strrchr(v[0], '/');
68 		if (progname && progname[1])
69 			progname++;
70 		else
71 			progname = v[0];
72 	}
73 	if (!progname)
74 		progname = "fsinfo";
75 
76 	while ((ch = getopt(c, v, "a:b:d:e:f:h:m:D:U:I:qv")) != EOF)
77 	switch (ch) {
78 	case 'a':
79 		autodir = optarg;
80 		break;
81 	case 'b':
82 		if (bootparams_pref)
83 			fatal("-b option specified twice");
84 		bootparams_pref = optarg;
85 		break;
86 	case 'd':
87 		if (dumpset_pref)
88 			fatal("-d option specified twice");
89 		dumpset_pref = optarg;
90 		break;
91 	case 'h':
92 		strncpy(hostname, optarg, sizeof(hostname)-1);
93 		break;
94 	case 'e':
95 		if (exportfs_pref)
96 			fatal("-e option specified twice");
97 		exportfs_pref = optarg;
98 		break;
99 	case 'f':
100 		if (fstab_pref)
101 			fatal("-f option specified twice");
102 		fstab_pref = optarg;
103 		break;
104 	case 'm':
105 		if (mount_pref)
106 			fatal("-m option specified twice");
107 		mount_pref = optarg;
108 		break;
109 	case 'q':
110 		verbose = -1;
111 		break;
112 	case 'v':
113 		verbose = 1;
114 		break;
115 	case 'I': case 'D': case 'U':
116 		sprintf(iptr, "-%c%s ", ch, optarg);
117 		iptr += strlen(iptr);
118 		break;
119 	default:
120 		usage++;
121 		break;
122 	}
123 
124 	if (c != optind) {
125 		g_argv = v + optind - 1;
126 		if (yywrap())
127 			fatal("Cannot read any input files");
128 	} else {
129 		usage++;
130 	}
131 
132 	if (usage) {
133 		fprintf(stderr,
134 "\
135 Usage: %s [-v] [-a autodir] [-h hostname] [-b bootparams] [-d dumpsets]\n\
136 \t[-e exports] [-f fstabs] [-m automounts]\n\
137 \t[-I dir] [-D|-U string[=string]] config ...\n", progname);
138 		exit(1);
139 	}
140 
141 
142 	if (g_argv[0])
143 		log("g_argv[0] = %s", g_argv[0]);
144 	else
145 		log("g_argv[0] = (nil)");
146 }
147 
148 /*
149  * Determine username of caller
150  */
151 static char *find_username()
152 {
153 	extern char *getlogin();
154 	extern char *getenv();
155 	char *u = getlogin();
156 	if (!u) {
157 		struct passwd *pw = getpwuid(getuid());
158 		if (pw)
159 			u = pw->pw_name;
160 	}
161 	if (!u)
162 		u = getenv("USER");
163 	if (!u)
164 		u = getenv("LOGNAME");
165 	if (!u)
166 		u = "root";
167 
168 	return strdup(u);
169 }
170 
171 /*
172  * MAIN
173  */
174 main(argc, argv)
175 int argc;
176 char *argv[];
177 {
178 	/*
179 	 * Process arguments
180 	 */
181 	get_args(argc, argv);
182 
183 	/*
184 	 * If no hostname given then use the local name
185 	 */
186 	if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) {
187 		perror("gethostname");
188 		exit(1);
189 	}
190 
191 	/*
192 	 * Get the username
193 	 */
194 	username = find_username();
195 
196 	/*
197 	 * New hosts and automounts
198 	 */
199 	list_of_hosts = new_que();
200 	list_of_automounts = new_que();
201 
202 	/*
203 	 * New dictionaries
204 	 */
205 	dict_of_volnames = new_dict();
206 	dict_of_hosts = new_dict();
207 
208 	/*
209 	 * Parse input
210 	 */
211 	show_area_being_processed("read config", 11);
212 	if (yyparse())
213 		errors = 1;
214 	errors += file_io_errors + parse_errors;
215 
216 	if (errors == 0) {
217 		/*
218 		 * Do semantic analysis of input
219 		 */
220 		analyze_hosts(list_of_hosts);
221 		analyze_automounts(list_of_automounts);
222 	}
223 
224 	/*
225 	 * Give up if errors
226 	 */
227 	if (errors == 0) {
228 		/*
229 		 * Output data files
230 		 */
231 
232 		write_atab(list_of_automounts);
233 		write_bootparams(list_of_hosts);
234 		write_dumpset(list_of_hosts);
235 		write_exportfs(list_of_hosts);
236 		write_fstab(list_of_hosts);
237 	}
238 
239 	col_cleanup(1);
240 
241 	exit(errors);
242 }
243