17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*d67944fbSScott Rotondo * Common Development and Distribution License (the "License").
6*d67944fbSScott Rotondo * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
227c478bd9Sstevel@tonic-gate /* All Rights Reserved */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
26*d67944fbSScott Rotondo * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
277c478bd9Sstevel@tonic-gate * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * generic interface to dfs commands.
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * usage: cmd [-F fstype] [-o fs_options] [ args ]
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * exec's /usr/lib/fs/<fstype>/<cmd>
367c478bd9Sstevel@tonic-gate * <cmd> is the basename of the command.
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * if -F is missing, fstype is the first entry in /etc/dfs/fstypes
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate #include <dirent.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes" /* dfs list */
517c478bd9Sstevel@tonic-gate #define FSCMD "/usr/lib/fs/%s/%s"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define ARGVPAD 4 /* non-[arg...] elements in new argv list: */
547c478bd9Sstevel@tonic-gate /* cmd name, -o, opts, (char *)0 terminator */
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static char *getfs();
57*d67944fbSScott Rotondo static int invalid();
587c478bd9Sstevel@tonic-gate void perror();
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate int
main(argc,argv)617c478bd9Sstevel@tonic-gate main(argc, argv)
627c478bd9Sstevel@tonic-gate int argc;
637c478bd9Sstevel@tonic-gate char **argv;
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate extern char *optarg;
667c478bd9Sstevel@tonic-gate extern int optind;
677c478bd9Sstevel@tonic-gate FILE *dfp; /* fp for dfs list */
687c478bd9Sstevel@tonic-gate int c, err = 0;
697c478bd9Sstevel@tonic-gate char subcmd[BUFSIZ]; /* fs specific command */
707c478bd9Sstevel@tonic-gate char *cmd; /* basename of this command */
717c478bd9Sstevel@tonic-gate char *fsname = NULL; /* file system name */
727c478bd9Sstevel@tonic-gate char *opts = NULL; /* -o options */
737c478bd9Sstevel@tonic-gate char **nargv; /* new argv list */
747c478bd9Sstevel@tonic-gate int nargc = 0; /* new argc */
757c478bd9Sstevel@tonic-gate static char usage[] =
767c478bd9Sstevel@tonic-gate "usage: %s [-F fstype] [-o fs_options ] [arg ...]\n";
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate cmd = strrchr(argv[0], '/'); /* find the basename */
797c478bd9Sstevel@tonic-gate if (cmd)
807c478bd9Sstevel@tonic-gate ++cmd;
817c478bd9Sstevel@tonic-gate else
827c478bd9Sstevel@tonic-gate cmd = argv[0];
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "F:o:")) != -1)
857c478bd9Sstevel@tonic-gate switch (c) {
867c478bd9Sstevel@tonic-gate case 'F':
877c478bd9Sstevel@tonic-gate err |= (fsname != NULL); /* at most one -F */
887c478bd9Sstevel@tonic-gate fsname = optarg;
897c478bd9Sstevel@tonic-gate break;
907c478bd9Sstevel@tonic-gate case 'o': /* fs specific options */
917c478bd9Sstevel@tonic-gate err |= (opts != NULL); /* at most one -o */
927c478bd9Sstevel@tonic-gate opts = optarg;
937c478bd9Sstevel@tonic-gate break;
947c478bd9Sstevel@tonic-gate case '?':
957c478bd9Sstevel@tonic-gate err = 1;
967c478bd9Sstevel@tonic-gate break;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate if (err) {
997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmd);
1007c478bd9Sstevel@tonic-gate exit(1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if ((dfp = fopen(DFSTYPES, "r")) == NULL) {
1047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open %s\n",
1057c478bd9Sstevel@tonic-gate cmd, DFSTYPES);
1067c478bd9Sstevel@tonic-gate exit(1);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (fsname) { /* generate fs specific command name */
1107c478bd9Sstevel@tonic-gate if (invalid(fsname, dfp)) { /* valid ? */
1117c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1127c478bd9Sstevel@tonic-gate "%s: invalid file system name\n", cmd);
1137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmd);
1147c478bd9Sstevel@tonic-gate exit(1);
1157c478bd9Sstevel@tonic-gate } else {
1167c478bd9Sstevel@tonic-gate (void) snprintf(subcmd, sizeof (subcmd),
1177c478bd9Sstevel@tonic-gate FSCMD, fsname, cmd);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate } else if (fsname = getfs(dfp)) { /* use 1st line in dfstypes */
1207c478bd9Sstevel@tonic-gate (void) snprintf(subcmd, sizeof (subcmd), FSCMD, fsname, cmd);
1217c478bd9Sstevel@tonic-gate } else {
1227c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1237c478bd9Sstevel@tonic-gate "%s: no file systems in %s\n", cmd, DFSTYPES);
1247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmd);
1257c478bd9Sstevel@tonic-gate exit(1);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /* allocate a block for the new argv list */
1297c478bd9Sstevel@tonic-gate if (!(nargv = (char **)malloc(sizeof (char *)*(argc-optind+ARGVPAD)))) {
1307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: malloc failed.\n", cmd);
1317c478bd9Sstevel@tonic-gate exit(1);
1327c478bd9Sstevel@tonic-gate /*NOTREACHED*/
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate nargv[nargc++] = cmd;
1357c478bd9Sstevel@tonic-gate if (opts) {
1367c478bd9Sstevel@tonic-gate nargv[nargc++] = "-o";
1377c478bd9Sstevel@tonic-gate nargv[nargc++] = opts;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate for (; optind <= argc; ++optind) /* this copies the last NULL */
1407c478bd9Sstevel@tonic-gate nargv[nargc++] = argv[optind];
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate (void) execvp(subcmd, nargv);
1437c478bd9Sstevel@tonic-gate perror(subcmd);
1447c478bd9Sstevel@tonic-gate return (1);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * invalid(name, f) - return non-zero if name is not in
1507c478bd9Sstevel@tonic-gate * the list of fs names in file f
1517c478bd9Sstevel@tonic-gate */
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate static int
invalid(name,f)1547c478bd9Sstevel@tonic-gate invalid(name, f)
1557c478bd9Sstevel@tonic-gate char *name; /* file system name */
1567c478bd9Sstevel@tonic-gate FILE *f; /* file of list of systems */
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate char *s;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate while (s = getfs(f)) /* while there's still hope ... */
1617c478bd9Sstevel@tonic-gate if (strcmp(s, name) == 0)
1627c478bd9Sstevel@tonic-gate return (0); /* we got it! */
1637c478bd9Sstevel@tonic-gate return (1);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate * getfs(fp) - get the next file system name from fp
1697c478bd9Sstevel@tonic-gate * ignoring lines starting with a #.
1707c478bd9Sstevel@tonic-gate * All leading whitespace is discarded.
1717c478bd9Sstevel@tonic-gate */
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate static char buf[BUFSIZ];
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate static char *
getfs(fp)1767c478bd9Sstevel@tonic-gate getfs(fp)
1777c478bd9Sstevel@tonic-gate FILE *fp;
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate char *s;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate while (s = fgets(buf, BUFSIZ, fp)) {
1827c478bd9Sstevel@tonic-gate while (isspace(*s)) /* leading whitespace doesn't count */
1837c478bd9Sstevel@tonic-gate ++s;
1847c478bd9Sstevel@tonic-gate if (*s != '#') { /* not a comment */
1857c478bd9Sstevel@tonic-gate char *t = s;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate while (!isspace(*t)) /* get the token */
1887c478bd9Sstevel@tonic-gate ++t;
1897c478bd9Sstevel@tonic-gate *t = '\0'; /* ignore rest of line */
1907c478bd9Sstevel@tonic-gate return (s);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate return (NULL); /* that's all, folks! */
1947c478bd9Sstevel@tonic-gate }
195