xref: /freebsd/bin/dd/args.c (revision a6deeaa2fb3b28a61ae56a85f2275f67fcf23262)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Keith Muller of the University of California, San Diego and Lance
9  * Visser of Convex Computer Corporation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <limits.h>
43 #include <signal.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "dd.h"
48 #include "extern.h"
49 
50 static int	c_arg(const void *, const void *);
51 static int	c_conv(const void *, const void *);
52 static int	c_iflag(const void *, const void *);
53 static int	c_oflag(const void *, const void *);
54 static void	f_bs(char *);
55 static void	f_cbs(char *);
56 static void	f_conv(char *);
57 static void	f_count(char *);
58 static void	f_files(char *);
59 static void	f_fillchar(char *);
60 static void	f_ibs(char *);
61 static void	f_if(char *);
62 static void	f_iflag(char *);
63 static void	f_obs(char *);
64 static void	f_of(char *);
65 static void	f_oflag(char *);
66 static void	f_seek(char *);
67 static void	f_skip(char *);
68 static void	f_speed(char *);
69 static void	f_status(char *);
70 static uintmax_t get_num(const char *);
71 static off_t	get_off_t(const char *);
72 
73 static const struct arg {
74 	const char *name;
75 	void (*f)(char *);
76 	uint64_t 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 	{ "fillchar",	f_fillchar,	C_FILL,	 C_FILL },
84 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
85 	{ "if",		f_if,		C_IF,	 C_IF },
86 	{ "iflag",	f_iflag,	0,	 0 },
87 	{ "iseek",	f_skip,		C_SKIP,	 C_SKIP },
88 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
89 	{ "of",		f_of,		C_OF,	 C_OF },
90 	{ "oflag",	f_oflag,	0,	 0 },
91 	{ "oseek",	f_seek,		C_SEEK,	 C_SEEK },
92 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
93 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
94 	{ "speed",	f_speed,	0,	 0 },
95 	{ "status",	f_status,	C_STATUS,C_STATUS },
96 };
97 
98 static char *oper;
99 
100 /*
101  * args -- parse JCL syntax of dd.
102  */
103 void
104 jcl(char **argv)
105 {
106 	const struct arg *ap;
107 	struct arg tmp;
108 	char *arg;
109 
110 	in.dbsz = out.dbsz = 512;
111 
112 	while ((oper = *++argv) != NULL) {
113 		if ((oper = strdup(oper)) == NULL)
114 			errx(1, "unable to allocate space for the argument \"%s\"", *argv);
115 		if ((arg = strchr(oper, '=')) == NULL)
116 			errx(1, "unknown operand %s", oper);
117 		*arg++ = '\0';
118 		if (!*arg)
119 			errx(1, "no value specified for %s", oper);
120 		tmp.name = oper;
121 		if (!(ap = bsearch(&tmp, args,
122 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
123 		    c_arg)))
124 			errx(1, "unknown operand %s", tmp.name);
125 		if (ddflags & ap->noset)
126 			errx(1, "%s: illegal argument combination or already set",
127 			    tmp.name);
128 		ddflags |= ap->set;
129 		ap->f(arg);
130 	}
131 
132 	/* Final sanity checks. */
133 
134 	if (ddflags & C_BS) {
135 		/*
136 		 * Bs is turned off by any conversion -- we assume the user
137 		 * just wanted to set both the input and output block sizes
138 		 * and didn't want the bs semantics, so we don't warn.
139 		 */
140 		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
141 		    C_UNBLOCK))
142 			ddflags &= ~C_BS;
143 
144 		/* Bs supersedes ibs and obs. */
145 		if (ddflags & C_BS && ddflags & (C_IBS | C_OBS))
146 			warnx("bs supersedes ibs and obs");
147 	}
148 
149 	/*
150 	 * Ascii/ebcdic and cbs implies block/unblock.
151 	 * Block/unblock requires cbs and vice-versa.
152 	 */
153 	if (ddflags & (C_BLOCK | C_UNBLOCK)) {
154 		if (!(ddflags & C_CBS))
155 			errx(1, "record operations require cbs");
156 		if (cbsz == 0)
157 			errx(1, "cbs cannot be zero");
158 		cfunc = ddflags & C_BLOCK ? block : unblock;
159 	} else if (ddflags & C_CBS) {
160 		if (ddflags & (C_ASCII | C_EBCDIC)) {
161 			if (ddflags & C_ASCII) {
162 				ddflags |= C_UNBLOCK;
163 				cfunc = unblock;
164 			} else {
165 				ddflags |= C_BLOCK;
166 				cfunc = block;
167 			}
168 		} else
169 			errx(1, "cbs meaningless if not doing record operations");
170 	} else
171 		cfunc = def;
172 }
173 
174 static int
175 c_arg(const void *a, const void *b)
176 {
177 
178 	return (strcmp(((const struct arg *)a)->name,
179 	    ((const struct arg *)b)->name));
180 }
181 
182 static void
183 f_bs(char *arg)
184 {
185 	uintmax_t res;
186 
187 	res = get_num(arg);
188 	if (res < 1 || res > SSIZE_MAX)
189 		errx(1, "bs must be between 1 and %zd", (ssize_t)SSIZE_MAX);
190 	in.dbsz = out.dbsz = (size_t)res;
191 }
192 
193 static void
194 f_cbs(char *arg)
195 {
196 	uintmax_t res;
197 
198 	res = get_num(arg);
199 	if (res < 1 || res > SSIZE_MAX)
200 		errx(1, "cbs must be between 1 and %zd", (ssize_t)SSIZE_MAX);
201 	cbsz = (size_t)res;
202 }
203 
204 static void
205 f_count(char *arg)
206 {
207 	uintmax_t res;
208 
209 	res = get_num(arg);
210 	if (res == UINTMAX_MAX)
211 		errc(1, ERANGE, "%s", oper);
212 	if (res == 0)
213 		cpy_cnt = UINTMAX_MAX;
214 	else
215 		cpy_cnt = res;
216 }
217 
218 static void
219 f_files(char *arg)
220 {
221 
222 	files_cnt = get_num(arg);
223 	if (files_cnt < 1)
224 		errx(1, "files must be between 1 and %zu", SIZE_MAX);
225 }
226 
227 static void
228 f_fillchar(char *arg)
229 {
230 
231 	if (strlen(arg) != 1)
232 		errx(1, "need exactly one fill char");
233 
234 	fill_char = arg[0];
235 }
236 
237 static void
238 f_ibs(char *arg)
239 {
240 	uintmax_t res;
241 
242 	if (!(ddflags & C_BS)) {
243 		res = get_num(arg);
244 		if (res < 1 || res > SSIZE_MAX)
245 			errx(1, "ibs must be between 1 and %zd",
246 			    (ssize_t)SSIZE_MAX);
247 		in.dbsz = (size_t)res;
248 	}
249 }
250 
251 static void
252 f_if(char *arg)
253 {
254 
255 	in.name = arg;
256 }
257 
258 static const struct iflag {
259 	const char *name;
260 	uint64_t set, noset;
261 } ilist[] = {
262 	{ "direct",	C_IDIRECT,	0 },
263 	{ "fullblock",	C_IFULLBLOCK,	C_SYNC },
264 };
265 
266 static void
267 f_iflag(char *arg)
268 {
269 	const struct iflag *ip;
270 	struct iflag tmp;
271 
272 	while (arg != NULL) {
273 		tmp.name = strsep(&arg, ",");
274 		ip = bsearch(&tmp, ilist, nitems(ilist), sizeof(struct iflag),
275 		    c_iflag);
276 		if (ip == NULL)
277 			errx(1, "unknown iflag %s", tmp.name);
278 		if (ddflags & ip->noset)
279 			errx(1, "%s: illegal conversion combination", tmp.name);
280 		ddflags |= ip->set;
281 	}
282 }
283 
284 static int
285 c_iflag(const void *a, const void *b)
286 {
287 
288 	return (strcmp(((const struct iflag *)a)->name,
289 	    ((const struct iflag *)b)->name));
290 }
291 
292 static void
293 f_obs(char *arg)
294 {
295 	uintmax_t res;
296 
297 	if (!(ddflags & C_BS)) {
298 		res = get_num(arg);
299 		if (res < 1 || res > SSIZE_MAX)
300 			errx(1, "obs must be between 1 and %zd",
301 			    (ssize_t)SSIZE_MAX);
302 		out.dbsz = (size_t)res;
303 	}
304 }
305 
306 static void
307 f_of(char *arg)
308 {
309 
310 	out.name = arg;
311 }
312 
313 static void
314 f_seek(char *arg)
315 {
316 
317 	out.offset = get_off_t(arg);
318 }
319 
320 static void
321 f_skip(char *arg)
322 {
323 
324 	in.offset = get_off_t(arg);
325 }
326 
327 static void
328 f_speed(char *arg)
329 {
330 
331 	speed = get_num(arg);
332 }
333 
334 static void
335 f_status(char *arg)
336 {
337 
338 	if (strcmp(arg, "none") == 0)
339 		ddflags |= C_NOINFO;
340 	else if (strcmp(arg, "noxfer") == 0)
341 		ddflags |= C_NOXFER;
342 	else if (strcmp(arg, "progress") == 0)
343 		ddflags |= C_PROGRESS;
344 	else
345 		errx(1, "unknown status %s", arg);
346 }
347 
348 static const struct conv {
349 	const char *name;
350 	uint64_t set, noset;
351 	const u_char *ctab;
352 } clist[] = {
353 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
354 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
355 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
356 	{ "fdatasync",	C_FDATASYNC,	0,		NULL },
357 	{ "fsync",	C_FSYNC,	0,		NULL },
358 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
359 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
360 	{ "noerror",	C_NOERROR,	0,		NULL },
361 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
362 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
363 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
364 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
365 	{ "osync",	C_OSYNC,	C_BS,		NULL },
366 	{ "pareven",	C_PAREVEN,	C_PARODD|C_PARSET|C_PARNONE, NULL},
367 	{ "parnone",	C_PARNONE,	C_PARODD|C_PARSET|C_PAREVEN, NULL},
368 	{ "parodd",	C_PARODD,	C_PAREVEN|C_PARSET|C_PARNONE, NULL},
369 	{ "parset",	C_PARSET,	C_PARODD|C_PAREVEN|C_PARNONE, NULL},
370 	{ "sparse",	C_SPARSE,	0,		NULL },
371 	{ "swab",	C_SWAB,		0,		NULL },
372 	{ "sync",	C_SYNC,		C_IFULLBLOCK,	NULL },
373 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
374 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
375 };
376 
377 static void
378 f_conv(char *arg)
379 {
380 	const struct conv *cp;
381 	struct conv tmp;
382 
383 	while (arg != NULL) {
384 		tmp.name = strsep(&arg, ",");
385 		cp = bsearch(&tmp, clist, nitems(clist), sizeof(struct conv),
386 		    c_conv);
387 		if (cp == NULL)
388 			errx(1, "unknown conversion %s", tmp.name);
389 		if (ddflags & cp->noset)
390 			errx(1, "%s: illegal conversion combination", tmp.name);
391 		ddflags |= cp->set;
392 		if (cp->ctab)
393 			ctab = cp->ctab;
394 	}
395 }
396 
397 static int
398 c_conv(const void *a, const void *b)
399 {
400 
401 	return (strcmp(((const struct conv *)a)->name,
402 	    ((const struct conv *)b)->name));
403 }
404 
405 static const struct oflag {
406 	const char *name;
407 	uint64_t set;
408 } olist[] = {
409 	{ "direct",	C_ODIRECT },
410 	{ "fsync",	C_OFSYNC },
411 	{ "sync",	C_OFSYNC },
412 };
413 
414 static void
415 f_oflag(char *arg)
416 {
417 	const struct oflag *op;
418 	struct oflag tmp;
419 
420 	while (arg != NULL) {
421 		tmp.name = strsep(&arg, ",");
422 		op = bsearch(&tmp, olist, nitems(olist), sizeof(struct oflag),
423 		    c_oflag);
424 		if (op == NULL)
425 			errx(1, "unknown open flag %s", tmp.name);
426 		ddflags |= op->set;
427 	}
428 }
429 
430 static int
431 c_oflag(const void *a, const void *b)
432 {
433 
434 	return (strcmp(((const struct oflag *)a)->name,
435 	    ((const struct oflag *)b)->name));
436 }
437 
438 static intmax_t
439 postfix_to_mult(const char expr)
440 {
441 	intmax_t mult;
442 
443 	mult = 0;
444 	switch (expr) {
445 	case 'B':
446 	case 'b':
447 		mult = 512;
448 		break;
449 	case 'K':
450 	case 'k':
451 		mult = 1 << 10;
452 		break;
453 	case 'M':
454 	case 'm':
455 		mult = 1 << 20;
456 		break;
457 	case 'G':
458 	case 'g':
459 		mult = 1 << 30;
460 		break;
461 	case 'T':
462 	case 't':
463 		mult = (uintmax_t)1 << 40;
464 		break;
465 	case 'P':
466 	case 'p':
467 		mult = (uintmax_t)1 << 50;
468 		break;
469 	case 'W':
470 	case 'w':
471 		mult = sizeof(int);
472 		break;
473 	}
474 
475 	return (mult);
476 }
477 
478 /*
479  * Convert an expression of the following forms to a uintmax_t.
480  * 	1) A positive decimal number.
481  *	2) A positive decimal number followed by a 'b' or 'B' (mult by 512).
482  *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
483  *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
484  *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
485  *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
486  *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
487  *	8) A positive decimal number followed by a 'w' or 'W' (mult by sizeof int).
488  *	9) Two or more positive decimal numbers (with/without [BbKkMmGgWw])
489  *	   separated by 'x' or 'X' (also '*' for backwards compatibility),
490  *	   specifying the product of the indicated values.
491  */
492 static uintmax_t
493 get_num(const char *val)
494 {
495 	uintmax_t num, mult, prevnum;
496 	char *expr;
497 
498 	errno = 0;
499 	num = strtoumax(val, &expr, 0);
500 	if (expr == val)			/* No valid digits. */
501 		errx(1, "%s: invalid numeric value", oper);
502 	if (errno != 0)
503 		err(1, "%s", oper);
504 
505 	mult = postfix_to_mult(*expr);
506 
507 	if (mult != 0) {
508 		prevnum = num;
509 		num *= mult;
510 		/* Check for overflow. */
511 		if (num / mult != prevnum)
512 			goto erange;
513 		expr++;
514 	}
515 
516 	switch (*expr) {
517 		case '\0':
518 			break;
519 		case '*':			/* Backward compatible. */
520 		case 'X':
521 		case 'x':
522 			mult = get_num(expr + 1);
523 			prevnum = num;
524 			num *= mult;
525 			if (num / mult == prevnum)
526 				break;
527 erange:
528 			errx(1, "%s: %s", oper, strerror(ERANGE));
529 		default:
530 			errx(1, "%s: illegal numeric value", oper);
531 	}
532 	return (num);
533 }
534 
535 /*
536  * Convert an expression of the following forms to an off_t.  This is the
537  * same as get_num(), but it uses signed numbers.
538  *
539  * The major problem here is that an off_t may not necessarily be a intmax_t.
540  */
541 static off_t
542 get_off_t(const char *val)
543 {
544 	intmax_t num, mult, prevnum;
545 	char *expr;
546 
547 	errno = 0;
548 	num = strtoimax(val, &expr, 0);
549 	if (expr == val)			/* No valid digits. */
550 		errx(1, "%s: invalid numeric value", oper);
551 	if (errno != 0)
552 		err(1, "%s", oper);
553 
554 	mult = postfix_to_mult(*expr);
555 
556 	if (mult != 0) {
557 		prevnum = num;
558 		num *= mult;
559 		/* Check for overflow. */
560 		if ((prevnum > 0) != (num > 0) || num / mult != prevnum)
561 			goto erange;
562 		expr++;
563 	}
564 
565 	switch (*expr) {
566 		case '\0':
567 			break;
568 		case '*':			/* Backward compatible. */
569 		case 'X':
570 		case 'x':
571 			mult = (intmax_t)get_off_t(expr + 1);
572 			prevnum = num;
573 			num *= mult;
574 			if ((prevnum > 0) == (num > 0) && num / mult == prevnum)
575 				break;
576 erange:
577 			errx(1, "%s: %s", oper, strerror(ERANGE));
578 		default:
579 			errx(1, "%s: illegal numeric value", oper);
580 	}
581 	return (num);
582 }
583