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