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