xref: /netbsd-src/sbin/mount_v7fs/mount_v7fs.c (revision 9255b46fb42fefe9849143c9ed6623356746daa3)
1 /*	$NetBSD: mount_v7fs.c,v 1.1 2011/06/27 11:52:58 uch Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 1993, 1994
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 #include <sys/cdefs.h>
61 #ifndef lint
62 __RCSID("$NetBSD: mount_v7fs.c,v 1.1 2011/06/27 11:52:58 uch Exp $");
63 #endif /* not lint */
64 
65 #include <sys/param.h>
66 #include <sys/mount.h>
67 
68 #include <err.h>
69 #include <errno.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 
73 #include <string.h>
74 #include <unistd.h>
75 
76 #include <mntopts.h>
77 
78 #include "mountprog.h"
79 #include "mount_v7fs.h"
80 
81 static const struct mntopt mopts[] = {
82 	MOPT_STDOPTS,
83 	MOPT_UPDATE,
84 	MOPT_GETARGS,
85 	MOPT_NULL,
86 };
87 
88 static void v7fs_usage(void) __dead;
89 
90 #ifndef MOUNT_NOMAIN
91 int
main(int argc,char ** argv)92 main(int argc, char **argv)
93 {
94 
95 	return mount_v7fs(argc, argv);
96 }
97 #endif
98 
99 int
mount_v7fs(int argc,char * argv[])100 mount_v7fs(int argc, char *argv[])
101 {
102 	struct v7fs_args args;
103 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
104 	const char *errcause;
105 	int mntflags;
106 
107 	mount_v7fs_parseargs(argc, argv, &args, &mntflags,
108 	    canon_dev, canon_dir);
109 
110 	if (mount(MOUNT_V7FS, canon_dir, mntflags, &args, sizeof args) == -1) {
111 		switch (errno) {
112 		case EMFILE:
113 			errcause = "mount table full";
114 			break;
115 		case EINVAL:
116 			if (mntflags & MNT_UPDATE)
117 				errcause =
118 			    "specified device does not match mounted device";
119 			else
120 				errcause = "incorrect super block";
121 			break;
122 		default:
123 			errcause = strerror(errno);
124 			break;
125 		}
126 		errx(EXIT_FAILURE, "%s on %s: %s", canon_dev, canon_dir,
127 		    errcause);
128 	}
129 
130 	if (mntflags & MNT_GETARGS) {
131 		printf("endian=%d\n", args.endian);
132 	}
133 
134 	return EXIT_SUCCESS;
135 }
136 
137 void
mount_v7fs_parseargs(int argc,char ** argv,struct v7fs_args * args,int * mntflags,char * canon_dev,char * canon_dir)138 mount_v7fs_parseargs(int argc, char **argv, struct v7fs_args *args,
139     int *mntflags, char *canon_dev, char *canon_dir)
140 {
141 	int ch;
142 	mntoptparse_t mp;
143 	int endian = _BYTE_ORDER;
144 	*mntflags = 0;
145 	optind = optreset = 1;		/* Reset for parse of new argv. */
146 	while ((ch = getopt(argc, argv, "o:B:")) != -1)
147 		switch (ch) {
148 		case 'o':
149 			mp = getmntopts(optarg, mopts, mntflags, 0);
150 			if (mp == NULL)
151 				err(1, "getmntopts");
152 			freemntopts(mp);
153 			break;
154 		case 'B':
155 		  switch (optarg[0]) {
156 		    case 'l':
157 		      endian = _LITTLE_ENDIAN;
158 		      break;
159 		    case 'b':
160 		      endian = _BIG_ENDIAN;
161 		      break;
162 		    case 'p':
163 		      endian = _PDP_ENDIAN;
164 		      break;
165 		    }
166 		  break;
167 		case '?':
168 
169 		default:
170 			v7fs_usage();
171 		}
172 	argc -= optind;
173 	argv += optind;
174 
175 	if (argc != 2)
176 		v7fs_usage();
177 
178 	pathadj(argv[0], canon_dev);
179 	args->endian = endian;
180 	args->fspec = canon_dev;
181 	pathadj(argv[1], canon_dir);
182 }
183 
184 static void
v7fs_usage(void)185 v7fs_usage(void)
186 {
187 
188 	fprintf(stderr, "usage: \n %s [-o options] [-B endian] special node\n",
189 	    getprogname());
190 	exit(EXIT_FAILURE);
191 }
192