]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/newfs_msdos/newfs_msdos.c
Restore r332345 and r332346 from head, merged to stable/11 as
[FreeBSD/FreeBSD.git] / sbin / newfs_msdos / newfs_msdos.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Robert Nordier
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
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34
35 #include <sys/param.h>
36
37 #include <err.h>
38 #include <errno.h>
39 #include <paths.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "mkfs_msdos.h"
46
47 #define argto1(arg, lo, msg)  argtou(arg, lo, 0xff, msg)
48 #define argto2(arg, lo, msg)  argtou(arg, lo, 0xffff, msg)
49 #define argto4(arg, lo, msg)  argtou(arg, lo, 0xffffffff, msg)
50 #define argtox(arg, lo, msg)  argtou(arg, lo, UINT_MAX, msg)
51
52 static u_int argtou(const char *, u_int, u_int, const char *);
53 static off_t argtooff(const char *, const char *);
54 static void usage(void);
55
56 /*
57  * Construct a FAT12, FAT16, or FAT32 file system.
58  */
59 int
60 main(int argc, char *argv[])
61 {
62     static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
63     struct msdos_options o;
64     const char *fname, *dtype;
65     char buf[MAXPATHLEN];
66     int ch;
67
68     memset(&o, 0, sizeof(o));
69
70     while ((ch = getopt(argc, argv, opts)) != -1)
71         switch (ch) {
72         case '@':
73             o.offset = argtooff(optarg, "offset");
74             break;
75         case 'N':
76             o.no_create = 1;
77             break;
78         case 'B':
79             o.bootstrap = optarg;
80             break;
81         case 'C':
82             o.create_size = argtooff(optarg, "create size");
83             break;
84         case 'F':
85             if (strcmp(optarg, "12") &&
86                 strcmp(optarg, "16") &&
87                 strcmp(optarg, "32"))
88                 errx(1, "%s: bad FAT type", optarg);
89             o.fat_type = atoi(optarg);
90             break;
91         case 'I':
92             o.volume_id = argto4(optarg, 0, "volume ID");
93             o.volume_id_set = 1;
94             break;
95         case 'L':
96             o.volume_label = optarg;
97             break;
98         case 'O':
99             o.OEM_string = optarg;
100             break;
101         case 'S':
102             o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
103             break;
104         case 'a':
105             o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
106             break;
107         case 'b':
108             o.block_size = argtox(optarg, 1, "block size");
109             o.sectors_per_cluster = 0;
110             break;
111         case 'c':
112             o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
113             o.block_size = 0;
114             break;
115         case 'e':
116             o.directory_entries = argto2(optarg, 1, "directory entries");
117             break;
118         case 'f':
119             o.floppy = optarg;
120             break;
121         case 'h':
122             o.drive_heads = argto2(optarg, 1, "drive heads");
123             break;
124         case 'i':
125             o.info_sector = argto2(optarg, 1, "info sector");
126             break;
127         case 'k':
128             o.backup_sector = argto2(optarg, 1, "backup sector");
129             break;
130         case 'm':
131             o.media_descriptor = argto1(optarg, 0, "media descriptor");
132             o.media_descriptor_set = 1;
133             break;
134         case 'n':
135             o.num_FAT = argto1(optarg, 1, "number of FATs");
136             break;
137         case 'o':
138             o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
139             o.hidden_sectors_set = 1;
140             break;
141         case 'r':
142             o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
143             break;
144         case 's':
145             o.size = argto4(optarg, 1, "file system size");
146             break;
147         case 'u':
148             o.sectors_per_track = argto2(optarg, 1, "sectors/track");
149             break;
150         default:
151             usage();
152         }
153     argc -= optind;
154     argv += optind;
155     if (argc < 1 || argc > 2)
156         usage();
157     fname = *argv++;
158     if (!o.create_size && !strchr(fname, '/')) {
159         snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
160         if (!(fname = strdup(buf)))
161             err(1, NULL);
162     }
163     dtype = *argv;
164     return !!mkfs_msdos(fname, dtype, &o);
165 }
166
167 /*
168  * Convert and check a numeric option argument.
169  */
170 static u_int
171 argtou(const char *arg, u_int lo, u_int hi, const char *msg)
172 {
173     char *s;
174     u_long x;
175
176     errno = 0;
177     x = strtoul(arg, &s, 0);
178     if (errno || !*arg || *s || x < lo || x > hi)
179         errx(1, "%s: bad %s", arg, msg);
180     return x;
181 }
182
183 /*
184  * Same for off_t, with optional skmgpP suffix
185  */
186 static off_t
187 argtooff(const char *arg, const char *msg)
188 {
189     char *s;
190     off_t x;
191
192     errno = 0;
193     x = strtoll(arg, &s, 0);
194     /* allow at most one extra char */
195     if (errno || x < 0 || (s[0] && s[1]) )
196         errx(1, "%s: bad %s", arg, msg);
197     if (*s) {   /* the extra char is the multiplier */
198         switch (*s) {
199         default:
200             errx(1, "%s: bad %s", arg, msg);
201             /* notreached */
202
203         case 's':               /* sector */
204         case 'S':
205             x <<= 9;            /* times 512 */
206             break;
207
208         case 'k':               /* kilobyte */
209         case 'K':
210             x <<= 10;           /* times 1024 */
211             break;
212
213         case 'm':               /* megabyte */
214         case 'M':
215             x <<= 20;           /* times 1024*1024 */
216             break;
217
218         case 'g':               /* gigabyte */
219         case 'G':
220             x <<= 30;           /* times 1024*1024*1024 */
221             break;
222
223         case 'p':               /* partition start */
224         case 'P':
225         case 'l':               /* partition length */
226         case 'L':
227             errx(1, "%s: not supported yet %s", arg, msg);
228             /* notreached */
229         }
230     }
231     return x;
232 }
233
234 /*
235  * Print usage message.
236  */
237 static void
238 usage(void)
239 {
240     fprintf(stderr,
241             "usage: %s [ -options ] special [disktype]\n", getprogname());
242     fprintf(stderr, "where the options are:\n");
243 static struct {
244     char o;
245     const char *h;
246 } opts[] = {
247 #define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
248 ALLOPTS
249 #undef AOPT
250     };
251     for (size_t i = 0; i < nitems(opts); i++)
252         fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
253     exit(1);
254 }