mount_msdos.c revision 1.30 1 /* $NetBSD: mount_msdos.c,v 1.30 2003/08/02 11:42:20 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 1994 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: mount_msdos.c,v 1.30 2003/08/02 11:42:20 jdolecek Exp $");
40 #endif /* not lint */
41
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/stat.h>
46 #include <msdosfs/msdosfsmount.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <grp.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <util.h>
56
57 #include <mntopts.h>
58 #include <fattr.h>
59
60 static const struct mntopt mopts[] = {
61 MOPT_STDOPTS,
62 MOPT_ASYNC,
63 MOPT_SYNC,
64 MOPT_UPDATE,
65 MOPT_GETARGS,
66 { NULL }
67 };
68
69 int main __P((int, char *[]));
70 int mount_msdos __P((int argc, char **argv));
71 static void usage __P((void)) __attribute__((__noreturn__));
72
73 #ifndef MOUNT_NOMAIN
74 int
75 main(argc, argv)
76 int argc;
77 char **argv;
78 {
79 return mount_msdos(argc, argv);
80 }
81 #endif
82
83 int
84 mount_msdos(argc, argv)
85 int argc;
86 char **argv;
87 {
88 struct msdosfs_args args;
89 struct stat sb;
90 int c, mntflags, set_gid, set_uid, set_mask, set_dirmask;
91 char *dev, *dir, ndir[MAXPATHLEN+1];
92
93 mntflags = set_gid = set_uid = set_mask = set_dirmask = 0;
94 (void)memset(&args, '\0', sizeof(args));
95
96 while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:")) != -1) {
97 switch (c) {
98 case 'G':
99 args.flags |= MSDOSFSMNT_GEMDOSFS;
100 break;
101 case 's':
102 args.flags |= MSDOSFSMNT_SHORTNAME;
103 break;
104 case 'l':
105 args.flags |= MSDOSFSMNT_LONGNAME;
106 break;
107 case '9':
108 args.flags |= MSDOSFSMNT_NOWIN95;
109 break;
110 case 'u':
111 args.uid = a_uid(optarg);
112 set_uid = 1;
113 break;
114 case 'g':
115 args.gid = a_gid(optarg);
116 set_gid = 1;
117 break;
118 case 'm':
119 args.mask = a_mask(optarg);
120 set_mask = 1;
121 break;
122 case 'M':
123 args.dirmask = a_mask(optarg);
124 set_dirmask = 1;
125 break;
126 case 'o':
127 getmntopts(optarg, mopts, &mntflags, 0);
128 break;
129 case '?':
130 default:
131 usage();
132 break;
133 }
134 }
135
136 if (optind + 2 != argc)
137 usage();
138
139 if (set_mask && !set_dirmask) {
140 args.dirmask = args.mask;
141 set_dirmask = 1;
142 } else if (set_dirmask && !set_mask) {
143 args.mask = args.dirmask;
144 set_mask = 1;
145 }
146
147 dev = argv[optind];
148 dir = argv[optind + 1];
149 if (dir[0] != '/') {
150 warnx("\"%s\" is a relative path.", dir);
151 if (getcwd(ndir, sizeof(ndir)) == NULL)
152 err(1, "getcwd");
153 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
154 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
155 dir = ndir;
156 warnx("using \"%s\" instead.", dir);
157 }
158
159 args.fspec = dev;
160 args.export.ex_root = -2; /* unchecked anyway on DOS fs */
161 if (mntflags & MNT_RDONLY)
162 args.export.ex_flags = MNT_EXRDONLY;
163 else
164 args.export.ex_flags = 0;
165 if (!set_gid || !set_uid || !set_mask) {
166 if (stat(dir, &sb) == -1)
167 err(1, "stat %s", dir);
168
169 if (!set_uid)
170 args.uid = sb.st_uid;
171 if (!set_gid)
172 args.gid = sb.st_gid;
173 if (!set_mask) {
174 args.mask = args.dirmask =
175 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
176 }
177 }
178 args.flags |= MSDOSFSMNT_VERSIONED;
179 args.version = MSDOSFSMNT_VERSION;
180
181 if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
182 err(1, "%s on %s", dev, dir);
183
184 if (mntflags & MNT_GETARGS) {
185 char buf[1024];
186 (void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
187 printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
188 args.uid, args.gid, args.mask, args.dirmask, buf);
189 }
190
191 exit (0);
192 }
193
194 static void
195 usage()
196 {
197
198 fprintf(stderr, "Usage:\nmount_msdos [-o options] [-u user] [-g group] [-m mask] [-M mask] [-G] bdev dir\n");
199 exit(1);
200 }
201