xref: /freebsd/sbin/mount/mount.c (revision 3047fefe49f57a673de8df152c199de12ec2c6d3)
1 /*-
2  * Copyright (c) 1980, 1989, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52 
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <fstab.h>
57 #include <pwd.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "extern.h"
65 #include "mntopts.h"
66 #include "pathnames.h"
67 
68 /* `meta' options */
69 #define MOUNT_META_OPTION_FSTAB		"fstab"
70 #define MOUNT_META_OPTION_CURRENT	"current"
71 
72 int debug, fstab_style, verbose;
73 
74 char   *catopt(char *, const char *);
75 struct statfs *getmntpt(const char *);
76 int	hasopt(const char *, const char *);
77 int	ismounted(struct fstab *, struct statfs *, int);
78 int	isremountable(const char *);
79 void	mangle(char *, int *, const char **);
80 char   *update_options(char *, char *, int);
81 int	mountfs(const char *, const char *, const char *,
82 			int, const char *, const char *);
83 void	remopt(char *, const char *);
84 void	prmount(struct statfs *);
85 void	putfsent(const struct statfs *);
86 void	usage(void);
87 char   *flags2opts(int);
88 
89 /* Map from mount options to printable formats. */
90 static struct opt {
91 	int o_opt;
92 	const char *o_name;
93 } optnames[] = {
94 	{ MNT_ASYNC,		"asynchronous" },
95 	{ MNT_EXPORTED,		"NFS exported" },
96 	{ MNT_LOCAL,		"local" },
97 	{ MNT_NOATIME,		"noatime" },
98 	{ MNT_NODEV,		"nodev" },
99 	{ MNT_NOEXEC,		"noexec" },
100 	{ MNT_NOSUID,		"nosuid" },
101 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
102 	{ MNT_QUOTA,		"with quotas" },
103 	{ MNT_RDONLY,		"read-only" },
104 	{ MNT_SYNCHRONOUS,	"synchronous" },
105 	{ MNT_UNION,		"union" },
106 	{ MNT_NOCLUSTERR,	"noclusterr" },
107 	{ MNT_NOCLUSTERW,	"noclusterw" },
108 	{ MNT_SUIDDIR,		"suiddir" },
109 	{ MNT_SOFTDEP,		"soft-updates" },
110 	{ MNT_MULTILABEL,	"multilabel" },
111 	{ MNT_ACLS,		"acls" },
112 	{ 0, NULL }
113 };
114 
115 /*
116  * List of VFS types that can be remounted without becoming mounted on top
117  * of each other.
118  * XXX Is this list correct?
119  */
120 static const char *
121 remountable_fs_names[] = {
122 	"ufs", "ffs", "ext2fs",
123 	0
124 };
125 
126 int
127 main(argc, argv)
128 	int argc;
129 	char * const argv[];
130 {
131 	const char *mntfromname, **vfslist, *vfstype;
132 	struct fstab *fs;
133 	struct statfs *mntbuf;
134 	FILE *mountdfp;
135 	pid_t pid;
136 	int all, ch, i, init_flags, mntsize, rval, have_fstab;
137 	char *cp, *ep, *options;
138 
139 	all = init_flags = 0;
140 	options = NULL;
141 	vfslist = NULL;
142 	vfstype = "ufs";
143 	while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
144 		switch (ch) {
145 		case 'a':
146 			all = 1;
147 			break;
148 		case 'd':
149 			debug = 1;
150 			break;
151 		case 'f':
152 			init_flags |= MNT_FORCE;
153 			break;
154 		case 'o':
155 			if (*optarg)
156 				options = catopt(options, optarg);
157 			break;
158 		case 'p':
159 			fstab_style = 1;
160 			verbose = 1;
161 			break;
162 		case 'r':
163 			options = catopt(options, "ro");
164 			break;
165 		case 't':
166 			if (vfslist != NULL)
167 				errx(1, "only one -t option may be specified");
168 			vfslist = makevfslist(optarg);
169 			vfstype = optarg;
170 			break;
171 		case 'u':
172 			init_flags |= MNT_UPDATE;
173 			break;
174 		case 'v':
175 			verbose = 1;
176 			break;
177 		case 'w':
178 			options = catopt(options, "noro");
179 			break;
180 		case '?':
181 		default:
182 			usage();
183 			/* NOTREACHED */
184 		}
185 	argc -= optind;
186 	argv += optind;
187 
188 #define	BADTYPE(type)							\
189 	(strcmp(type, FSTAB_RO) &&					\
190 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
191 
192 	rval = 0;
193 	switch (argc) {
194 	case 0:
195 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
196 			err(1, "getmntinfo");
197 		if (all) {
198 			while ((fs = getfsent()) != NULL) {
199 				if (BADTYPE(fs->fs_type))
200 					continue;
201 				if (checkvfsname(fs->fs_vfstype, vfslist))
202 					continue;
203 				if (hasopt(fs->fs_mntops, "noauto"))
204 					continue;
205 				if (!(init_flags & MNT_UPDATE) &&
206 				    ismounted(fs, mntbuf, mntsize))
207 					continue;
208 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
209 				    fs->fs_file, init_flags, options,
210 				    fs->fs_mntops))
211 					rval = 1;
212 			}
213 		} else if (fstab_style) {
214 			for (i = 0; i < mntsize; i++) {
215 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
216 					continue;
217 				putfsent(&mntbuf[i]);
218 			}
219 		} else {
220 			for (i = 0; i < mntsize; i++) {
221 				if (checkvfsname(mntbuf[i].f_fstypename,
222 				    vfslist))
223 					continue;
224 				prmount(&mntbuf[i]);
225 			}
226 		}
227 		exit(rval);
228 	case 1:
229 		if (vfslist != NULL)
230 			usage();
231 
232 		if (init_flags & MNT_UPDATE) {
233 			mntfromname = NULL;
234 			have_fstab = 0;
235 			if ((mntbuf = getmntpt(*argv)) == NULL)
236 				errx(1, "not currently mounted %s", *argv);
237 			/*
238 			 * Only get the mntflags from fstab if both mntpoint
239 			 * and mntspec are identical. Also handle the special
240 			 * case where just '/' is mounted and 'spec' is not
241 			 * identical with the one from fstab ('/dev' is missing
242 			 * in the spec-string at boot-time).
243 			 */
244 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
245 				if (strcmp(fs->fs_spec,
246 				    mntbuf->f_mntfromname) == 0 &&
247 				    strcmp(fs->fs_file,
248 				    mntbuf->f_mntonname) == 0) {
249 					have_fstab = 1;
250 					mntfromname = mntbuf->f_mntfromname;
251 				} else if (argv[0][0] == '/' &&
252 				    argv[0][1] == '\0') {
253 					fs = getfsfile("/");
254 					have_fstab = 1;
255 					mntfromname = fs->fs_spec;
256 				}
257 			}
258 			if (have_fstab) {
259 				options = update_options(options, fs->fs_mntops,
260 				    mntbuf->f_flags);
261 			} else {
262 				mntfromname = mntbuf->f_mntfromname;
263 				options = update_options(options, NULL,
264 				    mntbuf->f_flags);
265 			}
266 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
267 			    mntbuf->f_mntonname, init_flags, options, 0);
268 			break;
269 		}
270 		rmslashes(*argv, *argv);
271 		if ((fs = getfsfile(*argv)) == NULL &&
272 		    (fs = getfsspec(*argv)) == NULL)
273 			errx(1, "%s: unknown special file or file system",
274 			    *argv);
275 		if (BADTYPE(fs->fs_type))
276 			errx(1, "%s has unknown file system type",
277 			    *argv);
278 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
279 		    init_flags, options, fs->fs_mntops);
280 		break;
281 	case 2:
282 		/*
283 		 * If -t flag has not been specified, the path cannot be
284 		 * found, spec contains either a ':' or a '@', then assume
285 		 * that an NFS file system is being specified ala Sun.
286 		 * Check if the hostname contains only allowed characters
287 		 * to reduce false positives.  IPv6 addresses containing
288 		 * ':' will be correctly parsed only if the separator is '@'.
289 		 * The definition of a valid hostname is taken from RFC 1034.
290 		 */
291 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL) ||
292 		    ((ep = strchr(argv[0], ':')) != NULL)) {
293 			if (*ep == '@') {
294 				cp = ep + 1;
295 				ep = cp + strlen(cp);
296 			} else
297 				cp = argv[0];
298 			while (cp != ep) {
299 				if (!isdigit(*cp) && !isalpha(*cp) &&
300 				    *cp != '.' && *cp != '-' && *cp != ':')
301 					break;
302 				cp++;
303 			}
304 			if (cp == ep)
305 				vfstype = "nfs";
306 		}
307 		rval = mountfs(vfstype,
308 		    argv[0], argv[1], init_flags, options, NULL);
309 		break;
310 	default:
311 		usage();
312 		/* NOTREACHED */
313 	}
314 
315 	/*
316 	 * If the mount was successfully, and done by root, tell mountd the
317 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
318 	 */
319 	if (rval == 0 && getuid() == 0 &&
320 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
321 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
322 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
323 			err(1, "signal mountd");
324 		(void)fclose(mountdfp);
325 	}
326 
327 	exit(rval);
328 }
329 
330 int
331 ismounted(fs, mntbuf, mntsize)
332 	struct fstab *fs;
333 	struct statfs *mntbuf;
334 	int mntsize;
335 {
336 	int i;
337 
338 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
339 		/* the root file system can always be remounted */
340 		return (0);
341 
342 	for (i = mntsize - 1; i >= 0; --i)
343 		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
344 		    (!isremountable(fs->fs_vfstype) ||
345 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
346 			return (1);
347 	return (0);
348 }
349 
350 int
351 isremountable(vfsname)
352 	const char *vfsname;
353 {
354 	const char **cp;
355 
356 	for (cp = remountable_fs_names; *cp; cp++)
357 		if (strcmp(*cp, vfsname) == 0)
358 			return (1);
359 	return (0);
360 }
361 
362 int
363 hasopt(mntopts, option)
364 	const char *mntopts, *option;
365 {
366 	int negative, found;
367 	char *opt, *optbuf;
368 
369 	if (option[0] == 'n' && option[1] == 'o') {
370 		negative = 1;
371 		option += 2;
372 	} else
373 		negative = 0;
374 	optbuf = strdup(mntopts);
375 	found = 0;
376 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
377 		if (opt[0] == 'n' && opt[1] == 'o') {
378 			if (!strcasecmp(opt + 2, option))
379 				found = negative;
380 		} else if (!strcasecmp(opt, option))
381 			found = !negative;
382 	}
383 	free(optbuf);
384 	return (found);
385 }
386 
387 int
388 mountfs(vfstype, spec, name, flags, options, mntopts)
389 	const char *vfstype, *spec, *name, *options, *mntopts;
390 	int flags;
391 {
392 	/* List of directories containing mount_xxx subcommands. */
393 	static const char *edirs[] = {
394 		_PATH_SBIN,
395 		_PATH_USRSBIN,
396 		NULL
397 	};
398 	const char *argv[100], **edir;
399 	struct statfs sf;
400 	pid_t pid;
401 	int argc, i, status;
402 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
403 
404 #if __GNUC__
405 	(void)&optbuf;
406 	(void)&name;
407 #endif
408 
409 	/* resolve the mountpoint with realpath(3) */
410 	(void)checkpath(name, mntpath);
411 	name = mntpath;
412 
413 	if (mntopts == NULL)
414 		mntopts = "";
415 	if (options == NULL) {
416 		if (*mntopts == '\0') {
417 			options = "rw";
418 		} else {
419 			options = mntopts;
420 			mntopts = "";
421 		}
422 	}
423 	optbuf = catopt(strdup(mntopts), options);
424 
425 	if (strcmp(name, "/") == 0)
426 		flags |= MNT_UPDATE;
427 	if (flags & MNT_FORCE)
428 		optbuf = catopt(optbuf, "force");
429 	if (flags & MNT_RDONLY)
430 		optbuf = catopt(optbuf, "ro");
431 	/*
432 	 * XXX
433 	 * The mount_mfs (newfs) command uses -o to select the
434 	 * optimization mode.  We don't pass the default "-o rw"
435 	 * for that reason.
436 	 */
437 	if (flags & MNT_UPDATE)
438 		optbuf = catopt(optbuf, "update");
439 
440 	/* Compatibility glue. */
441 	if (strcmp(vfstype, "msdos") == 0)
442 		vfstype = "msdosfs";
443 
444 	argc = 0;
445 	argv[argc++] = vfstype;
446 	mangle(optbuf, &argc, argv);
447 	argv[argc++] = spec;
448 	argv[argc++] = name;
449 	argv[argc] = NULL;
450 
451 	if (debug) {
452 		(void)printf("exec: mount_%s", vfstype);
453 		for (i = 1; i < argc; i++)
454 			(void)printf(" %s", argv[i]);
455 		(void)printf("\n");
456 		return (0);
457 	}
458 
459 	switch (pid = fork()) {
460 	case -1:				/* Error. */
461 		warn("fork");
462 		free(optbuf);
463 		return (1);
464 	case 0:					/* Child. */
465 		if (strcmp(vfstype, "ufs") == 0)
466 			exit(mount_ufs(argc, (char * const *) argv));
467 
468 		/* Go find an executable. */
469 		for (edir = edirs; *edir; edir++) {
470 			(void)snprintf(execname,
471 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
472 			execv(execname, (char * const *)argv);
473 		}
474 		if (errno == ENOENT) {
475 			int len = 0;
476 			char *cp;
477 			for (edir = edirs; *edir; edir++)
478 				len += strlen(*edir) + 2;	/* ", " */
479 			if ((cp = malloc(len)) == NULL)
480 				errx(1, "malloc failed");
481 			cp[0] = '\0';
482 			for (edir = edirs; *edir; edir++) {
483 				strcat(cp, *edir);
484 				if (edir[1] != NULL)
485 					strcat(cp, ", ");
486 			}
487 			warn("exec mount_%s not found in %s", vfstype, cp);
488 		}
489 		exit(1);
490 		/* NOTREACHED */
491 	default:				/* Parent. */
492 		free(optbuf);
493 
494 		if (waitpid(pid, &status, 0) < 0) {
495 			warn("waitpid");
496 			return (1);
497 		}
498 
499 		if (WIFEXITED(status)) {
500 			if (WEXITSTATUS(status) != 0)
501 				return (WEXITSTATUS(status));
502 		} else if (WIFSIGNALED(status)) {
503 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
504 			return (1);
505 		}
506 
507 		if (verbose) {
508 			if (statfs(name, &sf) < 0) {
509 				warn("statfs %s", name);
510 				return (1);
511 			}
512 			if (fstab_style)
513 				putfsent(&sf);
514 			else
515 				prmount(&sf);
516 		}
517 		break;
518 	}
519 
520 	return (0);
521 }
522 
523 void
524 prmount(sfp)
525 	struct statfs *sfp;
526 {
527 	int flags;
528 	struct opt *o;
529 	struct passwd *pw;
530 
531 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
532 	    sfp->f_fstypename);
533 
534 	flags = sfp->f_flags & MNT_VISFLAGMASK;
535 	for (o = optnames; flags && o->o_opt; o++)
536 		if (flags & o->o_opt) {
537 			(void)printf(", %s", o->o_name);
538 			flags &= ~o->o_opt;
539 		}
540 	if (sfp->f_owner) {
541 		(void)printf(", mounted by ");
542 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
543 			(void)printf("%s", pw->pw_name);
544 		else
545 			(void)printf("%d", sfp->f_owner);
546 	}
547 	if (verbose) {
548 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
549 			(void)printf(", writes: sync %ld async %ld",
550 			    sfp->f_syncwrites, sfp->f_asyncwrites);
551 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
552 			(void)printf(", reads: sync %ld async %ld",
553 			    sfp->f_syncreads, sfp->f_asyncreads);
554 	}
555 	(void)printf(")\n");
556 }
557 
558 struct statfs *
559 getmntpt(name)
560 	const char *name;
561 {
562 	struct statfs *mntbuf;
563 	int i, mntsize;
564 
565 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
566 	for (i = mntsize - 1; i >= 0; i--) {
567 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
568 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
569 			return (&mntbuf[i]);
570 	}
571 	return (NULL);
572 }
573 
574 char *
575 catopt(s0, s1)
576 	char *s0;
577 	const char *s1;
578 {
579 	size_t i;
580 	char *cp;
581 
582 	if (s1 == NULL || *s1 == '\0')
583 		return s0;
584 
585 	if (s0 && *s0) {
586 		i = strlen(s0) + strlen(s1) + 1 + 1;
587 		if ((cp = malloc(i)) == NULL)
588 			errx(1, "malloc failed");
589 		(void)snprintf(cp, i, "%s,%s", s0, s1);
590 	} else
591 		cp = strdup(s1);
592 
593 	if (s0)
594 		free(s0);
595 	return (cp);
596 }
597 
598 void
599 mangle(options, argcp, argv)
600 	char *options;
601 	int *argcp;
602 	const char **argv;
603 {
604 	char *p, *s;
605 	int argc;
606 
607 	argc = *argcp;
608 	for (s = options; (p = strsep(&s, ",")) != NULL;)
609 		if (*p != '\0') {
610 			if (*p == '-') {
611 				argv[argc++] = p;
612 				p = strchr(p, '=');
613 				if (p) {
614 					*p = '\0';
615 					argv[argc++] = p+1;
616 				}
617 			} else if (strcmp(p, "rw") != 0) {
618 				argv[argc++] = "-o";
619 				argv[argc++] = p;
620 			}
621 		}
622 
623 	*argcp = argc;
624 }
625 
626 
627 char *
628 update_options(opts, fstab, curflags)
629 	char *opts;
630 	char *fstab;
631 	int curflags;
632 {
633 	char *o, *p;
634 	char *cur;
635 	char *expopt, *newopt, *tmpopt;
636 
637 	if (opts == NULL)
638 		return strdup("");
639 
640 	/* remove meta options from list */
641 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
642 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
643 	cur = flags2opts(curflags);
644 
645 	/*
646 	 * Expand all meta-options passed to us first.
647 	 */
648 	expopt = NULL;
649 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
650 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
651 			expopt = catopt(expopt, fstab);
652 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
653 			expopt = catopt(expopt, cur);
654 		else
655 			expopt = catopt(expopt, o);
656 	}
657 	free(cur);
658 	free(opts);
659 
660 	/*
661 	 * Remove previous contradictory arguments. Given option "foo" we
662 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
663 	 * and "foo" - so we can deal with possible options like "notice".
664 	 */
665 	newopt = NULL;
666 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
667 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
668 			errx(1, "malloc failed");
669 
670 		strcpy(tmpopt, "no");
671 		strcat(tmpopt, o);
672 		remopt(newopt, tmpopt);
673 		free(tmpopt);
674 
675 		if (strncmp("no", o, 2) == 0)
676 			remopt(newopt, o+2);
677 
678 		newopt = catopt(newopt, o);
679 	}
680 	free(expopt);
681 
682 	return newopt;
683 }
684 
685 void
686 remopt(string, opt)
687 	char *string;
688  	const char *opt;
689 {
690 	char *o, *p, *r;
691 
692 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
693 		return;
694 
695 	r = string;
696 
697 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
698 		if (strcmp(opt, o) != 0) {
699 			if (*r == ',' && *o != '\0')
700 				r++;
701 			while ((*r++ = *o++) != '\0')
702 			    ;
703 			*--r = ',';
704 		}
705 	}
706 	*r = '\0';
707 }
708 
709 void
710 usage()
711 {
712 
713 	(void)fprintf(stderr, "%s\n%s\n%s\n",
714 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
715 "       mount [-adfpruvw] [-t ufs | external_type]",
716 "       mount [-dfpruvw] special | node");
717 	exit(1);
718 }
719 
720 void
721 putfsent(ent)
722 	const struct statfs *ent;
723 {
724 	struct fstab *fst;
725 	char *opts;
726 
727 	opts = flags2opts(ent->f_flags);
728 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
729 	    ent->f_fstypename, opts);
730 	free(opts);
731 
732 	if ((fst = getfsspec(ent->f_mntfromname)))
733 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
734 	else if ((fst = getfsfile(ent->f_mntonname)))
735 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
736 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
737 		if (strcmp(ent->f_mntonname, "/") == 0)
738 			printf("\t1 1\n");
739 		else
740 			printf("\t2 2\n");
741 	} else
742 		printf("\t0 0\n");
743 }
744 
745 
746 char *
747 flags2opts(flags)
748 	int flags;
749 {
750 	char *res;
751 
752 	res = NULL;
753 
754 	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
755 
756 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
757 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
758 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
759 	if (flags & MNT_NODEV)		res = catopt(res, "nodev");
760 	if (flags & MNT_UNION)		res = catopt(res, "union");
761 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
762 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
763 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
764 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
765 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
766 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
767 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
768 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
769 
770 	return res;
771 }
772