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