Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.29
      1  1.29  christos /*	$NetBSD: mount.c,v 1.29 1997/07/04 15:17:57 christos Exp $	*/
      2  1.16       cgd 
      3   1.1       cgd /*
      4  1.12   mycroft  * Copyright (c) 1980, 1989, 1993, 1994
      5  1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.29  christos #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.29  christos __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
     39  1.29  christos 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.16       cgd #if 0
     44  1.16       cgd static char sccsid[] = "@(#)mount.c	8.19 (Berkeley) 4/19/94";
     45  1.16       cgd #else
     46  1.29  christos __RCSID("$NetBSD: mount.c,v 1.29 1997/07/04 15:17:57 christos Exp $");
     47  1.16       cgd #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/param.h>
     51  1.12   mycroft #include <sys/mount.h>
     52   1.1       cgd #include <sys/wait.h>
     53  1.12   mycroft 
     54  1.12   mycroft #include <err.h>
     55  1.12   mycroft #include <errno.h>
     56   1.1       cgd #include <fstab.h>
     57  1.12   mycroft #include <signal.h>
     58   1.1       cgd #include <stdio.h>
     59   1.1       cgd #include <stdlib.h>
     60  1.12   mycroft #include <string.h>
     61  1.12   mycroft #include <unistd.h>
     62  1.12   mycroft 
     63   1.1       cgd #include "pathnames.h"
     64   1.1       cgd 
     65  1.15   mycroft int	debug, verbose;
     66  1.15   mycroft char	**typelist = NULL;
     67   1.1       cgd 
     68  1.15   mycroft int	selected __P((const char *));
     69  1.12   mycroft char   *catopt __P((char *, const char *));
     70  1.12   mycroft struct statfs
     71  1.12   mycroft        *getmntpt __P((const char *));
     72  1.12   mycroft int	hasopt __P((const char *, const char *));
     73  1.15   mycroft void	maketypelist __P((char *));
     74  1.12   mycroft void	mangle __P((char *, int *, const char **));
     75  1.12   mycroft int	mountfs __P((const char *, const char *, const char *,
     76  1.22       cgd 			int, const char *, const char *, int));
     77  1.13   mycroft void	prmount __P((struct statfs *));
     78  1.12   mycroft void	usage __P((void));
     79  1.29  christos int	main __P((int, char *[]));
     80  1.12   mycroft 
     81  1.12   mycroft /* Map from mount otions to printable formats. */
     82  1.12   mycroft static struct opt {
     83  1.12   mycroft 	int o_opt;
     84  1.24       cgd 	int o_silent;
     85  1.12   mycroft 	const char *o_name;
     86  1.12   mycroft } optnames[] = {
     87  1.24       cgd 	{ MNT_ASYNC,		0,	"asynchronous" },
     88  1.24       cgd 	{ MNT_DEFEXPORTED,	1,	"exported to the world" },
     89  1.24       cgd 	{ MNT_EXKERB,		1,	"kerberos uid mapping" },
     90  1.24       cgd 	{ MNT_EXPORTED,		0,	"NFS exported" },
     91  1.24       cgd 	{ MNT_EXPORTANON,	1,	"anon uid mapping" },
     92  1.24       cgd 	{ MNT_EXRDONLY,		1,	"exported read-only" },
     93  1.24       cgd 	{ MNT_LOCAL,		0,	"local" },
     94  1.26     mikel 	{ MNT_NOATIME,		0,	"noatime" },
     95  1.25       cgd 	{ MNT_NOCOREDUMP,	0,	"nocoredump" },
     96  1.24       cgd 	{ MNT_NODEV,		0,	"nodev" },
     97  1.24       cgd 	{ MNT_NOEXEC,		0,	"noexec" },
     98  1.24       cgd 	{ MNT_NOSUID,		0,	"nosuid" },
     99  1.24       cgd 	{ MNT_QUOTA,		0,	"with quotas" },
    100  1.24       cgd 	{ MNT_RDONLY,		0,	"read-only" },
    101  1.24       cgd 	{ MNT_ROOTFS,		1,	"root file system" },
    102  1.24       cgd 	{ MNT_SYNCHRONOUS,	0,	"synchronous" },
    103  1.24       cgd 	{ MNT_UNION,		0,	"union" },
    104  1.27        pk 	{ 0 }
    105   1.1       cgd };
    106   1.1       cgd 
    107  1.12   mycroft int
    108  1.12   mycroft main(argc, argv)
    109   1.1       cgd 	int argc;
    110  1.29  christos 	char *argv[];
    111   1.1       cgd {
    112  1.15   mycroft 	const char *mntonname, *vfstype;
    113  1.12   mycroft 	struct fstab *fs;
    114   1.9       cgd 	struct statfs *mntbuf;
    115  1.12   mycroft 	FILE *mountdfp;
    116  1.22       cgd 	int all, ch, forceall, i, init_flags, mntsize, rval;
    117  1.12   mycroft 	char *options;
    118   1.1       cgd 
    119  1.22       cgd 	all = forceall = init_flags = 0;
    120  1.12   mycroft 	options = NULL;
    121  1.23       jtc 	vfstype = "ffs";
    122  1.22       cgd 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != EOF)
    123  1.12   mycroft 		switch (ch) {
    124  1.22       cgd 		case 'A':
    125  1.22       cgd 			all = forceall = 1;
    126  1.22       cgd 			break;
    127   1.1       cgd 		case 'a':
    128   1.1       cgd 			all = 1;
    129   1.1       cgd 			break;
    130  1.12   mycroft 		case 'd':
    131  1.12   mycroft 			debug = 1;
    132  1.12   mycroft 			break;
    133   1.1       cgd 		case 'f':
    134  1.12   mycroft 			init_flags |= MNT_FORCE;
    135  1.12   mycroft 			break;
    136  1.12   mycroft 		case 'o':
    137  1.12   mycroft 			if (*optarg)
    138  1.12   mycroft 				options = catopt(options, optarg);
    139   1.1       cgd 			break;
    140   1.1       cgd 		case 'r':
    141  1.12   mycroft 			init_flags |= MNT_RDONLY;
    142  1.12   mycroft 			break;
    143  1.12   mycroft 		case 't':
    144  1.15   mycroft 			if (typelist != NULL)
    145  1.12   mycroft 				errx(1, "only one -t option may be specified.");
    146  1.15   mycroft 			maketypelist(optarg);
    147  1.12   mycroft 			vfstype = optarg;
    148   1.1       cgd 			break;
    149   1.1       cgd 		case 'u':
    150  1.12   mycroft 			init_flags |= MNT_UPDATE;
    151   1.1       cgd 			break;
    152   1.1       cgd 		case 'v':
    153   1.1       cgd 			verbose = 1;
    154   1.1       cgd 			break;
    155   1.1       cgd 		case 'w':
    156  1.12   mycroft 			init_flags &= ~MNT_RDONLY;
    157   1.1       cgd 			break;
    158   1.1       cgd 		case '?':
    159   1.1       cgd 		default:
    160   1.1       cgd 			usage();
    161   1.1       cgd 			/* NOTREACHED */
    162   1.1       cgd 		}
    163   1.1       cgd 	argc -= optind;
    164   1.1       cgd 	argv += optind;
    165   1.1       cgd 
    166  1.12   mycroft #define	BADTYPE(type)							\
    167  1.12   mycroft 	(strcmp(type, FSTAB_RO) &&					\
    168  1.12   mycroft 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    169  1.12   mycroft 
    170  1.12   mycroft 	rval = 0;
    171  1.12   mycroft 	switch (argc) {
    172  1.12   mycroft 	case 0:
    173  1.12   mycroft 		if (all)
    174  1.12   mycroft 			while ((fs = getfsent()) != NULL) {
    175  1.12   mycroft 				if (BADTYPE(fs->fs_type))
    176  1.12   mycroft 					continue;
    177  1.15   mycroft 				if (!selected(fs->fs_vfstype))
    178  1.12   mycroft 					continue;
    179  1.12   mycroft 				if (hasopt(fs->fs_mntops, "noauto"))
    180  1.12   mycroft 					continue;
    181  1.12   mycroft 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    182  1.12   mycroft 				    fs->fs_file, init_flags, options,
    183  1.22       cgd 				    fs->fs_mntops, !forceall))
    184  1.12   mycroft 					rval = 1;
    185  1.12   mycroft 			}
    186  1.12   mycroft 		else {
    187  1.12   mycroft 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    188  1.12   mycroft 				err(1, "getmntinfo");
    189  1.12   mycroft 			for (i = 0; i < mntsize; i++) {
    190  1.15   mycroft 				if (!selected(mntbuf[i].f_fstypename))
    191  1.12   mycroft 					continue;
    192  1.13   mycroft 				prmount(&mntbuf[i]);
    193  1.12   mycroft 			}
    194   1.1       cgd 		}
    195   1.1       cgd 		exit(rval);
    196  1.12   mycroft 	case 1:
    197  1.15   mycroft 		if (typelist != NULL)
    198   1.1       cgd 			usage();
    199   1.1       cgd 
    200  1.12   mycroft 		if (init_flags & MNT_UPDATE) {
    201  1.12   mycroft 			if ((mntbuf = getmntpt(*argv)) == NULL)
    202  1.12   mycroft 				errx(1,
    203  1.12   mycroft 				    "unknown special file or file system %s.",
    204  1.12   mycroft 				    *argv);
    205  1.12   mycroft 			if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
    206  1.12   mycroft 				errx(1, "can't find fstab entry for %s.",
    207  1.12   mycroft 				    *argv);
    208  1.12   mycroft 			/* If it's an update, ignore the fstab file options. */
    209  1.12   mycroft 			fs->fs_mntops = NULL;
    210  1.12   mycroft 			mntonname = mntbuf->f_mntonname;
    211  1.12   mycroft 		} else {
    212  1.12   mycroft 			if ((fs = getfsfile(*argv)) == NULL &&
    213  1.12   mycroft 			    (fs = getfsspec(*argv)) == NULL)
    214  1.12   mycroft 				errx(1,
    215  1.12   mycroft 				    "%s: unknown special file or file system.",
    216  1.12   mycroft 				    *argv);
    217  1.12   mycroft 			if (BADTYPE(fs->fs_type))
    218  1.12   mycroft 				errx(1, "%s has unknown file system type.",
    219  1.12   mycroft 				    *argv);
    220  1.12   mycroft 			mntonname = fs->fs_file;
    221  1.12   mycroft 		}
    222  1.12   mycroft 		rval = mountfs(fs->fs_vfstype, fs->fs_spec,
    223  1.22       cgd 		    mntonname, init_flags, options, fs->fs_mntops, 0);
    224  1.12   mycroft 		break;
    225  1.12   mycroft 	case 2:
    226   1.1       cgd 		/*
    227  1.12   mycroft 		 * If -t flag has not been specified, and spec contains either
    228  1.12   mycroft 		 * a ':' or a '@' then assume that an NFS filesystem is being
    229  1.12   mycroft 		 * specified ala Sun.
    230   1.1       cgd 		 */
    231  1.15   mycroft 		if (typelist == NULL && strpbrk(argv[0], ":@") != NULL)
    232  1.12   mycroft 			vfstype = "nfs";
    233  1.12   mycroft 		rval = mountfs(vfstype,
    234  1.22       cgd 		    argv[0], argv[1], init_flags, options, NULL, 0);
    235  1.12   mycroft 		break;
    236  1.12   mycroft 	default:
    237  1.12   mycroft 		usage();
    238  1.12   mycroft 		/* NOTREACHED */
    239   1.1       cgd 	}
    240   1.1       cgd 
    241  1.12   mycroft 	/*
    242  1.12   mycroft 	 * If the mount was successfully, and done by root, tell mountd the
    243  1.12   mycroft 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    244  1.12   mycroft 	 */
    245  1.12   mycroft 	if (rval == 0 && getuid() == 0 &&
    246  1.12   mycroft 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    247  1.29  christos 		int pid;
    248  1.29  christos 
    249  1.29  christos 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    250  1.12   mycroft 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    251  1.12   mycroft 			err(1, "signal mountd");
    252  1.12   mycroft 		(void)fclose(mountdfp);
    253   1.1       cgd 	}
    254   1.1       cgd 
    255  1.12   mycroft 	exit(rval);
    256   1.1       cgd }
    257   1.1       cgd 
    258  1.12   mycroft int
    259  1.12   mycroft hasopt(mntopts, option)
    260  1.12   mycroft 	const char *mntopts, *option;
    261   1.1       cgd {
    262  1.12   mycroft 	int negative, found;
    263  1.12   mycroft 	char *opt, *optbuf;
    264   1.1       cgd 
    265  1.12   mycroft 	if (option[0] == 'n' && option[1] == 'o') {
    266  1.12   mycroft 		negative = 1;
    267  1.12   mycroft 		option += 2;
    268  1.12   mycroft 	} else
    269  1.12   mycroft 		negative = 0;
    270  1.12   mycroft 	optbuf = strdup(mntopts);
    271  1.12   mycroft 	found = 0;
    272  1.12   mycroft 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    273  1.12   mycroft 		if (opt[0] == 'n' && opt[1] == 'o') {
    274  1.12   mycroft 			if (!strcasecmp(opt + 2, option))
    275  1.12   mycroft 				found = negative;
    276  1.12   mycroft 		} else if (!strcasecmp(opt, option))
    277  1.12   mycroft 			found = !negative;
    278  1.12   mycroft 	}
    279  1.12   mycroft 	free(optbuf);
    280  1.12   mycroft 	return (found);
    281   1.1       cgd }
    282   1.1       cgd 
    283  1.12   mycroft int
    284  1.22       cgd mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
    285  1.12   mycroft 	const char *vfstype, *spec, *name, *options, *mntopts;
    286  1.22       cgd 	int flags, skipmounted;
    287   1.1       cgd {
    288  1.12   mycroft 	/* List of directories containing mount_xxx subcommands. */
    289  1.12   mycroft 	static const char *edirs[] = {
    290  1.12   mycroft 		_PATH_SBIN,
    291  1.12   mycroft 		_PATH_USRSBIN,
    292  1.12   mycroft 		NULL
    293  1.12   mycroft 	};
    294  1.12   mycroft 	const char *argv[100], **edir;
    295  1.12   mycroft 	struct statfs sf;
    296  1.12   mycroft 	pid_t pid;
    297  1.12   mycroft 	int argc, i, status;
    298  1.12   mycroft 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
    299  1.29  christos #ifdef __GNUC__
    300  1.29  christos 	(void) &name;
    301  1.29  christos 	(void) &optbuf;
    302  1.29  christos #endif
    303   1.1       cgd 
    304  1.12   mycroft 	if (realpath(name, mntpath) == NULL) {
    305  1.20   ghudson 		warn("realpath %s", name);
    306  1.12   mycroft 		return (1);
    307  1.12   mycroft 	}
    308   1.1       cgd 
    309  1.12   mycroft 	name = mntpath;
    310   1.1       cgd 
    311  1.12   mycroft 	if (mntopts == NULL)
    312  1.12   mycroft 		mntopts = "";
    313  1.12   mycroft 	if (options == NULL) {
    314  1.12   mycroft 		if (*mntopts == '\0')
    315  1.12   mycroft 			options = "rw";
    316  1.12   mycroft 		else {
    317  1.12   mycroft 			options = mntopts;
    318  1.12   mycroft 			mntopts = "";
    319  1.12   mycroft 		}
    320  1.12   mycroft 	}
    321  1.12   mycroft 	optbuf = catopt(strdup(mntopts), options);
    322  1.12   mycroft 
    323  1.12   mycroft 	if (strcmp(name, "/") == 0)
    324  1.12   mycroft 		flags |= MNT_UPDATE;
    325  1.22       cgd 	else if (skipmounted) {
    326  1.21       cgd 		if (statfs(name, &sf) < 0) {
    327  1.21       cgd 			warn("statfs %s", name);
    328  1.21       cgd 			return (1);
    329  1.21       cgd 		}
    330  1.21       cgd 		/* XXX can't check f_mntfromname, thanks to mfs, union, etc. */
    331  1.21       cgd 		if (strncmp(name, sf.f_mntonname, MNAMELEN) == 0 &&
    332  1.21       cgd 		    strncmp(vfstype, sf.f_fstypename, MFSNAMELEN) == 0) {
    333  1.21       cgd 			if (verbose)
    334  1.21       cgd 				(void)printf("%s on %s type %.*s: %s\n",
    335  1.21       cgd 				    sf.f_mntfromname, sf.f_mntonname,
    336  1.21       cgd 			            MFSNAMELEN, sf.f_fstypename,
    337  1.21       cgd 				    "already mounted");
    338  1.21       cgd 			return (0);
    339  1.21       cgd 		}
    340  1.21       cgd 	}
    341  1.12   mycroft 	if (flags & MNT_FORCE)
    342  1.12   mycroft 		optbuf = catopt(optbuf, "force");
    343  1.12   mycroft 	if (flags & MNT_RDONLY)
    344  1.12   mycroft 		optbuf = catopt(optbuf, "ro");
    345  1.12   mycroft 	/*
    346  1.12   mycroft 	 * XXX
    347  1.12   mycroft 	 * The mount_mfs (newfs) command uses -o to select the
    348  1.12   mycroft 	 * optimisation mode.  We don't pass the default "-o rw"
    349  1.12   mycroft 	 * for that reason.
    350  1.12   mycroft 	 */
    351  1.12   mycroft 	if (flags & MNT_UPDATE)
    352  1.12   mycroft 		optbuf = catopt(optbuf, "update");
    353  1.12   mycroft 
    354  1.12   mycroft 	argc = 0;
    355  1.12   mycroft 	argv[argc++] = vfstype;
    356  1.12   mycroft 	mangle(optbuf, &argc, argv);
    357  1.12   mycroft 	argv[argc++] = spec;
    358  1.12   mycroft 	argv[argc++] = name;
    359  1.12   mycroft 	argv[argc] = NULL;
    360  1.12   mycroft 
    361  1.12   mycroft 	if (debug) {
    362  1.12   mycroft 		(void)printf("exec: mount_%s", vfstype);
    363  1.12   mycroft 		for (i = 1; i < argc; i++)
    364  1.12   mycroft 			(void)printf(" %s", argv[i]);
    365  1.12   mycroft 		(void)printf("\n");
    366  1.12   mycroft 		return (0);
    367  1.12   mycroft 	}
    368   1.5   mycroft 
    369  1.12   mycroft 	switch (pid = vfork()) {
    370  1.12   mycroft 	case -1:				/* Error. */
    371  1.12   mycroft 		warn("vfork");
    372  1.12   mycroft 		free(optbuf);
    373  1.12   mycroft 		return (1);
    374  1.12   mycroft 	case 0:					/* Child. */
    375  1.12   mycroft 		/* Go find an executable. */
    376  1.12   mycroft 		edir = edirs;
    377  1.12   mycroft 		do {
    378  1.12   mycroft 			(void)snprintf(execname,
    379  1.12   mycroft 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
    380  1.12   mycroft 			execv(execname, (char * const *)argv);
    381  1.12   mycroft 			if (errno != ENOENT)
    382  1.12   mycroft 				warn("exec %s for %s", execname, name);
    383  1.12   mycroft 		} while (*++edir != NULL);
    384   1.1       cgd 
    385  1.12   mycroft 		if (errno == ENOENT)
    386  1.28        pk 			warnx("mount_%s not found for %s", vfstype, name);
    387  1.12   mycroft 		exit(1);
    388  1.12   mycroft 		/* NOTREACHED */
    389  1.12   mycroft 	default:				/* Parent. */
    390  1.12   mycroft 		free(optbuf);
    391   1.1       cgd 
    392  1.12   mycroft 		if (waitpid(pid, &status, 0) < 0) {
    393  1.12   mycroft 			warn("waitpid");
    394  1.12   mycroft 			return (1);
    395   1.1       cgd 		}
    396  1.12   mycroft 
    397  1.12   mycroft 		if (WIFEXITED(status)) {
    398  1.12   mycroft 			if (WEXITSTATUS(status) != 0)
    399  1.12   mycroft 				return (WEXITSTATUS(status));
    400  1.12   mycroft 		} else if (WIFSIGNALED(status)) {
    401  1.17       jtc 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    402  1.12   mycroft 			return (1);
    403   1.1       cgd 		}
    404  1.12   mycroft 
    405  1.12   mycroft 		if (verbose) {
    406  1.12   mycroft 			if (statfs(name, &sf) < 0) {
    407  1.12   mycroft 				warn("statfs %s", name);
    408  1.12   mycroft 				return (1);
    409  1.12   mycroft 			}
    410  1.13   mycroft 			prmount(&sf);
    411   1.1       cgd 		}
    412  1.12   mycroft 		break;
    413   1.1       cgd 	}
    414  1.12   mycroft 
    415  1.12   mycroft 	return (0);
    416   1.1       cgd }
    417   1.1       cgd 
    418  1.12   mycroft void
    419  1.13   mycroft prmount(sf)
    420  1.13   mycroft 	struct statfs *sf;
    421  1.13   mycroft {
    422  1.12   mycroft 	int flags;
    423  1.12   mycroft 	struct opt *o;
    424  1.12   mycroft 	int f;
    425   1.1       cgd 
    426  1.19       cgd 	(void)printf("%s on %s type %.*s", sf->f_mntfromname, sf->f_mntonname,
    427  1.18       cgd 	    MFSNAMELEN, sf->f_fstypename);
    428   1.1       cgd 
    429  1.13   mycroft 	flags = sf->f_flags & MNT_VISFLAGMASK;
    430  1.12   mycroft 	for (f = 0, o = optnames; flags && o->o_opt; o++)
    431  1.12   mycroft 		if (flags & o->o_opt) {
    432  1.24       cgd 			if (!o->o_silent)
    433  1.24       cgd 				(void)printf("%s%s", !f++ ? " (" : ", ",
    434  1.24       cgd 				    o->o_name);
    435  1.12   mycroft 			flags &= ~o->o_opt;
    436  1.12   mycroft 		}
    437  1.24       cgd 	if (flags)
    438  1.27        pk 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    439  1.24       cgd 		    flags & (flags - 1) ? "s" : "", flags);
    440  1.12   mycroft 	(void)printf(f ? ")\n" : "\n");
    441   1.1       cgd }
    442   1.1       cgd 
    443   1.1       cgd struct statfs *
    444   1.1       cgd getmntpt(name)
    445  1.12   mycroft 	const char *name;
    446   1.1       cgd {
    447   1.1       cgd 	struct statfs *mntbuf;
    448  1.12   mycroft 	int i, mntsize;
    449   1.1       cgd 
    450   1.1       cgd 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    451  1.12   mycroft 	for (i = 0; i < mntsize; i++)
    452  1.12   mycroft 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    453  1.12   mycroft 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    454   1.1       cgd 			return (&mntbuf[i]);
    455  1.12   mycroft 	return (NULL);
    456   1.1       cgd }
    457   1.1       cgd 
    458  1.15   mycroft static enum { IN_LIST, NOT_IN_LIST } which;
    459  1.15   mycroft 
    460  1.12   mycroft int
    461  1.15   mycroft selected(type)
    462  1.15   mycroft 	const char *type;
    463   1.1       cgd {
    464  1.15   mycroft 	char **av;
    465   1.1       cgd 
    466  1.15   mycroft 	/* If no type specified, it's always selected. */
    467  1.15   mycroft 	if (typelist == NULL)
    468  1.15   mycroft 		return (1);
    469  1.15   mycroft 	for (av = typelist; *av != NULL; ++av)
    470  1.18       cgd 		if (!strncmp(type, *av, MFSNAMELEN))
    471  1.15   mycroft 			return (which == IN_LIST ? 1 : 0);
    472  1.15   mycroft 	return (which == IN_LIST ? 0 : 1);
    473   1.1       cgd }
    474   1.1       cgd 
    475  1.15   mycroft void
    476  1.15   mycroft maketypelist(fslist)
    477   1.1       cgd 	char *fslist;
    478   1.1       cgd {
    479  1.12   mycroft 	int i;
    480  1.15   mycroft 	char *nextcp, **av;
    481  1.15   mycroft 
    482  1.15   mycroft 	if ((fslist == NULL) || (fslist[0] == '\0'))
    483  1.15   mycroft 		errx(1, "empty type list");
    484   1.1       cgd 
    485  1.15   mycroft 	/*
    486  1.15   mycroft 	 * XXX
    487  1.15   mycroft 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    488  1.15   mycroft 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    489  1.15   mycroft 	 */
    490   1.1       cgd 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    491   1.1       cgd 		fslist += 2;
    492  1.15   mycroft 		which = NOT_IN_LIST;
    493  1.15   mycroft 	} else
    494  1.15   mycroft 		which = IN_LIST;
    495  1.15   mycroft 
    496  1.15   mycroft 	/* Count the number of types. */
    497  1.29  christos 	for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')) != NULL; i++)
    498  1.15   mycroft 		++nextcp;
    499  1.15   mycroft 
    500  1.15   mycroft 	/* Build an array of that many types. */
    501  1.15   mycroft 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    502  1.29  christos 		err(1, "%s", "");
    503  1.15   mycroft 	av[0] = fslist;
    504  1.29  christos 	for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')) != NULL; i++) {
    505  1.15   mycroft 		*nextcp = '\0';
    506  1.15   mycroft 		av[i] = ++nextcp;
    507   1.1       cgd 	}
    508  1.15   mycroft 	/* Terminate the array. */
    509  1.15   mycroft 	av[i] = NULL;
    510   1.1       cgd }
    511   1.1       cgd 
    512  1.12   mycroft char *
    513  1.12   mycroft catopt(s0, s1)
    514  1.12   mycroft 	char *s0;
    515  1.12   mycroft 	const char *s1;
    516  1.12   mycroft {
    517  1.12   mycroft 	size_t i;
    518  1.12   mycroft 	char *cp;
    519  1.12   mycroft 
    520  1.12   mycroft 	if (s0 && *s0) {
    521  1.12   mycroft 		i = strlen(s0) + strlen(s1) + 1 + 1;
    522  1.12   mycroft 		if ((cp = malloc(i)) == NULL)
    523  1.29  christos 			err(1, "%s", "");
    524  1.12   mycroft 		(void)snprintf(cp, i, "%s,%s", s0, s1);
    525  1.12   mycroft 	} else
    526  1.12   mycroft 		cp = strdup(s1);
    527   1.1       cgd 
    528  1.12   mycroft 	if (s0)
    529  1.12   mycroft 		free(s0);
    530  1.12   mycroft 	return (cp);
    531   1.1       cgd }
    532   1.1       cgd 
    533  1.12   mycroft void
    534  1.12   mycroft mangle(options, argcp, argv)
    535  1.12   mycroft 	char *options;
    536  1.12   mycroft 	int *argcp;
    537  1.12   mycroft 	const char **argv;
    538   1.1       cgd {
    539  1.12   mycroft 	char *p, *s;
    540  1.12   mycroft 	int argc;
    541   1.1       cgd 
    542  1.12   mycroft 	argc = *argcp;
    543  1.12   mycroft 	for (s = options; (p = strsep(&s, ",")) != NULL;)
    544  1.12   mycroft 		if (*p != '\0')
    545  1.12   mycroft 			if (*p == '-') {
    546  1.12   mycroft 				argv[argc++] = p;
    547  1.12   mycroft 				p = strchr(p, '=');
    548  1.12   mycroft 				if (p) {
    549  1.12   mycroft 					*p = '\0';
    550  1.12   mycroft 					argv[argc++] = p+1;
    551   1.1       cgd 				}
    552  1.12   mycroft 			} else if (strcmp(p, "rw") != 0) {
    553  1.12   mycroft 				argv[argc++] = "-o";
    554  1.12   mycroft 				argv[argc++] = p;
    555   1.1       cgd 			}
    556  1.12   mycroft 
    557  1.12   mycroft 	*argcp = argc;
    558   1.1       cgd }
    559   1.1       cgd 
    560  1.12   mycroft void
    561  1.12   mycroft usage()
    562   1.1       cgd {
    563   1.1       cgd 
    564  1.12   mycroft 	(void)fprintf(stderr,
    565  1.12   mycroft 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
    566  1.23       jtc 		"[-dfruvw] [-o options] [-t ffs | external_type]",
    567  1.12   mycroft 			"special node",
    568  1.23       jtc 		"[-adfruvw] [-t ffs | external_type]",
    569  1.12   mycroft 		"[-dfruvw] special | node");
    570  1.12   mycroft 	exit(1);
    571   1.1       cgd }
    572