xref: /freebsd/bin/dd/args.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
94b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #ifndef lint
37cbf6f7d3SPhilippe Charnier #if 0
381ba0e048SPhilippe Charnier static char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
39cbf6f7d3SPhilippe Charnier #endif
404b88c807SRodney W. Grimes #endif /* not lint */
415eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
425eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #include <sys/types.h>
454b88c807SRodney W. Grimes 
4677a798b3SAlan Somers #include <ctype.h>
474b88c807SRodney W. Grimes #include <err.h>
484b88c807SRodney W. Grimes #include <errno.h>
497503d74fSMark Murray #include <inttypes.h>
504b88c807SRodney W. Grimes #include <limits.h>
514ac11639SEitan Adler #include <signal.h>
524b88c807SRodney W. Grimes #include <stdlib.h>
534b88c807SRodney W. Grimes #include <string.h>
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes #include "dd.h"
564b88c807SRodney W. Grimes #include "extern.h"
574b88c807SRodney W. Grimes 
58f9bcb0beSWarner Losh static int	c_arg(const void *, const void *);
59f9bcb0beSWarner Losh static int	c_conv(const void *, const void *);
60f9bcb0beSWarner Losh static void	f_bs(char *);
61f9bcb0beSWarner Losh static void	f_cbs(char *);
62f9bcb0beSWarner Losh static void	f_conv(char *);
63f9bcb0beSWarner Losh static void	f_count(char *);
64f9bcb0beSWarner Losh static void	f_files(char *);
65e3edab4aSRobert Watson static void	f_fillchar(char *);
66f9bcb0beSWarner Losh static void	f_ibs(char *);
67f9bcb0beSWarner Losh static void	f_if(char *);
68f9bcb0beSWarner Losh static void	f_obs(char *);
69f9bcb0beSWarner Losh static void	f_of(char *);
70f9bcb0beSWarner Losh static void	f_seek(char *);
71f9bcb0beSWarner Losh static void	f_skip(char *);
7275e55112SEdward Tomasz Napierala static void	f_speed(char *);
73c3f5e9c5SXin LI static void	f_status(char *);
747503d74fSMark Murray static uintmax_t get_num(const char *);
75f9bcb0beSWarner Losh static off_t	get_off_t(const char *);
764b88c807SRodney W. Grimes 
7758687472SBrian Feldman static const struct arg {
7867f80d12SBrian Feldman 	const char *name;
79f9bcb0beSWarner Losh 	void (*f)(char *);
804b88c807SRodney W. Grimes 	u_int set, noset;
814b88c807SRodney W. Grimes } args[] = {
824b88c807SRodney W. Grimes 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
834b88c807SRodney W. Grimes 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
844b88c807SRodney W. Grimes 	{ "conv",	f_conv,		0,	 0 },
854b88c807SRodney W. Grimes 	{ "count",	f_count,	C_COUNT, C_COUNT },
864b88c807SRodney W. Grimes 	{ "files",	f_files,	C_FILES, C_FILES },
87e3edab4aSRobert Watson 	{ "fillchar",	f_fillchar,	C_FILL,	 C_FILL },
884b88c807SRodney W. Grimes 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
894b88c807SRodney W. Grimes 	{ "if",		f_if,		C_IF,	 C_IF },
90a6d413e7SBrian Feldman 	{ "iseek",	f_skip,		C_SKIP,	 C_SKIP },
914b88c807SRodney W. Grimes 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
924b88c807SRodney W. Grimes 	{ "of",		f_of,		C_OF,	 C_OF },
93a6d413e7SBrian Feldman 	{ "oseek",	f_seek,		C_SEEK,	 C_SEEK },
944b88c807SRodney W. Grimes 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
954b88c807SRodney W. Grimes 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
9675e55112SEdward Tomasz Napierala 	{ "speed",	f_speed,	0,	 0 },
97c3f5e9c5SXin LI 	{ "status",	f_status,	C_STATUS,C_STATUS },
984b88c807SRodney W. Grimes };
994b88c807SRodney W. Grimes 
1004b88c807SRodney W. Grimes static char *oper;
1014b88c807SRodney W. Grimes 
1024b88c807SRodney W. Grimes /*
1034b88c807SRodney W. Grimes  * args -- parse JCL syntax of dd.
1044b88c807SRodney W. Grimes  */
1054b88c807SRodney W. Grimes void
106f9bcb0beSWarner Losh jcl(char **argv)
1074b88c807SRodney W. Grimes {
1084b88c807SRodney W. Grimes 	struct arg *ap, tmp;
1094b88c807SRodney W. Grimes 	char *arg;
1104b88c807SRodney W. Grimes 
1114b88c807SRodney W. Grimes 	in.dbsz = out.dbsz = 512;
1124b88c807SRodney W. Grimes 
113ad66f7eeSPoul-Henning Kamp 	while ((oper = *++argv) != NULL) {
1144c339742SEivind Eklund 		if ((oper = strdup(oper)) == NULL)
11554946e00SBrian Feldman 			errx(1, "unable to allocate space for the argument \"%s\"", *argv);
1164b88c807SRodney W. Grimes 		if ((arg = strchr(oper, '=')) == NULL)
1174b88c807SRodney W. Grimes 			errx(1, "unknown operand %s", oper);
1184b88c807SRodney W. Grimes 		*arg++ = '\0';
1194b88c807SRodney W. Grimes 		if (!*arg)
1204b88c807SRodney W. Grimes 			errx(1, "no value specified for %s", oper);
1214b88c807SRodney W. Grimes 		tmp.name = oper;
12254946e00SBrian Feldman 		if (!(ap = (struct arg *)bsearch(&tmp, args,
12354946e00SBrian Feldman 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
12454946e00SBrian Feldman 		    c_arg)))
1254b88c807SRodney W. Grimes 			errx(1, "unknown operand %s", tmp.name);
1264b88c807SRodney W. Grimes 		if (ddflags & ap->noset)
12754946e00SBrian Feldman 			errx(1, "%s: illegal argument combination or already set",
12854946e00SBrian Feldman 			    tmp.name);
1294b88c807SRodney W. Grimes 		ddflags |= ap->set;
1304b88c807SRodney W. Grimes 		ap->f(arg);
1314b88c807SRodney W. Grimes 	}
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes 	/* Final sanity checks. */
1344b88c807SRodney W. Grimes 
1354b88c807SRodney W. Grimes 	if (ddflags & C_BS) {
1364b88c807SRodney W. Grimes 		/*
1374b88c807SRodney W. Grimes 		 * Bs is turned off by any conversion -- we assume the user
1384b88c807SRodney W. Grimes 		 * just wanted to set both the input and output block sizes
1394b88c807SRodney W. Grimes 		 * and didn't want the bs semantics, so we don't warn.
1404b88c807SRodney W. Grimes 		 */
141c15c898eSBrian Feldman 		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
142c15c898eSBrian Feldman 		    C_UNBLOCK))
1434b88c807SRodney W. Grimes 			ddflags &= ~C_BS;
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 		/* Bs supersedes ibs and obs. */
1464b88c807SRodney W. Grimes 		if (ddflags & C_BS && ddflags & (C_IBS | C_OBS))
1474b88c807SRodney W. Grimes 			warnx("bs supersedes ibs and obs");
1484b88c807SRodney W. Grimes 	}
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes 	/*
1514b88c807SRodney W. Grimes 	 * Ascii/ebcdic and cbs implies block/unblock.
1524b88c807SRodney W. Grimes 	 * Block/unblock requires cbs and vice-versa.
1534b88c807SRodney W. Grimes 	 */
1544b88c807SRodney W. Grimes 	if (ddflags & (C_BLOCK | C_UNBLOCK)) {
1554b88c807SRodney W. Grimes 		if (!(ddflags & C_CBS))
1564b88c807SRodney W. Grimes 			errx(1, "record operations require cbs");
1574b88c807SRodney W. Grimes 		if (cbsz == 0)
1584b88c807SRodney W. Grimes 			errx(1, "cbs cannot be zero");
1594b88c807SRodney W. Grimes 		cfunc = ddflags & C_BLOCK ? block : unblock;
1604b88c807SRodney W. Grimes 	} else if (ddflags & C_CBS) {
1614b88c807SRodney W. Grimes 		if (ddflags & (C_ASCII | C_EBCDIC)) {
1624b88c807SRodney W. Grimes 			if (ddflags & C_ASCII) {
1634b88c807SRodney W. Grimes 				ddflags |= C_UNBLOCK;
1644b88c807SRodney W. Grimes 				cfunc = unblock;
1654b88c807SRodney W. Grimes 			} else {
1664b88c807SRodney W. Grimes 				ddflags |= C_BLOCK;
1674b88c807SRodney W. Grimes 				cfunc = block;
1684b88c807SRodney W. Grimes 			}
1694b88c807SRodney W. Grimes 		} else
1704b88c807SRodney W. Grimes 			errx(1, "cbs meaningless if not doing record operations");
1714b88c807SRodney W. Grimes 	} else
1724b88c807SRodney W. Grimes 		cfunc = def;
1734b88c807SRodney W. Grimes }
1744b88c807SRodney W. Grimes 
1754b88c807SRodney W. Grimes static int
176f9bcb0beSWarner Losh c_arg(const void *a, const void *b)
1774b88c807SRodney W. Grimes {
1784b88c807SRodney W. Grimes 
179dd923702SBrian Feldman 	return (strcmp(((const struct arg *)a)->name,
180dd923702SBrian Feldman 	    ((const struct arg *)b)->name));
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes static void
184f9bcb0beSWarner Losh f_bs(char *arg)
1854b88c807SRodney W. Grimes {
186f4cfd28bSKurt Jaeger 	uintmax_t res;
1874b88c807SRodney W. Grimes 
188f4cfd28bSKurt Jaeger 	res = get_num(arg);
189f4cfd28bSKurt Jaeger 	if (res < 1 || res > SSIZE_MAX)
1905ad496a2SAlan Somers 		errx(1, "bs must be between 1 and %zd", (ssize_t)SSIZE_MAX);
191f4cfd28bSKurt Jaeger 	in.dbsz = out.dbsz = (size_t)res;
1924b88c807SRodney W. Grimes }
1934b88c807SRodney W. Grimes 
1944b88c807SRodney W. Grimes static void
195f9bcb0beSWarner Losh f_cbs(char *arg)
1964b88c807SRodney W. Grimes {
197f4cfd28bSKurt Jaeger 	uintmax_t res;
1984b88c807SRodney W. Grimes 
199f4cfd28bSKurt Jaeger 	res = get_num(arg);
200f4cfd28bSKurt Jaeger 	if (res < 1 || res > SSIZE_MAX)
2015ad496a2SAlan Somers 		errx(1, "cbs must be between 1 and %zd", (ssize_t)SSIZE_MAX);
202f4cfd28bSKurt Jaeger 	cbsz = (size_t)res;
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes static void
206f9bcb0beSWarner Losh f_count(char *arg)
2074b88c807SRodney W. Grimes {
20877a798b3SAlan Somers 	uintmax_t res;
2094b88c807SRodney W. Grimes 
21077a798b3SAlan Somers 	res = get_num(arg);
21177a798b3SAlan Somers 	if (res == UINTMAX_MAX)
21277a798b3SAlan Somers 		errc(1, ERANGE, "%s", oper);
213f4cfd28bSKurt Jaeger 	if (res == 0)
21477a798b3SAlan Somers 		cpy_cnt = UINTMAX_MAX;
215f4cfd28bSKurt Jaeger 	else
21677a798b3SAlan Somers 		cpy_cnt = res;
2174b88c807SRodney W. Grimes }
2184b88c807SRodney W. Grimes 
2194b88c807SRodney W. Grimes static void
220f9bcb0beSWarner Losh f_files(char *arg)
2214b88c807SRodney W. Grimes {
2224b88c807SRodney W. Grimes 
223e14f7e78SBrian Feldman 	files_cnt = get_num(arg);
2247599187eSBrian Feldman 	if (files_cnt < 1)
22577a798b3SAlan Somers 		errx(1, "files must be between 1 and %zu", SIZE_MAX);
2264b88c807SRodney W. Grimes }
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes static void
229e3edab4aSRobert Watson f_fillchar(char *arg)
230e3edab4aSRobert Watson {
231e3edab4aSRobert Watson 
232e3edab4aSRobert Watson 	if (strlen(arg) != 1)
233e3edab4aSRobert Watson 		errx(1, "need exactly one fill char");
234e3edab4aSRobert Watson 
235e3edab4aSRobert Watson 	fill_char = arg[0];
236e3edab4aSRobert Watson }
237e3edab4aSRobert Watson 
238e3edab4aSRobert Watson static void
239f9bcb0beSWarner Losh f_ibs(char *arg)
2404b88c807SRodney W. Grimes {
241f4cfd28bSKurt Jaeger 	uintmax_t res;
2424b88c807SRodney W. Grimes 
24354946e00SBrian Feldman 	if (!(ddflags & C_BS)) {
244f4cfd28bSKurt Jaeger 		res = get_num(arg);
245f4cfd28bSKurt Jaeger 		if (res < 1 || res > SSIZE_MAX)
24677a798b3SAlan Somers 			errx(1, "ibs must be between 1 and %zd",
2475ad496a2SAlan Somers 			    (ssize_t)SSIZE_MAX);
248f4cfd28bSKurt Jaeger 		in.dbsz = (size_t)res;
24954946e00SBrian Feldman 	}
2504b88c807SRodney W. Grimes }
2514b88c807SRodney W. Grimes 
2524b88c807SRodney W. Grimes static void
253f9bcb0beSWarner Losh f_if(char *arg)
2544b88c807SRodney W. Grimes {
2554b88c807SRodney W. Grimes 
2564b88c807SRodney W. Grimes 	in.name = arg;
2574b88c807SRodney W. Grimes }
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes static void
260f9bcb0beSWarner Losh f_obs(char *arg)
2614b88c807SRodney W. Grimes {
262f4cfd28bSKurt Jaeger 	uintmax_t res;
2634b88c807SRodney W. Grimes 
26454946e00SBrian Feldman 	if (!(ddflags & C_BS)) {
265f4cfd28bSKurt Jaeger 		res = get_num(arg);
266f4cfd28bSKurt Jaeger 		if (res < 1 || res > SSIZE_MAX)
26777a798b3SAlan Somers 			errx(1, "obs must be between 1 and %zd",
2685ad496a2SAlan Somers 			    (ssize_t)SSIZE_MAX);
269f4cfd28bSKurt Jaeger 		out.dbsz = (size_t)res;
27054946e00SBrian Feldman 	}
2714b88c807SRodney W. Grimes }
2724b88c807SRodney W. Grimes 
2734b88c807SRodney W. Grimes static void
274f9bcb0beSWarner Losh f_of(char *arg)
2754b88c807SRodney W. Grimes {
2764b88c807SRodney W. Grimes 
2774b88c807SRodney W. Grimes 	out.name = arg;
2784b88c807SRodney W. Grimes }
2794b88c807SRodney W. Grimes 
2804b88c807SRodney W. Grimes static void
281f9bcb0beSWarner Losh f_seek(char *arg)
2824b88c807SRodney W. Grimes {
2834b88c807SRodney W. Grimes 
2844ed95537SBrian Feldman 	out.offset = get_off_t(arg);
2854b88c807SRodney W. Grimes }
2864b88c807SRodney W. Grimes 
2874b88c807SRodney W. Grimes static void
288f9bcb0beSWarner Losh f_skip(char *arg)
2894b88c807SRodney W. Grimes {
2904b88c807SRodney W. Grimes 
2914ed95537SBrian Feldman 	in.offset = get_off_t(arg);
2924b88c807SRodney W. Grimes }
2934b88c807SRodney W. Grimes 
294c3f5e9c5SXin LI static void
29575e55112SEdward Tomasz Napierala f_speed(char *arg)
29675e55112SEdward Tomasz Napierala {
29775e55112SEdward Tomasz Napierala 
29875e55112SEdward Tomasz Napierala 	speed = get_num(arg);
29975e55112SEdward Tomasz Napierala }
30075e55112SEdward Tomasz Napierala 
30175e55112SEdward Tomasz Napierala static void
302c3f5e9c5SXin LI f_status(char *arg)
303c3f5e9c5SXin LI {
304c3f5e9c5SXin LI 
305c3f5e9c5SXin LI 	if (strcmp(arg, "none") == 0)
306c3f5e9c5SXin LI 		ddflags |= C_NOINFO;
307c3f5e9c5SXin LI 	else if (strcmp(arg, "noxfer") == 0)
308c3f5e9c5SXin LI 		ddflags |= C_NOXFER;
309c3f5e9c5SXin LI 	else
310c3f5e9c5SXin LI 		errx(1, "unknown status %s", arg);
311c3f5e9c5SXin LI }
312c3f5e9c5SXin LI 
31358687472SBrian Feldman static const struct conv {
31467f80d12SBrian Feldman 	const char *name;
3154b88c807SRodney W. Grimes 	u_int set, noset;
31658687472SBrian Feldman 	const u_char *ctab;
3174b88c807SRodney W. Grimes } clist[] = {
3184b88c807SRodney W. Grimes 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
3194b88c807SRodney W. Grimes 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
3204b88c807SRodney W. Grimes 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
3214b88c807SRodney W. Grimes 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
3224b88c807SRodney W. Grimes 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
3234b88c807SRodney W. Grimes 	{ "noerror",	C_NOERROR,	0,		NULL },
3244b88c807SRodney W. Grimes 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
3254b88c807SRodney W. Grimes 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
3264b88c807SRodney W. Grimes 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
3274b88c807SRodney W. Grimes 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
3284b88c807SRodney W. Grimes 	{ "osync",	C_OSYNC,	C_BS,		NULL },
3296a3d33acSPoul-Henning Kamp 	{ "pareven",	C_PAREVEN,	C_PARODD|C_PARSET|C_PARNONE, NULL},
3306a3d33acSPoul-Henning Kamp 	{ "parnone",	C_PARNONE,	C_PARODD|C_PARSET|C_PAREVEN, NULL},
3316a3d33acSPoul-Henning Kamp 	{ "parodd",	C_PARODD,	C_PAREVEN|C_PARSET|C_PARNONE, NULL},
3326a3d33acSPoul-Henning Kamp 	{ "parset",	C_PARSET,	C_PARODD|C_PAREVEN|C_PARNONE, NULL},
3331898febeSJoerg Wunsch 	{ "sparse",	C_SPARSE,	0,		NULL },
3344b88c807SRodney W. Grimes 	{ "swab",	C_SWAB,		0,		NULL },
3354b88c807SRodney W. Grimes 	{ "sync",	C_SYNC,		0,		NULL },
3364b88c807SRodney W. Grimes 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
3374b88c807SRodney W. Grimes 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
3384b88c807SRodney W. Grimes };
3394b88c807SRodney W. Grimes 
3404b88c807SRodney W. Grimes static void
341f9bcb0beSWarner Losh f_conv(char *arg)
3424b88c807SRodney W. Grimes {
3434b88c807SRodney W. Grimes 	struct conv *cp, tmp;
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 	while (arg != NULL) {
3464b88c807SRodney W. Grimes 		tmp.name = strsep(&arg, ",");
34758687472SBrian Feldman 		cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv),
34858687472SBrian Feldman 		    sizeof(struct conv), c_conv);
34958687472SBrian Feldman 		if (cp == NULL)
3504b88c807SRodney W. Grimes 			errx(1, "unknown conversion %s", tmp.name);
3514b88c807SRodney W. Grimes 		if (ddflags & cp->noset)
3524b88c807SRodney W. Grimes 			errx(1, "%s: illegal conversion combination", tmp.name);
3534b88c807SRodney W. Grimes 		ddflags |= cp->set;
3544b88c807SRodney W. Grimes 		if (cp->ctab)
3554b88c807SRodney W. Grimes 			ctab = cp->ctab;
3564b88c807SRodney W. Grimes 	}
3574b88c807SRodney W. Grimes }
3584b88c807SRodney W. Grimes 
3594b88c807SRodney W. Grimes static int
360f9bcb0beSWarner Losh c_conv(const void *a, const void *b)
3614b88c807SRodney W. Grimes {
3624b88c807SRodney W. Grimes 
363dd923702SBrian Feldman 	return (strcmp(((const struct conv *)a)->name,
364dd923702SBrian Feldman 	    ((const struct conv *)b)->name));
3654b88c807SRodney W. Grimes }
3664b88c807SRodney W. Grimes 
367f3041442SEdward Tomasz Napierala static intmax_t
36866d082c8SEdward Tomasz Napierala postfix_to_mult(const char expr)
36966d082c8SEdward Tomasz Napierala {
370f3041442SEdward Tomasz Napierala 	intmax_t mult;
37166d082c8SEdward Tomasz Napierala 
37266d082c8SEdward Tomasz Napierala 	mult = 0;
37366d082c8SEdward Tomasz Napierala 	switch (expr) {
37466d082c8SEdward Tomasz Napierala 	case 'B':
37566d082c8SEdward Tomasz Napierala 	case 'b':
37666d082c8SEdward Tomasz Napierala 		mult = 512;
37766d082c8SEdward Tomasz Napierala 		break;
37866d082c8SEdward Tomasz Napierala 	case 'K':
37966d082c8SEdward Tomasz Napierala 	case 'k':
38066d082c8SEdward Tomasz Napierala 		mult = 1 << 10;
38166d082c8SEdward Tomasz Napierala 		break;
38266d082c8SEdward Tomasz Napierala 	case 'M':
38366d082c8SEdward Tomasz Napierala 	case 'm':
38466d082c8SEdward Tomasz Napierala 		mult = 1 << 20;
38566d082c8SEdward Tomasz Napierala 		break;
38666d082c8SEdward Tomasz Napierala 	case 'G':
38766d082c8SEdward Tomasz Napierala 	case 'g':
38866d082c8SEdward Tomasz Napierala 		mult = 1 << 30;
38966d082c8SEdward Tomasz Napierala 		break;
390e53d0abfSEdward Tomasz Napierala 	case 'T':
391e53d0abfSEdward Tomasz Napierala 	case 't':
392e53d0abfSEdward Tomasz Napierala 		mult = (uintmax_t)1 << 40;
393e53d0abfSEdward Tomasz Napierala 		break;
394e53d0abfSEdward Tomasz Napierala 	case 'P':
395e53d0abfSEdward Tomasz Napierala 	case 'p':
396e53d0abfSEdward Tomasz Napierala 		mult = (uintmax_t)1 << 50;
397e53d0abfSEdward Tomasz Napierala 		break;
39866d082c8SEdward Tomasz Napierala 	case 'W':
39966d082c8SEdward Tomasz Napierala 	case 'w':
40066d082c8SEdward Tomasz Napierala 		mult = sizeof(int);
40166d082c8SEdward Tomasz Napierala 		break;
40266d082c8SEdward Tomasz Napierala 	}
40366d082c8SEdward Tomasz Napierala 
40466d082c8SEdward Tomasz Napierala 	return (mult);
40566d082c8SEdward Tomasz Napierala }
40666d082c8SEdward Tomasz Napierala 
4074b88c807SRodney W. Grimes /*
4087503d74fSMark Murray  * Convert an expression of the following forms to a uintmax_t.
4094b88c807SRodney W. Grimes  * 	1) A positive decimal number.
4101e1d03d7SPawel Jakub Dawidek  *	2) A positive decimal number followed by a 'b' or 'B' (mult by 512).
4111e1d03d7SPawel Jakub Dawidek  *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
4121e1d03d7SPawel Jakub Dawidek  *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
4131e1d03d7SPawel Jakub Dawidek  *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
414c075c8bbSEdward Tomasz Napierala  *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
415c075c8bbSEdward Tomasz Napierala  *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
416c075c8bbSEdward Tomasz Napierala  *	8) A positive decimal number followed by a 'w' or 'W' (mult by sizeof int).
417c075c8bbSEdward Tomasz Napierala  *	9) Two or more positive decimal numbers (with/without [BbKkMmGgWw])
4181e1d03d7SPawel Jakub Dawidek  *	   separated by 'x' or 'X' (also '*' for backwards compatibility),
4191e1d03d7SPawel Jakub Dawidek  *	   specifying the product of the indicated values.
4204b88c807SRodney W. Grimes  */
4217503d74fSMark Murray static uintmax_t
422f9bcb0beSWarner Losh get_num(const char *val)
4234b88c807SRodney W. Grimes {
4247503d74fSMark Murray 	uintmax_t num, mult, prevnum;
4254ed95537SBrian Feldman 	char *expr;
4264ed95537SBrian Feldman 
4274ed95537SBrian Feldman 	errno = 0;
428d8192361SEdward Tomasz Napierala 	num = strtoumax(val, &expr, 0);
4294ed95537SBrian Feldman 	if (expr == val)			/* No valid digits. */
430674677bdSEdward Tomasz Napierala 		errx(1, "%s: invalid numeric value", oper);
431674677bdSEdward Tomasz Napierala 	if (errno != 0)
432674677bdSEdward Tomasz Napierala 		err(1, "%s", oper);
4334ed95537SBrian Feldman 
43466d082c8SEdward Tomasz Napierala 	mult = postfix_to_mult(*expr);
4354ed95537SBrian Feldman 
4364ed95537SBrian Feldman 	if (mult != 0) {
4374ed95537SBrian Feldman 		prevnum = num;
4384ed95537SBrian Feldman 		num *= mult;
4394ed95537SBrian Feldman 		/* Check for overflow. */
4404ed95537SBrian Feldman 		if (num / mult != prevnum)
4414ed95537SBrian Feldman 			goto erange;
4424ed95537SBrian Feldman 		expr++;
4434ed95537SBrian Feldman 	}
4444ed95537SBrian Feldman 
4454ed95537SBrian Feldman 	switch (*expr) {
4464ed95537SBrian Feldman 		case '\0':
4474ed95537SBrian Feldman 			break;
4484ed95537SBrian Feldman 		case '*':			/* Backward compatible. */
4491e1d03d7SPawel Jakub Dawidek 		case 'X':
4504ed95537SBrian Feldman 		case 'x':
4514ed95537SBrian Feldman 			mult = get_num(expr + 1);
4524ed95537SBrian Feldman 			prevnum = num;
4534ed95537SBrian Feldman 			num *= mult;
4544ed95537SBrian Feldman 			if (num / mult == prevnum)
4554ed95537SBrian Feldman 				break;
4564ed95537SBrian Feldman erange:
4574ed95537SBrian Feldman 			errx(1, "%s: %s", oper, strerror(ERANGE));
4584ed95537SBrian Feldman 		default:
4594ed95537SBrian Feldman 			errx(1, "%s: illegal numeric value", oper);
4604ed95537SBrian Feldman 	}
4614ed95537SBrian Feldman 	return (num);
4624ed95537SBrian Feldman }
4634ed95537SBrian Feldman 
4644ed95537SBrian Feldman /*
4654ed95537SBrian Feldman  * Convert an expression of the following forms to an off_t.  This is the
4664ed95537SBrian Feldman  * same as get_num(), but it uses signed numbers.
4674ed95537SBrian Feldman  *
4687503d74fSMark Murray  * The major problem here is that an off_t may not necessarily be a intmax_t.
4694ed95537SBrian Feldman  */
4704ed95537SBrian Feldman static off_t
471f9bcb0beSWarner Losh get_off_t(const char *val)
4724ed95537SBrian Feldman {
4737503d74fSMark Murray 	intmax_t num, mult, prevnum;
4744b88c807SRodney W. Grimes 	char *expr;
4754b88c807SRodney W. Grimes 
47654946e00SBrian Feldman 	errno = 0;
477d8192361SEdward Tomasz Napierala 	num = strtoimax(val, &expr, 0);
47858687472SBrian Feldman 	if (expr == val)			/* No valid digits. */
479674677bdSEdward Tomasz Napierala 		errx(1, "%s: invalid numeric value", oper);
480674677bdSEdward Tomasz Napierala 	if (errno != 0)
481674677bdSEdward Tomasz Napierala 		err(1, "%s", oper);
4824b88c807SRodney W. Grimes 
48366d082c8SEdward Tomasz Napierala 	mult = postfix_to_mult(*expr);
4844b88c807SRodney W. Grimes 
4854ed95537SBrian Feldman 	if (mult != 0) {
4864ed95537SBrian Feldman 		prevnum = num;
4874ed95537SBrian Feldman 		num *= mult;
4884ed95537SBrian Feldman 		/* Check for overflow. */
4894ed95537SBrian Feldman 		if ((prevnum > 0) != (num > 0) || num / mult != prevnum)
4904ed95537SBrian Feldman 			goto erange;
4914ed95537SBrian Feldman 		expr++;
4924ed95537SBrian Feldman 	}
4934ed95537SBrian Feldman 
4944b88c807SRodney W. Grimes 	switch (*expr) {
4954b88c807SRodney W. Grimes 		case '\0':
4964b88c807SRodney W. Grimes 			break;
4974b88c807SRodney W. Grimes 		case '*':			/* Backward compatible. */
4981e1d03d7SPawel Jakub Dawidek 		case 'X':
4994b88c807SRodney W. Grimes 		case 'x':
5007503d74fSMark Murray 			mult = (intmax_t)get_off_t(expr + 1);
5014ed95537SBrian Feldman 			prevnum = num;
5024ed95537SBrian Feldman 			num *= mult;
5032fb08072SBrian Feldman 			if ((prevnum > 0) == (num > 0) && num / mult == prevnum)
5044b88c807SRodney W. Grimes 				break;
505e14f7e78SBrian Feldman erange:
506e14f7e78SBrian Feldman 			errx(1, "%s: %s", oper, strerror(ERANGE));
5074b88c807SRodney W. Grimes 		default:
5084b88c807SRodney W. Grimes 			errx(1, "%s: illegal numeric value", oper);
5094b88c807SRodney W. Grimes 	}
5104b88c807SRodney W. Grimes 	return (num);
5114b88c807SRodney W. Grimes }
512