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