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.
34 static const char rcsid[] =
38 #include <sys/param.h>
39 #include <sys/mount.h>
42 #include <fs/msdosfs/msdosfsmount.h>
50 /* must be after stdio to declare fparseln */
60 * XXX - no way to specify "foo=<bar>"-type options; that's what we'd
61 * want for "-u", "-g", "-m", "-L", and "-W".
63 static struct mntopt mopts[] = {
68 #ifdef MSDOSFSMNT_GEMDOSFS
69 { "gemdosfs", 0, MSDOSFSMNT_GEMDOSFS, 1 },
71 { "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 },
72 { "longnames", 0, MSDOSFSMNT_LONGNAME, 1 },
73 { "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 },
77 static gid_t a_gid(char *);
78 static uid_t a_uid(char *);
79 static mode_t a_mask(char *);
80 static void usage(void) __dead2;
81 static void load_u2wtable(struct msdosfs_args *, char *);
82 static void load_ultable(struct msdosfs_args *, char *);
89 struct msdosfs_args args;
91 int c, mntflags, set_gid, set_uid, set_mask;
92 char *dev, *dir, mntpath[MAXPATHLEN];
94 mntflags = set_gid = set_uid = set_mask = 0;
95 (void)memset(&args, '\0', sizeof(args));
96 args.magic = MSDOSFS_ARGSMAGIC;
98 while ((c = getopt(argc, argv, "sl9u:g:m:o:L:W:")) != -1) {
100 #ifdef MSDOSFSMNT_GEMDOSFS
102 args.flags |= MSDOSFSMNT_GEMDOSFS;
106 args.flags |= MSDOSFSMNT_SHORTNAME;
109 args.flags |= MSDOSFSMNT_LONGNAME;
112 args.flags |= MSDOSFSMNT_NOWIN95;
115 args.uid = a_uid(optarg);
119 args.gid = a_gid(optarg);
123 args.mask = a_mask(optarg);
127 load_ultable(&args, optarg);
128 args.flags |= MSDOSFSMNT_ULTABLE;
131 load_u2wtable(&args, optarg);
132 args.flags |= MSDOSFSMNT_U2WTABLE;
135 getmntopts(optarg, mopts, &mntflags, &args.flags);
144 if (optind + 2 != argc)
148 dir = argv[optind + 1];
151 * Resolve the mountpoint with realpath(3) and remove unnecessary
152 * slashes from the devicename if there are any.
154 (void)checkpath(dir, mntpath);
155 (void)rmslashes(dev, dev);
158 args.export.ex_root = -2; /* unchecked anyway on DOS fs */
159 if (mntflags & MNT_RDONLY)
160 args.export.ex_flags = MNT_EXRDONLY;
162 args.export.ex_flags = 0;
163 if (!set_gid || !set_uid || !set_mask) {
164 if (stat(mntpath, &sb) == -1)
165 err(EX_OSERR, "stat %s", mntpath);
168 args.uid = sb.st_uid;
170 args.gid = sb.st_gid;
172 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
175 if (mount("msdosfs", mntpath, mntflags, &args) < 0)
176 err(EX_OSERR, "%s", dev);
189 if ((gr = getgrnam(s)) != NULL)
192 for (gname = s; *s && isdigit(*s); ++s);
196 errx(EX_NOUSER, "unknown group id: %s", gname);
209 if ((pw = getpwnam(s)) != NULL)
212 for (uname = s; *s && isdigit(*s); ++s);
216 errx(EX_NOUSER, "unknown user id: %s", uname);
230 if (*s >= '0' && *s <= '7') {
232 rv = strtol(optarg, &ep, 8);
234 if (!done || rv < 0 || *ep)
235 errx(EX_USAGE, "invalid file mode: %s", s);
242 fprintf(stderr, "%s\n%s\n",
243 "usage: mount_msdosfs [-o options] [-u user] [-g group] [-m mask]",
244 " [-s] [-l] [-9] [-L locale] [-W table] bdev dir");
249 load_u2wtable (pargs, name)
250 struct msdosfs_args *pargs;
262 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
266 if ((f = fopen(fn, "r")) == NULL)
267 err(EX_NOINPUT, "%s", fn);
269 for (i = 0; i < 16; i++) {
271 if (p != NULL) free(p);
272 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
273 errx(EX_DATAERR, "can't read u2w table row %d near line %d", i, line);
274 while (isspace((unsigned char)*s))
276 } while (*s == '\0');
277 if (sscanf(s, "%i%i%i%i%i%i%i%i",
278 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
279 errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %d", i, line);
280 for (j = 0; j < 8; j++)
281 pargs->u2w[i * 8 + j] = code[j];
283 for (i = 0; i < 16; i++) {
286 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
287 errx(EX_DATAERR, "can't read d2u table row %d near line %d", i, line);
288 while (isspace((unsigned char)*s))
290 } while (*s == '\0');
291 if (sscanf(s, "%i%i%i%i%i%i%i%i",
292 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
293 errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %d", i, line);
294 for (j = 0; j < 8; j++)
295 pargs->d2u[i * 8 + j] = code[j];
297 for (i = 0; i < 16; i++) {
300 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
301 errx(EX_DATAERR, "can't read u2d table row %d near line %d", i, line);
302 while (isspace((unsigned char)*s))
304 } while (*s == '\0');
305 if (sscanf(s, "%i%i%i%i%i%i%i%i",
306 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
307 errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %d", i, line);
308 for (j = 0; j < 8; j++)
309 pargs->u2d[i * 8 + j] = code[j];
316 load_ultable (pargs, name)
317 struct msdosfs_args *pargs;
322 if (setlocale(LC_CTYPE, name) == NULL)
323 err(EX_CONFIG, "%s", name);
324 for (i = 0; i < 128; i++) {
325 pargs->ul[i] = tolower(i | 0x80);
326 pargs->lu[i] = toupper(i | 0x80);