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