Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sbin / mount_hpfs / mount_hpfs.c
1 /*
2  * Copyright (c) 1994 Christopher G. Demetriou
3  * Copyright (c) 1999 Semen Ustimenko (semenu@FreeBSD.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sbin/mount_hpfs/mount_hpfs.c,v 1.1 1999/12/09 19:09:15 semenu Exp $
32  */
33
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <fs/hpfs/hpfsmount.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <grp.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48
49 #include "mntopts.h"
50
51 static struct mntopt mopts[] = {
52         MOPT_STDOPTS,
53         { NULL }
54 };
55
56 static gid_t    a_gid __P((char *));
57 static uid_t    a_uid __P((char *));
58 static mode_t   a_mask __P((char *));
59 static void     usage __P((void)) __dead2;
60 static void     load_u2wtable __P((struct hpfs_args *, char *));
61
62 int
63 main(argc, argv)
64         int argc;
65         char **argv;
66 {
67         struct hpfs_args args;
68         struct stat sb;
69         int c, mntflags, set_gid, set_uid, set_mask,error;
70         int forcerw = 0;
71         char *dev, *dir, ndir[MAXPATHLEN+1];
72 #if __FreeBSD_version >= 300000
73         struct vfsconf vfc;
74 #else
75         struct vfsconf *vfc;
76 #endif
77
78         mntflags = set_gid = set_uid = set_mask = 0;
79         (void)memset(&args, '\0', sizeof(args));
80
81         while ((c = getopt(argc, argv, "u:g:m:o:c:W:F")) !=  -1) {
82                 switch (c) {
83                 case 'F':
84                         forcerw=1;
85                         break;
86                 case 'u':
87                         args.uid = a_uid(optarg);
88                         set_uid = 1;
89                         break;
90                 case 'g':
91                         args.gid = a_gid(optarg);
92                         set_gid = 1;
93                         break;
94                 case 'm':
95                         args.mode = a_mask(optarg);
96                         set_mask = 1;
97                         break;
98                 case 'o':
99                         getmntopts(optarg, mopts, &mntflags, 0);
100                         break;
101                 case 'W':
102                         load_u2wtable(&args, optarg);
103                         args.flags |= HPFSMNT_TABLES;
104                         break;
105                 case '?':
106                 default:
107                         usage();
108                         break;
109                 }
110         }
111
112         if (optind + 2 != argc)
113                 usage();
114
115         if (!(mntflags & MNT_RDONLY) && !forcerw) {
116                 warnx("Write support is BETA, you need -F flag to enable RW mount!");
117                 exit (111);
118         }
119
120         dev = argv[optind];
121         dir = argv[optind + 1];
122         if (dir[0] != '/') {
123                 warnx("\"%s\" is a relative path", dir);
124                 if (getcwd(ndir, sizeof(ndir)) == NULL)
125                         err(EX_OSERR, "getcwd");
126                 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
127                 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
128                 dir = ndir;
129                 warnx("using \"%s\" instead", dir);
130         }
131
132         args.fspec = dev;
133         args.export.ex_root = 65534;    /* unchecked anyway on DOS fs */
134         if (mntflags & MNT_RDONLY)
135                 args.export.ex_flags = MNT_EXRDONLY;
136         else
137                 args.export.ex_flags = 0;
138
139         if (!set_gid || !set_uid || !set_mask) {
140                 if (stat(dir, &sb) == -1)
141                         err(EX_OSERR, "stat %s", dir);
142
143                 if (!set_uid)
144                         args.uid = sb.st_uid;
145                 if (!set_gid)
146                         args.gid = sb.st_gid;
147                 if (!set_mask)
148                         args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
149         }
150
151 #if __FreeBSD_version >= 300000
152         error = getvfsbyname("hpfs", &vfc);
153         if(error && vfsisloadable("hpfs")) {
154                 if(vfsload("hpfs"))
155 #else
156         vfc = getvfsbyname("hpfs");
157         if(!vfc && vfsisloadable("hpfs")) {
158                 if(vfsload("hpfs"))
159 #endif
160                         err(EX_OSERR, "vfsload(hpfs)");
161                 endvfsent();    /* clear cache */
162 #if __FreeBSD_version >= 300000
163                 error = getvfsbyname("hpfs", &vfc);
164 #else
165                 vfc = getvfsbyname("hpfs");
166 #endif
167         }
168 #if __FreeBSD_version >= 300000
169         if (error)
170 #else
171         if (!vfc)
172 #endif
173                 errx(EX_OSERR, "hpfs filesystem is not available");
174
175 #if __FreeBSD_version >= 300000
176         if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
177 #else
178         if (mount(vfc->vfc_index, dir, mntflags, &args) < 0)
179 #endif
180                 err(EX_OSERR, "%s", dev);
181
182         exit (0);
183 }
184
185 gid_t
186 a_gid(s)
187         char *s;
188 {
189         struct group *gr;
190         char *gname;
191         gid_t gid;
192
193         if ((gr = getgrnam(s)) != NULL)
194                 gid = gr->gr_gid;
195         else {
196                 for (gname = s; *s && isdigit(*s); ++s);
197                 if (!*s)
198                         gid = atoi(gname);
199                 else
200                         errx(EX_NOUSER, "unknown group id: %s", gname);
201         }
202         return (gid);
203 }
204
205 uid_t
206 a_uid(s)
207         char *s;
208 {
209         struct passwd *pw;
210         char *uname;
211         uid_t uid;
212
213         if ((pw = getpwnam(s)) != NULL)
214                 uid = pw->pw_uid;
215         else {
216                 for (uname = s; *s && isdigit(*s); ++s);
217                 if (!*s)
218                         uid = atoi(uname);
219                 else
220                         errx(EX_NOUSER, "unknown user id: %s", uname);
221         }
222         return (uid);
223 }
224
225 mode_t
226 a_mask(s)
227         char *s;
228 {
229         int done, rv=0;
230         char *ep;
231
232         done = 0;
233         if (*s >= '0' && *s <= '7') {
234                 done = 1;
235                 rv = strtol(optarg, &ep, 8);
236         }
237         if (!done || rv < 0 || *ep)
238                 errx(EX_USAGE, "invalid file mode: %s", s);
239         return (rv);
240 }
241
242 void
243 usage()
244 {
245         fprintf(stderr, "usage: mount_hpfs [-u user] [-g group] [-m mask] bdev dir\n");
246         exit(EX_USAGE);
247 }
248
249 void
250 load_u2wtable (pargs, name)
251         struct hpfs_args *pargs;
252         char *name;
253 {
254         FILE *f;
255         int i, code;
256         char buf[128];
257         char *fn;
258
259         if (*name == '/')
260                 fn = name;
261         else {
262                 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
263                 buf[127] = '\0';
264                 fn = buf;
265         }
266         if ((f = fopen(fn, "r")) == NULL)
267                 err(EX_NOINPUT, "%s", fn);
268         for (i = 0; i < 128; i++) {
269                 if (fscanf(f, "%i", &code) != 1)
270                         errx(EX_DATAERR, "u2w: missing item number %d", i);
271                 /* pargs->u2w[i] = code; */
272         }
273         for (i = 0; i < 128; i++) {
274                 if (fscanf(f, "%i", &code) != 1)
275                         errx(EX_DATAERR, "d2u: missing item number %d", i);
276                 pargs->d2u[i] = code;
277         }
278         for (i = 0; i < 128; i++) {
279                 if (fscanf(f, "%i", &code) != 1)
280                         errx(EX_DATAERR, "u2d: missing item number %d", i);
281                 pargs->u2d[i] = code;
282         }
283         fclose(f);
284 }