xref: /netbsd-src/sbin/mount_msdos/mount_msdos.c (revision 76043b45b14f636672a2827af50577828064fde4)
1*76043b45Sjdolecek /* $NetBSD: mount_msdos.c,v 1.50 2016/08/21 22:27:20 jdolecek Exp $ */
20114e805Scgd 
3ae77b4fbScgd /*
4d464e461Scgd  * Copyright (c) 1994 Christopher G. Demetriou
5d464e461Scgd  * All rights reserved.
6d464e461Scgd  *
7d464e461Scgd  * Redistribution and use in source and binary forms, with or without
8d464e461Scgd  * modification, are permitted provided that the following conditions
9d464e461Scgd  * are met:
10d464e461Scgd  * 1. Redistributions of source code must retain the above copyright
11d464e461Scgd  *    notice, this list of conditions and the following disclaimer.
12d464e461Scgd  * 2. Redistributions in binary form must reproduce the above copyright
13d464e461Scgd  *    notice, this list of conditions and the following disclaimer in the
14d464e461Scgd  *    documentation and/or other materials provided with the distribution.
15d464e461Scgd  * 3. All advertising materials mentioning features or use of this software
16d464e461Scgd  *    must display the following acknowledgement:
17db755e7cScgd  *          This product includes software developed for the
18c39c2e62Sgrant  *          NetBSD Project.  See http://www.NetBSD.org/ for
19db755e7cScgd  *          information about NetBSD.
20d464e461Scgd  * 4. The name of the author may not be used to endorse or promote products
21db755e7cScgd  *    derived from this software without specific prior written permission.
22d464e461Scgd  *
23d464e461Scgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24d464e461Scgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25d464e461Scgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26d464e461Scgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27d464e461Scgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28d464e461Scgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29d464e461Scgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30d464e461Scgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31d464e461Scgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32d464e461Scgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33db755e7cScgd  *
34db755e7cScgd  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35ae77b4fbScgd  */
36aee4b07bSmycroft 
37f46ca99bSlukem #include <sys/cdefs.h>
38aee4b07bSmycroft #ifndef lint
39*76043b45Sjdolecek __RCSID("$NetBSD: mount_msdos.c,v 1.50 2016/08/21 22:27:20 jdolecek Exp $");
40aee4b07bSmycroft #endif /* not lint */
41aee4b07bSmycroft 
421717de7cScgd #include <sys/param.h>
43ae77b4fbScgd #include <sys/mount.h>
448636037fScgd #include <sys/stat.h>
45e5bc90f4Sfvdl #include <msdosfs/msdosfsmount.h>
468636037fScgd #include <err.h>
478636037fScgd #include <grp.h>
488636037fScgd #include <pwd.h>
49d464e461Scgd #include <stdio.h>
50d464e461Scgd #include <stdlib.h>
51d464e461Scgd #include <string.h>
52ce112dfcSitojun #include <time.h>
531717de7cScgd #include <unistd.h>
549f9ee297Schristos #include <util.h>
55ae77b4fbScgd 
56899bce09Sjdolecek #include <mntopts.h>
5767f5c87fScgd 
5899fed726Spooka #include "mountprog.h"
598188a289Spooka #include "mount_msdos.h"
608188a289Spooka 
61debb6d80Sjdolecek static const struct mntopt mopts[] = {
6267f5c87fScgd 	MOPT_STDOPTS,
631aec36e4Sjdolecek 	MOPT_ASYNC,
641aec36e4Sjdolecek 	MOPT_SYNC,
65c35f9138Scgd 	MOPT_UPDATE,
6628dd2289Sjdolecek 	MOPT_GETARGS,
675afd1c0aSchristos 	MOPT_NULL,
6867f5c87fScgd };
6967f5c87fScgd 
708b0f9554Sperry static void	usage(void) __dead;
71ae77b4fbScgd 
72debb6d80Sjdolecek #ifndef MOUNT_NOMAIN
73ae77b4fbScgd int
main(int argc,char ** argv)7460fef079Sxtraeme main(int argc, char **argv)
75ae77b4fbScgd {
768188a289Spooka 
778188a289Spooka 	setprogname(argv[0]);
78debb6d80Sjdolecek 	return mount_msdos(argc, argv);
79debb6d80Sjdolecek }
80debb6d80Sjdolecek #endif
81debb6d80Sjdolecek 
828188a289Spooka void
mount_msdos_parseargs(int argc,char ** argv,struct msdosfs_args * args,int * mntflags,char * canon_dev,char * canon_dir)838188a289Spooka mount_msdos_parseargs(int argc, char **argv,
848188a289Spooka 	struct msdosfs_args *args, int *mntflags,
858188a289Spooka 	char *canon_dev, char *canon_dir)
86debb6d80Sjdolecek {
878636037fScgd 	struct stat sb;
888188a289Spooka 	int c, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
898188a289Spooka 	char *dev, *dir;
90ce112dfcSitojun 	time_t now;
91ce112dfcSitojun 	struct tm *tm;
927067dcb9Schristos 	mntoptparse_t mp;
93ae77b4fbScgd 
948188a289Spooka 	*mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
958188a289Spooka 	(void)memset(args, '\0', sizeof(*args));
96ae77b4fbScgd 
97223c7df5Smlelstv 	while ((c = getopt(argc, argv, "Gsl9Uu:g:m:M:o:t:")) != -1) {
98ae77b4fbScgd 		switch (c) {
9985d9dba2Sleo 		case 'G':
1008188a289Spooka 			args->flags |= MSDOSFSMNT_GEMDOSFS;
10185d9dba2Sleo 			break;
102a96f4bc2Sws 		case 's':
1038188a289Spooka 			args->flags |= MSDOSFSMNT_SHORTNAME;
104a96f4bc2Sws 			break;
105a96f4bc2Sws 		case 'l':
1068188a289Spooka 			args->flags |= MSDOSFSMNT_LONGNAME;
107a96f4bc2Sws 			break;
108a96f4bc2Sws 		case '9':
1098188a289Spooka 			args->flags |= MSDOSFSMNT_NOWIN95;
110a96f4bc2Sws 			break;
111223c7df5Smlelstv 		case 'U':
112223c7df5Smlelstv 			args->flags |= MSDOSFSMNT_UTF8;
113223c7df5Smlelstv 			break;
1148636037fScgd 		case 'u':
1158188a289Spooka 			args->uid = a_uid(optarg);
1168636037fScgd 			set_uid = 1;
1178636037fScgd 			break;
1188636037fScgd 		case 'g':
1198188a289Spooka 			args->gid = a_gid(optarg);
1208636037fScgd 			set_gid = 1;
1218636037fScgd 			break;
1228636037fScgd 		case 'm':
1238188a289Spooka 			args->mask = a_mask(optarg);
1248636037fScgd 			set_mask = 1;
1258636037fScgd 			break;
12662e0ed44Sjdolecek 		case 'M':
1278188a289Spooka 			args->dirmask = a_mask(optarg);
12862e0ed44Sjdolecek 			set_dirmask = 1;
12962e0ed44Sjdolecek 			break;
13067f5c87fScgd 		case 'o':
1318188a289Spooka 			mp = getmntopts(optarg, mopts, mntflags, 0);
1327067dcb9Schristos 			if (mp == NULL)
1337067dcb9Schristos 				err(1, "getmntopts");
1347067dcb9Schristos 			freemntopts(mp);
13567f5c87fScgd 			break;
136ce112dfcSitojun 		case 't':
1378188a289Spooka 			args->gmtoff = atoi(optarg);
138ce112dfcSitojun 			set_gmtoff = 1;
139ce112dfcSitojun 			break;
1408636037fScgd 		case '?':
141ae77b4fbScgd 		default:
142ae77b4fbScgd 			usage();
1438636037fScgd 			break;
144ae77b4fbScgd 		}
145ae77b4fbScgd 	}
146ae77b4fbScgd 
147ae77b4fbScgd 	if (optind + 2 != argc)
148ae77b4fbScgd 		usage();
149ae77b4fbScgd 
15062e0ed44Sjdolecek 	if (set_mask && !set_dirmask) {
1518188a289Spooka 		args->dirmask = args->mask;
15262e0ed44Sjdolecek 		set_dirmask = 1;
15362e0ed44Sjdolecek 	} else if (set_dirmask && !set_mask) {
1548188a289Spooka 		args->mask = args->dirmask;
15562e0ed44Sjdolecek 		set_mask = 1;
15662e0ed44Sjdolecek 	}
15762e0ed44Sjdolecek 
158ae77b4fbScgd 	dev = argv[optind];
159ae77b4fbScgd 	dir = argv[optind + 1];
160fdd16c54Serh 
16199fed726Spooka 	pathadj(dev, canon_dev);
16299fed726Spooka 	pathadj(dir, canon_dir);
163ae77b4fbScgd 
16496c1f485Spooka 	args->fspec = canon_dev;
1658636037fScgd 	if (!set_gid || !set_uid || !set_mask) {
1668636037fScgd 		if (stat(dir, &sb) == -1)
1678636037fScgd 			err(1, "stat %s", dir);
168ae77b4fbScgd 
1698636037fScgd 		if (!set_uid)
1708188a289Spooka 			args->uid = sb.st_uid;
1718636037fScgd 		if (!set_gid)
1728188a289Spooka 			args->gid = sb.st_gid;
17362e0ed44Sjdolecek 		if (!set_mask) {
1748188a289Spooka 			args->mask = args->dirmask =
17562e0ed44Sjdolecek 				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
176ae77b4fbScgd 		}
17762e0ed44Sjdolecek 	}
178ce112dfcSitojun 
179ce112dfcSitojun 	if (!set_gmtoff) {
180ce112dfcSitojun 		/* use user's time zone as default */
181ce112dfcSitojun 		time(&now);
182ce112dfcSitojun 		tm = localtime(&now);
1838188a289Spooka 		args->gmtoff = tm->tm_gmtoff;
184ce112dfcSitojun 
185ce112dfcSitojun 	}
1868188a289Spooka 	args->flags |= MSDOSFSMNT_VERSIONED;
1878188a289Spooka 	args->version = MSDOSFSMNT_VERSION;
1888188a289Spooka }
189ae77b4fbScgd 
1908188a289Spooka int
mount_msdos(int argc,char ** argv)1918188a289Spooka mount_msdos(int argc, char **argv)
1928188a289Spooka {
1938188a289Spooka 	struct msdosfs_args args;
1948188a289Spooka 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
1958188a289Spooka 	int mntflags;
1968188a289Spooka 
1978188a289Spooka 	mount_msdos_parseargs(argc, argv, &args, &mntflags,
1988188a289Spooka 	    canon_dev, canon_dir);
1998188a289Spooka 
2008188a289Spooka 	if (mount(MOUNT_MSDOS, canon_dir, mntflags, &args, sizeof args) == -1)
2018188a289Spooka 		err(1, "%s on %s", canon_dev, canon_dir);
2028636037fScgd 
2039f9ee297Schristos 	if (mntflags & MNT_GETARGS) {
2049f9ee297Schristos 		char buf[1024];
2059f9ee297Schristos 		(void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
206*76043b45Sjdolecek 		printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, gmtoff=%d, flags=%s\n",
207*76043b45Sjdolecek 		    args.uid, args.gid, args.mask, args.dirmask,
208*76043b45Sjdolecek 		    args.gmtoff, buf);
2099f9ee297Schristos 	}
2109f9ee297Schristos 
211ae77b4fbScgd 	exit (0);
212ae77b4fbScgd }
2138636037fScgd 
214debb6d80Sjdolecek static void
usage(void)21560fef079Sxtraeme usage(void)
2168636037fScgd {
217998e3b5dSmycroft 
218c1535033Smlelstv 	fprintf(stderr, "usage: %s [-9GlsU] [-g gid] [-M mask] [-m mask] "
2198188a289Spooka 	    "[-o options]\n\t[-t gmtoff] [-u uid] special mountpath\n",
2208188a289Spooka 	    getprogname());
2218636037fScgd 	exit(1);
2228636037fScgd }
223