mount_msdos.c revision 1.12 1 /* $NetBSD: mount_msdos.c,v 1.12 1996/01/19 21:14:46 leo 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 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
20 *
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.
31 */
32
33 #ifndef lint
34 static char rcsid[] = "$NetBSD: mount_msdos.c,v 1.12 1996/01/19 21:14:46 leo Exp $";
35 #endif /* not lint */
36
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <ctype.h>
42 #include <err.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include "mntopts.h"
51
52 struct mntopt mopts[] = {
53 MOPT_STDOPTS,
54 { NULL }
55 };
56
57 gid_t a_gid __P((char *));
58 uid_t a_uid __P((char *));
59 mode_t a_mask __P((char *));
60 void usage __P((void));
61
62 int
63 main(argc, argv)
64 int argc;
65 char **argv;
66 {
67 struct msdosfs_args args;
68 struct stat sb;
69 int c, mntflags, set_gid, set_uid, set_mask;
70 char *dev, *dir, ndir[MAXPATHLEN+1];
71
72 mntflags = set_gid = set_uid = set_mask = 0;
73 (void)memset(&args, '\0', sizeof(args));
74
75 while ((c = getopt(argc, argv, "Gsl9u:g:m:o:")) != EOF) {
76 switch (c) {
77 case 'G':
78 args.flags |= MSDOSFSMNT_GEMDOSFS;
79 break;
80 case 's':
81 args.flags |= MSDOSFSMNT_SHORTNAME;
82 break;
83 case 'l':
84 args.flags |= MSDOSFSMNT_LONGNAME;
85 break;
86 case '9':
87 args.flags |= MSDOSFSMNT_NOWIN95;
88 break;
89 case 'u':
90 args.uid = a_uid(optarg);
91 set_uid = 1;
92 break;
93 case 'g':
94 args.gid = a_gid(optarg);
95 set_gid = 1;
96 break;
97 case 'm':
98 args.mask = a_mask(optarg);
99 set_mask = 1;
100 break;
101 case 'o':
102 getmntopts(optarg, mopts, &mntflags);
103 break;
104 case '?':
105 default:
106 usage();
107 break;
108 }
109 }
110
111 if (optind + 2 != argc)
112 usage();
113
114 dev = argv[optind];
115 dir = argv[optind + 1];
116 if (dir[0] != '/') {
117 warnx("\"%s\" is a relative path.", dir);
118 if (getcwd(ndir, sizeof(ndir)) == NULL)
119 err(1, "getcwd");
120 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
121 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
122 dir = ndir;
123 warnx("using \"%s\" instead.", dir);
124 }
125
126 args.fspec = dev;
127 args.export.ex_root = -2; /* unchecked anyway on DOS fs */
128 if (mntflags & MNT_RDONLY)
129 args.export.ex_flags = MNT_EXRDONLY;
130 else
131 args.export.ex_flags = 0;
132 if (!set_gid || !set_uid || !set_mask) {
133 if (stat(dir, &sb) == -1)
134 err(1, "stat %s", dir);
135
136 if (!set_uid)
137 args.uid = sb.st_uid;
138 if (!set_gid)
139 args.gid = sb.st_gid;
140 if (!set_mask)
141 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
142 }
143
144 if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
145 err(1, "mount");
146
147 exit (0);
148 }
149
150 gid_t
151 a_gid(s)
152 char *s;
153 {
154 struct group *gr;
155 char *gname;
156 gid_t gid;
157
158 if ((gr = getgrnam(s)) != NULL)
159 gid = gr->gr_gid;
160 else {
161 for (gname = s; *s && isdigit(*s); ++s);
162 if (!*s)
163 gid = atoi(gname);
164 else
165 errx(1, "unknown group id: %s", gname);
166 }
167 return (gid);
168 }
169
170 uid_t
171 a_uid(s)
172 char *s;
173 {
174 struct passwd *pw;
175 char *uname;
176 uid_t uid;
177
178 if ((pw = getpwnam(s)) != NULL)
179 uid = pw->pw_uid;
180 else {
181 for (uname = s; *s && isdigit(*s); ++s);
182 if (!*s)
183 uid = atoi(uname);
184 else
185 errx(1, "unknown user id: %s", uname);
186 }
187 return (uid);
188 }
189
190 mode_t
191 a_mask(s)
192 char *s;
193 {
194 int done, rv;
195 char *ep;
196
197 done = 0;
198 if (*s >= '0' && *s <= '7') {
199 done = 1;
200 rv = strtol(optarg, &ep, 8);
201 }
202 if (!done || rv < 0 || *ep)
203 errx(1, "invalid file mode: %s", s);
204 return (rv);
205 }
206
207 void
208 usage()
209 {
210
211 fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
212 exit(1);
213 }
214