11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Keith Muller of the University of California, San Diego and Lance
71556Srgrimes * Visser of Convex Computer Corporation.
81556Srgrimes *
91556Srgrimes * Redistribution and use in source and binary forms, with or without
101556Srgrimes * modification, are permitted provided that the following conditions
111556Srgrimes * are met:
121556Srgrimes * 1. Redistributions of source code must retain the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer.
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes */
331556Srgrimes
341556Srgrimes#ifndef lint
3535773Scharnier#if 0
3636007Scharnierstatic char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
3735773Scharnier#endif
381556Srgrimes#endif /* not lint */
3999109Sobrien#include <sys/cdefs.h>
4099109Sobrien__FBSDID("$FreeBSD: releng/11.0/bin/dd/args.c 296156 2016-02-28 10:27:12Z trasz $");
411556Srgrimes
421556Srgrimes#include <sys/types.h>
431556Srgrimes
441556Srgrimes#include <err.h>
451556Srgrimes#include <errno.h>
46111629Smarkm#include <inttypes.h>
471556Srgrimes#include <limits.h>
48250469Seadler#include <signal.h>
491556Srgrimes#include <stdlib.h>
501556Srgrimes#include <string.h>
511556Srgrimes
521556Srgrimes#include "dd.h"
531556Srgrimes#include "extern.h"
541556Srgrimes
5590108Simpstatic int	c_arg(const void *, const void *);
5690108Simpstatic int	c_conv(const void *, const void *);
5790108Simpstatic void	f_bs(char *);
5890108Simpstatic void	f_cbs(char *);
5990108Simpstatic void	f_conv(char *);
6090108Simpstatic void	f_count(char *);
6190108Simpstatic void	f_files(char *);
62133762Srwatsonstatic void	f_fillchar(char *);
6390108Simpstatic void	f_ibs(char *);
6490108Simpstatic void	f_if(char *);
6590108Simpstatic void	f_obs(char *);
6690108Simpstatic void	f_of(char *);
6790108Simpstatic void	f_seek(char *);
6890108Simpstatic void	f_skip(char *);
69296156Straszstatic void	f_speed(char *);
70264059Sdelphijstatic void	f_status(char *);
71111629Smarkmstatic uintmax_t get_num(const char *);
7290108Simpstatic off_t	get_off_t(const char *);
731556Srgrimes
7451208Sgreenstatic const struct arg {
7554245Sgreen	const char *name;
7690108Simp	void (*f)(char *);
771556Srgrimes	u_int set, noset;
781556Srgrimes} args[] = {
791556Srgrimes	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
801556Srgrimes	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
811556Srgrimes	{ "conv",	f_conv,		0,	 0 },
821556Srgrimes	{ "count",	f_count,	C_COUNT, C_COUNT },
831556Srgrimes	{ "files",	f_files,	C_FILES, C_FILES },
84133762Srwatson	{ "fillchar",	f_fillchar,	C_FILL,	 C_FILL },
851556Srgrimes	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
861556Srgrimes	{ "if",		f_if,		C_IF,	 C_IF },
8757523Sgreen	{ "iseek",	f_skip,		C_SKIP,	 C_SKIP },
881556Srgrimes	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
891556Srgrimes	{ "of",		f_of,		C_OF,	 C_OF },
9057523Sgreen	{ "oseek",	f_seek,		C_SEEK,	 C_SEEK },
911556Srgrimes	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
921556Srgrimes	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
93296156Strasz	{ "speed",	f_speed,	0,	 0 },
94264059Sdelphij	{ "status",	f_status,	C_STATUS,C_STATUS },
951556Srgrimes};
961556Srgrimes
971556Srgrimesstatic char *oper;
981556Srgrimes
991556Srgrimes/*
1001556Srgrimes * args -- parse JCL syntax of dd.
1011556Srgrimes */
1021556Srgrimesvoid
10390108Simpjcl(char **argv)
1041556Srgrimes{
1051556Srgrimes	struct arg *ap, tmp;
1061556Srgrimes	char *arg;
1071556Srgrimes
1081556Srgrimes	in.dbsz = out.dbsz = 512;
1091556Srgrimes
11019720Sphk	while ((oper = *++argv) != NULL) {
11130230Seivind		if ((oper = strdup(oper)) == NULL)
11248051Sgreen			errx(1, "unable to allocate space for the argument \"%s\"", *argv);
1131556Srgrimes		if ((arg = strchr(oper, '=')) == NULL)
1141556Srgrimes			errx(1, "unknown operand %s", oper);
1151556Srgrimes		*arg++ = '\0';
1161556Srgrimes		if (!*arg)
1171556Srgrimes			errx(1, "no value specified for %s", oper);
1181556Srgrimes		tmp.name = oper;
11948051Sgreen		if (!(ap = (struct arg *)bsearch(&tmp, args,
12048051Sgreen		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
12148051Sgreen		    c_arg)))
1221556Srgrimes			errx(1, "unknown operand %s", tmp.name);
1231556Srgrimes		if (ddflags & ap->noset)
12448051Sgreen			errx(1, "%s: illegal argument combination or already set",
12548051Sgreen			    tmp.name);
1261556Srgrimes		ddflags |= ap->set;
1271556Srgrimes		ap->f(arg);
1281556Srgrimes	}
1291556Srgrimes
1301556Srgrimes	/* Final sanity checks. */
1311556Srgrimes
1321556Srgrimes	if (ddflags & C_BS) {
1331556Srgrimes		/*
1341556Srgrimes		 * Bs is turned off by any conversion -- we assume the user
1351556Srgrimes		 * just wanted to set both the input and output block sizes
1361556Srgrimes		 * and didn't want the bs semantics, so we don't warn.
1371556Srgrimes		 */
13862311Sgreen		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
13962311Sgreen		    C_UNBLOCK))
1401556Srgrimes			ddflags &= ~C_BS;
1411556Srgrimes
1421556Srgrimes		/* Bs supersedes ibs and obs. */
14362311Sgreen		if (ddflags & C_BS && ddflags & (C_IBS | C_OBS))
1441556Srgrimes			warnx("bs supersedes ibs and obs");
1451556Srgrimes	}
1461556Srgrimes
1471556Srgrimes	/*
1481556Srgrimes	 * Ascii/ebcdic and cbs implies block/unblock.
1491556Srgrimes	 * Block/unblock requires cbs and vice-versa.
1501556Srgrimes	 */
15151249Sgreen	if (ddflags & (C_BLOCK | C_UNBLOCK)) {
1521556Srgrimes		if (!(ddflags & C_CBS))
1531556Srgrimes			errx(1, "record operations require cbs");
1541556Srgrimes		if (cbsz == 0)
1551556Srgrimes			errx(1, "cbs cannot be zero");
1561556Srgrimes		cfunc = ddflags & C_BLOCK ? block : unblock;
1571556Srgrimes	} else if (ddflags & C_CBS) {
15851249Sgreen		if (ddflags & (C_ASCII | C_EBCDIC)) {
1591556Srgrimes			if (ddflags & C_ASCII) {
1601556Srgrimes				ddflags |= C_UNBLOCK;
1611556Srgrimes				cfunc = unblock;
1621556Srgrimes			} else {
1631556Srgrimes				ddflags |= C_BLOCK;
1641556Srgrimes				cfunc = block;
1651556Srgrimes			}
1661556Srgrimes		} else
1671556Srgrimes			errx(1, "cbs meaningless if not doing record operations");
1681556Srgrimes	} else
1691556Srgrimes		cfunc = def;
17051249Sgreen
17151208Sgreen	/*
17251208Sgreen	 * Bail out if the calculation of a file offset would overflow.
17351208Sgreen	 */
174111629Smarkm	if (in.offset > OFF_MAX / (ssize_t)in.dbsz ||
175111629Smarkm	    out.offset > OFF_MAX / (ssize_t)out.dbsz)
176273743Spi		errx(1, "seek offsets cannot be larger than %jd",
177273743Spi		    (intmax_t)OFF_MAX);
1781556Srgrimes}
1791556Srgrimes
1801556Srgrimesstatic int
18190108Simpc_arg(const void *a, const void *b)
1821556Srgrimes{
1831556Srgrimes
18454278Sgreen	return (strcmp(((const struct arg *)a)->name,
18554278Sgreen	    ((const struct arg *)b)->name));
1861556Srgrimes}
1871556Srgrimes
1881556Srgrimesstatic void
18990108Simpf_bs(char *arg)
1901556Srgrimes{
191273743Spi	uintmax_t res;
1921556Srgrimes
193273743Spi	res = get_num(arg);
194273743Spi	if (res < 1 || res > SSIZE_MAX)
195273743Spi		errx(1, "bs must be between 1 and %jd", (intmax_t)SSIZE_MAX);
196273743Spi	in.dbsz = out.dbsz = (size_t)res;
1971556Srgrimes}
1981556Srgrimes
1991556Srgrimesstatic void
20090108Simpf_cbs(char *arg)
2011556Srgrimes{
202273743Spi	uintmax_t res;
2031556Srgrimes
204273743Spi	res = get_num(arg);
205273743Spi	if (res < 1 || res > SSIZE_MAX)
206273743Spi		errx(1, "cbs must be between 1 and %jd", (intmax_t)SSIZE_MAX);
207273743Spi	cbsz = (size_t)res;
2081556Srgrimes}
2091556Srgrimes
2101556Srgrimesstatic void
21190108Simpf_count(char *arg)
2121556Srgrimes{
213273743Spi	intmax_t res;
2141556Srgrimes
215273743Spi	res = (intmax_t)get_num(arg);
216273743Spi	if (res < 0)
217273743Spi		errx(1, "count cannot be negative");
218273743Spi	if (res == 0)
219273743Spi		cpy_cnt = (uintmax_t)-1;
220273743Spi	else
221273743Spi		cpy_cnt = (uintmax_t)res;
2221556Srgrimes}
2231556Srgrimes
2241556Srgrimesstatic void
22590108Simpf_files(char *arg)
2261556Srgrimes{
2271556Srgrimes
22851137Sgreen	files_cnt = get_num(arg);
22951249Sgreen	if (files_cnt < 1)
230273743Spi		errx(1, "files must be between 1 and %jd", (uintmax_t)-1);
2311556Srgrimes}
2321556Srgrimes
2331556Srgrimesstatic void
234133762Srwatsonf_fillchar(char *arg)
235133762Srwatson{
236133762Srwatson
237133762Srwatson	if (strlen(arg) != 1)
238133762Srwatson		errx(1, "need exactly one fill char");
239133762Srwatson
240133762Srwatson	fill_char = arg[0];
241133762Srwatson}
242133762Srwatson
243133762Srwatsonstatic void
24490108Simpf_ibs(char *arg)
2451556Srgrimes{
246273743Spi	uintmax_t res;
2471556Srgrimes
24848051Sgreen	if (!(ddflags & C_BS)) {
249273743Spi		res = get_num(arg);
250273743Spi		if (res < 1 || res > SSIZE_MAX)
251273743Spi			errx(1, "ibs must be between 1 and %jd",
252273743Spi			    (intmax_t)SSIZE_MAX);
253273743Spi		in.dbsz = (size_t)res;
25448051Sgreen	}
2551556Srgrimes}
2561556Srgrimes
2571556Srgrimesstatic void
25890108Simpf_if(char *arg)
2591556Srgrimes{
2601556Srgrimes
2611556Srgrimes	in.name = arg;
2621556Srgrimes}
2631556Srgrimes
2641556Srgrimesstatic void
26590108Simpf_obs(char *arg)
2661556Srgrimes{
267273743Spi	uintmax_t res;
2681556Srgrimes
26948051Sgreen	if (!(ddflags & C_BS)) {
270273743Spi		res = get_num(arg);
271273743Spi		if (res < 1 || res > SSIZE_MAX)
272273743Spi			errx(1, "obs must be between 1 and %jd",
273273743Spi			    (intmax_t)SSIZE_MAX);
274273743Spi		out.dbsz = (size_t)res;
27548051Sgreen	}
2761556Srgrimes}
2771556Srgrimes
2781556Srgrimesstatic void
27990108Simpf_of(char *arg)
2801556Srgrimes{
2811556Srgrimes
2821556Srgrimes	out.name = arg;
2831556Srgrimes}
2841556Srgrimes
2851556Srgrimesstatic void
28690108Simpf_seek(char *arg)
2871556Srgrimes{
2881556Srgrimes
28989788Sgreen	out.offset = get_off_t(arg);
2901556Srgrimes}
2911556Srgrimes
2921556Srgrimesstatic void
29390108Simpf_skip(char *arg)
2941556Srgrimes{
2951556Srgrimes
29689788Sgreen	in.offset = get_off_t(arg);
2971556Srgrimes}
2981556Srgrimes
299264059Sdelphijstatic void
300296156Straszf_speed(char *arg)
301296156Strasz{
302296156Strasz
303296156Strasz	speed = get_num(arg);
304296156Strasz}
305296156Strasz
306296156Straszstatic void
307264059Sdelphijf_status(char *arg)
308264059Sdelphij{
309264059Sdelphij
310264059Sdelphij	if (strcmp(arg, "none") == 0)
311264059Sdelphij		ddflags |= C_NOINFO;
312264059Sdelphij	else if (strcmp(arg, "noxfer") == 0)
313264059Sdelphij		ddflags |= C_NOXFER;
314264059Sdelphij	else
315264059Sdelphij		errx(1, "unknown status %s", arg);
316264059Sdelphij}
317264059Sdelphij
31851208Sgreenstatic const struct conv {
31954245Sgreen	const char *name;
3201556Srgrimes	u_int set, noset;
32151208Sgreen	const u_char *ctab;
3221556Srgrimes} clist[] = {
3231556Srgrimes	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
3241556Srgrimes	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
3251556Srgrimes	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
3261556Srgrimes	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
3271556Srgrimes	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
3281556Srgrimes	{ "noerror",	C_NOERROR,	0,		NULL },
3291556Srgrimes	{ "notrunc",	C_NOTRUNC,	0,		NULL },
3301556Srgrimes	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
3311556Srgrimes	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
3321556Srgrimes	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
33331120Sjoerg	{ "osync",	C_OSYNC,	C_BS,		NULL },
334126667Sphk	{ "pareven",	C_PAREVEN,	C_PARODD|C_PARSET|C_PARNONE, NULL},
335126667Sphk	{ "parnone",	C_PARNONE,	C_PARODD|C_PARSET|C_PAREVEN, NULL},
336126667Sphk	{ "parodd",	C_PARODD,	C_PAREVEN|C_PARSET|C_PARNONE, NULL},
337126667Sphk	{ "parset",	C_PARSET,	C_PARODD|C_PAREVEN|C_PARNONE, NULL},
33830312Sjoerg	{ "sparse",	C_SPARSE,	0,		NULL },
3391556Srgrimes	{ "swab",	C_SWAB,		0,		NULL },
3401556Srgrimes	{ "sync",	C_SYNC,		0,		NULL },
3411556Srgrimes	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
3421556Srgrimes	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
3431556Srgrimes};
3441556Srgrimes
3451556Srgrimesstatic void
34690108Simpf_conv(char *arg)
3471556Srgrimes{
3481556Srgrimes	struct conv *cp, tmp;
3491556Srgrimes
3501556Srgrimes	while (arg != NULL) {
3511556Srgrimes		tmp.name = strsep(&arg, ",");
35251208Sgreen		cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv),
35351208Sgreen		    sizeof(struct conv), c_conv);
35451208Sgreen		if (cp == NULL)
3551556Srgrimes			errx(1, "unknown conversion %s", tmp.name);
3561556Srgrimes		if (ddflags & cp->noset)
3571556Srgrimes			errx(1, "%s: illegal conversion combination", tmp.name);
3581556Srgrimes		ddflags |= cp->set;
3591556Srgrimes		if (cp->ctab)
3601556Srgrimes			ctab = cp->ctab;
3611556Srgrimes	}
3621556Srgrimes}
3631556Srgrimes
3641556Srgrimesstatic int
36590108Simpc_conv(const void *a, const void *b)
3661556Srgrimes{
3671556Srgrimes
36854278Sgreen	return (strcmp(((const struct conv *)a)->name,
36954278Sgreen	    ((const struct conv *)b)->name));
3701556Srgrimes}
3711556Srgrimes
372295427Straszstatic intmax_t
373295260Straszpostfix_to_mult(const char expr)
374295260Strasz{
375295427Strasz	intmax_t mult;
376295260Strasz
377295260Strasz	mult = 0;
378295260Strasz	switch (expr) {
379295260Strasz	case 'B':
380295260Strasz	case 'b':
381295260Strasz		mult = 512;
382295260Strasz		break;
383295260Strasz	case 'K':
384295260Strasz	case 'k':
385295260Strasz		mult = 1 << 10;
386295260Strasz		break;
387295260Strasz	case 'M':
388295260Strasz	case 'm':
389295260Strasz		mult = 1 << 20;
390295260Strasz		break;
391295260Strasz	case 'G':
392295260Strasz	case 'g':
393295260Strasz		mult = 1 << 30;
394295260Strasz		break;
395295261Strasz	case 'T':
396295261Strasz	case 't':
397295261Strasz		mult = (uintmax_t)1 << 40;
398295261Strasz		break;
399295261Strasz	case 'P':
400295261Strasz	case 'p':
401295261Strasz		mult = (uintmax_t)1 << 50;
402295261Strasz		break;
403295260Strasz	case 'W':
404295260Strasz	case 'w':
405295260Strasz		mult = sizeof(int);
406295260Strasz		break;
407295260Strasz	}
408295260Strasz
409295260Strasz	return (mult);
410295260Strasz}
411295260Strasz
4121556Srgrimes/*
413111629Smarkm * Convert an expression of the following forms to a uintmax_t.
4141556Srgrimes * 	1) A positive decimal number.
415132933Spjd *	2) A positive decimal number followed by a 'b' or 'B' (mult by 512).
416132933Spjd *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
417132933Spjd *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
418132933Spjd *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
419295428Strasz *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
420295428Strasz *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
421295428Strasz *	8) A positive decimal number followed by a 'w' or 'W' (mult by sizeof int).
422295428Strasz *	9) Two or more positive decimal numbers (with/without [BbKkMmGgWw])
423132933Spjd *	   separated by 'x' or 'X' (also '*' for backwards compatibility),
424132933Spjd *	   specifying the product of the indicated values.
4251556Srgrimes */
426111629Smarkmstatic uintmax_t
42790108Simpget_num(const char *val)
4281556Srgrimes{
429111629Smarkm	uintmax_t num, mult, prevnum;
4301556Srgrimes	char *expr;
4311556Srgrimes
43248051Sgreen	errno = 0;
433295426Strasz	num = strtoumax(val, &expr, 0);
434295854Strasz	if (expr == val)			/* No valid digits. */
435295854Strasz		errx(1, "%s: invalid numeric value", oper);
436295854Strasz	if (errno != 0)
43748051Sgreen		err(1, "%s", oper);
4381556Srgrimes
439295260Strasz	mult = postfix_to_mult(*expr);
4401556Srgrimes
44189788Sgreen	if (mult != 0) {
44289788Sgreen		prevnum = num;
44389788Sgreen		num *= mult;
44489788Sgreen		/* Check for overflow. */
44589788Sgreen		if (num / mult != prevnum)
44689788Sgreen			goto erange;
44789788Sgreen		expr++;
44889788Sgreen	}
44989788Sgreen
45051137Sgreen	switch (*expr) {
4511556Srgrimes		case '\0':
4521556Srgrimes			break;
4531556Srgrimes		case '*':			/* Backward compatible. */
454132933Spjd		case 'X':
4551556Srgrimes		case 'x':
45689788Sgreen			mult = get_num(expr + 1);
45789788Sgreen			prevnum = num;
45889788Sgreen			num *= mult;
45989788Sgreen			if (num / mult == prevnum)
46051137Sgreen				break;
46151137Sgreenerange:
46251137Sgreen			errx(1, "%s: %s", oper, strerror(ERANGE));
4631556Srgrimes		default:
4641556Srgrimes			errx(1, "%s: illegal numeric value", oper);
4651556Srgrimes	}
4661556Srgrimes	return (num);
4671556Srgrimes}
46851208Sgreen
46989788Sgreen/*
47089788Sgreen * Convert an expression of the following forms to an off_t.  This is the
47189788Sgreen * same as get_num(), but it uses signed numbers.
47289788Sgreen *
473111629Smarkm * The major problem here is that an off_t may not necessarily be a intmax_t.
47489788Sgreen */
47551208Sgreenstatic off_t
47690108Simpget_off_t(const char *val)
47751208Sgreen{
478111629Smarkm	intmax_t num, mult, prevnum;
47989788Sgreen	char *expr;
48051208Sgreen
48189788Sgreen	errno = 0;
482295426Strasz	num = strtoimax(val, &expr, 0);
483295854Strasz	if (expr == val)			/* No valid digits. */
484295854Strasz		errx(1, "%s: invalid numeric value", oper);
485295854Strasz	if (errno != 0)
48689788Sgreen		err(1, "%s", oper);
48789788Sgreen
488295260Strasz	mult = postfix_to_mult(*expr);
48989788Sgreen
49089788Sgreen	if (mult != 0) {
49189788Sgreen		prevnum = num;
49289788Sgreen		num *= mult;
49389788Sgreen		/* Check for overflow. */
49489788Sgreen		if ((prevnum > 0) != (num > 0) || num / mult != prevnum)
49589788Sgreen			goto erange;
49689788Sgreen		expr++;
49789788Sgreen	}
49889788Sgreen
49989788Sgreen	switch (*expr) {
50089788Sgreen		case '\0':
50189788Sgreen			break;
50289788Sgreen		case '*':			/* Backward compatible. */
503132933Spjd		case 'X':
50489788Sgreen		case 'x':
505111629Smarkm			mult = (intmax_t)get_off_t(expr + 1);
50689788Sgreen			prevnum = num;
50789788Sgreen			num *= mult;
50890331Sgreen			if ((prevnum > 0) == (num > 0) && num / mult == prevnum)
50989788Sgreen				break;
51089788Sgreenerange:
51189788Sgreen			errx(1, "%s: %s", oper, strerror(ERANGE));
51289788Sgreen		default:
51389788Sgreen			errx(1, "%s: illegal numeric value", oper);
51489788Sgreen	}
51589788Sgreen	return (num);
51651208Sgreen}
517