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