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>
41 #include <sys/iconv.h>
42 #include <sys/linker.h>
43 #include <sys/module.h>
45 #include <fs/msdosfs/msdosfsmount.h>
53 /* must be after stdio to declare fparseln */
62 #define TRANSITION_PERIOD_HACK
65 * XXX - no way to specify "foo=<bar>"-type options; that's what we'd
66 * want for "-u", "-g", "-m", "-M", "-L", "-D", and "-W".
68 static struct mntopt mopts[] = {
73 { "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 },
74 { "longnames", 0, MSDOSFSMNT_LONGNAME, 1 },
75 { "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 },
79 static gid_t a_gid(char *);
80 static uid_t a_uid(char *);
81 static mode_t a_mask(char *);
82 static void usage(void) __dead2;
83 static int set_charset(struct msdosfs_args *);
86 main(int argc, char **argv)
88 struct msdosfs_args args;
90 int c, mntflags, set_gid, set_uid, set_mask, set_dirmask;
91 char *dev, *dir, mntpath[MAXPATHLEN], *csp;
93 mntflags = set_gid = set_uid = set_mask = set_dirmask = 0;
94 (void)memset(&args, '\0', sizeof(args));
95 args.magic = MSDOSFS_ARGSMAGIC;
100 #ifdef TRANSITION_PERIOD_HACK
101 while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
103 while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:")) != -1) {
107 args.flags |= MSDOSFSMNT_SHORTNAME;
110 args.flags |= MSDOSFSMNT_LONGNAME;
113 args.flags |= MSDOSFSMNT_NOWIN95;
116 args.uid = a_uid(optarg);
120 args.gid = a_gid(optarg);
124 args.mask = a_mask(optarg);
128 args.dirmask = a_mask(optarg);
132 if (setlocale(LC_CTYPE, optarg) == NULL)
133 err(EX_CONFIG, "%s", optarg);
134 csp = strchr(optarg,'.');
136 err(EX_CONFIG, "%s", optarg);
137 args.cs_local = malloc(ICONV_CSNMAXLEN);
138 if (args.cs_local == NULL)
139 err(EX_OSERR, "malloc()");
140 strncpy(args.cs_local,
141 kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT),
145 args.cs_dos = malloc(ICONV_CSNMAXLEN);
146 if (args.cs_dos == NULL)
147 err(EX_OSERR, "malloc()");
148 strncpy(args.cs_dos, optarg, ICONV_CSNMAXLEN);
151 getmntopts(optarg, mopts, &mntflags, &args.flags);
153 #ifdef TRANSITION_PERIOD_HACK
155 args.cs_local = malloc(ICONV_CSNMAXLEN);
156 if (args.cs_local == NULL)
157 err(EX_OSERR, "malloc()");
158 args.cs_dos = malloc(ICONV_CSNMAXLEN);
159 if (args.cs_dos == NULL)
160 err(EX_OSERR, "malloc()");
161 if (strcmp(optarg, "iso22dos") == 0) {
162 strcpy(args.cs_local, "ISO8859-2");
163 strcpy(args.cs_dos, "CP852");
164 } else if (strcmp(optarg, "iso72dos") == 0) {
165 strcpy(args.cs_local, "ISO8859-7");
166 strcpy(args.cs_dos, "CP737");
167 } else if (strcmp(optarg, "koi2dos") == 0) {
168 strcpy(args.cs_local, "KOI8-R");
169 strcpy(args.cs_dos, "CP866");
170 } else if (strcmp(optarg, "koi8u2dos") == 0) {
171 strcpy(args.cs_local, "KOI8-U");
172 strcpy(args.cs_dos, "CP866");
174 err(EX_NOINPUT, "%s", optarg);
177 #endif /* TRANSITION_PERIOD_HACK */
185 if (optind + 2 != argc)
188 if (set_mask && !set_dirmask) {
189 args.dirmask = args.mask;
192 else if (set_dirmask && !set_mask) {
193 args.mask = args.dirmask;
198 dir = argv[optind + 1];
201 if (set_charset(&args) == -1)
202 err(EX_OSERR, "msdosfs_iconv");
203 args.flags |= MSDOSFSMNT_KICONV;
204 } else if (args.cs_dos) {
205 if ((args.cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
206 err(EX_OSERR, "malloc()");
207 strcpy(args.cs_local, "ISO8859-1");
208 if (set_charset(&args) == -1)
209 err(EX_OSERR, "msdosfs_iconv");
210 args.flags |= MSDOSFSMNT_KICONV;
214 * Resolve the mountpoint with realpath(3) and remove unnecessary
215 * slashes from the devicename if there are any.
217 (void)checkpath(dir, mntpath);
218 (void)rmslashes(dev, dev);
221 args.export.ex_root = -2; /* unchecked anyway on DOS fs */
222 if (mntflags & MNT_RDONLY)
223 args.export.ex_flags = MNT_EXRDONLY;
225 args.export.ex_flags = 0;
226 if (!set_gid || !set_uid || !set_mask) {
227 if (stat(mntpath, &sb) == -1)
228 err(EX_OSERR, "stat %s", mntpath);
231 args.uid = sb.st_uid;
233 args.gid = sb.st_gid;
235 args.mask = args.dirmask =
236 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
239 if (mount("msdosfs", mntpath, mntflags, &args) < 0)
240 err(EX_OSERR, "%s", dev);
253 if ((gr = getgrnam(s)) != NULL)
256 for (gname = s; *s && isdigit(*s); ++s);
260 errx(EX_NOUSER, "unknown group id: %s", gname);
273 if ((pw = getpwnam(s)) != NULL)
276 for (uname = s; *s && isdigit(*s); ++s);
280 errx(EX_NOUSER, "unknown user id: %s", uname);
294 if (*s >= '0' && *s <= '7') {
296 rv = strtol(optarg, &ep, 8);
298 if (!done || rv < 0 || *ep)
299 errx(EX_USAGE, "invalid file mode: %s", s);
306 #ifdef TRANSITION_PERIOD_HACK
307 fprintf(stderr, "%s\n%s\n%s\n",
308 "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
309 " [-M mask] [-m mask] [-o options] [-u uid]",
310 " [-W table] special node");
312 fprintf(stderr, "%s\n%s\n%s\n",
313 "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
314 " [-M mask] [-m mask] [-o options] [-u uid]",
321 set_charset(struct msdosfs_args *args)
325 if (modfind("msdosfs_iconv") < 0)
326 if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
327 warnx("cannot find or load \"msdosfs_iconv\" kernel module");
331 if ((args->cs_win = malloc(ICONV_CSNMAXLEN)) == NULL)
333 strncpy(args->cs_win, ENCODING_UNICODE, ICONV_CSNMAXLEN);
334 error = kiconv_add_xlat16_cspairs(args->cs_win, args->cs_local);
338 error = kiconv_add_xlat16_cspairs(args->cs_dos, args->cs_local);
342 if ((args->cs_dos = malloc(ICONV_CSNMAXLEN)) == NULL)
344 strcpy(args->cs_dos, args->cs_local);
345 error = kiconv_add_xlat16_cspair(args->cs_local, args->cs_local,
346 KICONV_FROM_UPPER | KICONV_LOWER);