1 /* $NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $ */
4 * Copyright (c) 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sbin/mount_msdos/mount_msdos.c,v 1.19.2.1 2000/07/20 10:35:13 kris Exp $
33 * $DragonFly: src/sbin/mount_msdos/mount_msdos.c,v 1.8 2008/11/02 21:52:46 swildner Exp $
36 #include <sys/param.h>
37 #include <sys/mount.h>
39 #include <sys/iconv.h>
40 #include <sys/linker.h>
41 #include <sys/module.h>
43 #include <vfs/msdosfs/msdosfsmount.h>
45 #include "../msdosfs/msdosfsmount.h"
53 /* must be after stdio to declare fparseln */
63 * XXX - no way to specify "foo=<bar>"-type options; that's what we'd
64 * want for "-u", "-g", "-m", "-L", and "-D".
66 static struct mntopt mopts[] = {
71 #ifdef MSDOSFSMNT_GEMDOSFS
72 { "gemdosfs", 0, MSDOSFSMNT_GEMDOSFS, 1 },
74 { "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 },
75 { "longnames", 0, MSDOSFSMNT_LONGNAME, 1 },
76 { "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 },
80 static gid_t a_gid(char *);
81 static uid_t a_uid(char *);
82 static mode_t a_mask(char *);
83 static void usage(void) __dead2;
84 int set_charset(struct msdosfs_args*, const char*, const char*);
87 main(int argc, char **argv)
89 struct msdosfs_args args;
91 int c, error, mntflags, set_gid, set_uid, set_mask;
92 char *dev, *dir, mntpath[MAXPATHLEN], *csp;
93 const char *quirk = NULL;
94 char *cs_local = NULL;
97 mntflags = set_gid = set_uid = set_mask = 0;
98 memset(&args, '\0', sizeof(args));
99 args.magic = MSDOSFS_ARGSMAGIC;
101 while ((c = getopt(argc, argv, "sl9u:g:m:o:L:D:")) != -1) {
103 #ifdef MSDOSFSMNT_GEMDOSFS
105 args.flags |= MSDOSFSMNT_GEMDOSFS;
109 args.flags |= MSDOSFSMNT_SHORTNAME;
112 args.flags |= MSDOSFSMNT_LONGNAME;
115 args.flags |= MSDOSFSMNT_NOWIN95;
118 args.uid = a_uid(optarg);
122 args.gid = a_gid(optarg);
126 args.mask = a_mask(optarg);
130 if (setlocale(LC_CTYPE, optarg) == NULL)
131 err(EX_CONFIG, "%s", optarg);
132 csp = strchr(optarg,'.');
134 err(EX_CONFIG, "%s", optarg);
135 quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
136 cs_local = strdup(quirk);
137 args.flags |= MSDOSFSMNT_KICONV;
141 cs_dos = strdup(optarg);
142 args.flags |= MSDOSFSMNT_KICONV;
145 getmntopts(optarg, mopts, &mntflags, &args.flags);
154 if (optind + 2 != argc)
158 dir = argv[optind + 1];
161 * Resolve the mountpoint with realpath(3) and remove unnecessary
162 * slashes from the devicename if there are any.
164 checkpath(dir, mntpath);
168 args.export.ex_root = -2; /* unchecked anyway on DOS fs */
170 if (cs_local != NULL) {
171 if (set_charset(&args, cs_local, cs_dos) == -1)
172 err(EX_OSERR, "msdos_iconv");
173 } else if (cs_dos != NULL) {
174 if (set_charset(&args, "ISO8859-1", cs_dos) == -1)
175 err(EX_OSERR, "msdos_iconv");
178 if (mntflags & MNT_RDONLY)
179 args.export.ex_flags = MNT_EXRDONLY;
181 args.export.ex_flags = 0;
182 if (!set_gid || !set_uid || !set_mask) {
183 if (stat(mntpath, &sb) == -1)
184 err(EX_OSERR, "stat %s", mntpath);
187 args.uid = sb.st_uid;
189 args.gid = sb.st_gid;
191 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
194 error = getvfsbyname("msdos", &vfc);
195 if (error && vfsisloadable("msdos")) {
196 if (vfsload("msdos"))
197 err(EX_OSERR, "vfsload(msdos)");
198 endvfsent(); /* clear cache */
199 error = getvfsbyname("msdos", &vfc);
202 errx(EX_OSERR, "msdos filesystem is not available");
204 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
205 err(EX_OSERR, "%s", dev);
217 if ((gr = getgrnam(s)) != NULL)
220 for (gname = s; *s && isdigit(*s); ++s);
224 errx(EX_NOUSER, "unknown group id: %s", gname);
236 if ((pw = getpwnam(s)) != NULL)
239 for (uname = s; *s && isdigit(*s); ++s);
243 errx(EX_NOUSER, "unknown user id: %s", uname);
256 if (*s >= '0' && *s <= '7') {
258 rv = strtol(optarg, &ep, 8);
260 if (!done || rv < 0 || *ep)
261 errx(EX_USAGE, "invalid file mode: %s", s);
268 fprintf(stderr, "%s\n%s\n",
269 "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask]",
270 " [-s] [-l] [-9] [-L locale] [-D codepage] bdev dir");
275 set_charset(struct msdosfs_args *args, const char *cs_local, const char *cs_dos)
278 if (modfind("msdos_iconv") < 0) {
279 if (kldload("msdos_iconv") < 0 || modfind("msdos_iconv") < 0)
281 warnx("cannot find or load \"msdos_iconv\" kernel module");
285 snprintf(args->cs_local, ICONV_CSNMAXLEN, "%s", cs_local);
286 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
290 cs_dos = strdup("CP437");
291 snprintf(args->cs_dos, ICONV_CSNMAXLEN, "%s", cs_dos);
292 error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);