mount_ados.c revision 1.2 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_ados.c,v 1.2 1994/07/30 19:04:01 chopps Exp $";
33 #endif /* not lint */
34
35 #include <sys/cdefs.h>
36 #include <sys/param.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 #include <unistd.h>
47
48 #ifndef LETS_GET_SMALL
49 gid_t a_gid __P((char *));
50 uid_t a_uid __P((char *));
51 mode_t a_mask __P((char *));
52 #endif
53 void usage __P((void));
54
55 int
56 main(argc, argv)
57 int argc;
58 char **argv;
59 {
60 struct adosfs_args args;
61 struct stat sb;
62 int c, opts, set_gid, set_uid, set_mask;
63 char *dev, *dir, ndir[MAXPATHLEN+1];
64
65 opts = set_gid = set_uid = set_mask = 0;
66 (void)memset(&args, '\0', sizeof(args));
67
68 while ((c = getopt(argc, argv, "F:u:g:m:")) != EOF) {
69 switch (c) {
70 case 'F':
71 opts |= atoi(optarg);
72 break;
73 #ifndef LETS_GET_SMALL
74 case 'u':
75 args.uid = a_uid(optarg);
76 set_uid = 1;
77 break;
78 case 'g':
79 args.gid = a_gid(optarg);
80 set_gid = 1;
81 break;
82 case 'm':
83 args.mask = a_mask(optarg);
84 set_mask = 1;
85 break;
86 #endif
87 case '?':
88 default:
89 usage();
90 break;
91 }
92 }
93
94 if (optind + 2 != argc)
95 usage();
96
97 dev = argv[optind];
98 dir = argv[optind + 1];
99 if (dir[0] != '/') {
100 warnx("\"%s\" is a relative path.", dir);
101 if (getcwd(ndir, sizeof(ndir)) == NULL)
102 err(1, "getcwd");
103 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
104 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
105 dir = ndir;
106 warnx("using \"%s\" instead.", dir);
107 }
108
109 args.fspec = dev;
110 if (!set_gid || !set_uid || !set_mask) {
111 if (stat(dir, &sb) == -1)
112 err(1, "stat %s", dir);
113
114 if (!set_uid)
115 args.uid = sb.st_uid;
116 if (!set_gid)
117 args.gid = sb.st_gid;
118 if (!set_mask)
119 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
120 }
121
122 if (mount(MOUNT_ADOSFS, dir, opts, &args) < 0)
123 err(1, "mount");
124
125 exit (0);
126 }
127
128 #ifndef LETS_GET_SMALL
129 gid_t
130 a_gid(s)
131 char *s;
132 {
133 struct group *gr;
134 char *gname;
135 gid_t gid;
136
137 if ((gr = getgrnam(s)) != NULL)
138 gid = gr->gr_gid;
139 else {
140 for (gname = s; *s && isdigit(*s); ++s);
141 if (!*s)
142 gid = atoi(gname);
143 else
144 errx(1, "unknown group id: %s", gname);
145 }
146 return (gid);
147 }
148
149 uid_t
150 a_uid(s)
151 char *s;
152 {
153 struct passwd *pw;
154 char *uname;
155 uid_t uid;
156
157 if ((pw = getpwnam(s)) != NULL)
158 uid = pw->pw_uid;
159 else {
160 for (uname = s; *s && isdigit(*s); ++s);
161 if (!*s)
162 uid = atoi(uname);
163 else
164 errx(1, "unknown user id: %s", uname);
165 }
166 return (uid);
167 }
168
169 mode_t
170 a_mask(s)
171 char *s;
172 {
173 int done, rv;
174 char *ep;
175
176 done = 0;
177 if (*s >= '0' && *s <= '7') {
178 done = 1;
179 rv = strtol(optarg, &ep, 8);
180 }
181 if (!done || rv < 0 || *ep)
182 errx(1, "invalid file mode: %s", s);
183 return (rv);
184 }
185
186 void
187 usage()
188 {
189 fprintf(stderr, "usage: mount_ados [-F flags] [-u user] [-g group] [-m mask] bdev dir\n");
190 exit(1);
191 }
192
193 #else
194
195 void
196 usage()
197 {
198 fprintf(stderr, "usage: mount_ados [-F flags] bdev dir\n");
199 exit(1);
200 }
201 #endif
202