xref: /freebsd/sbin/mount/mount.c (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
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, "adF:fo: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 			setfstab(optarg);
153 			break;
154 		case 'f':
155 			init_flags |= MNT_FORCE;
156 			break;
157 		case 'o':
158 			if (*optarg)
159 				options = catopt(options, optarg);
160 			break;
161 		case 'p':
162 			fstab_style = 1;
163 			verbose = 1;
164 			break;
165 		case 'r':
166 			options = catopt(options, "ro");
167 			break;
168 		case 't':
169 			if (vfslist != NULL)
170 				errx(1, "only one -t option may be specified");
171 			vfslist = makevfslist(optarg);
172 			vfstype = optarg;
173 			break;
174 		case 'u':
175 			init_flags |= MNT_UPDATE;
176 			break;
177 		case 'v':
178 			verbose = 1;
179 			break;
180 		case 'w':
181 			options = catopt(options, "noro");
182 			break;
183 		case '?':
184 		default:
185 			usage();
186 			/* NOTREACHED */
187 		}
188 	argc -= optind;
189 	argv += optind;
190 
191 #define	BADTYPE(type)							\
192 	(strcmp(type, FSTAB_RO) &&					\
193 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
194 
195 	rval = 0;
196 	switch (argc) {
197 	case 0:
198 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
199 			err(1, "getmntinfo");
200 		if (all) {
201 			while ((fs = getfsent()) != NULL) {
202 				if (BADTYPE(fs->fs_type))
203 					continue;
204 				if (checkvfsname(fs->fs_vfstype, vfslist))
205 					continue;
206 				if (hasopt(fs->fs_mntops, "noauto"))
207 					continue;
208 				if (!(init_flags & MNT_UPDATE) &&
209 				    ismounted(fs, mntbuf, mntsize))
210 					continue;
211 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
212 				    fs->fs_file, init_flags, options,
213 				    fs->fs_mntops))
214 					rval = 1;
215 			}
216 		} else if (fstab_style) {
217 			for (i = 0; i < mntsize; i++) {
218 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
219 					continue;
220 				putfsent(&mntbuf[i]);
221 			}
222 		} else {
223 			for (i = 0; i < mntsize; i++) {
224 				if (checkvfsname(mntbuf[i].f_fstypename,
225 				    vfslist))
226 					continue;
227 				prmount(&mntbuf[i]);
228 			}
229 		}
230 		exit(rval);
231 	case 1:
232 		if (vfslist != NULL)
233 			usage();
234 
235 		if (init_flags & MNT_UPDATE) {
236 			mntfromname = NULL;
237 			have_fstab = 0;
238 			if ((mntbuf = getmntpt(*argv)) == NULL)
239 				errx(1, "not currently mounted %s", *argv);
240 			/*
241 			 * Only get the mntflags from fstab if both mntpoint
242 			 * and mntspec are identical. Also handle the special
243 			 * case where just '/' is mounted and 'spec' is not
244 			 * identical with the one from fstab ('/dev' is missing
245 			 * in the spec-string at boot-time).
246 			 */
247 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
248 				if (strcmp(fs->fs_spec,
249 				    mntbuf->f_mntfromname) == 0 &&
250 				    strcmp(fs->fs_file,
251 				    mntbuf->f_mntonname) == 0) {
252 					have_fstab = 1;
253 					mntfromname = mntbuf->f_mntfromname;
254 				} else if (argv[0][0] == '/' &&
255 				    argv[0][1] == '\0') {
256 					fs = getfsfile("/");
257 					have_fstab = 1;
258 					mntfromname = fs->fs_spec;
259 				}
260 			}
261 			if (have_fstab) {
262 				options = update_options(options, fs->fs_mntops,
263 				    mntbuf->f_flags);
264 			} else {
265 				mntfromname = mntbuf->f_mntfromname;
266 				options = update_options(options, NULL,
267 				    mntbuf->f_flags);
268 			}
269 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
270 			    mntbuf->f_mntonname, init_flags, options, 0);
271 			break;
272 		}
273 		rmslashes(*argv, *argv);
274 		if ((fs = getfsfile(*argv)) == NULL &&
275 		    (fs = getfsspec(*argv)) == NULL)
276 			errx(1, "%s: unknown special file or file system",
277 			    *argv);
278 		if (BADTYPE(fs->fs_type))
279 			errx(1, "%s has unknown file system type",
280 			    *argv);
281 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
282 		    init_flags, options, fs->fs_mntops);
283 		break;
284 	case 2:
285 		/*
286 		 * If -t flag has not been specified, the path cannot be
287 		 * found, spec contains either a ':' or a '@', then assume
288 		 * that an NFS file system is being specified ala Sun.
289 		 * Check if the hostname contains only allowed characters
290 		 * to reduce false positives.  IPv6 addresses containing
291 		 * ':' will be correctly parsed only if the separator is '@'.
292 		 * The definition of a valid hostname is taken from RFC 1034.
293 		 */
294 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL) ||
295 		    ((ep = strchr(argv[0], ':')) != NULL)) {
296 			if (*ep == '@') {
297 				cp = ep + 1;
298 				ep = cp + strlen(cp);
299 			} else
300 				cp = argv[0];
301 			while (cp != ep) {
302 				if (!isdigit(*cp) && !isalpha(*cp) &&
303 				    *cp != '.' && *cp != '-' && *cp != ':')
304 					break;
305 				cp++;
306 			}
307 			if (cp == ep)
308 				vfstype = "nfs";
309 		}
310 		rval = mountfs(vfstype,
311 		    argv[0], argv[1], init_flags, options, NULL);
312 		break;
313 	default:
314 		usage();
315 		/* NOTREACHED */
316 	}
317 
318 	/*
319 	 * If the mount was successfully, and done by root, tell mountd the
320 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
321 	 */
322 	if (rval == 0 && getuid() == 0 &&
323 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
324 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
325 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
326 			err(1, "signal mountd");
327 		(void)fclose(mountdfp);
328 	}
329 
330 	exit(rval);
331 }
332 
333 int
334 ismounted(fs, mntbuf, mntsize)
335 	struct fstab *fs;
336 	struct statfs *mntbuf;
337 	int mntsize;
338 {
339 	int i;
340 
341 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
342 		/* the root file system can always be remounted */
343 		return (0);
344 
345 	for (i = mntsize - 1; i >= 0; --i)
346 		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
347 		    (!isremountable(fs->fs_vfstype) ||
348 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
349 			return (1);
350 	return (0);
351 }
352 
353 int
354 isremountable(vfsname)
355 	const char *vfsname;
356 {
357 	const char **cp;
358 
359 	for (cp = remountable_fs_names; *cp; cp++)
360 		if (strcmp(*cp, vfsname) == 0)
361 			return (1);
362 	return (0);
363 }
364 
365 int
366 hasopt(mntopts, option)
367 	const char *mntopts, *option;
368 {
369 	int negative, found;
370 	char *opt, *optbuf;
371 
372 	if (option[0] == 'n' && option[1] == 'o') {
373 		negative = 1;
374 		option += 2;
375 	} else
376 		negative = 0;
377 	optbuf = strdup(mntopts);
378 	found = 0;
379 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
380 		if (opt[0] == 'n' && opt[1] == 'o') {
381 			if (!strcasecmp(opt + 2, option))
382 				found = negative;
383 		} else if (!strcasecmp(opt, option))
384 			found = !negative;
385 	}
386 	free(optbuf);
387 	return (found);
388 }
389 
390 int
391 mountfs(vfstype, spec, name, flags, options, mntopts)
392 	const char *vfstype, *spec, *name, *options, *mntopts;
393 	int flags;
394 {
395 	/* List of directories containing mount_xxx subcommands. */
396 	static const char *edirs[] = {
397 		_PATH_SBIN,
398 		_PATH_USRSBIN,
399 		NULL
400 	};
401 	const char *argv[100], **edir;
402 	struct statfs sf;
403 	pid_t pid;
404 	int argc, i, status;
405 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
406 
407 #if __GNUC__
408 	(void)&optbuf;
409 	(void)&name;
410 #endif
411 
412 	/* resolve the mountpoint with realpath(3) */
413 	(void)checkpath(name, mntpath);
414 	name = mntpath;
415 
416 	if (mntopts == NULL)
417 		mntopts = "";
418 	if (options == NULL) {
419 		if (*mntopts == '\0') {
420 			options = "rw";
421 		} else {
422 			options = mntopts;
423 			mntopts = "";
424 		}
425 	}
426 	optbuf = catopt(strdup(mntopts), options);
427 
428 	if (strcmp(name, "/") == 0)
429 		flags |= MNT_UPDATE;
430 	if (flags & MNT_FORCE)
431 		optbuf = catopt(optbuf, "force");
432 	if (flags & MNT_RDONLY)
433 		optbuf = catopt(optbuf, "ro");
434 	/*
435 	 * XXX
436 	 * The mount_mfs (newfs) command uses -o to select the
437 	 * optimization mode.  We don't pass the default "-o rw"
438 	 * for that reason.
439 	 */
440 	if (flags & MNT_UPDATE)
441 		optbuf = catopt(optbuf, "update");
442 
443 	/* Compatibility glue. */
444 	if (strcmp(vfstype, "msdos") == 0)
445 		vfstype = "msdosfs";
446 
447 	argc = 0;
448 	argv[argc++] = vfstype;
449 	mangle(optbuf, &argc, argv);
450 	argv[argc++] = spec;
451 	argv[argc++] = name;
452 	argv[argc] = NULL;
453 
454 	if (debug) {
455 		(void)printf("exec: mount_%s", vfstype);
456 		for (i = 1; i < argc; i++)
457 			(void)printf(" %s", argv[i]);
458 		(void)printf("\n");
459 		return (0);
460 	}
461 
462 	switch (pid = fork()) {
463 	case -1:				/* Error. */
464 		warn("fork");
465 		free(optbuf);
466 		return (1);
467 	case 0:					/* Child. */
468 		if (strcmp(vfstype, "ufs") == 0)
469 			exit(mount_ufs(argc, (char * const *) argv));
470 
471 		/* Go find an executable. */
472 		for (edir = edirs; *edir; edir++) {
473 			(void)snprintf(execname,
474 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
475 			execv(execname, (char * const *)argv);
476 		}
477 		if (errno == ENOENT) {
478 			int len = 0;
479 			char *cp;
480 			for (edir = edirs; *edir; edir++)
481 				len += strlen(*edir) + 2;	/* ", " */
482 			if ((cp = malloc(len)) == NULL)
483 				errx(1, "malloc failed");
484 			cp[0] = '\0';
485 			for (edir = edirs; *edir; edir++) {
486 				strcat(cp, *edir);
487 				if (edir[1] != NULL)
488 					strcat(cp, ", ");
489 			}
490 			warn("exec mount_%s not found in %s", vfstype, cp);
491 		}
492 		exit(1);
493 		/* NOTREACHED */
494 	default:				/* Parent. */
495 		free(optbuf);
496 
497 		if (waitpid(pid, &status, 0) < 0) {
498 			warn("waitpid");
499 			return (1);
500 		}
501 
502 		if (WIFEXITED(status)) {
503 			if (WEXITSTATUS(status) != 0)
504 				return (WEXITSTATUS(status));
505 		} else if (WIFSIGNALED(status)) {
506 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
507 			return (1);
508 		}
509 
510 		if (verbose) {
511 			if (statfs(name, &sf) < 0) {
512 				warn("statfs %s", name);
513 				return (1);
514 			}
515 			if (fstab_style)
516 				putfsent(&sf);
517 			else
518 				prmount(&sf);
519 		}
520 		break;
521 	}
522 
523 	return (0);
524 }
525 
526 void
527 prmount(sfp)
528 	struct statfs *sfp;
529 {
530 	int flags;
531 	struct opt *o;
532 	struct passwd *pw;
533 
534 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
535 	    sfp->f_fstypename);
536 
537 	flags = sfp->f_flags & MNT_VISFLAGMASK;
538 	for (o = optnames; flags && o->o_opt; o++)
539 		if (flags & o->o_opt) {
540 			(void)printf(", %s", o->o_name);
541 			flags &= ~o->o_opt;
542 		}
543 	if (sfp->f_owner) {
544 		(void)printf(", mounted by ");
545 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
546 			(void)printf("%s", pw->pw_name);
547 		else
548 			(void)printf("%d", sfp->f_owner);
549 	}
550 	if (verbose) {
551 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
552 			(void)printf(", writes: sync %ld async %ld",
553 			    sfp->f_syncwrites, sfp->f_asyncwrites);
554 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
555 			(void)printf(", reads: sync %ld async %ld",
556 			    sfp->f_syncreads, sfp->f_asyncreads);
557 	}
558 	(void)printf(")\n");
559 }
560 
561 struct statfs *
562 getmntpt(name)
563 	const char *name;
564 {
565 	struct statfs *mntbuf;
566 	int i, mntsize;
567 
568 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
569 	for (i = mntsize - 1; i >= 0; i--) {
570 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
571 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
572 			return (&mntbuf[i]);
573 	}
574 	return (NULL);
575 }
576 
577 char *
578 catopt(s0, s1)
579 	char *s0;
580 	const char *s1;
581 {
582 	size_t i;
583 	char *cp;
584 
585 	if (s1 == NULL || *s1 == '\0')
586 		return s0;
587 
588 	if (s0 && *s0) {
589 		i = strlen(s0) + strlen(s1) + 1 + 1;
590 		if ((cp = malloc(i)) == NULL)
591 			errx(1, "malloc failed");
592 		(void)snprintf(cp, i, "%s,%s", s0, s1);
593 	} else
594 		cp = strdup(s1);
595 
596 	if (s0)
597 		free(s0);
598 	return (cp);
599 }
600 
601 void
602 mangle(options, argcp, argv)
603 	char *options;
604 	int *argcp;
605 	const char **argv;
606 {
607 	char *p, *s;
608 	int argc;
609 
610 	argc = *argcp;
611 	for (s = options; (p = strsep(&s, ",")) != NULL;)
612 		if (*p != '\0') {
613 			if (*p == '-') {
614 				argv[argc++] = p;
615 				p = strchr(p, '=');
616 				if (p) {
617 					*p = '\0';
618 					argv[argc++] = p+1;
619 				}
620 			} else if (strcmp(p, "rw") != 0) {
621 				argv[argc++] = "-o";
622 				argv[argc++] = p;
623 			}
624 		}
625 
626 	*argcp = argc;
627 }
628 
629 
630 char *
631 update_options(opts, fstab, curflags)
632 	char *opts;
633 	char *fstab;
634 	int curflags;
635 {
636 	char *o, *p;
637 	char *cur;
638 	char *expopt, *newopt, *tmpopt;
639 
640 	if (opts == NULL)
641 		return strdup("");
642 
643 	/* remove meta options from list */
644 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
645 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
646 	cur = flags2opts(curflags);
647 
648 	/*
649 	 * Expand all meta-options passed to us first.
650 	 */
651 	expopt = NULL;
652 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
653 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
654 			expopt = catopt(expopt, fstab);
655 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
656 			expopt = catopt(expopt, cur);
657 		else
658 			expopt = catopt(expopt, o);
659 	}
660 	free(cur);
661 	free(opts);
662 
663 	/*
664 	 * Remove previous contradictory arguments. Given option "foo" we
665 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
666 	 * and "foo" - so we can deal with possible options like "notice".
667 	 */
668 	newopt = NULL;
669 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
670 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
671 			errx(1, "malloc failed");
672 
673 		strcpy(tmpopt, "no");
674 		strcat(tmpopt, o);
675 		remopt(newopt, tmpopt);
676 		free(tmpopt);
677 
678 		if (strncmp("no", o, 2) == 0)
679 			remopt(newopt, o+2);
680 
681 		newopt = catopt(newopt, o);
682 	}
683 	free(expopt);
684 
685 	return newopt;
686 }
687 
688 void
689 remopt(string, opt)
690 	char *string;
691  	const char *opt;
692 {
693 	char *o, *p, *r;
694 
695 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
696 		return;
697 
698 	r = string;
699 
700 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
701 		if (strcmp(opt, o) != 0) {
702 			if (*r == ',' && *o != '\0')
703 				r++;
704 			while ((*r++ = *o++) != '\0')
705 			    ;
706 			*--r = ',';
707 		}
708 	}
709 	*r = '\0';
710 }
711 
712 void
713 usage()
714 {
715 
716 	(void)fprintf(stderr, "%s\n%s\n%s\n",
717 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
718 "       mount [-adfpruvw] [ -F fstab] [-t ufs | external_type]",
719 "       mount [-dfpruvw] special | node");
720 	exit(1);
721 }
722 
723 void
724 putfsent(ent)
725 	const struct statfs *ent;
726 {
727 	struct fstab *fst;
728 	char *opts;
729 
730 	opts = flags2opts(ent->f_flags);
731 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
732 	    ent->f_fstypename, opts);
733 	free(opts);
734 
735 	if ((fst = getfsspec(ent->f_mntfromname)))
736 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
737 	else if ((fst = getfsfile(ent->f_mntonname)))
738 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
739 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
740 		if (strcmp(ent->f_mntonname, "/") == 0)
741 			printf("\t1 1\n");
742 		else
743 			printf("\t2 2\n");
744 	} else
745 		printf("\t0 0\n");
746 }
747 
748 
749 char *
750 flags2opts(flags)
751 	int flags;
752 {
753 	char *res;
754 
755 	res = NULL;
756 
757 	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
758 
759 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
760 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
761 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
762 	if (flags & MNT_NODEV)		res = catopt(res, "nodev");
763 	if (flags & MNT_UNION)		res = catopt(res, "union");
764 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
765 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
766 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
767 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
768 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
769 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
770 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
771 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
772 
773 	return res;
774 }
775