xref: /netbsd-src/sbin/mount_msdos/mount_msdos.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: mount_msdos.c,v 1.28 2003/05/03 15:37:08 christos 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.28 2003/05/03 15:37:08 christos 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 	{ NULL }
66 };
67 
68 int	main __P((int, char *[]));
69 int	mount_msdos __P((int argc, char **argv));
70 static void	usage __P((void));
71 
72 #ifndef MOUNT_NOMAIN
73 int
74 main(argc, argv)
75 	int argc;
76 	char **argv;
77 {
78 	return mount_msdos(argc, argv);
79 }
80 #endif
81 
82 int
83 mount_msdos(argc, argv)
84 	int argc;
85 	char **argv;
86 {
87 	struct msdosfs_args args;
88 	struct stat sb;
89 	int c, mntflags, set_gid, set_uid, set_mask;
90 	char *dev, *dir, ndir[MAXPATHLEN+1];
91 
92 	mntflags = set_gid = set_uid = set_mask = 0;
93 	(void)memset(&args, '\0', sizeof(args));
94 
95 	while ((c = getopt(argc, argv, "Gsl9u:g:m:o:")) != -1) {
96 		switch (c) {
97 		case 'G':
98 			args.flags |= MSDOSFSMNT_GEMDOSFS;
99 			break;
100 		case 's':
101 			args.flags |= MSDOSFSMNT_SHORTNAME;
102 			break;
103 		case 'l':
104 			args.flags |= MSDOSFSMNT_LONGNAME;
105 			break;
106 		case '9':
107 			args.flags |= MSDOSFSMNT_NOWIN95;
108 			break;
109 		case 'u':
110 			args.uid = a_uid(optarg);
111 			set_uid = 1;
112 			break;
113 		case 'g':
114 			args.gid = a_gid(optarg);
115 			set_gid = 1;
116 			break;
117 		case 'm':
118 			args.mask = a_mask(optarg);
119 			set_mask = 1;
120 			break;
121 		case 'o':
122 			getmntopts(optarg, mopts, &mntflags, 0);
123 			break;
124 		case '?':
125 		default:
126 			usage();
127 			break;
128 		}
129 	}
130 
131 	if (optind + 2 != argc)
132 		usage();
133 
134 	dev = argv[optind];
135 	dir = argv[optind + 1];
136 	if (dir[0] != '/') {
137 		warnx("\"%s\" is a relative path.", dir);
138 		if (getcwd(ndir, sizeof(ndir)) == NULL)
139 			err(1, "getcwd");
140 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
141 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
142 		dir = ndir;
143 		warnx("using \"%s\" instead.", dir);
144 	}
145 
146 	args.fspec = dev;
147 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
148 	if (mntflags & MNT_RDONLY)
149 		args.export.ex_flags = MNT_EXRDONLY;
150 	else
151 		args.export.ex_flags = 0;
152 	if (!set_gid || !set_uid || !set_mask) {
153 		if (stat(dir, &sb) == -1)
154 			err(1, "stat %s", dir);
155 
156 		if (!set_uid)
157 			args.uid = sb.st_uid;
158 		if (!set_gid)
159 			args.gid = sb.st_gid;
160 		if (!set_mask)
161 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
162 	}
163 
164 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
165 		err(1, "%s on %s", dev, dir);
166 
167 	if (mntflags & MNT_GETARGS) {
168 		char buf[1024];
169 		(void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
170 		printf("uid=%d, gid=%d, mask=0%o, flags=%s\n", args.uid,
171 		    args.gid, args.mask, buf);
172 	}
173 
174 	exit (0);
175 }
176 
177 static void
178 usage()
179 {
180 
181 	fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
182 	exit(1);
183 }
184