xref: /openbsd-src/bin/dd/args.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: args.c,v 1.11 2001/10/06 22:31:54 millert Exp $	*/
2 /*	$NetBSD: args.c,v 1.7 1996/03/01 01:18:58 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Keith Muller of the University of California, San Diego and Lance
10  * Visser of Convex Computer Corporation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
44 #else
45 static char rcsid[] = "$OpenBSD: args.c,v 1.11 2001/10/06 22:31:54 millert Exp $";
46 #endif
47 #endif /* not lint */
48 
49 #include <sys/types.h>
50 #include <sys/time.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 
59 #include "dd.h"
60 #include "extern.h"
61 
62 static int	c_arg __P((const void *, const void *));
63 static int	c_conv __P((const void *, const void *));
64 static void	f_bs __P((char *));
65 static void	f_cbs __P((char *));
66 static void	f_conv __P((char *));
67 static void	f_count __P((char *));
68 static void	f_files __P((char *));
69 static void	f_ibs __P((char *));
70 static void	f_if __P((char *));
71 static void	f_obs __P((char *));
72 static void	f_of __P((char *));
73 static void	f_seek __P((char *));
74 static void	f_skip __P((char *));
75 static size_t	get_bsz __P((char *));
76 static off_t	get_off __P((char *));
77 
78 static const struct arg {
79 	char *name;
80 	void (*f) __P((char *));
81 	u_int set, noset;
82 } args[] = {
83 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
84 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
85 	{ "conv",	f_conv,		0,	 0 },
86 	{ "count",	f_count,	C_COUNT, C_COUNT },
87 	{ "files",	f_files,	C_FILES, C_FILES },
88 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
89 	{ "if",		f_if,		C_IF,	 C_IF },
90 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
91 	{ "of",		f_of,		C_OF,	 C_OF },
92 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
93 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
94 };
95 
96 static char *oper;
97 
98 /*
99  * args -- parse JCL syntax of dd.
100  */
101 void
102 jcl(argv)
103 	char **argv;
104 {
105 	struct arg *ap, tmp;
106 	char *arg;
107 
108 	in.dbsz = out.dbsz = 512;
109 
110 	while ((oper = *++argv) != NULL) {
111 		if ((oper = strdup(oper)) == NULL)
112 			errx(1, "out of memory");
113 		if ((arg = strchr(oper, '=')) == NULL)
114 			errx(1, "unknown operand %s", oper);
115 		*arg++ = '\0';
116 		if (!*arg)
117 			errx(1, "no value specified for %s", oper);
118 		tmp.name = oper;
119 		if (!(ap = (struct arg *)bsearch(&tmp, args,
120 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
121 		    c_arg)))
122 			errx(1, "unknown operand %s", tmp.name);
123 		if (ddflags & ap->noset)
124 			errx(1, "%s: illegal argument combination or already set",
125 			    tmp.name);
126 		ddflags |= ap->set;
127 		ap->f(arg);
128 	}
129 
130 	/* Final sanity checks. */
131 
132 	if (ddflags & C_BS) {
133 		/*
134 		 * Bs is turned off by any conversion -- we assume the user
135 		 * just wanted to set both the input and output block sizes
136 		 * and didn't want the bs semantics, so we don't warn.
137 		 */
138 		if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
139 			ddflags &= ~C_BS;
140 
141 		/* Bs supersedes ibs and obs. */
142 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
143 			warnx("bs supersedes ibs and obs");
144 	}
145 
146 	/*
147 	 * Ascii/ebcdic and cbs implies block/unblock.
148 	 * Block/unblock requires cbs and vice-versa.
149 	 */
150 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
151 		if (!(ddflags & C_CBS))
152 			errx(1, "record operations require cbs");
153 		if (cbsz == 0)
154 			errx(1, "cbs cannot be zero");
155 		cfunc = ddflags & C_BLOCK ? block : unblock;
156 	} else if (ddflags & C_CBS) {
157 		if (ddflags & (C_ASCII|C_EBCDIC)) {
158 			if (ddflags & C_ASCII) {
159 				ddflags |= C_UNBLOCK;
160 				cfunc = unblock;
161 			} else {
162 				ddflags |= C_BLOCK;
163 				cfunc = block;
164 			}
165 		} else
166 			errx(1, "cbs meaningless if not doing record operations");
167 		if (cbsz == 0)
168 			errx(1, "cbs cannot be zero");
169 	} else
170 		cfunc = def;
171 
172 	if (in.dbsz == 0 || out.dbsz == 0)
173 		errx(1, "buffer sizes cannot be zero");
174 
175 	/*
176 	 * Read and write take size_t's as arguments.  Lseek, however,
177 	 * takes an off_t (quad).
178 	 */
179 	if (in.dbsz > SIZE_T_MAX || out.dbsz > SIZE_T_MAX)
180 		errx(1, "buffer sizes cannot be greater than %u", SIZE_T_MAX);
181 	if (in.offset > QUAD_MAX / in.dbsz || out.offset > QUAD_MAX / out.dbsz)
182 		errx(1, "seek offsets cannot be larger than %qd", QUAD_MAX);
183 }
184 
185 static int
186 c_arg(a, b)
187 	const void *a, *b;
188 {
189 
190 	return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
191 }
192 
193 static void
194 f_bs(arg)
195 	char *arg;
196 {
197 
198 	in.dbsz = out.dbsz = get_bsz(arg);
199 }
200 
201 static void
202 f_cbs(arg)
203 	char *arg;
204 {
205 
206 	cbsz = get_bsz(arg);
207 }
208 
209 static void
210 f_count(arg)
211 	char *arg;
212 {
213 
214 	if ((cpy_cnt = get_bsz(arg)) == 0)
215 		cpy_cnt = (size_t)-1;
216 }
217 
218 static void
219 f_files(arg)
220 	char *arg;
221 {
222 
223 	files_cnt = get_bsz(arg);
224 }
225 
226 static void
227 f_ibs(arg)
228 	char *arg;
229 {
230 
231 	if (!(ddflags & C_BS))
232 		in.dbsz = get_bsz(arg);
233 }
234 
235 static void
236 f_if(arg)
237 	char *arg;
238 {
239 
240 	in.name = arg;
241 }
242 
243 static void
244 f_obs(arg)
245 	char *arg;
246 {
247 
248 	if (!(ddflags & C_BS))
249 		out.dbsz = get_bsz(arg);
250 }
251 
252 static void
253 f_of(arg)
254 	char *arg;
255 {
256 
257 	out.name = arg;
258 }
259 
260 static void
261 f_seek(arg)
262 	char *arg;
263 {
264 
265 	out.offset = get_off(arg);
266 }
267 
268 static void
269 f_skip(arg)
270 	char *arg;
271 {
272 
273 	in.offset = get_off(arg);
274 }
275 
276 #ifdef	NO_CONV
277 /* Build a small version (i.e. for a ramdisk root) */
278 static void
279 f_conv(arg)
280 	char *arg;
281 {
282 	errx(1, "conv option disabled");
283 }
284 #else	/* NO_CONV */
285 
286 static const struct conv {
287 	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 	{ "swab",	C_SWAB,		0,		NULL },
303 	{ "sync",	C_SYNC,		0,		NULL },
304 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
305 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
306 };
307 
308 static void
309 f_conv(arg)
310 	char *arg;
311 {
312 	struct conv *cp, tmp;
313 
314 	while (arg != NULL) {
315 		tmp.name = strsep(&arg, ",");
316 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
317 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
318 		    c_conv)))
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(a, b)
330 	const void *a, *b;
331 {
332 
333 	return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
334 }
335 
336 #endif	/* NO_CONV */
337 
338 /*
339  * Convert an expression of the following forms to a size_t
340  * 	1) A positive decimal number.
341  *	2) A positive decimal number followed by a b (mult by 512).
342  *	3) A positive decimal number followed by a k (mult by 1024).
343  *	4) A positive decimal number followed by a m (mult by 512).
344  *	5) A positive decimal number followed by a w (mult by sizeof int)
345  *	6) Two or more positive decimal numbers (with/without k,b or w).
346  *	   separated by x (also * for backwards compatibility), specifying
347  *	   the product of the indicated values.
348  */
349 static size_t
350 get_bsz(val)
351 	char *val;
352 {
353 	size_t num, t;
354 	char *expr;
355 
356 	num = strtoul(val, &expr, 0);
357 	if (num == SIZE_T_MAX)			/* Overflow. */
358 		err(1, "%s", oper);
359 	if (expr == val)			/* No digits. */
360 		errx(1, "%s: illegal numeric value", oper);
361 
362 	switch(*expr) {
363 	case 'b':
364 		t = num;
365 		num *= 512;
366 		if (t > num)
367 			goto erange;
368 		++expr;
369 		break;
370 	case 'k':
371 		t = num;
372 		num *= 1024;
373 		if (t > num)
374 			goto erange;
375 		++expr;
376 		break;
377 	case 'm':
378 		t = num;
379 		num *= 1048576;
380 		if (t > num)
381 			goto erange;
382 		++expr;
383 		break;
384 	case 'w':
385 		t = num;
386 		num *= sizeof(int);
387 		if (t > num)
388 			goto erange;
389 		++expr;
390 		break;
391 	}
392 
393 	switch(*expr) {
394 		case '\0':
395 			break;
396 		case '*':			/* Backward compatible. */
397 		case 'x':
398 			t = num;
399 			num *= get_bsz(expr + 1);
400 			if (t > num)
401 erange:				errx(1, "%s: %s", oper, strerror(ERANGE));
402 			break;
403 		default:
404 			errx(1, "%s: illegal numeric value", oper);
405 	}
406 	return (num);
407 }
408 
409 /*
410  * Convert an expression of the following forms to an off_t
411  * 	1) A positive decimal number.
412  *	2) A positive decimal number followed by a b (mult by 512).
413  *	3) A positive decimal number followed by a k (mult by 1024).
414  *	4) A positive decimal number followed by a m (mult by 512).
415  *	5) A positive decimal number followed by a w (mult by sizeof int)
416  *	6) Two or more positive decimal numbers (with/without k,b or w).
417  *	   separated by x (also * for backwards compatibility), specifying
418  *	   the product of the indicated values.
419  */
420 static off_t
421 get_off(val)
422 	char *val;
423 {
424 	off_t num, t;
425 	char *expr;
426 
427 	num = strtoq(val, &expr, 0);
428 	if (num == QUAD_MAX)			/* Overflow. */
429 		err(1, "%s", oper);
430 	if (expr == val)			/* No digits. */
431 		errx(1, "%s: illegal numeric value", oper);
432 
433 	switch(*expr) {
434 	case 'b':
435 		t = num;
436 		num *= 512;
437 		if (t > num)
438 			goto erange;
439 		++expr;
440 		break;
441 	case 'k':
442 		t = num;
443 		num *= 1024;
444 		if (t > num)
445 			goto erange;
446 		++expr;
447 		break;
448 	case 'm':
449 		t = num;
450 		num *= 1048576;
451 		if (t > num)
452 			goto erange;
453 		++expr;
454 		break;
455 	case 'w':
456 		t = num;
457 		num *= sizeof(int);
458 		if (t > num)
459 			goto erange;
460 		++expr;
461 		break;
462 	}
463 
464 	switch(*expr) {
465 		case '\0':
466 			break;
467 		case '*':			/* Backward compatible. */
468 		case 'x':
469 			t = num;
470 			num *= get_off(expr + 1);
471 			if (t > num)
472 erange:				errx(1, "%s: %s", oper, strerror(ERANGE));
473 			break;
474 		default:
475 			errx(1, "%s: illegal numeric value", oper);
476 	}
477 	return (num);
478 }
479