Home | History | Annotate | Line # | Download | only in mount_msdos
mount_msdos.c revision 1.32
      1 /* $NetBSD: mount_msdos.c,v 1.32 2003/09/08 07:21:59 wiz 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.32 2003/09/08 07:21:59 wiz 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 <time.h>
     55 #include <unistd.h>
     56 #include <util.h>
     57 
     58 #include <mntopts.h>
     59 #include <fattr.h>
     60 
     61 static const struct mntopt mopts[] = {
     62 	MOPT_STDOPTS,
     63 	MOPT_ASYNC,
     64 	MOPT_SYNC,
     65 	MOPT_UPDATE,
     66 	MOPT_GETARGS,
     67 	{ NULL }
     68 };
     69 
     70 int	main __P((int, char *[]));
     71 int	mount_msdos __P((int argc, char **argv));
     72 static void	usage __P((void)) __attribute__((__noreturn__));
     73 
     74 #ifndef MOUNT_NOMAIN
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char **argv;
     79 {
     80 	return mount_msdos(argc, argv);
     81 }
     82 #endif
     83 
     84 int
     85 mount_msdos(argc, argv)
     86 	int argc;
     87 	char **argv;
     88 {
     89 	struct msdosfs_args args;
     90 	struct stat sb;
     91 	int c, mntflags, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
     92 	char *dev, *dir, ndir[MAXPATHLEN+1];
     93 	time_t now;
     94 	struct tm *tm;
     95 
     96 	mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
     97 	(void)memset(&args, '\0', sizeof(args));
     98 
     99 	while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:t:")) != -1) {
    100 		switch (c) {
    101 		case 'G':
    102 			args.flags |= MSDOSFSMNT_GEMDOSFS;
    103 			break;
    104 		case 's':
    105 			args.flags |= MSDOSFSMNT_SHORTNAME;
    106 			break;
    107 		case 'l':
    108 			args.flags |= MSDOSFSMNT_LONGNAME;
    109 			break;
    110 		case '9':
    111 			args.flags |= MSDOSFSMNT_NOWIN95;
    112 			break;
    113 		case 'u':
    114 			args.uid = a_uid(optarg);
    115 			set_uid = 1;
    116 			break;
    117 		case 'g':
    118 			args.gid = a_gid(optarg);
    119 			set_gid = 1;
    120 			break;
    121 		case 'm':
    122 			args.mask = a_mask(optarg);
    123 			set_mask = 1;
    124 			break;
    125 		case 'M':
    126 			args.dirmask = a_mask(optarg);
    127 			set_dirmask = 1;
    128 			break;
    129 		case 'o':
    130 			getmntopts(optarg, mopts, &mntflags, 0);
    131 			break;
    132 		case 't':
    133 			args.gmtoff = atoi(optarg);
    134 			set_gmtoff = 1;
    135 			break;
    136 		case '?':
    137 		default:
    138 			usage();
    139 			break;
    140 		}
    141 	}
    142 
    143 	if (optind + 2 != argc)
    144 		usage();
    145 
    146 	if (set_mask && !set_dirmask) {
    147 		args.dirmask = args.mask;
    148 		set_dirmask = 1;
    149 	} else if (set_dirmask && !set_mask) {
    150 		args.mask = args.dirmask;
    151 		set_mask = 1;
    152 	}
    153 
    154 	dev = argv[optind];
    155 	dir = argv[optind + 1];
    156 	if (dir[0] != '/') {
    157 		warnx("\"%s\" is a relative path.", dir);
    158 		if (getcwd(ndir, sizeof(ndir)) == NULL)
    159 			err(1, "getcwd");
    160 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
    161 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
    162 		dir = ndir;
    163 		warnx("using \"%s\" instead.", dir);
    164 	}
    165 
    166 	args.fspec = dev;
    167 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
    168 	if (mntflags & MNT_RDONLY)
    169 		args.export.ex_flags = MNT_EXRDONLY;
    170 	else
    171 		args.export.ex_flags = 0;
    172 	if (!set_gid || !set_uid || !set_mask) {
    173 		if (stat(dir, &sb) == -1)
    174 			err(1, "stat %s", dir);
    175 
    176 		if (!set_uid)
    177 			args.uid = sb.st_uid;
    178 		if (!set_gid)
    179 			args.gid = sb.st_gid;
    180 		if (!set_mask) {
    181 			args.mask = args.dirmask =
    182 				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    183 		}
    184 	}
    185 
    186 	if (!set_gmtoff) {
    187 		/* use user's time zone as default */
    188 		time(&now);
    189 		tm = localtime(&now);
    190 		args.gmtoff = tm->tm_gmtoff;
    191 
    192 	}
    193 	args.flags |= MSDOSFSMNT_VERSIONED;
    194 	args.version = MSDOSFSMNT_VERSION;
    195 
    196 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
    197 		err(1, "%s on %s", dev, dir);
    198 
    199 	if (mntflags & MNT_GETARGS) {
    200 		char buf[1024];
    201 		(void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
    202 		printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
    203 		    args.uid, args.gid, args.mask, args.dirmask, buf);
    204 	}
    205 
    206 	exit (0);
    207 }
    208 
    209 static void
    210 usage()
    211 {
    212 
    213 	fprintf(stderr, "Usage:\nmount_msdos [-9Gls] [-g gid] [-M mask] [-m mask] [-o options]\n"
    214 			"\t[-t gmtoff] [-u uid] special node\n");
    215 	exit(1);
    216 }
    217