Deleted Added
full compact
args.c (54245) args.c (54278)
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 54245 1999-12-07 03:32:37Z green $";
43 "$FreeBSD: head/bin/dd/args.c 54278 1999-12-08 02:44:46Z 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 *));
71static off_t get_offset __P((char *));
72
73static const struct arg {
74 const 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 /*
166 * Bail out if the calculation of a file offset would overflow.
167 */
168 if (in.offset > QUAD_MAX / in.dbsz || out.offset > QUAD_MAX / out.dbsz)
169 errx(1, "seek offsets cannot be larger than %qd", QUAD_MAX);
170}
171
172static int
173c_arg(a, b)
174 const void *a, *b;
175{
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 *));
72
73static const struct arg {
74 const 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 /*
166 * Bail out if the calculation of a file offset would overflow.
167 */
168 if (in.offset > QUAD_MAX / in.dbsz || out.offset > QUAD_MAX / out.dbsz)
169 errx(1, "seek offsets cannot be larger than %qd", QUAD_MAX);
170}
171
172static int
173c_arg(a, b)
174 const void *a, *b;
175{
176 typedef const struct arg *c_arg_p;
177
176
178 return (strcmp(((c_arg_p)a)->name, ((c_arg_p)b)->name));
177 return (strcmp(((const struct arg *)a)->name,
178 ((const struct arg *)b)->name));
179}
180
181static void
182f_bs(arg)
183 char *arg;
184{
185 quad_t res;
186
187 res = get_num(arg);
188 if (res < 1 || res > SSIZE_MAX)
189 errx(1, "bs must be between 1 and %d", SSIZE_MAX);
190 in.dbsz = out.dbsz = (size_t)res;
191}
192
193static void
194f_cbs(arg)
195 char *arg;
196{
197 quad_t res;
198
199 res = get_num(arg);
200 if (res < 1 || res > SSIZE_MAX)
201 errx(1, "cbs must be between 1 and %d", SSIZE_MAX);
202 cbsz = (size_t)res;
203}
204
205static void
206f_count(arg)
207 char *arg;
208{
209
210 cpy_cnt = get_num(arg);
211 if (cpy_cnt < 0)
212 errx(1, "count cannot be negative");
213 if (cpy_cnt == 0)
214 cpy_cnt = -1;
215}
216
217static void
218f_files(arg)
219 char *arg;
220{
221
222 files_cnt = get_num(arg);
223 if (files_cnt < 1)
224 errx(1, "files must be between 1 and %qd", QUAD_MAX);
225}
226
227static void
228f_ibs(arg)
229 char *arg;
230{
231 quad_t res;
232
233 if (!(ddflags & C_BS)) {
234 res = get_num(arg);
235 if (res < 1 || res > SSIZE_MAX)
236 errx(1, "ibs must be between 1 and %d", SSIZE_MAX);
237 in.dbsz = (size_t)res;
238 }
239}
240
241static void
242f_if(arg)
243 char *arg;
244{
245
246 in.name = arg;
247}
248
249static void
250f_obs(arg)
251 char *arg;
252{
253 quad_t res;
254
255 if (!(ddflags & C_BS)) {
256 res = get_num(arg);
257 if (res < 1 || res > SSIZE_MAX)
258 errx(1, "obs must be between 1 and %d", SSIZE_MAX);
259 out.dbsz = (size_t)res;
260 }
261}
262
263static void
264f_of(arg)
265 char *arg;
266{
267
268 out.name = arg;
269}
270
271static void
272f_seek(arg)
273 char *arg;
274{
275
276 out.offset = get_offset(arg);
277}
278
279static void
280f_skip(arg)
281 char *arg;
282{
283
284 in.offset = get_offset(arg);
285}
286
287static const struct conv {
288 const char *name;
289 u_int set, noset;
290 const u_char *ctab;
291} clist[] = {
292 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
293 { "block", C_BLOCK, C_UNBLOCK, NULL },
294 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
295 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
296 { "lcase", C_LCASE, C_UCASE, NULL },
297 { "noerror", C_NOERROR, 0, NULL },
298 { "notrunc", C_NOTRUNC, 0, NULL },
299 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
300 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
301 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
302 { "osync", C_OSYNC, C_BS, NULL },
303 { "sparse", C_SPARSE, 0, NULL },
304 { "swab", C_SWAB, 0, NULL },
305 { "sync", C_SYNC, 0, NULL },
306 { "ucase", C_UCASE, C_LCASE, NULL },
307 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
308};
309
310static void
311f_conv(arg)
312 char *arg;
313{
314 struct conv *cp, tmp;
315
316 while (arg != NULL) {
317 tmp.name = strsep(&arg, ",");
318 cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv),
319 sizeof(struct conv), c_conv);
320 if (cp == NULL)
321 errx(1, "unknown conversion %s", tmp.name);
322 if (ddflags & cp->noset)
323 errx(1, "%s: illegal conversion combination", tmp.name);
324 ddflags |= cp->set;
325 if (cp->ctab)
326 ctab = cp->ctab;
327 }
328}
329
330static int
331c_conv(a, b)
332 const void *a, *b;
333{
179}
180
181static void
182f_bs(arg)
183 char *arg;
184{
185 quad_t res;
186
187 res = get_num(arg);
188 if (res < 1 || res > SSIZE_MAX)
189 errx(1, "bs must be between 1 and %d", SSIZE_MAX);
190 in.dbsz = out.dbsz = (size_t)res;
191}
192
193static void
194f_cbs(arg)
195 char *arg;
196{
197 quad_t res;
198
199 res = get_num(arg);
200 if (res < 1 || res > SSIZE_MAX)
201 errx(1, "cbs must be between 1 and %d", SSIZE_MAX);
202 cbsz = (size_t)res;
203}
204
205static void
206f_count(arg)
207 char *arg;
208{
209
210 cpy_cnt = get_num(arg);
211 if (cpy_cnt < 0)
212 errx(1, "count cannot be negative");
213 if (cpy_cnt == 0)
214 cpy_cnt = -1;
215}
216
217static void
218f_files(arg)
219 char *arg;
220{
221
222 files_cnt = get_num(arg);
223 if (files_cnt < 1)
224 errx(1, "files must be between 1 and %qd", QUAD_MAX);
225}
226
227static void
228f_ibs(arg)
229 char *arg;
230{
231 quad_t res;
232
233 if (!(ddflags & C_BS)) {
234 res = get_num(arg);
235 if (res < 1 || res > SSIZE_MAX)
236 errx(1, "ibs must be between 1 and %d", SSIZE_MAX);
237 in.dbsz = (size_t)res;
238 }
239}
240
241static void
242f_if(arg)
243 char *arg;
244{
245
246 in.name = arg;
247}
248
249static void
250f_obs(arg)
251 char *arg;
252{
253 quad_t res;
254
255 if (!(ddflags & C_BS)) {
256 res = get_num(arg);
257 if (res < 1 || res > SSIZE_MAX)
258 errx(1, "obs must be between 1 and %d", SSIZE_MAX);
259 out.dbsz = (size_t)res;
260 }
261}
262
263static void
264f_of(arg)
265 char *arg;
266{
267
268 out.name = arg;
269}
270
271static void
272f_seek(arg)
273 char *arg;
274{
275
276 out.offset = get_offset(arg);
277}
278
279static void
280f_skip(arg)
281 char *arg;
282{
283
284 in.offset = get_offset(arg);
285}
286
287static const struct conv {
288 const char *name;
289 u_int set, noset;
290 const u_char *ctab;
291} clist[] = {
292 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
293 { "block", C_BLOCK, C_UNBLOCK, NULL },
294 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
295 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
296 { "lcase", C_LCASE, C_UCASE, NULL },
297 { "noerror", C_NOERROR, 0, NULL },
298 { "notrunc", C_NOTRUNC, 0, NULL },
299 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
300 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
301 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
302 { "osync", C_OSYNC, C_BS, NULL },
303 { "sparse", C_SPARSE, 0, NULL },
304 { "swab", C_SWAB, 0, NULL },
305 { "sync", C_SYNC, 0, NULL },
306 { "ucase", C_UCASE, C_LCASE, NULL },
307 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
308};
309
310static void
311f_conv(arg)
312 char *arg;
313{
314 struct conv *cp, tmp;
315
316 while (arg != NULL) {
317 tmp.name = strsep(&arg, ",");
318 cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv),
319 sizeof(struct conv), c_conv);
320 if (cp == NULL)
321 errx(1, "unknown conversion %s", tmp.name);
322 if (ddflags & cp->noset)
323 errx(1, "%s: illegal conversion combination", tmp.name);
324 ddflags |= cp->set;
325 if (cp->ctab)
326 ctab = cp->ctab;
327 }
328}
329
330static int
331c_conv(a, b)
332 const void *a, *b;
333{
334 typedef const struct conv *c_conv_p;
335
334
336 return (strcmp(((c_conv_p)a)->name, ((c_conv_p)b)->name));
335 return (strcmp(((const struct conv *)a)->name,
336 ((const struct conv *)b)->name));
337}
338
339/*
340 * Convert an expression of the following forms to a quad_t.
341 * 1) A positive decimal number.
342 * 2) A positive decimal number followed by a b (mult by 512.)
343 * 3) A positive decimal number followed by a k (mult by 1 << 10.)
344 * 4) A positive decimal number followed by a m (mult by 1 << 20.)
345 * 5) A positive decimal number followed by a g (mult by 1 << 30.)
346 * 5) A positive decimal number followed by a w (mult by sizeof int.)
347 * 6) Two or more positive decimal numbers (with/without [bkmgw])
348 * separated by x (also * for backwards compatibility), specifying
349 * the product of the indicated values.
350 */
351static quad_t
352get_num(val)
353 char *val;
354{
355 quad_t num, t;
356 char *expr;
357
358 errno = 0;
359 num = strtoq(val, &expr, 0);
360 if (errno != 0) /* Overflow or underflow. */
361 err(1, "%s", oper);
362
363 if (expr == val) /* No valid digits. */
364 errx(1, "%s: illegal numeric value", oper);
365
366 switch (*expr) {
367 case 'b':
368 t = num;
369 num *= 512;
370 if (t > num)
371 goto erange;
372 ++expr;
373 break;
374 case 'k':
375 t = num;
376 num *= 1 << 10;
377 if (t > num)
378 goto erange;
379 ++expr;
380 break;
381 case 'm':
382 t = num;
383 num *= 1 << 20;
384 if (t > num)
385 goto erange;
386 ++expr;
387 break;
388 case 'g':
389 t = num;
390 num *= 1 << 30;
391 if (t > num)
392 goto erange;
393 ++expr;
394 break;
395 case 'w':
396 t = num;
397 num *= sizeof(int);
398 if (t > num)
399 goto erange;
400 ++expr;
401 break;
402 }
403
404 switch (*expr) {
405 case '\0':
406 break;
407 case '*': /* Backward compatible. */
408 case 'x':
409 t = num;
410 num *= get_num(expr + 1);
411 if (t <= num)
412 break;
413erange:
414 errx(1, "%s: %s", oper, strerror(ERANGE));
415 default:
416 errx(1, "%s: illegal numeric value", oper);
417 }
418 return (num);
419}
420
421static off_t
422get_offset(val)
423 char *val;
424{
425 quad_t num;
426
427 num = get_num(val);
428 if (num > QUAD_MAX || num < 0) /* XXX quad_t != off_t */
429 errx(1, "%s: illegal offset", oper); /* Too big/negative. */
430 return ((off_t)num);
431}
337}
338
339/*
340 * Convert an expression of the following forms to a quad_t.
341 * 1) A positive decimal number.
342 * 2) A positive decimal number followed by a b (mult by 512.)
343 * 3) A positive decimal number followed by a k (mult by 1 << 10.)
344 * 4) A positive decimal number followed by a m (mult by 1 << 20.)
345 * 5) A positive decimal number followed by a g (mult by 1 << 30.)
346 * 5) A positive decimal number followed by a w (mult by sizeof int.)
347 * 6) Two or more positive decimal numbers (with/without [bkmgw])
348 * separated by x (also * for backwards compatibility), specifying
349 * the product of the indicated values.
350 */
351static quad_t
352get_num(val)
353 char *val;
354{
355 quad_t num, t;
356 char *expr;
357
358 errno = 0;
359 num = strtoq(val, &expr, 0);
360 if (errno != 0) /* Overflow or underflow. */
361 err(1, "%s", oper);
362
363 if (expr == val) /* No valid digits. */
364 errx(1, "%s: illegal numeric value", oper);
365
366 switch (*expr) {
367 case 'b':
368 t = num;
369 num *= 512;
370 if (t > num)
371 goto erange;
372 ++expr;
373 break;
374 case 'k':
375 t = num;
376 num *= 1 << 10;
377 if (t > num)
378 goto erange;
379 ++expr;
380 break;
381 case 'm':
382 t = num;
383 num *= 1 << 20;
384 if (t > num)
385 goto erange;
386 ++expr;
387 break;
388 case 'g':
389 t = num;
390 num *= 1 << 30;
391 if (t > num)
392 goto erange;
393 ++expr;
394 break;
395 case 'w':
396 t = num;
397 num *= sizeof(int);
398 if (t > num)
399 goto erange;
400 ++expr;
401 break;
402 }
403
404 switch (*expr) {
405 case '\0':
406 break;
407 case '*': /* Backward compatible. */
408 case 'x':
409 t = num;
410 num *= get_num(expr + 1);
411 if (t <= num)
412 break;
413erange:
414 errx(1, "%s: %s", oper, strerror(ERANGE));
415 default:
416 errx(1, "%s: illegal numeric value", oper);
417 }
418 return (num);
419}
420
421static off_t
422get_offset(val)
423 char *val;
424{
425 quad_t num;
426
427 num = get_num(val);
428 if (num > QUAD_MAX || num < 0) /* XXX quad_t != off_t */
429 errx(1, "%s: illegal offset", oper); /* Too big/negative. */
430 return ((off_t)num);
431}