Deleted Added
full compact
args.c (51137) args.c (51208)
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
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
41#endif
42static const char rcsid[] =
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
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
41#endif
42static const char rcsid[] =
43 "$FreeBSD: head/bin/dd/args.c 51137 1999-09-11 00:02:42Z green $";
43 "$FreeBSD: head/bin/dd/args.c 51208 1999-09-12 16:51:53Z green $";
44#endif /* not lint */
45
46#include <sys/types.h>
47
48#include <err.h>
49#include <errno.h>
50#include <limits.h>
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 quad_t get_num __P((char *));
44#endif /* not lint */
45
46#include <sys/types.h>
47
48#include <err.h>
49#include <errno.h>
50#include <limits.h>
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 quad_t get_num __P((char *));
71static off_t get_offset __P((char *));
71
72
72static struct arg {
73static const 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 } else
162 cfunc = def;
74 char *name;
75 void (*f) __P((char *));
76 u_int set, noset;
77} args[] = {
78 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
79 { "cbs", f_cbs, C_CBS, C_CBS },
80 { "conv", f_conv, 0, 0 },
81 { "count", f_count, C_COUNT, C_COUNT },
82 { "files", f_files, C_FILES, C_FILES },
83 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
84 { "if", f_if, C_IF, C_IF },
85 { "obs", f_obs, C_OBS, C_BS|C_OBS },
86 { "of", f_of, C_OF, C_OF },
87 { "seek", f_seek, C_SEEK, C_SEEK },
88 { "skip", f_skip, C_SKIP, C_SKIP },
89};
90
91static char *oper;
92
93/*
94 * args -- parse JCL syntax of dd.
95 */
96void
97jcl(argv)
98 char **argv;
99{
100 struct arg *ap, tmp;
101 char *arg;
102
103 in.dbsz = out.dbsz = 512;
104
105 while ((oper = *++argv) != NULL) {
106 if ((oper = strdup(oper)) == NULL)
107 errx(1, "unable to allocate space for the argument \"%s\"", *argv);
108 if ((arg = strchr(oper, '=')) == NULL)
109 errx(1, "unknown operand %s", oper);
110 *arg++ = '\0';
111 if (!*arg)
112 errx(1, "no value specified for %s", oper);
113 tmp.name = oper;
114 if (!(ap = (struct arg *)bsearch(&tmp, args,
115 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
116 c_arg)))
117 errx(1, "unknown operand %s", tmp.name);
118 if (ddflags & ap->noset)
119 errx(1, "%s: illegal argument combination or already set",
120 tmp.name);
121 ddflags |= ap->set;
122 ap->f(arg);
123 }
124
125 /* Final sanity checks. */
126
127 if (ddflags & C_BS) {
128 /*
129 * Bs is turned off by any conversion -- we assume the user
130 * just wanted to set both the input and output block sizes
131 * and didn't want the bs semantics, so we don't warn.
132 */
133 if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
134 ddflags &= ~C_BS;
135
136 /* Bs supersedes ibs and obs. */
137 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
138 warnx("bs supersedes ibs and obs");
139 }
140
141 /*
142 * Ascii/ebcdic and cbs implies block/unblock.
143 * Block/unblock requires cbs and vice-versa.
144 */
145 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
146 if (!(ddflags & C_CBS))
147 errx(1, "record operations require cbs");
148 if (cbsz == 0)
149 errx(1, "cbs cannot be zero");
150 cfunc = ddflags & C_BLOCK ? block : unblock;
151 } else if (ddflags & C_CBS) {
152 if (ddflags & (C_ASCII|C_EBCDIC)) {
153 if (ddflags & C_ASCII) {
154 ddflags |= C_UNBLOCK;
155 cfunc = unblock;
156 } else {
157 ddflags |= C_BLOCK;
158 cfunc = block;
159 }
160 } else
161 errx(1, "cbs meaningless if not doing record operations");
162 } else
163 cfunc = def;
164 /*
165 * Bail out if the calculation of a file offset would overflow.
166 */
167 if (in.offset > QUAD_MAX / in.dbsz || out.offset > QUAD_MAX / out.dbsz)
168 errx(1, "seek offsets cannot be larger than %qd", QUAD_MAX);
163}
164
165static int
166c_arg(a, b)
167 const void *a, *b;
168{
169
170 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
171}
172
173static void
174f_bs(arg)
175 char *arg;
176{
169}
170
171static int
172c_arg(a, b)
173 const void *a, *b;
174{
175
176 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
177}
178
179static void
180f_bs(arg)
181 char *arg;
182{
177 quad_t res = get_num(arg);
183 quad_t res;
178
184
179 if (res < 1 || res > INT_MAX)
180 errx(1, "bs must be between 1 and %d", INT_MAX);
181 in.dbsz = out.dbsz = (int)res;
185 res = get_num(arg);
186 if (res < 1 || res > SSIZE_MAX)
187 errx(1, "bs must be between 1 and %d", SSIZE_MAX);
188 in.dbsz = out.dbsz = (size_t)res;
182}
183
184static void
185f_cbs(arg)
186 char *arg;
187{
189}
190
191static void
192f_cbs(arg)
193 char *arg;
194{
188 quad_t res = get_num(arg);
195 quad_t res;
189
196
190 if (res < 1 || res > INT_MAX)
191 errx(1, "cbs must be between 1 and %d", INT_MAX);
192 cbsz = (int)res;
197 res = get_num(arg);
198 if (res < 1 || res > SSIZE_MAX)
199 errx(1, "cbs must be between 1 and %d", SSIZE_MAX);
200 cbsz = (size_t)res;
193}
194
195static void
196f_count(arg)
197 char *arg;
198{
199
200 cpy_cnt = get_num(arg);
201
202 if (!cpy_cnt)
203 terminate(0);
201}
202
203static void
204f_count(arg)
205 char *arg;
206{
207
208 cpy_cnt = get_num(arg);
209
210 if (!cpy_cnt)
211 terminate(0);
204 if (cpy_cnt < 0)
205 errx(1, "count cannot be negative");
206}
207
208static void
209f_files(arg)
210 char *arg;
211{
212
213 files_cnt = get_num(arg);
212}
213
214static void
215f_files(arg)
216 char *arg;
217{
218
219 files_cnt = get_num(arg);
214 if (files_cnt < 0)
215 errx(1, "files cannot be negative");
216}
217
218static void
219f_ibs(arg)
220 char *arg;
221{
220}
221
222static void
223f_ibs(arg)
224 char *arg;
225{
226 quad_t res;
222
223 if (!(ddflags & C_BS)) {
227
228 if (!(ddflags & C_BS)) {
224 quad_t res = get_num(arg);
225
226 if (res < 1 || res > INT_MAX)
229 res = get_num(arg);
230 if (res < 1 || res > SSIZE_MAX)
227 errx(1, "ibs must be between 1 and %d", INT_MAX);
228 in.dbsz = (int)res;
229 }
230}
231
232static void
233f_if(arg)
234 char *arg;
235{
236
237 in.name = arg;
238}
239
240static void
241f_obs(arg)
242 char *arg;
243{
231 errx(1, "ibs must be between 1 and %d", INT_MAX);
232 in.dbsz = (int)res;
233 }
234}
235
236static void
237f_if(arg)
238 char *arg;
239{
240
241 in.name = arg;
242}
243
244static void
245f_obs(arg)
246 char *arg;
247{
248 quad_t res;
244
245 if (!(ddflags & C_BS)) {
249
250 if (!(ddflags & C_BS)) {
246 quad_t res = get_num(arg);
247
248 if (res < 1 || res > INT_MAX)
249 errx(1, "ibs must be between 1 and %d", INT_MAX);
250 out.dbsz = (int)res;
251 res = get_num(arg);
252 if (res < 1 || res > SSIZE_MAX)
253 errx(1, "obs must be between 1 and %d", SSIZE_MAX);
254 out.dbsz = (size_t)res;
251 }
252}
253
254static void
255f_of(arg)
256 char *arg;
257{
258
259 out.name = arg;
260}
261
262static void
263f_seek(arg)
264 char *arg;
265{
266
255 }
256}
257
258static void
259f_of(arg)
260 char *arg;
261{
262
263 out.name = arg;
264}
265
266static void
267f_seek(arg)
268 char *arg;
269{
270
267 out.offset = get_num(arg);
271 out.offset = get_offset(arg);
268}
269
270static void
271f_skip(arg)
272 char *arg;
273{
274
272}
273
274static void
275f_skip(arg)
276 char *arg;
277{
278
275 in.offset = get_num(arg);
279 in.offset = get_offset(arg);
276}
277
280}
281
278static struct conv {
282static const struct conv {
279 char *name;
280 u_int set, noset;
283 char *name;
284 u_int set, noset;
281 u_char *ctab;
285 const u_char *ctab;
282} clist[] = {
283 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
284 { "block", C_BLOCK, C_UNBLOCK, NULL },
285 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
286 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
287 { "lcase", C_LCASE, C_UCASE, NULL },
288 { "noerror", C_NOERROR, 0, NULL },
289 { "notrunc", C_NOTRUNC, 0, NULL },
290 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
291 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
292 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
293 { "osync", C_OSYNC, C_BS, NULL },
294 { "sparse", C_SPARSE, 0, NULL },
295 { "swab", C_SWAB, 0, NULL },
296 { "sync", C_SYNC, 0, NULL },
297 { "ucase", C_UCASE, C_LCASE, NULL },
298 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
299};
300
301static void
302f_conv(arg)
303 char *arg;
304{
305 struct conv *cp, tmp;
306
307 while (arg != NULL) {
308 tmp.name = strsep(&arg, ",");
286} clist[] = {
287 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
288 { "block", C_BLOCK, C_UNBLOCK, NULL },
289 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
290 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
291 { "lcase", C_LCASE, C_UCASE, NULL },
292 { "noerror", C_NOERROR, 0, NULL },
293 { "notrunc", C_NOTRUNC, 0, NULL },
294 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
295 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
296 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
297 { "osync", C_OSYNC, C_BS, NULL },
298 { "sparse", C_SPARSE, 0, NULL },
299 { "swab", C_SWAB, 0, NULL },
300 { "sync", C_SYNC, 0, NULL },
301 { "ucase", C_UCASE, C_LCASE, NULL },
302 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
303};
304
305static void
306f_conv(arg)
307 char *arg;
308{
309 struct conv *cp, tmp;
310
311 while (arg != NULL) {
312 tmp.name = strsep(&arg, ",");
309 if (!(cp = (struct conv *)bsearch(&tmp, clist,
310 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
311 c_conv)))
313 cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv),
314 sizeof(struct conv), c_conv);
315 if (cp == NULL)
312 errx(1, "unknown conversion %s", tmp.name);
313 if (ddflags & cp->noset)
314 errx(1, "%s: illegal conversion combination", tmp.name);
315 ddflags |= cp->set;
316 if (cp->ctab)
317 ctab = cp->ctab;
318 }
319}
320
321static int
322c_conv(a, b)
323 const void *a, *b;
324{
325
326 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
327}
328
329/*
330 * Convert an expression of the following forms to a quad_t.
331 * 1) A positive decimal number.
332 * 2) A positive decimal number followed by a b (mult by 512.)
333 * 3) A positive decimal number followed by a k (mult by 1 << 10.)
334 * 4) A positive decimal number followed by a m (mult by 1 << 20.)
335 * 5) A positive decimal number followed by a g (mult by 1 << 30.)
336 * 5) A positive decimal number followed by a w (mult by sizeof int.)
337 * 6) Two or more positive decimal numbers (with/without [bkmgw])
338 * separated by x (also * for backwards compatibility), specifying
339 * the product of the indicated values.
340 */
341static quad_t
342get_num(val)
343 char *val;
344{
345 quad_t num, t;
346 char *expr;
347
348 errno = 0;
349 num = strtoq(val, &expr, 0);
316 errx(1, "unknown conversion %s", tmp.name);
317 if (ddflags & cp->noset)
318 errx(1, "%s: illegal conversion combination", tmp.name);
319 ddflags |= cp->set;
320 if (cp->ctab)
321 ctab = cp->ctab;
322 }
323}
324
325static int
326c_conv(a, b)
327 const void *a, *b;
328{
329
330 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
331}
332
333/*
334 * Convert an expression of the following forms to a quad_t.
335 * 1) A positive decimal number.
336 * 2) A positive decimal number followed by a b (mult by 512.)
337 * 3) A positive decimal number followed by a k (mult by 1 << 10.)
338 * 4) A positive decimal number followed by a m (mult by 1 << 20.)
339 * 5) A positive decimal number followed by a g (mult by 1 << 30.)
340 * 5) A positive decimal number followed by a w (mult by sizeof int.)
341 * 6) Two or more positive decimal numbers (with/without [bkmgw])
342 * separated by x (also * for backwards compatibility), specifying
343 * the product of the indicated values.
344 */
345static quad_t
346get_num(val)
347 char *val;
348{
349 quad_t num, t;
350 char *expr;
351
352 errno = 0;
353 num = strtoq(val, &expr, 0);
350 if (errno) /* Overflow or underflow. */
354 if (errno != 0) /* Overflow or underflow. */
351 err(1, "%s", oper);
352
355 err(1, "%s", oper);
356
353 if (expr == val) /* Not a valid number */
357 if (expr == val) /* No valid digits. */
354 errx(1, "%s: illegal numeric value", oper);
355
356 switch (*expr) {
357 case 'b':
358 t = num;
359 num *= 512;
360 if (t > num)
361 goto erange;
362 ++expr;
363 break;
364 case 'k':
365 t = num;
366 num *= 1 << 10;
367 if (t > num)
368 goto erange;
369 ++expr;
370 break;
371 case 'm':
372 t = num;
373 num *= 1 << 20;
374 if (t > num)
375 goto erange;
376 ++expr;
377 break;
378 case 'g':
379 t = num;
380 num *= 1 << 30;
381 if (t > num)
382 goto erange;
383 ++expr;
384 break;
385 case 'w':
386 t = num;
387 num *= sizeof(int);
388 if (t > num)
389 goto erange;
390 ++expr;
391 break;
392 }
393
394 switch (*expr) {
395 case '\0':
396 break;
397 case '*': /* Backward compatible. */
398 case 'x':
399 t = num;
400 num *= get_num(expr + 1);
401 if (t <= num)
402 break;
403erange:
404 errx(1, "%s: %s", oper, strerror(ERANGE));
405 default:
406 errx(1, "%s: illegal numeric value", oper);
407 }
408 return (num);
409}
358 errx(1, "%s: illegal numeric value", oper);
359
360 switch (*expr) {
361 case 'b':
362 t = num;
363 num *= 512;
364 if (t > num)
365 goto erange;
366 ++expr;
367 break;
368 case 'k':
369 t = num;
370 num *= 1 << 10;
371 if (t > num)
372 goto erange;
373 ++expr;
374 break;
375 case 'm':
376 t = num;
377 num *= 1 << 20;
378 if (t > num)
379 goto erange;
380 ++expr;
381 break;
382 case 'g':
383 t = num;
384 num *= 1 << 30;
385 if (t > num)
386 goto erange;
387 ++expr;
388 break;
389 case 'w':
390 t = num;
391 num *= sizeof(int);
392 if (t > num)
393 goto erange;
394 ++expr;
395 break;
396 }
397
398 switch (*expr) {
399 case '\0':
400 break;
401 case '*': /* Backward compatible. */
402 case 'x':
403 t = num;
404 num *= get_num(expr + 1);
405 if (t <= num)
406 break;
407erange:
408 errx(1, "%s: %s", oper, strerror(ERANGE));
409 default:
410 errx(1, "%s: illegal numeric value", oper);
411 }
412 return (num);
413}
414
415static off_t
416get_offset(val)
417 char *val;
418{
419 quad_t num;
420
421 num = get_num(val);
422 if (num > QUAD_MAX || num < 0) /* XXX quad_t != off_t */
423 errx(1, "%s: illegal offset", oper); /* Too big/negative. */
424 return ((off_t)num);
425}