Deleted Added
full compact
args.c (31120) args.c (35773)
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * $Id: args.c,v 1.10 1997/10/11 20:09:04 joerg Exp $
38 */
39
40#ifndef lint
36 */
37
38#ifndef lint
39#if 0
41static char const sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
40static char const sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
41#endif
42static const char rcsid[] =
43 "$Id$";
42#endif /* not lint */
43
44#include <sys/types.h>
45
46#include <err.h>
47#include <errno.h>
48#include <limits.h>
44#endif /* not lint */
45
46#include <sys/types.h>
47
48#include <err.h>
49#include <errno.h>
50#include <limits.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include "dd.h"
54#include "extern.h"
55
56static int c_arg __P((const void *, const void *));
57static int c_conv __P((const void *, const void *));
58static void f_bs __P((char *));
59static void f_cbs __P((char *));
60static void f_conv __P((char *));
61static void f_count __P((char *));
62static void f_files __P((char *));
63static void f_ibs __P((char *));
64static void f_if __P((char *));
65static void f_obs __P((char *));
66static void f_of __P((char *));
67static void f_seek __P((char *));
68static void f_skip __P((char *));
69static u_long get_bsz __P((char *));
70
71static struct arg {
72 char *name;
73 void (*f) __P((char *));
74 u_int set, noset;
75} args[] = {
76 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
77 { "cbs", f_cbs, C_CBS, C_CBS },
78 { "conv", f_conv, 0, 0 },
79 { "count", f_count, C_COUNT, C_COUNT },
80 { "files", f_files, C_FILES, C_FILES },
81 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
82 { "if", f_if, C_IF, C_IF },
83 { "obs", f_obs, C_OBS, C_BS|C_OBS },
84 { "of", f_of, C_OF, C_OF },
85 { "seek", f_seek, C_SEEK, C_SEEK },
86 { "skip", f_skip, C_SKIP, C_SKIP },
87};
88
89static char *oper;
90
91/*
92 * args -- parse JCL syntax of dd.
93 */
94void
95jcl(argv)
96 char **argv;
97{
98 struct arg *ap, tmp;
99 char *arg;
100
101 in.dbsz = out.dbsz = 512;
102
103 while ((oper = *++argv) != NULL) {
104 if ((oper = strdup(oper)) == NULL)
105 errx(1, "unable to allocate space for the argument \"%s\"", *argv);
106 if ((arg = strchr(oper, '=')) == NULL)
107 errx(1, "unknown operand %s", oper);
108 *arg++ = '\0';
109 if (!*arg)
110 errx(1, "no value specified for %s", oper);
111 tmp.name = oper;
112 if (!(ap = (struct arg *)bsearch(&tmp, args,
113 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
114 c_arg)))
115 errx(1, "unknown operand %s", tmp.name);
116 if (ddflags & ap->noset)
117 errx(1, "%s: illegal argument combination or already set",
118 tmp.name);
119 ddflags |= ap->set;
120 ap->f(arg);
121 }
122
123 /* Final sanity checks. */
124
125 if (ddflags & C_BS) {
126 /*
127 * Bs is turned off by any conversion -- we assume the user
128 * just wanted to set both the input and output block sizes
129 * and didn't want the bs semantics, so we don't warn.
130 */
131 if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
132 ddflags &= ~C_BS;
133
134 /* Bs supersedes ibs and obs. */
135 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
136 warnx("bs supersedes ibs and obs");
137 }
138
139 /*
140 * Ascii/ebcdic and cbs implies block/unblock.
141 * Block/unblock requires cbs and vice-versa.
142 */
143 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
144 if (!(ddflags & C_CBS))
145 errx(1, "record operations require cbs");
146 if (cbsz == 0)
147 errx(1, "cbs cannot be zero");
148 cfunc = ddflags & C_BLOCK ? block : unblock;
149 } else if (ddflags & C_CBS) {
150 if (ddflags & (C_ASCII|C_EBCDIC)) {
151 if (ddflags & C_ASCII) {
152 ddflags |= C_UNBLOCK;
153 cfunc = unblock;
154 } else {
155 ddflags |= C_BLOCK;
156 cfunc = block;
157 }
158 } else
159 errx(1, "cbs meaningless if not doing record operations");
160 if (cbsz == 0)
161 errx(1, "cbs cannot be zero");
162 } else
163 cfunc = def;
164
165 if (in.dbsz == 0 || out.dbsz == 0)
166 errx(1, "buffer sizes cannot be zero");
167
168 /*
169 * Read, write and seek calls take ints as arguments. Seek sizes
170 * could be larger if we wanted to do it in stages or check only
171 * regular files, but it's probably not worth it.
172 */
173 if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
174 errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
175 if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
176 errx(1, "seek offsets cannot be larger than %d", INT_MAX);
177}
178
179static int
180c_arg(a, b)
181 const void *a, *b;
182{
183
184 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
185}
186
187static void
188f_bs(arg)
189 char *arg;
190{
191
192 in.dbsz = out.dbsz = (int)get_bsz(arg);
193}
194
195static void
196f_cbs(arg)
197 char *arg;
198{
199
200 cbsz = (int)get_bsz(arg);
201}
202
203static void
204f_count(arg)
205 char *arg;
206{
207
208 cpy_cnt = (u_int)get_bsz(arg);
209 if (!cpy_cnt)
210 terminate(0);
211}
212
213static void
214f_files(arg)
215 char *arg;
216{
217
218 files_cnt = (int)get_bsz(arg);
219}
220
221static void
222f_ibs(arg)
223 char *arg;
224{
225
226 if (!(ddflags & C_BS))
227 in.dbsz = (int)get_bsz(arg);
228}
229
230static void
231f_if(arg)
232 char *arg;
233{
234
235 in.name = arg;
236}
237
238static void
239f_obs(arg)
240 char *arg;
241{
242
243 if (!(ddflags & C_BS))
244 out.dbsz = (int)get_bsz(arg);
245}
246
247static void
248f_of(arg)
249 char *arg;
250{
251
252 out.name = arg;
253}
254
255static void
256f_seek(arg)
257 char *arg;
258{
259
260 out.offset = (u_int)get_bsz(arg);
261}
262
263static void
264f_skip(arg)
265 char *arg;
266{
267
268 in.offset = (u_int)get_bsz(arg);
269}
270
271static struct conv {
272 char *name;
273 u_int set, noset;
274 u_char *ctab;
275} clist[] = {
276 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
277 { "block", C_BLOCK, C_UNBLOCK, NULL },
278 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
279 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
280 { "lcase", C_LCASE, C_UCASE, NULL },
281 { "noerror", C_NOERROR, 0, NULL },
282 { "notrunc", C_NOTRUNC, 0, NULL },
283 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
284 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
285 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
286 { "osync", C_OSYNC, C_BS, NULL },
287 { "sparse", C_SPARSE, 0, NULL },
288 { "swab", C_SWAB, 0, NULL },
289 { "sync", C_SYNC, 0, NULL },
290 { "ucase", C_UCASE, C_LCASE, NULL },
291 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
292};
293
294static void
295f_conv(arg)
296 char *arg;
297{
298 struct conv *cp, tmp;
299
300 while (arg != NULL) {
301 tmp.name = strsep(&arg, ",");
302 if (!(cp = (struct conv *)bsearch(&tmp, clist,
303 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
304 c_conv)))
305 errx(1, "unknown conversion %s", tmp.name);
306 if (ddflags & cp->noset)
307 errx(1, "%s: illegal conversion combination", tmp.name);
308 ddflags |= cp->set;
309 if (cp->ctab)
310 ctab = cp->ctab;
311 }
312}
313
314static int
315c_conv(a, b)
316 const void *a, *b;
317{
318
319 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
320}
321
322/*
323 * Convert an expression of the following forms to an unsigned long.
324 * 1) A positive decimal number.
325 * 2) A positive decimal number followed by a b (mult by 512).
326 * 3) A positive decimal number followed by a k (mult by 1024).
327 * 4) A positive decimal number followed by a m (mult by 512).
328 * 5) A positive decimal number followed by a w (mult by sizeof int)
329 * 6) Two or more positive decimal numbers (with/without k,b or w).
330 * seperated by x (also * for backwards compatibility), specifying
331 * the product of the indicated values.
332 */
333static u_long
334get_bsz(val)
335 char *val;
336{
337 u_long num, t;
338 char *expr;
339
340 num = strtoul(val, &expr, 0);
341 if (num == ULONG_MAX) /* Overflow. */
342 err(1, "%s", oper);
343 if (expr == val) /* No digits. */
344 errx(1, "%s: illegal numeric value", oper);
345
346 switch(*expr) {
347 case 'b':
348 t = num;
349 num *= 512;
350 if (t > num)
351 goto erange;
352 ++expr;
353 break;
354 case 'k':
355 t = num;
356 num *= 1024;
357 if (t > num)
358 goto erange;
359 ++expr;
360 break;
361 case 'm':
362 t = num;
363 num *= 1048576;
364 if (t > num)
365 goto erange;
366 ++expr;
367 break;
368 case 'w':
369 t = num;
370 num *= sizeof(int);
371 if (t > num)
372 goto erange;
373 ++expr;
374 break;
375 }
376
377 switch(*expr) {
378 case '\0':
379 break;
380 case '*': /* Backward compatible. */
381 case 'x':
382 t = num;
383 num *= get_bsz(expr + 1);
384 if (t > num)
385erange: errx(1, "%s: %s", oper, strerror(ERANGE));
386 break;
387 default:
388 errx(1, "%s: illegal numeric value", oper);
389 }
390 return (num);
391}
51#include <stdlib.h>
52#include <string.h>
53
54#include "dd.h"
55#include "extern.h"
56
57static int c_arg __P((const void *, const void *));
58static int c_conv __P((const void *, const void *));
59static void f_bs __P((char *));
60static void f_cbs __P((char *));
61static void f_conv __P((char *));
62static void f_count __P((char *));
63static void f_files __P((char *));
64static void f_ibs __P((char *));
65static void f_if __P((char *));
66static void f_obs __P((char *));
67static void f_of __P((char *));
68static void f_seek __P((char *));
69static void f_skip __P((char *));
70static u_long get_bsz __P((char *));
71
72static struct arg {
73 char *name;
74 void (*f) __P((char *));
75 u_int set, noset;
76} args[] = {
77 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
78 { "cbs", f_cbs, C_CBS, C_CBS },
79 { "conv", f_conv, 0, 0 },
80 { "count", f_count, C_COUNT, C_COUNT },
81 { "files", f_files, C_FILES, C_FILES },
82 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
83 { "if", f_if, C_IF, C_IF },
84 { "obs", f_obs, C_OBS, C_BS|C_OBS },
85 { "of", f_of, C_OF, C_OF },
86 { "seek", f_seek, C_SEEK, C_SEEK },
87 { "skip", f_skip, C_SKIP, C_SKIP },
88};
89
90static char *oper;
91
92/*
93 * args -- parse JCL syntax of dd.
94 */
95void
96jcl(argv)
97 char **argv;
98{
99 struct arg *ap, tmp;
100 char *arg;
101
102 in.dbsz = out.dbsz = 512;
103
104 while ((oper = *++argv) != NULL) {
105 if ((oper = strdup(oper)) == NULL)
106 errx(1, "unable to allocate space for the argument \"%s\"", *argv);
107 if ((arg = strchr(oper, '=')) == NULL)
108 errx(1, "unknown operand %s", oper);
109 *arg++ = '\0';
110 if (!*arg)
111 errx(1, "no value specified for %s", oper);
112 tmp.name = oper;
113 if (!(ap = (struct arg *)bsearch(&tmp, args,
114 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
115 c_arg)))
116 errx(1, "unknown operand %s", tmp.name);
117 if (ddflags & ap->noset)
118 errx(1, "%s: illegal argument combination or already set",
119 tmp.name);
120 ddflags |= ap->set;
121 ap->f(arg);
122 }
123
124 /* Final sanity checks. */
125
126 if (ddflags & C_BS) {
127 /*
128 * Bs is turned off by any conversion -- we assume the user
129 * just wanted to set both the input and output block sizes
130 * and didn't want the bs semantics, so we don't warn.
131 */
132 if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
133 ddflags &= ~C_BS;
134
135 /* Bs supersedes ibs and obs. */
136 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
137 warnx("bs supersedes ibs and obs");
138 }
139
140 /*
141 * Ascii/ebcdic and cbs implies block/unblock.
142 * Block/unblock requires cbs and vice-versa.
143 */
144 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
145 if (!(ddflags & C_CBS))
146 errx(1, "record operations require cbs");
147 if (cbsz == 0)
148 errx(1, "cbs cannot be zero");
149 cfunc = ddflags & C_BLOCK ? block : unblock;
150 } else if (ddflags & C_CBS) {
151 if (ddflags & (C_ASCII|C_EBCDIC)) {
152 if (ddflags & C_ASCII) {
153 ddflags |= C_UNBLOCK;
154 cfunc = unblock;
155 } else {
156 ddflags |= C_BLOCK;
157 cfunc = block;
158 }
159 } else
160 errx(1, "cbs meaningless if not doing record operations");
161 if (cbsz == 0)
162 errx(1, "cbs cannot be zero");
163 } else
164 cfunc = def;
165
166 if (in.dbsz == 0 || out.dbsz == 0)
167 errx(1, "buffer sizes cannot be zero");
168
169 /*
170 * Read, write and seek calls take ints as arguments. Seek sizes
171 * could be larger if we wanted to do it in stages or check only
172 * regular files, but it's probably not worth it.
173 */
174 if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
175 errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
176 if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
177 errx(1, "seek offsets cannot be larger than %d", INT_MAX);
178}
179
180static int
181c_arg(a, b)
182 const void *a, *b;
183{
184
185 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
186}
187
188static void
189f_bs(arg)
190 char *arg;
191{
192
193 in.dbsz = out.dbsz = (int)get_bsz(arg);
194}
195
196static void
197f_cbs(arg)
198 char *arg;
199{
200
201 cbsz = (int)get_bsz(arg);
202}
203
204static void
205f_count(arg)
206 char *arg;
207{
208
209 cpy_cnt = (u_int)get_bsz(arg);
210 if (!cpy_cnt)
211 terminate(0);
212}
213
214static void
215f_files(arg)
216 char *arg;
217{
218
219 files_cnt = (int)get_bsz(arg);
220}
221
222static void
223f_ibs(arg)
224 char *arg;
225{
226
227 if (!(ddflags & C_BS))
228 in.dbsz = (int)get_bsz(arg);
229}
230
231static void
232f_if(arg)
233 char *arg;
234{
235
236 in.name = arg;
237}
238
239static void
240f_obs(arg)
241 char *arg;
242{
243
244 if (!(ddflags & C_BS))
245 out.dbsz = (int)get_bsz(arg);
246}
247
248static void
249f_of(arg)
250 char *arg;
251{
252
253 out.name = arg;
254}
255
256static void
257f_seek(arg)
258 char *arg;
259{
260
261 out.offset = (u_int)get_bsz(arg);
262}
263
264static void
265f_skip(arg)
266 char *arg;
267{
268
269 in.offset = (u_int)get_bsz(arg);
270}
271
272static struct conv {
273 char *name;
274 u_int set, noset;
275 u_char *ctab;
276} clist[] = {
277 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
278 { "block", C_BLOCK, C_UNBLOCK, NULL },
279 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
280 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
281 { "lcase", C_LCASE, C_UCASE, NULL },
282 { "noerror", C_NOERROR, 0, NULL },
283 { "notrunc", C_NOTRUNC, 0, NULL },
284 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
285 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
286 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
287 { "osync", C_OSYNC, C_BS, NULL },
288 { "sparse", C_SPARSE, 0, NULL },
289 { "swab", C_SWAB, 0, NULL },
290 { "sync", C_SYNC, 0, NULL },
291 { "ucase", C_UCASE, C_LCASE, NULL },
292 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
293};
294
295static void
296f_conv(arg)
297 char *arg;
298{
299 struct conv *cp, tmp;
300
301 while (arg != NULL) {
302 tmp.name = strsep(&arg, ",");
303 if (!(cp = (struct conv *)bsearch(&tmp, clist,
304 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
305 c_conv)))
306 errx(1, "unknown conversion %s", tmp.name);
307 if (ddflags & cp->noset)
308 errx(1, "%s: illegal conversion combination", tmp.name);
309 ddflags |= cp->set;
310 if (cp->ctab)
311 ctab = cp->ctab;
312 }
313}
314
315static int
316c_conv(a, b)
317 const void *a, *b;
318{
319
320 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
321}
322
323/*
324 * Convert an expression of the following forms to an unsigned long.
325 * 1) A positive decimal number.
326 * 2) A positive decimal number followed by a b (mult by 512).
327 * 3) A positive decimal number followed by a k (mult by 1024).
328 * 4) A positive decimal number followed by a m (mult by 512).
329 * 5) A positive decimal number followed by a w (mult by sizeof int)
330 * 6) Two or more positive decimal numbers (with/without k,b or w).
331 * seperated by x (also * for backwards compatibility), specifying
332 * the product of the indicated values.
333 */
334static u_long
335get_bsz(val)
336 char *val;
337{
338 u_long num, t;
339 char *expr;
340
341 num = strtoul(val, &expr, 0);
342 if (num == ULONG_MAX) /* Overflow. */
343 err(1, "%s", oper);
344 if (expr == val) /* No digits. */
345 errx(1, "%s: illegal numeric value", oper);
346
347 switch(*expr) {
348 case 'b':
349 t = num;
350 num *= 512;
351 if (t > num)
352 goto erange;
353 ++expr;
354 break;
355 case 'k':
356 t = num;
357 num *= 1024;
358 if (t > num)
359 goto erange;
360 ++expr;
361 break;
362 case 'm':
363 t = num;
364 num *= 1048576;
365 if (t > num)
366 goto erange;
367 ++expr;
368 break;
369 case 'w':
370 t = num;
371 num *= sizeof(int);
372 if (t > num)
373 goto erange;
374 ++expr;
375 break;
376 }
377
378 switch(*expr) {
379 case '\0':
380 break;
381 case '*': /* Backward compatible. */
382 case 'x':
383 t = num;
384 num *= get_bsz(expr + 1);
385 if (t > num)
386erange: errx(1, "%s: %s", oper, strerror(ERANGE));
387 break;
388 default:
389 errx(1, "%s: illegal numeric value", oper);
390 }
391 return (num);
392}