4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * generic interface to dfshares, dfmounts.
36 * usage: dfshares [-F fstype] [-o fs_options] [-h] [ args ]
38 * exec's /usr/lib/fs/<fstype>/<cmd>
39 * <cmd> is the basename of the command.
41 * if -F is missing, fstype is the first entry in /etc/dfs/fstypes
44 #include <sys/types.h>
55 #define DFSTYPES "/etc/dfs/fstypes" /* dfs list */
56 #define FSCMD "/usr/lib/fs/%s/%s"
59 * non-[arg...] elements in new argv list:
60 * cmd name, , -h, -o, opts, (char *)0 terminator
64 static char *getfs(FILE *);
65 static int invalid(const char *, FILE *);
68 main(int argc
, char **argv
)
70 FILE *dfp
; /* fp for dfs list */
72 char subcmd
[BUFSIZ
]; /* fs specific command */
73 char *cmd
; /* basename of this command */
74 char *fsname
= NULL
; /* file system name */
75 char *opts
= NULL
; /* -o options */
76 char **nargv
; /* new argv list */
78 int nargc
= 0; /* new argc */
79 pid_t pid
; /* pid for fork */
80 int retval
; /* exit status from exec'd commad */
81 int showall
= (argc
<= 1); /* show all resources */
83 "usage: %s [-F fstype] [-h] [-o fs_options ] [arg ...]\n";
85 cmd
= strrchr(argv
[0], '/'); /* find the basename */
91 while ((c
= getopt(argc
, argv
, "hF:o:")) != -1)
94 hflag
= 1; /* no header ... pass to subcommand */
97 err
|= (fsname
!= NULL
); /* at most one -F */
100 case 'o': /* fs specific options */
101 err
|= (opts
!= NULL
); /* at most one -o */
109 (void) fprintf(stderr
, usage
, cmd
);
113 if ((dfp
= fopen(DFSTYPES
, "r")) == NULL
) {
114 (void) fprintf(stderr
, "%s: cannot open %s\n", cmd
, DFSTYPES
);
118 /* allocate a block for the new argv list */
119 if (!(nargv
= (char **)malloc(sizeof (char *)*(argc
-optind
+ARGVPAD
)))) {
120 (void) fprintf(stderr
, "%s: malloc failed.\n", cmd
);
123 nargv
[nargc
++] = cmd
;
125 nargv
[nargc
++] = "-h";
127 nargv
[nargc
++] = "-o";
128 nargv
[nargc
++] = opts
;
130 for (; optind
<= argc
; ++optind
) /* this copies the last NULL */
131 nargv
[nargc
++] = argv
[optind
];
133 if (showall
) { /* command with no args -- show all dfs's */
134 while (fsname
= getfs(dfp
)) {
135 (void) snprintf(subcmd
, sizeof (subcmd
),
137 switch (pid
= fork()) { /* do the subcommand */
139 (void) execvp(subcmd
, nargv
);
145 while (wait(&retval
) != pid
)
147 /* take exit status into account */
148 err
|= (retval
& 0xff00) >> 8;
151 (void) fprintf(stderr
,
152 "%s: fork failed - try again later.\n",
158 if (pid
== 0) { /* we never got into the loop! */
159 (void) fprintf(stderr
,
160 "%s: no file systems in %s\n",
162 (void) fprintf(stderr
, usage
, cmd
);
169 if (fsname
) { /* generate fs specific command name */
170 if (invalid(fsname
, dfp
)) { /* valid ? */
171 (void) fprintf(stderr
,
172 "%s: invalid file system name\n", cmd
);
173 (void) fprintf(stderr
, usage
, cmd
);
177 (void) snprintf(subcmd
, sizeof (subcmd
),
179 } else if (fsname
= getfs(dfp
)) /* use 1st line in dfstypes */
180 (void) snprintf(subcmd
, sizeof (subcmd
), FSCMD
, fsname
, cmd
);
182 (void) fprintf(stderr
,
183 "%s: no file systems in %s\n", cmd
, DFSTYPES
);
184 (void) fprintf(stderr
, usage
, cmd
);
188 (void) execvp(subcmd
, nargv
);
189 perror(subcmd
); /* execvp failed */
195 * invalid(name, f) - return non-zero if name is not in
196 * the list of fs names in file f
200 invalid(const char *name
, /* file system name */
201 FILE *f
) /* file of list of file system types */
205 while (s
= getfs(f
)) /* while there's still hope ... */
206 if (strcmp(s
, name
) == 0)
207 return (0); /* we got it! */
213 * getfs(fp) - get the next file system name from fp
214 * ignoring lines starting with a #.
215 * All leading whitespace is discarded.
218 static char buf
[BUFSIZ
];
225 while (s
= fgets(buf
, BUFSIZ
, fp
)) {
226 while (isspace(*s
)) /* leading whitespace doesn't count */
228 if (*s
!= '#') { /* not a comment */
231 while (!isspace(*t
)) /* get the token */
233 *t
= '\0'; /* ignore rest of line */
237 return (NULL
); /* that's all, folks! */