Home | History | Annotate | Line # | Download | only in mount_msdos
mount_msdos.c revision 1.6
      1 /*
      2  * Copyright (c) 1994 Christopher G. Demetriou
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *      This product includes software developed by Christopher G. Demetriou.
     16  * 4. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef lint
     32 static char rcsid[] = "$Id: mount_msdos.c,v 1.6 1994/04/08 01:27:02 cgd Exp $";
     33 #endif /* not lint */
     34 
     35 #include <sys/cdefs.h>
     36 #include <sys/types.h>
     37 #include <sys/mount.h>
     38 #include <sys/stat.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 
     47 gid_t	a_gid __P((char *));
     48 uid_t	a_uid __P((char *));
     49 mode_t	a_mask __P((char *));
     50 void	usage __P((void));
     51 
     52 int
     53 main(argc, argv)
     54 	int argc;
     55 	char **argv;
     56 {
     57 	struct msdosfs_args args;
     58 	struct stat sb;
     59 	int c, opts, set_gid, set_uid, set_mask;
     60 	char *dev, *dir;
     61 
     62 	opts = set_gid = set_uid = set_mask = 0;
     63 	(void)memset(&args, '\0', sizeof(args));
     64 
     65 	while ((c = getopt(argc, argv, "F:u:g:m:")) != EOF) {
     66 		switch (c) {
     67 		case 'F':
     68 			opts |= atoi(optarg);
     69 			break;
     70 		case 'u':
     71 			args.uid = a_uid(optarg);
     72 			set_uid = 1;
     73 			break;
     74 		case 'g':
     75 			args.gid = a_gid(optarg);
     76 			set_gid = 1;
     77 			break;
     78 		case 'm':
     79 			args.mask = a_mask(optarg);
     80 			set_mask = 1;
     81 			break;
     82 		case '?':
     83 		default:
     84 			usage();
     85 			break;
     86 		}
     87 	}
     88 
     89 	if (optind + 2 != argc)
     90 		usage();
     91 
     92 	dev = argv[optind];
     93 	dir = argv[optind + 1];
     94 
     95 	args.fspec = dev;
     96 	if (!set_gid || !set_uid || !set_mask) {
     97 		if (stat(dir, &sb) == -1)
     98 			err(1, "stat %s", dir);
     99 
    100 		if (!set_uid)
    101 			args.uid = sb.st_uid;
    102 		if (!set_gid)
    103 			args.gid = sb.st_gid;
    104 		if (!set_mask)
    105 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    106 	}
    107 
    108 	if (mount(MOUNT_MSDOS, dir, opts, &args) < 0)
    109 		err(1, "mount");
    110 
    111 	exit (0);
    112 }
    113 
    114 gid_t
    115 a_gid(s)
    116 	char *s;
    117 {
    118 	struct group *gr;
    119 	char *gname;
    120 	gid_t gid;
    121 
    122 	if ((gr = getgrnam(s)) != NULL)
    123 		gid = gr->gr_gid;
    124 	else {
    125 		for (gname = s; *s && isdigit(*s); ++s);
    126 		if (!*s)
    127 			gid = atoi(gname);
    128 		else
    129 			errx(1, "unknown group id: %s", gname);
    130 	}
    131 	return (gid);
    132 }
    133 
    134 uid_t
    135 a_uid(s)
    136 	char *s;
    137 {
    138 	struct passwd *pw;
    139 	char *uname;
    140 	uid_t uid;
    141 
    142 	if ((pw = getpwnam(s)) != NULL)
    143 		uid = pw->pw_uid;
    144 	else {
    145 		for (uname = s; *s && isdigit(*s); ++s);
    146 		if (!*s)
    147 			uid = atoi(uname);
    148 		else
    149 			errx(1, "unknown user id: %s", uname);
    150 	}
    151 	return (uid);
    152 }
    153 
    154 mode_t
    155 a_mask(s)
    156 	char *s;
    157 {
    158 	int done, rv;
    159 	char *ep;
    160 
    161 	done = 0;
    162 	if (*s >= '0' && *s <= '7') {
    163 		done = 1;
    164 		rv = strtol(optarg, &ep, 8);
    165 	}
    166 	if (!done || rv < 0 || *ep)
    167 		errx(1, "invalid file mode: %s", s);
    168 	return (rv);
    169 }
    170 
    171 void
    172 usage()
    173 {
    174 	fprintf(stderr, "usage: mount_msdos [-F flags] [-u user] [-g group] [-m mask] bdev dir\n");
    175 	exit(1);
    176 }
    177