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