/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

/*
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 		PROPRIETARY NOTICE (Combined)
 *
 * This source code is unpublished proprietary information
 * constituting, or derived under license from AT&T's UNIX(r) System V.
 * In addition, portions of such source code were derived from Berkeley
 * 4.3 BSD under license from the Regents of the University of
 * California.
 *
 *
 *
 * 		Copyright Notice
 *
 * Notice of copyright on this source code product does not indicate
 * publication.
 *
 * Copyright (c) 1986-1989,1996-1999 by Sun Microsystems, Inc.
 * All rights reserved.
 *
 * 	(c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
 *		All rights reserved.
 *
 */

#pragma ident	"@(#)mount.c	1.61	99/09/23 SMI"	/* SVr4.0 1.15 */

/*
 * mount
 */
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/mkdev.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mntent.h>
#include <stdlib.h>

#define	bcopy(f, t, n)	memcpy(t, f, n)
#define	bzero(s, n)	memset(s, 0, n)
#define	bcmp(s, d, n)	memcmp(s, d, n)

#define	index(s, r)	strchr(s, r)
#define	rindex(s, r)	strrchr(s, r)

#include <sys/errno.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mnttab.h>
#include <sys/mount.h>
#include <sys/mntio.h>
#include <sys/wait.h>
#include <sys/fstyp.h>
#include <sys/fsid.h>
#include <sys/vfstab.h>
#include <sys/filio.h>

#include <sys/fs/ufs_mount.h>
#include <sys/fs/ufs_filio.h>

#include <locale.h>
#include <fslib.h>

static int	ro = 0;
static int	nosuid = 0;
static int	largefiles = 0; /* flag - add default nolargefiles to mnttab */

static int	gflg = 0;
static int	mflg = 0;
static int 	Oflg = 0;
static int	qflg = 0;

extern int	optind;
extern char	*optarg;

#define	NAME_MAX	64		/* sizeof "fstype myname" */

extern int errno;

static int	checkislog(char *);
static void	disable_logging(char *, char *);
static int	eatmntopt(struct mnttab *, char *);
static void	enable_logging(char *, char *);
static void	fixopts(struct mnttab *, char *);
static void	mountfs(struct mnttab *);
static void	replace_opts(char *, int, char *, char *);
static int	replace_opts_dflt(char *, int, const char *, const char *);
static void	rmopt(struct mnttab *, char *);
static void	rpterr(char *, char *);
static void	usage(void);

static char	fstype[] = MNTTYPE_UFS;
static char	opts[MAX_MNTOPT_STR];
static char	typename[NAME_MAX], *myname;
static char	*fop_subopts[] = { MNTOPT_ONERROR, NULL };
#define	NOMATCH	(-1)
#define	ONERROR	(0)		/* index within fop_subopts */

static struct fop_subopt {
	char	*str;
	int	 flag;
} fop_subopt_list[] = {
	{ UFSMNT_ONERROR_PANIC_STR,	UFSMNT_ONERROR_PANIC	},
	{ UFSMNT_ONERROR_LOCK_STR,	UFSMNT_ONERROR_LOCK	},
	{ UFSMNT_ONERROR_UMOUNT_STR,	UFSMNT_ONERROR_UMOUNT	},
	{ NULL,				UFSMNT_ONERROR_DEFAULT	}
};

/*
 * Find opt in mntopt
 */
static char *
findopt(char *mntopt, char *opt)
{
	int nc, optlen = strlen(opt);

	while (*mntopt) {
		nc = strcspn(mntopt, ", =");
		if (strncmp(mntopt, opt, nc) == 0)
			if (optlen == nc)
				return (mntopt);
		mntopt += nc;
		mntopt += strspn(mntopt, ", =");
	}
	return (NULL);
}

void
main(int argc, char *argv[])
{
	struct mnttab mnt;
	int	c;

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	myname = strrchr(argv[0], '/');
	if (myname)
		myname++;
	else
		myname = argv[0];
	(void) sprintf(typename, "%s %s", fstype, myname);
	argv[0] = typename;

	opts[0] = '\0';

	/*
	 * Set options
	 */
	while ((c = getopt(argc, argv, "gmo:pqrVO")) != EOF) {
		switch (c) {

		case 'g':
			gflg++;
			break;

		case 'o':
			(void) strcpy(opts, optarg);
			break;

		case 'O':
			Oflg++;
			break;

		case 'r':
			ro++;
			break;

		case 'm':
			mflg++;
			break;

		case 'q':
			qflg++;
			break;

		default:
			usage();
		}
	}

	if (geteuid() != 0) {
		(void) fprintf(stderr, gettext("Must be root to use mount\n"));
		exit(32);
	}

	if ((argc - optind) != 2)
		usage();

	mnt.mnt_special = argv[optind];
	mnt.mnt_mountp = argv[optind+1];
	mnt.mnt_fstype = fstype;

	/*
	 * Process options.  The resulting options string overwrites the
	 * original.
	 *
	 * XXX:	This code doesn't do a good job of resolving options that are
	 *	specified multiple times or that are given in conflicting
	 *	forms (e.g., both "largefiles" and "nolargefiles").  It also
	 *	doesn't produce well defined behavior for options that may
	 *	also be specified as flags (e.g, "-r" and "ro"/"rw") when both
	 *	are present.
	 *
	 *	The proper way to deal with such conflicts is to start with
	 *	the default value (i.e., the one if no flag or option is
	 *	specified), override it with the last mentioned option pair
	 *	in the -o option string, and finally, override that with
	 *	the flag value. This allows "mount -r" command to mount a
	 *	file system read only that is listed rw in /etc/vfstab.
	 */
	mnt.mnt_mntopts = opts;
	if (findopt(mnt.mnt_mntopts, "m"))
		mflg++;
	if (findopt(mnt.mnt_mntopts, MNTOPT_NOSUID))
		nosuid++;
	replace_opts(opts, nosuid, MNTOPT_NOSUID, MNTOPT_SUID);
	replace_opts(opts, ro, MNTOPT_RO, MNTOPT_RW);
	replace_opts(opts, largefiles, MNTOPT_NOLARGEFILES, MNTOPT_LARGEFILES);
	gflg = replace_opts_dflt(opts, gflg, MNTOPT_GLOBAL, MNTOPT_NOGLOBAL);

	if (findopt(mnt.mnt_mntopts, MNTOPT_RQ)) {
		rmopt(&mnt, MNTOPT_RQ);
		replace_opts(opts, 1, MNTOPT_QUOTA, MNTOPT_NOQUOTA);
	}

	mountfs(&mnt);
	/* NOTREACHED */
}

static void
reportlogerror(int ret, char *mp, char *special, char *cmd, fiolog_t *flp)
{
	/* No error */
	if ((ret != -1) && (flp->error == FIOLOG_ENONE))
		return;

	/* logging was not enabled/disabled */
	if (ret == -1 || flp->error != FIOLOG_ENONE)
		fprintf(stderr, gettext("Could not %s logging"
				" for %s on %s.\n"), cmd, mp, special);

	/* ioctl returned error */
	if (ret == -1)
		return;

	/* Some more info */
	switch (flp->error) {
	case FIOLOG_ENONE :
		if (flp->nbytes_requested &&
		    (flp->nbytes_requested != flp->nbytes_actual)) {
			fprintf(stderr, gettext("The log has been resized"
					" from %d bytes to %d bytes.\n"),
					flp->nbytes_requested,
					flp->nbytes_actual);
		}
		return;
	case FIOLOG_ETRANS :
		fprintf(stderr, gettext("Solstice DiskSuite Logging"
				" is already enabled.\n"));
		fprintf(stderr, gettext("Please see the DiskSuite"
				" commands metadetach(1M)"
				" or metaclear(1M).\n"));
		break;
	case FIOLOG_EROFS :
		fprintf(stderr, gettext("File system is mounted read only.\n"));
		fprintf(stderr, gettext("Please see the remount"
				" option described in mount_ufs(1M).\n"));
		break;
	case FIOLOG_EULOCK :
		fprintf(stderr, gettext("File system is locked.\n"));
		fprintf(stderr, gettext("Please see the -u option described"
				" in lockfs(1M).\n"));
		break;
	case FIOLOG_EWLOCK :
		fprintf(stderr, gettext("The file system could not be"
					" write locked.\n"));
		fprintf(stderr, gettext("Please see the -w option described"
				" in lockfs(1M).\n"));
		break;
	case FIOLOG_ECLEAN :
		fprintf(stderr, gettext("The file system may not be"
					" stable.\n"));
		fprintf(stderr, gettext("Please see the -n option"
				" for fsck(1M).\n"));
		break;
	case FIOLOG_ENOULOCK :
		fprintf(stderr, gettext("The file system could not be"
					" unlocked.\n"));
		fprintf(stderr, gettext("Please see the -u option described"
				" in lockfs(1M).\n"));
		break;
	default :
		fprintf(stderr, gettext("Unknown internal error"
					" %d.\n"), flp->error);
		break;
	}
}

static int
checkislog(char *mp)
{
	int fd;
	uint32_t islog;

	fd = open(mp, O_RDONLY);
	islog = 0;
	(void) ioctl(fd, _FIOISLOG, &islog);
	close(fd);
	return ((int)islog);
}

static void
enable_logging(char *mp, char *special)
{
	int fd, ret, islog;
	fiolog_t fl;

	fd = open(mp, O_RDONLY);
	if (fd == -1) {
		perror(mp);
		return;
	}
	fl.nbytes_requested = 0;
	fl.nbytes_actual = 0;
	fl.error = FIOLOG_ENONE;
	ret = ioctl(fd, _FIOLOGENABLE, &fl);
	if (ret == -1)
		perror(mp);
	close(fd);

	/* is logging enabled? */
	islog = checkislog(mp);

	/* report errors, if any */
	if (ret == -1 || !islog)
		reportlogerror(ret, mp, special, "enable", &fl);
}

static void
disable_logging(char *mp, char *special)
{
	int fd, ret, islog;
	fiolog_t fl;

	fd = open(mp, O_RDONLY);
	if (fd == -1) {
		perror(mp);
		return;
	}
	fl.error = FIOLOG_ENONE;
	ret = ioctl(fd, _FIOLOGDISABLE, &fl);
	if (ret == -1)
		perror(mp);
	close(fd);

	/* is logging enabled? */
	islog = checkislog(mp);

	/* report errors, if any */
	if (ret == -1 || islog)
		reportlogerror(ret, mp, special, "disable", &fl);
}


/*
 * attempt to mount file system, return errno or 0
 */
void
mountfs(struct mnttab *mnt)
{
	char			 opt[1024];
	char			 opt2[1024];
	char			 opt3[1024];
	char			*opts =	opt;
	int			 flags = MS_OPTIONSTR;
	struct ufs_args		 args;
	int			 need_separator = 0;

	(void) bzero((char *)&args, sizeof (args));
	(void) strcpy(opts, mnt->mnt_mntopts);
	opt2[0] = '\0';

	flags |= Oflg ? MS_OVERLAY : 0;
	flags |= eatmntopt(mnt, MNTOPT_RO) ? MS_RDONLY : 0;
	flags |= eatmntopt(mnt, MNTOPT_NOSUID) ? MS_NOSUID : 0;
	flags |= eatmntopt(mnt, MNTOPT_REMOUNT) ? MS_REMOUNT : 0;
	flags |= eatmntopt(mnt, MNTOPT_GLOBAL) ? MS_GLOBAL : 0;

	/* Save UFS specific options for mount(2) to parse directly. */
	(void) strcpy(opt3, mnt->mnt_mntopts);

	if (eatmntopt(mnt, MNTOPT_NOINTR))
		args.flags |= UFSMNT_NOINTR;
	if (eatmntopt(mnt, MNTOPT_INTR))
		args.flags &= ~UFSMNT_NOINTR;
	if (eatmntopt(mnt, MNTOPT_SYNCDIR))
		args.flags |= UFSMNT_SYNCDIR;
	if (eatmntopt(mnt, MNTOPT_FORCEDIRECTIO)) {
		args.flags |= UFSMNT_FORCEDIRECTIO;
		args.flags &= ~UFSMNT_NOFORCEDIRECTIO;
	}
	if (eatmntopt(mnt, MNTOPT_NOFORCEDIRECTIO)) {
		args.flags |= UFSMNT_NOFORCEDIRECTIO;
		args.flags &= ~UFSMNT_FORCEDIRECTIO;
	}
	if (eatmntopt(mnt, MNTOPT_NOSETSEC))
		args.flags |= UFSMNT_NOSETSEC;
	if (eatmntopt(mnt, MNTOPT_LARGEFILES))
		args.flags |= UFSMNT_LARGEFILES;
	if (eatmntopt(mnt, MNTOPT_NOLARGEFILES))
		args.flags &= ~UFSMNT_LARGEFILES;
	if (eatmntopt(mnt, MNTOPT_LOGGING))
		args.flags |= UFSMNT_LOGGING;
	if (eatmntopt(mnt, MNTOPT_NOLOGGING))
		args.flags &= ~UFSMNT_LOGGING;
	if (eatmntopt(mnt, MNTOPT_NOATIME))
		args.flags |= UFSMNT_NOATIME;
	if (eatmntopt(mnt, MNTOPT_DFRATIME))
		args.flags &= ~UFSMNT_NODFRATIME;
	if (eatmntopt(mnt, MNTOPT_NODFRATIME))
		args.flags |= UFSMNT_NODFRATIME;

	while (*opts != '\0') {
		char	*argval;

		switch (getsubopt(&opts, fop_subopts, &argval)) {
		case ONERROR:
			if (argval) {
				struct fop_subopt	*s;
				int			 found = 0;

				for (s = fop_subopt_list;
				    s->str && !found;
				    s++) {
					if (strcmp(argval, s->str) == 0) {
						args.flags |= s->flag;
						found = 1;
					}
				}
				if (!found) {
					usage();
				}

				if (need_separator)
					(void) strcat(opt2, ",");
				(void) strcat(opt2, MNTOPT_ONERROR);
				(void) strcat(opt2, "=");
				(void) strcat(opt2, argval);
				need_separator = 1;

			} else {
				args.flags |= UFSMNT_ONERROR_DEFAULT;
			}
			break;

		case NOMATCH:
		default:
			if (argval) {
				if (need_separator)
					(void) strcat(opt2, ",");
				(void) strcat(opt2, argval);
				need_separator = 1;
			}
			break;

		}
	}

	if (*opt2 != '\0')
		strcpy(opt, opt2);
	opts = opt;
	if ((args.flags & UFSMNT_ONERROR_FLGMASK) == 0)
		args.flags |= UFSMNT_ONERROR_DEFAULT;

	(void) signal(SIGHUP,  SIG_IGN);
	(void) signal(SIGQUIT, SIG_IGN);
	(void) signal(SIGINT,  SIG_IGN);

	errno = 0;
	flags |= MS_DATA | MS_OPTIONSTR;
	if (mflg)
		flags |= MS_NOMNTTAB;
	if (flags & MS_REMOUNT) {
		replace_opts(mnt->mnt_mntopts, 1, MNTOPT_RW, MNTOPT_RO);
	}
	fixopts(mnt, opts);
	if (mount(mnt->mnt_special, mnt->mnt_mountp, flags, fstype,
		&args, sizeof (args), mnt->mnt_mntopts, MAX_MNTOPT_STR) != 0) {
		rpterr(mnt->mnt_special, mnt->mnt_mountp);
		exit(32);
	}

	if (args.flags & UFSMNT_LOGGING)
		enable_logging(mnt->mnt_mountp, mnt->mnt_special);
	else if (!(flags & MS_RDONLY))
		disable_logging(mnt->mnt_mountp, mnt->mnt_special);

	if (*opts && !qflg) {
		(void) fprintf(stderr, gettext(
		    "mount: %s on %s - WARNING unknown options \"%s\"\n"),
		    mnt->mnt_special, mnt->mnt_mountp, opts);
	}
	if (checkislog(mnt->mnt_mountp)) {
		/* update mnttab file if necessary */
		if (!mflg) {
			struct stat64 statb;
			struct mnttagdesc mtdesc;
			int fd;

			if (stat64(mnt->mnt_mountp, &statb) != 0)
				exit(32);
			/* do tag ioctl */
			mtdesc.mtd_major = major(statb.st_dev);
			mtdesc.mtd_minor = minor(statb.st_dev);
			mtdesc.mtd_mntpt = mnt->mnt_mountp;
			mtdesc.mtd_tag = MNTOPT_LOGGING;
			if ((fd = open(MNTTAB, O_RDONLY, 0)) < 0)
				exit(32);
			if (ioctl(fd, MNTIOC_SETTAG, &mtdesc) != 0) {
				close(fd);
				exit(32);
			}
			close(fd);
		}
	}
	exit(0);
}

/*
 * same as findopt but remove the option from the option string and return
 * true or false
 */
static int
eatmntopt(struct mnttab *mnt, char *opt)
{
	int has;

	has = (findopt(mnt->mnt_mntopts, opt) != NULL);
	rmopt(mnt, opt);
	return (has);
}

/*
 * remove an option string from the option list
 */
static void
rmopt(struct mnttab *mnt, char *opt)
{
	char *str;
	char *optstart;

	while (optstart = findopt(mnt->mnt_mntopts, opt)) {
		for (str = optstart;
		    *str != ','	&& *str != '\0' && *str != ' ';
		    str++)
			/* NULL */;
		if (*str == ',') {
			str++;
		} else if (optstart != mnt->mnt_mntopts) {
			optstart--;
		}
		while (*optstart++ = *str++)
			;
	}
}

/*
 * mnt->mnt_ops has un-eaten opts, opts is the original opts list.
 * Set mnt->mnt_opts to the original list minus the un-eaten opts.
 * Set "opts" to the un-eaten opts minus the "default" options ("rw",
 * "hard", "noquota", "noauto", "bg", and "largefiles".  If there are any
 * options left after this, they are uneaten because they are unknown; our
 * caller will print a warning message.
 */
static void
fixopts(struct mnttab *mnt, char *opts)
{
	char *comma;
	char *ue;
	char uneaten[1024];

	/*
	 * Remove the options we know about
	 */
	rmopt(mnt, MNTOPT_RW);
	rmopt(mnt, MNTOPT_REMOUNT);
	rmopt(mnt, MNTOPT_RO);
	rmopt(mnt, MNTOPT_NOQUOTA);
	rmopt(mnt, MNTOPT_QUOTA);
	rmopt(mnt, MNTOPT_ONERROR);
/*	rmopt(mnt, MNTOPT_NOAUTO); */
	rmopt(mnt, MNTOPT_NOLOGGING);
	rmopt(mnt, MNTOPT_LOGGING);
	rmopt(mnt, MNTOPT_SUID);
	rmopt(mnt, MNTOPT_NOSUID);
	rmopt(mnt, "f");
	rmopt(mnt, "m");
	rmopt(mnt, MNTOPT_NOLARGEFILES);
	rmopt(mnt, MNTOPT_LARGEFILES);
	rmopt(mnt, MNTOPT_SYNCDIR);
	rmopt(mnt, MNTOPT_FORCEDIRECTIO);
	rmopt(mnt, MNTOPT_NOFORCEDIRECTIO);
	rmopt(mnt, MNTOPT_INTR);
	rmopt(mnt, MNTOPT_NOINTR);
	rmopt(mnt, MNTOPT_NOSETSEC);
	rmopt(mnt, MNTOPT_GLOBAL);
	rmopt(mnt, MNTOPT_NOGLOBAL);

	/*
	 * Save the uneaten options
	 */
	(void) strcpy(uneaten, mnt->mnt_mntopts);

	/*
	 * Set the options for ``/etc/mnttab'' to be the original
	 * options from main(); except for the option "f" and "remount".
	 */
	(void) strcpy(mnt->mnt_mntopts, opts);
	rmopt(mnt, "f");
	rmopt(mnt, MNTOPT_REMOUNT);

	/*
	 * opts now points to the uneaten options.
	 *
	 * The caller of this function will report the uneaten options as
	 * errors.
	 */

	(void) strcpy(opts, uneaten);

	/*
	 * Remove uneaten options from the /etc/mnttab options
	 */
	for (ue = uneaten; *ue; ) {
		for (comma = ue; *comma != '\0' && *comma != ','; comma++)
			;
		if (*comma == ',') {
			*comma = '\0';
			rmopt(mnt, ue);
			ue = comma+1;
		} else {
			rmopt(mnt, ue);
			ue = comma;
		}
	}

	/*
	 * Woops, something is wrong, there are no more options! At the very
	 * least, set options to the minimal defaults.
	 */
	if (*mnt->mnt_mntopts == '\0') {
		(void) sprintf(mnt->mnt_mntopts, "%s,%s",
		    MNTOPT_RW, MNTOPT_NOLARGEFILES);
	} else {
		rmopt(mnt, MNTOPT_LOGGING);
		rmopt(mnt, MNTOPT_NOLOGGING);
	}
}

static void
usage()
{
	(void) fprintf(stdout, gettext(
"ufs usage:\n"
"mount [-F ufs] [generic options] [-o suboptions] {special | mount_point}\n"));
	(void) fprintf(stdout, gettext(
			"\tsuboptions are: \n"
			"\t	ro,rw,nosuid,remount,f,m,\n"
			"\t	global,noglobal,\n"
			"\t	largefiles,nolargefiles,\n"
			"\t	forcedirectio,noforcedirectio\n"
			"\t	logging,nologging,\n"
			"\t	onerror[={panic | lock | umount}]\n"));

	exit(32);
}

/*
 * Returns the next option in the option string.
 */
static char *
getnextopt(char **p)
{
	char *cp = *p;
	char *retstr;

	while (*cp && isspace(*cp))
		cp++;
	retstr = cp;
	while (*cp && *cp != ',')
		cp++;
	if (*cp) {
		*cp = '\0';
		cp++;
	}
	*p = cp;
	return (retstr);
}

/*
 * "trueopt" and "falseopt" are two settings of a Boolean option.
 * If "flag" is true, forcibly set the option to the "true" setting; otherwise,
 * if the option isn't present, set it to the false setting.
 */
static void
replace_opts(char *options, int flag, char *trueopt, char *falseopt)
{
	char *f;
	char *tmpoptsp;
	int found;
	char tmptopts[MNTMAXSTR];

	(void) strcpy(tmptopts, options);
	tmpoptsp = tmptopts;
	(void) strcpy(options, "");

	found = 0;
	for (f = getnextopt(&tmpoptsp); *f; f = getnextopt(&tmpoptsp)) {
		if (options[0] != '\0')
			(void) strcat(options, ",");
		if (strcmp(f, trueopt) == 0) {
			(void) strcat(options, f);
			found++;
		} else if (strcmp(f, falseopt) == 0) {
			if (flag)
				(void) strcat(options, trueopt);
			else
				(void) strcat(options, f);
			found++;
		} else
			(void) strcat(options, f);
	}
	if (!found) {
		if (options[0] != '\0')
			(void) strcat(options, ",");
		(void) strcat(options, flag ? trueopt : falseopt);
	}
}

/*
 * "trueopt" and "falseopt" are two settings of a Boolean option and "dflt" is
 * a default value for the option.  Rewrite the contents of options to include
 * only the last mentioned occurrence of trueopt and falseopt.  If neither is
 * mentioned, append one or the other to options, according to the value of
 * dflt.  Return the resulting value of the option in boolean form.
 *
 * Note that the routine is implemented to have the resulting occurrence of
 * trueopt or falseopt appear at the end of the resulting option string.
 *
 * N.B.	This routine should take the place of replace_opts, but there are
 *	probably some compatibility issues to resolve before doing so.  It
 *	should certainly be used to handle new options that don't have
 *	compatibility issues.
 */
static int
replace_opts_dflt(
	char *options,
	int dflt,
	const char *trueopt,
	const char *falseopt)
{
	char *f;
	char *tmpoptsp;
	int last;
	char tmptopts[MNTMAXSTR];

	/*
	 * Transfer the contents of options to tmptopts, in anticipation of
	 * copying a subset of the contents back to options.
	 */
	(void) strcpy(tmptopts, options);
	tmpoptsp = tmptopts;
	(void) strcpy(options, "");

	/*
	 * Loop over each option value, copying non-matching values back into
	 * options and updating the last seen occurrence of trueopt or
	 * falseopt.
	 */
	last = dflt;
	for (f = getnextopt(&tmpoptsp); *f; f = getnextopt(&tmpoptsp)) {
		/* Check for both forms of the option of interest. */
		if (strcmp(f, trueopt) == 0) {
			last = 1;
		} else if (strcmp(f, falseopt) == 0) {
			last = 0;
		} else {
			/* Not what we're looking for; transcribe. */
			if (options[0] != '\0')
				(void) strcat(options, ",");
			(void) strcat(options, f);
		}
	}

	/*
	 * Transcribe the correct form of the option of interest, using the
	 * default value if it wasn't overwritten above.
	 */
	if (options[0] != '\0')
		(void) strcat(options, ",");
	(void) strcat(options, last ? trueopt : falseopt);

	return (last);
}

static void
rpterr(char *bs, char *mp)
{
	switch (errno) {
	case EPERM:
		(void) fprintf(stderr, gettext("%s: not super user\n"), myname);
		break;
	case ENXIO:
		(void) fprintf(stderr, gettext("%s: %s no such device\n"),
								myname, bs);
		break;
	case ENOTDIR:
		(void) fprintf(stderr,
			gettext(
	"%s: %s not a directory\n\tor a component of %s is not a directory\n"),
			myname, mp, bs);
		break;
	case ENOENT:
		(void) fprintf(stderr, gettext(
			"%s: %s or %s, no such file or directory\n"),
			myname, bs, mp);
		break;
	case EINVAL:
		(void) fprintf(stderr, gettext("%s: %s is not this fstype.\n"),
			myname, bs);
		break;
	case EBUSY:
		(void) fprintf(stderr, gettext(
	"%s: %s is already mounted, %s is busy,\n"
	"\tor the allowable number of mount points has been exceeded\n"),
		myname, bs, mp);
		break;
	case ENOTBLK:
		(void) fprintf(stderr, gettext(
			"%s: %s not a block device\n"), myname, bs);
		break;
	case EROFS:
		(void) fprintf(stderr, gettext("%s: %s write-protected\n"),
			myname, bs);
		break;
	case ENOSPC:
		(void) fprintf(stderr, gettext(
			"%s: the state of %s is not okay\n"
			"\tand it was attempted to be mounted read/write\n"),
			myname, bs);
		(void) printf(gettext(
			"mount: Please run fsck and try again\n"));
		break;
	case EFBIG:
		(void) fprintf(stderr, gettext(
			"%s: large files may be present on %s,\n"
			"\tand it was attempted to be mounted nolargefiles\n"),
		    myname, bs);
		break;
	default:
		perror(myname);
		(void) fprintf(stderr, gettext("%s: cannot mount %s\n"),
				myname, bs);
	}
}
