1152344Srodrigc/*
2152344Srodrigc * Copyright (c) 1992, 1993, 1994
3152344Srodrigc *	The Regents of the University of California.  All rights reserved.
4152344Srodrigc *
5152344Srodrigc * This code is derived from software donated to Berkeley by
6152344Srodrigc * Jan-Simon Pendry.
7152344Srodrigc *
8152344Srodrigc * Redistribution and use in source and binary forms, with or without
9152344Srodrigc * modification, are permitted provided that the following conditions
10152344Srodrigc * are met:
11152344Srodrigc * 1. Redistributions of source code must retain the above copyright
12152344Srodrigc *    notice, this list of conditions and the following disclaimer.
13152344Srodrigc * 2. Redistributions in binary form must reproduce the above copyright
14152344Srodrigc *    notice, this list of conditions and the following disclaimer in the
15152344Srodrigc *    documentation and/or other materials provided with the distribution.
16152344Srodrigc * 4. Neither the name of the University nor the names of its contributors
17152344Srodrigc *    may be used to endorse or promote products derived from this software
18152344Srodrigc *    without specific prior written permission.
19152344Srodrigc *
20152344Srodrigc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21152344Srodrigc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22152344Srodrigc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23152344Srodrigc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24152344Srodrigc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25152344Srodrigc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26152344Srodrigc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27152344Srodrigc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28152344Srodrigc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29152344Srodrigc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30152344Srodrigc * SUCH DAMAGE.
31152344Srodrigc */
32152344Srodrigc
33152344Srodrigc#ifndef lint
34152344Srodrigcstatic const char copyright[] =
35152344Srodrigc"@(#) Copyright (c) 1992, 1993, 1994\n\
36152344Srodrigc	The Regents of the University of California.  All rights reserved.\n";
37152344Srodrigc#endif /* not lint */
38152344Srodrigc
39152344Srodrigc#ifndef lint
40152344Srodrigc#if 0
41152344Srodrigcstatic char sccsid[] = "@(#)mount_fs.c	8.6 (Berkeley) 4/26/95";
42152344Srodrigc#endif
43152344Srodrigcstatic const char rcsid[] =
44152344Srodrigc	"$FreeBSD$";
45152344Srodrigc#endif /* not lint */
46152344Srodrigc
47152344Srodrigc#include <sys/param.h>
48152344Srodrigc#include <sys/mount.h>
49152344Srodrigc
50152344Srodrigc#include <err.h>
51152344Srodrigc#include <getopt.h>
52152344Srodrigc#include <libgen.h>
53152344Srodrigc#include <stdio.h>
54152344Srodrigc#include <stdlib.h>
55152344Srodrigc#include <string.h>
56152344Srodrigc#include <unistd.h>
57152344Srodrigc
58152344Srodrigc#include "extern.h"
59152344Srodrigc#include "mntopts.h"
60152344Srodrigc
61152344Srodrigcstruct mntopt mopts[] = {
62152344Srodrigc	MOPT_STDOPTS,
63152344Srodrigc	MOPT_END
64152344Srodrigc};
65152344Srodrigc
66152344Srodrigcstatic void
67152344Srodrigcusage(void)
68152344Srodrigc{
69152344Srodrigc	(void)fprintf(stderr,
70152344Srodrigc		"usage: mount [-t fstype] [-o options] target_fs mount_point\n");
71152344Srodrigc	exit(1);
72152344Srodrigc}
73152344Srodrigc
74152344Srodrigcint
75152344Srodrigcmount_fs(const char *vfstype, int argc, char *argv[])
76152344Srodrigc{
77152344Srodrigc	struct iovec *iov;
78152344Srodrigc	int iovlen;
79152344Srodrigc	int mntflags = 0;
80152344Srodrigc	int ch;
81152344Srodrigc	char *dev, *dir, mntpath[MAXPATHLEN];
82152344Srodrigc	char fstype[32];
83164977Srodrigc	char errmsg[255];
84152344Srodrigc	char *p, *val;
85152344Srodrigc
86186291Sobrien	strlcpy(fstype, vfstype, sizeof(fstype));
87164977Srodrigc	memset(errmsg, 0, sizeof(errmsg));
88152344Srodrigc
89152344Srodrigc	getmnt_silent = 1;
90152344Srodrigc	iov = NULL;
91152344Srodrigc	iovlen = 0;
92152344Srodrigc
93152344Srodrigc	optind = optreset = 1;		/* Reset for parse of new argv. */
94152344Srodrigc	while ((ch = getopt(argc, argv, "o:")) != -1) {
95152344Srodrigc		switch(ch) {
96152344Srodrigc		case 'o':
97152344Srodrigc			getmntopts(optarg, mopts, &mntflags, 0);
98152344Srodrigc			p = strchr(optarg, '=');
99152344Srodrigc			val = NULL;
100152344Srodrigc			if (p != NULL) {
101152344Srodrigc				*p = '\0';
102152344Srodrigc				val = p + 1;
103152344Srodrigc			}
104152354Srodrigc			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
105152344Srodrigc			break;
106152344Srodrigc		case '?':
107152344Srodrigc		default:
108152344Srodrigc			usage();
109152344Srodrigc		}
110152344Srodrigc	}
111152344Srodrigc
112152344Srodrigc	argc -= optind;
113152344Srodrigc	argv += optind;
114152344Srodrigc	if (argc != 2)
115152344Srodrigc		usage();
116152344Srodrigc
117152344Srodrigc	dev = argv[0];
118152344Srodrigc	dir = argv[1];
119152344Srodrigc
120152344Srodrigc	(void)checkpath(dir, mntpath);
121152344Srodrigc	(void)rmslashes(dev, dev);
122152344Srodrigc
123152354Srodrigc	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
124152354Srodrigc	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
125152354Srodrigc	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
126164977Srodrigc	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
127152344Srodrigc
128231591Sjh	if (nmount(iov, iovlen, mntflags) == -1) {
129231596Sjh		if (*errmsg != '\0')
130231596Sjh			warn("%s: %s", dev, errmsg);
131231596Sjh		else
132231596Sjh			warn("%s", dev);
133231591Sjh		return (1);
134231591Sjh	}
135231591Sjh	return (0);
136152344Srodrigc}
137