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