1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * generic interface to dfs commands. 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * usage: cmd [-F fstype] [-o fs_options] [ args ] 37*7c478bd9Sstevel@tonic-gate * 38*7c478bd9Sstevel@tonic-gate * exec's /usr/lib/fs/<fstype>/<cmd> 39*7c478bd9Sstevel@tonic-gate * <cmd> is the basename of the command. 40*7c478bd9Sstevel@tonic-gate * 41*7c478bd9Sstevel@tonic-gate * if -F is missing, fstype is the first entry in /etc/dfs/fstypes 42*7c478bd9Sstevel@tonic-gate */ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 46*7c478bd9Sstevel@tonic-gate #include <ctype.h> 47*7c478bd9Sstevel@tonic-gate #include <stdio.h> 48*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 49*7c478bd9Sstevel@tonic-gate #include <unistd.h> 50*7c478bd9Sstevel@tonic-gate #include <dirent.h> 51*7c478bd9Sstevel@tonic-gate #include <string.h> 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes" /* dfs list */ 54*7c478bd9Sstevel@tonic-gate #define FSCMD "/usr/lib/fs/%s/%s" 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate #define ARGVPAD 4 /* non-[arg...] elements in new argv list: */ 57*7c478bd9Sstevel@tonic-gate /* cmd name, -o, opts, (char *)0 terminator */ 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static char *getfs(); 60*7c478bd9Sstevel@tonic-gate void perror(); 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate int 63*7c478bd9Sstevel@tonic-gate main(argc, argv) 64*7c478bd9Sstevel@tonic-gate int argc; 65*7c478bd9Sstevel@tonic-gate char **argv; 66*7c478bd9Sstevel@tonic-gate { 67*7c478bd9Sstevel@tonic-gate static int invalid(); 68*7c478bd9Sstevel@tonic-gate extern char *optarg; 69*7c478bd9Sstevel@tonic-gate extern int optind; 70*7c478bd9Sstevel@tonic-gate FILE *dfp; /* fp for dfs list */ 71*7c478bd9Sstevel@tonic-gate int c, err = 0; 72*7c478bd9Sstevel@tonic-gate char subcmd[BUFSIZ]; /* fs specific command */ 73*7c478bd9Sstevel@tonic-gate char *cmd; /* basename of this command */ 74*7c478bd9Sstevel@tonic-gate char *fsname = NULL; /* file system name */ 75*7c478bd9Sstevel@tonic-gate char *opts = NULL; /* -o options */ 76*7c478bd9Sstevel@tonic-gate char **nargv; /* new argv list */ 77*7c478bd9Sstevel@tonic-gate int nargc = 0; /* new argc */ 78*7c478bd9Sstevel@tonic-gate static char usage[] = 79*7c478bd9Sstevel@tonic-gate "usage: %s [-F fstype] [-o fs_options ] [arg ...]\n"; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate cmd = strrchr(argv[0], '/'); /* find the basename */ 82*7c478bd9Sstevel@tonic-gate if (cmd) 83*7c478bd9Sstevel@tonic-gate ++cmd; 84*7c478bd9Sstevel@tonic-gate else 85*7c478bd9Sstevel@tonic-gate cmd = argv[0]; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "F:o:")) != -1) 88*7c478bd9Sstevel@tonic-gate switch (c) { 89*7c478bd9Sstevel@tonic-gate case 'F': 90*7c478bd9Sstevel@tonic-gate err |= (fsname != NULL); /* at most one -F */ 91*7c478bd9Sstevel@tonic-gate fsname = optarg; 92*7c478bd9Sstevel@tonic-gate break; 93*7c478bd9Sstevel@tonic-gate case 'o': /* fs specific options */ 94*7c478bd9Sstevel@tonic-gate err |= (opts != NULL); /* at most one -o */ 95*7c478bd9Sstevel@tonic-gate opts = optarg; 96*7c478bd9Sstevel@tonic-gate break; 97*7c478bd9Sstevel@tonic-gate case '?': 98*7c478bd9Sstevel@tonic-gate err = 1; 99*7c478bd9Sstevel@tonic-gate break; 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate if (err) { 102*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmd); 103*7c478bd9Sstevel@tonic-gate exit(1); 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate if ((dfp = fopen(DFSTYPES, "r")) == NULL) { 107*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open %s\n", 108*7c478bd9Sstevel@tonic-gate cmd, DFSTYPES); 109*7c478bd9Sstevel@tonic-gate exit(1); 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate if (fsname) { /* generate fs specific command name */ 113*7c478bd9Sstevel@tonic-gate if (invalid(fsname, dfp)) { /* valid ? */ 114*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 115*7c478bd9Sstevel@tonic-gate "%s: invalid file system name\n", cmd); 116*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmd); 117*7c478bd9Sstevel@tonic-gate exit(1); 118*7c478bd9Sstevel@tonic-gate } else { 119*7c478bd9Sstevel@tonic-gate (void) snprintf(subcmd, sizeof (subcmd), 120*7c478bd9Sstevel@tonic-gate FSCMD, fsname, cmd); 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate } else if (fsname = getfs(dfp)) { /* use 1st line in dfstypes */ 123*7c478bd9Sstevel@tonic-gate (void) snprintf(subcmd, sizeof (subcmd), FSCMD, fsname, cmd); 124*7c478bd9Sstevel@tonic-gate } else { 125*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 126*7c478bd9Sstevel@tonic-gate "%s: no file systems in %s\n", cmd, DFSTYPES); 127*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmd); 128*7c478bd9Sstevel@tonic-gate exit(1); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate /* allocate a block for the new argv list */ 132*7c478bd9Sstevel@tonic-gate if (!(nargv = (char **)malloc(sizeof (char *)*(argc-optind+ARGVPAD)))) { 133*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: malloc failed.\n", cmd); 134*7c478bd9Sstevel@tonic-gate exit(1); 135*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate nargv[nargc++] = cmd; 138*7c478bd9Sstevel@tonic-gate if (opts) { 139*7c478bd9Sstevel@tonic-gate nargv[nargc++] = "-o"; 140*7c478bd9Sstevel@tonic-gate nargv[nargc++] = opts; 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate for (; optind <= argc; ++optind) /* this copies the last NULL */ 143*7c478bd9Sstevel@tonic-gate nargv[nargc++] = argv[optind]; 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate (void) execvp(subcmd, nargv); 146*7c478bd9Sstevel@tonic-gate perror(subcmd); 147*7c478bd9Sstevel@tonic-gate return (1); 148*7c478bd9Sstevel@tonic-gate } 149*7c478bd9Sstevel@tonic-gate 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate /* 152*7c478bd9Sstevel@tonic-gate * invalid(name, f) - return non-zero if name is not in 153*7c478bd9Sstevel@tonic-gate * the list of fs names in file f 154*7c478bd9Sstevel@tonic-gate */ 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate static int 157*7c478bd9Sstevel@tonic-gate invalid(name, f) 158*7c478bd9Sstevel@tonic-gate char *name; /* file system name */ 159*7c478bd9Sstevel@tonic-gate FILE *f; /* file of list of systems */ 160*7c478bd9Sstevel@tonic-gate { 161*7c478bd9Sstevel@tonic-gate char *s; 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate while (s = getfs(f)) /* while there's still hope ... */ 164*7c478bd9Sstevel@tonic-gate if (strcmp(s, name) == 0) 165*7c478bd9Sstevel@tonic-gate return (0); /* we got it! */ 166*7c478bd9Sstevel@tonic-gate return (1); 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate /* 171*7c478bd9Sstevel@tonic-gate * getfs(fp) - get the next file system name from fp 172*7c478bd9Sstevel@tonic-gate * ignoring lines starting with a #. 173*7c478bd9Sstevel@tonic-gate * All leading whitespace is discarded. 174*7c478bd9Sstevel@tonic-gate */ 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate static char buf[BUFSIZ]; 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate static char * 179*7c478bd9Sstevel@tonic-gate getfs(fp) 180*7c478bd9Sstevel@tonic-gate FILE *fp; 181*7c478bd9Sstevel@tonic-gate { 182*7c478bd9Sstevel@tonic-gate char *s; 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate while (s = fgets(buf, BUFSIZ, fp)) { 185*7c478bd9Sstevel@tonic-gate while (isspace(*s)) /* leading whitespace doesn't count */ 186*7c478bd9Sstevel@tonic-gate ++s; 187*7c478bd9Sstevel@tonic-gate if (*s != '#') { /* not a comment */ 188*7c478bd9Sstevel@tonic-gate char *t = s; 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate while (!isspace(*t)) /* get the token */ 191*7c478bd9Sstevel@tonic-gate ++t; 192*7c478bd9Sstevel@tonic-gate *t = '\0'; /* ignore rest of line */ 193*7c478bd9Sstevel@tonic-gate return (s); 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate return (NULL); /* that's all, folks! */ 197*7c478bd9Sstevel@tonic-gate } 198