Commit | Line | Data |
---|---|---|
74f0ca3f JSP |
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 | * Redistribution and use in source and binary forms are permitted provided | |
13 | * that: (1) source distributions retain this entire copyright notice and | |
14 | * comment, and (2) distributions including binaries display the following | |
15 | * acknowledgement: ``This product includes software developed by the | |
16 | * University of California, Berkeley and its contributors'' in the | |
17 | * documentation or other materials provided with the distribution and in | |
18 | * all advertising materials mentioning features or use of this software. | |
19 | * Neither the name of the University nor the names of its contributors may | |
20 | * be used to endorse or promote products derived from this software without | |
21 | * specific prior written permission. | |
22 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED | |
23 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | |
24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
25 | * | |
26 | * @(#)fsinfo.c 5.1 (Berkeley) %G% | |
27 | */ | |
28 | ||
29 | /* | |
30 | * fsinfo | |
31 | */ | |
32 | ||
33 | #include "../fsinfo/fsinfo.h" | |
34 | #include "fsi_gram.h" | |
35 | #include <pwd.h> | |
36 | ||
37 | qelem *list_of_hosts; | |
38 | qelem *list_of_automounts; | |
39 | dict *dict_of_volnames; | |
40 | dict *dict_of_hosts; | |
41 | char *autodir = "/a"; | |
42 | char hostname[MAXHOSTNAMELEN+1]; | |
43 | char *username; | |
44 | int file_io_errors; | |
45 | int parse_errors; | |
46 | int errors; | |
47 | int verbose; | |
48 | char idvbuf[1024]; | |
49 | ||
50 | char **g_argv; | |
51 | char *progname; | |
52 | ||
53 | /* | |
54 | * Output file prefixes | |
55 | */ | |
56 | char *exportfs_pref; | |
57 | char *fstab_pref; | |
58 | char *dumpset_pref; | |
59 | char *mount_pref; | |
60 | char *bootparams_pref; | |
61 | ||
62 | /* | |
63 | * Argument cracking... | |
64 | */ | |
65 | static void get_args(c, v) | |
66 | int c; | |
67 | char *v[]; | |
68 | { | |
69 | extern char *optarg; | |
70 | extern int optind; | |
71 | int ch; | |
72 | int usage = 0; | |
73 | char *iptr = idvbuf; | |
74 | ||
75 | /* | |
76 | * Determine program name | |
77 | */ | |
78 | if (v[0]) { | |
79 | progname = strrchr(v[0], '/'); | |
80 | if (progname && progname[1]) | |
81 | progname++; | |
82 | else | |
83 | progname = v[0]; | |
84 | } | |
85 | if (!progname) | |
86 | progname = "fsinfo"; | |
87 | ||
88 | while ((ch = getopt(c, v, "a:b:d:e:f:h:m:D:U:I:qv")) != EOF) | |
89 | switch (ch) { | |
90 | case 'a': | |
91 | autodir = optarg; | |
92 | break; | |
93 | case 'b': | |
94 | if (bootparams_pref) | |
95 | fatal("-b option specified twice"); | |
96 | bootparams_pref = optarg; | |
97 | break; | |
98 | case 'd': | |
99 | if (dumpset_pref) | |
100 | fatal("-d option specified twice"); | |
101 | dumpset_pref = optarg; | |
102 | break; | |
103 | case 'h': | |
104 | strncpy(hostname, optarg, sizeof(hostname)-1); | |
105 | break; | |
106 | case 'e': | |
107 | if (exportfs_pref) | |
108 | fatal("-e option specified twice"); | |
109 | exportfs_pref = optarg; | |
110 | break; | |
111 | case 'f': | |
112 | if (fstab_pref) | |
113 | fatal("-f option specified twice"); | |
114 | fstab_pref = optarg; | |
115 | break; | |
116 | case 'm': | |
117 | if (mount_pref) | |
118 | fatal("-m option specified twice"); | |
119 | mount_pref = optarg; | |
120 | break; | |
121 | case 'q': | |
122 | verbose = -1; | |
123 | break; | |
124 | case 'v': | |
125 | verbose = 1; | |
126 | break; | |
127 | case 'I': case 'D': case 'U': | |
128 | sprintf(iptr, "-%c%s ", ch, optarg); | |
129 | iptr += strlen(iptr); | |
130 | break; | |
131 | default: | |
132 | usage++; | |
133 | break; | |
134 | } | |
135 | ||
136 | if (c != optind) { | |
137 | g_argv = v + optind - 1; | |
138 | if (yywrap()) | |
139 | fatal("Cannot read any input files"); | |
140 | } else { | |
141 | usage++; | |
142 | } | |
143 | ||
144 | if (usage) { | |
145 | fprintf(stderr, | |
146 | "\ | |
147 | Usage: %s [-v] [-a autodir] [-h hostname] [-b bootparams] [-d dumpsets]\n\ | |
148 | \t[-e exports] [-f fstabs] [-m automounts]\n\ | |
149 | \t[-I dir] [-D|-U string[=string]] config ...\n", progname); | |
150 | exit(1); | |
151 | } | |
152 | ||
153 | ||
154 | if (g_argv[0]) | |
155 | log("g_argv[0] = %s", g_argv[0]); | |
156 | else | |
157 | log("g_argv[0] = (nil)"); | |
158 | } | |
159 | ||
160 | /* | |
161 | * Determine username of caller | |
162 | */ | |
163 | static char *find_username() | |
164 | { | |
165 | extern char *getlogin(); | |
166 | extern char *getenv(); | |
167 | char *u = getlogin(); | |
168 | if (!u) { | |
169 | struct passwd *pw = getpwuid(getuid()); | |
170 | if (pw) | |
171 | u = pw->pw_name; | |
172 | } | |
173 | if (!u) | |
174 | u = getenv("USER"); | |
175 | if (!u) | |
176 | u = getenv("LOGNAME"); | |
177 | if (!u) | |
178 | u = "root"; | |
179 | ||
180 | return strdup(u); | |
181 | } | |
182 | ||
183 | /* | |
184 | * MAIN | |
185 | */ | |
186 | main(argc, argv) | |
187 | int argc; | |
188 | char *argv[]; | |
189 | { | |
190 | /* | |
191 | * Process arguments | |
192 | */ | |
193 | get_args(argc, argv); | |
194 | ||
195 | /* | |
196 | * If no hostname given then use the local name | |
197 | */ | |
198 | if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) { | |
199 | perror("gethostname"); | |
200 | exit(1); | |
201 | } | |
202 | ||
203 | /* | |
204 | * Get the username | |
205 | */ | |
206 | username = find_username(); | |
207 | ||
208 | /* | |
209 | * New hosts and automounts | |
210 | */ | |
211 | list_of_hosts = new_que(); | |
212 | list_of_automounts = new_que(); | |
213 | ||
214 | /* | |
215 | * New dictionaries | |
216 | */ | |
217 | dict_of_volnames = new_dict(); | |
218 | dict_of_hosts = new_dict(); | |
219 | ||
220 | /* | |
221 | * Parse input | |
222 | */ | |
223 | show_area_being_processed("read config", 11); | |
224 | if (yyparse()) | |
225 | errors = 1; | |
226 | errors += file_io_errors + parse_errors; | |
227 | ||
228 | if (errors == 0) { | |
229 | /* | |
230 | * Do semantic analysis of input | |
231 | */ | |
232 | analyze_hosts(list_of_hosts); | |
233 | analyze_automounts(list_of_automounts); | |
234 | } | |
235 | ||
236 | /* | |
237 | * Give up if errors | |
238 | */ | |
239 | if (errors == 0) { | |
240 | /* | |
241 | * Output data files | |
242 | */ | |
243 | ||
244 | write_atab(list_of_automounts); | |
245 | write_bootparams(list_of_hosts); | |
246 | write_dumpset(list_of_hosts); | |
247 | write_exportfs(list_of_hosts); | |
248 | write_fstab(list_of_hosts); | |
249 | } | |
250 | ||
251 | col_cleanup(1); | |
252 | ||
253 | exit(errors); | |
254 | } |