mlx(4): Remove unused defines.
[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/param.h>
35 #include <sys/mount.h>
36 #include <sys/stat.h>
37 #include <vfs/hpfs/hpfsmount.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <grp.h>
41 #include <mntopts.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 static struct mntopt mopts[] = {
50         MOPT_STDOPTS,
51         MOPT_NULL
52 };
53
54 static gid_t    a_gid(char *);
55 static uid_t    a_uid(char *);
56 static mode_t   a_mask(char *);
57 static void     usage(void) __dead2;
58 #if 0
59 static void     load_u2wtable(struct hpfs_args *, char *);
60 #endif
61
62 int
63 main(int argc, char **argv)
64 {
65         struct hpfs_args args;
66         struct stat sb;
67         int c, mntflags, set_gid, set_uid, set_mask,error;
68         char *dev, *dir, ndir[MAXPATHLEN+1];
69         struct vfsconf vfc;
70
71         mntflags = set_gid = set_uid = set_mask = 0;
72         memset(&args, '\0', sizeof(args));
73
74         while ((c = getopt(argc, argv, "u:g:m:o:c:W:")) !=  -1) {
75                 switch (c) {
76                 case 'u':
77                         args.uid = a_uid(optarg);
78                         set_uid = 1;
79                         break;
80                 case 'g':
81                         args.gid = a_gid(optarg);
82                         set_gid = 1;
83                         break;
84                 case 'm':
85                         args.mode = a_mask(optarg);
86                         set_mask = 1;
87                         break;
88                 case 'o':
89                         getmntopts(optarg, mopts, &mntflags, 0);
90                         break;
91 #if 0
92                 case 'W':
93                         load_u2wtable(&args, optarg);
94                         args.flags |= HPFSMNT_TABLES;
95                         break;
96 #endif
97                 case '?':
98                 default:
99                         usage();
100                         break;
101                 }
102         }
103
104         if (optind + 2 != argc)
105                 usage();
106
107         mntflags |= MNT_RDONLY;
108
109         dev = argv[optind];
110         dir = argv[optind + 1];
111         if (dir[0] != '/') {
112                 warnx("\"%s\" is a relative path", dir);
113                 if (getcwd(ndir, sizeof(ndir)) == NULL)
114                         err(EX_OSERR, "getcwd");
115                 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
116                 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
117                 dir = ndir;
118                 warnx("using \"%s\" instead", dir);
119         }
120
121         args.fspec = dev;
122         args.export.ex_root = 65534;    /* unchecked anyway on DOS fs */
123         if (mntflags & MNT_RDONLY)
124                 args.export.ex_flags = MNT_EXRDONLY;
125         else
126                 args.export.ex_flags = 0;
127
128         if (!set_gid || !set_uid || !set_mask) {
129                 if (stat(dir, &sb) == -1)
130                         err(EX_OSERR, "stat %s", dir);
131
132                 if (!set_uid)
133                         args.uid = sb.st_uid;
134                 if (!set_gid)
135                         args.gid = sb.st_gid;
136                 if (!set_mask)
137                         args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
138         }
139
140         error = getvfsbyname("hpfs", &vfc);
141         if(error && vfsisloadable("hpfs")) {
142                 if(vfsload("hpfs"))
143                         err(EX_OSERR, "vfsload(hpfs)");
144                 endvfsent();    /* clear cache */
145                 error = getvfsbyname("hpfs", &vfc);
146         }
147         if (error)
148                 errx(EX_OSERR, "hpfs filesystem is not available");
149
150         if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
151                 err(EX_OSERR, "%s", dev);
152
153         exit (0);
154 }
155
156 gid_t
157 a_gid(char *s)
158 {
159         struct group *gr;
160         char *gname;
161         gid_t gid;
162
163         if ((gr = getgrnam(s)) != NULL)
164                 gid = gr->gr_gid;
165         else {
166                 for (gname = s; *s && isdigit(*s); ++s);
167                 if (!*s)
168                         gid = atoi(gname);
169                 else
170                         errx(EX_NOUSER, "unknown group id: %s", gname);
171         }
172         return (gid);
173 }
174
175 uid_t
176 a_uid(char *s)
177 {
178         struct passwd *pw;
179         char *uname;
180         uid_t uid;
181
182         if ((pw = getpwnam(s)) != NULL)
183                 uid = pw->pw_uid;
184         else {
185                 for (uname = s; *s && isdigit(*s); ++s);
186                 if (!*s)
187                         uid = atoi(uname);
188                 else
189                         errx(EX_NOUSER, "unknown user id: %s", uname);
190         }
191         return (uid);
192 }
193
194 mode_t
195 a_mask(char *s)
196 {
197         int done, rv=0;
198         char *ep;
199
200         done = 0;
201         if (*s >= '0' && *s <= '7') {
202                 done = 1;
203                 rv = strtol(optarg, &ep, 8);
204         }
205         if (!done || rv < 0 || *ep)
206                 errx(EX_USAGE, "invalid file mode: %s", s);
207         return (rv);
208 }
209
210 void
211 usage(void)
212 {
213         fprintf(stderr, "usage: mount_hpfs [-u user] [-g group] [-m mask] bdev dir\n");
214         exit(EX_USAGE);
215 }
216
217 #if 0
218 void
219 load_u2wtable (struct hpfs_args *pargs, char *name)
220 {
221         FILE *f;
222         int i, code;
223         char buf[128];
224         char *fn;
225
226         if (*name == '/')
227                 fn = name;
228         else {
229                 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
230                 buf[127] = '\0';
231                 fn = buf;
232         }
233         if ((f = fopen(fn, "r")) == NULL)
234                 err(EX_NOINPUT, "%s", fn);
235         for (i = 0; i < 128; i++) {
236                 if (fscanf(f, "%i", &code) != 1)
237                         errx(EX_DATAERR, "u2w: missing item number %d", i);
238                 /* pargs->u2w[i] = code; */
239         }
240         for (i = 0; i < 128; i++) {
241                 if (fscanf(f, "%i", &code) != 1)
242                         errx(EX_DATAERR, "d2u: missing item number %d", i);
243                 pargs->d2u[i] = code;
244         }
245         for (i = 0; i < 128; i++) {
246                 if (fscanf(f, "%i", &code) != 1)
247                         errx(EX_DATAERR, "u2d: missing item number %d", i);
248                 pargs->u2d[i] = code;
249         }
250         fclose(f);
251 }
252 #endif