Home | History | Annotate | Line # | Download | only in dd
args.c revision 1.17
      1 /*	$NetBSD: args.c,v 1.17 2001/11/25 06:53:48 lukem Exp $	*/
      2 
      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. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
     44 #else
     45 __RCSID("$NetBSD: args.c,v 1.17 2001/11/25 06:53:48 lukem 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(const void *, const void *);
     63 static int	c_conv(const void *, const void *);
     64 static void	f_bs(char *);
     65 static void	f_cbs(char *);
     66 static void	f_conv(char *);
     67 static void	f_count(char *);
     68 static void	f_files(char *);
     69 static void	f_ibs(char *);
     70 static void	f_if(char *);
     71 static void	f_obs(char *);
     72 static void	f_of(char *);
     73 static void	f_seek(char *);
     74 static void	f_skip(char *);
     75 static void	f_progress(char *);
     76 static u_long	get_bsz(const char *);
     77 
     78 static const struct arg {
     79 	const char *name;
     80 	void (*f)(char *);
     81 	u_int set, noset;
     82 } args[] = {
     83      /* the array needs to be sorted by the first column so
     84         bsearch() can be used to find commands quickly */
     85 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
     86 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
     87 	{ "conv",	f_conv,		0,	 0 },
     88 	{ "count",	f_count,	C_COUNT, C_COUNT },
     89 	{ "files",	f_files,	C_FILES, C_FILES },
     90 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
     91 	{ "if",		f_if,		C_IF,	 C_IF },
     92 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
     93 	{ "of",		f_of,		C_OF,	 C_OF },
     94 	{ "progress",	f_progress,	0,	 0 },
     95 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
     96 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
     97 };
     98 
     99 static char *oper;
    100 
    101 /*
    102  * args -- parse JCL syntax of dd.
    103  */
    104 void
    105 jcl(char **argv)
    106 {
    107 	struct arg *ap, tmp;
    108 	char *arg;
    109 
    110 	in.dbsz = out.dbsz = 512;
    111 
    112 	while ((oper = *++argv) != NULL) {
    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,
    125 			    "%s: illegal argument combination or already set",
    126 			    tmp.name);
    127 		ddflags |= ap->set;
    128 		ap->f(arg);
    129 	}
    130 
    131 	/* Final sanity checks. */
    132 
    133 	if (ddflags & C_BS) {
    134 		/*
    135 		 * Bs is turned off by any conversion -- we assume the user
    136 		 * just wanted to set both the input and output block sizes
    137 		 * and didn't want the bs semantics, so we don't warn.
    138 		 */
    139 		if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
    140 			ddflags &= ~C_BS;
    141 
    142 		/* Bs supersedes ibs and obs. */
    143 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
    144 			warnx("bs supersedes ibs and obs");
    145 	}
    146 
    147 	/*
    148 	 * Ascii/ebcdic and cbs implies block/unblock.
    149 	 * Block/unblock requires cbs and vice-versa.
    150 	 */
    151 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
    152 		if (!(ddflags & C_CBS))
    153 			errx(1, "record operations require cbs");
    154 		if (cbsz == 0)
    155 			errx(1, "cbs cannot be zero");
    156 		cfunc = ddflags & C_BLOCK ? block : unblock;
    157 	} else if (ddflags & C_CBS) {
    158 		if (ddflags & (C_ASCII|C_EBCDIC)) {
    159 			if (ddflags & C_ASCII) {
    160 				ddflags |= C_UNBLOCK;
    161 				cfunc = unblock;
    162 			} else {
    163 				ddflags |= C_BLOCK;
    164 				cfunc = block;
    165 			}
    166 		} else
    167 			errx(1,
    168 			    "cbs meaningless if not doing record operations");
    169 		if (cbsz == 0)
    170 			errx(1, "cbs cannot be zero");
    171 	} else
    172 		cfunc = def;
    173 
    174 	if (in.dbsz == 0 || out.dbsz == 0)
    175 		errx(1, "buffer sizes cannot be zero");
    176 
    177 	/*
    178 	 * Check to make sure that the buffers are not too large.
    179 	 */
    180 	if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
    181 		errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
    182 
    183 	/* Read, write and seek calls take off_t as arguments.
    184 	 *
    185 	 * The following check is not done because an off_t is a quad
    186 	 *  for current NetBSD implementations.
    187 	 *
    188 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
    189 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
    190 	 */
    191 }
    192 
    193 static int
    194 c_arg(const void *a, const void *b)
    195 {
    196 
    197 	return (strcmp(((const struct arg *)a)->name,
    198 	    ((const struct arg *)b)->name));
    199 }
    200 
    201 static void
    202 f_bs(char *arg)
    203 {
    204 
    205 	in.dbsz = out.dbsz = (int)get_bsz(arg);
    206 }
    207 
    208 static void
    209 f_cbs(char *arg)
    210 {
    211 
    212 	cbsz = (int)get_bsz(arg);
    213 }
    214 
    215 static void
    216 f_count(char *arg)
    217 {
    218 
    219 	cpy_cnt = (u_int)get_bsz(arg);
    220 	if (!cpy_cnt)
    221 		terminate(0);
    222 }
    223 
    224 static void
    225 f_files(char *arg)
    226 {
    227 
    228 	files_cnt = (int)get_bsz(arg);
    229 }
    230 
    231 static void
    232 f_ibs(char *arg)
    233 {
    234 
    235 	if (!(ddflags & C_BS))
    236 		in.dbsz = (int)get_bsz(arg);
    237 }
    238 
    239 static void
    240 f_if(char *arg)
    241 {
    242 
    243 	in.name = arg;
    244 }
    245 
    246 static void
    247 f_obs(char *arg)
    248 {
    249 
    250 	if (!(ddflags & C_BS))
    251 		out.dbsz = (int)get_bsz(arg);
    252 }
    253 
    254 static void
    255 f_of(char *arg)
    256 {
    257 
    258 	out.name = arg;
    259 }
    260 
    261 static void
    262 f_seek(char *arg)
    263 {
    264 
    265 	out.offset = (u_int)get_bsz(arg);
    266 }
    267 
    268 static void
    269 f_skip(char *arg)
    270 {
    271 
    272 	in.offset = (u_int)get_bsz(arg);
    273 }
    274 
    275 static void
    276 f_progress(char *arg)
    277 {
    278 
    279 	if (*arg != '0')
    280 		progress = 1;
    281 }
    282 
    283 #ifdef	NO_CONV
    284 /* Build a small version (i.e. for a ramdisk root) */
    285 static void
    286 f_conv(char *arg)
    287 {
    288 
    289 	errx(1, "conv option disabled");
    290 }
    291 #else	/* NO_CONV */
    292 
    293 static const struct conv {
    294 	const char *name;
    295 	u_int set, noset;
    296 	const u_char *ctab;
    297 } clist[] = {
    298 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
    299 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
    300 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
    301 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
    302 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
    303 	{ "noerror",	C_NOERROR,	0,		NULL },
    304 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
    305 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
    306 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
    307 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
    308 	{ "osync",	C_OSYNC,	C_BS,		NULL },
    309 	{ "swab",	C_SWAB,		0,		NULL },
    310 	{ "sync",	C_SYNC,		0,		NULL },
    311 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
    312 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
    313 };
    314 
    315 static void
    316 f_conv(char *arg)
    317 {
    318 	struct conv *cp, tmp;
    319 
    320 	while (arg != NULL) {
    321 		tmp.name = strsep(&arg, ",");
    322 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
    323 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
    324 		    c_conv)))
    325 			errx(1, "unknown conversion %s", tmp.name);
    326 		if (ddflags & cp->noset)
    327 			errx(1, "%s: illegal conversion combination", tmp.name);
    328 		ddflags |= cp->set;
    329 		if (cp->ctab)
    330 			ctab = cp->ctab;
    331 	}
    332 }
    333 
    334 static int
    335 c_conv(const void *a, const void *b)
    336 {
    337 
    338 	return (strcmp(((const struct conv *)a)->name,
    339 	    ((const struct conv *)b)->name));
    340 }
    341 
    342 #endif	/* NO_CONV */
    343 
    344 /*
    345  * Convert an expression of the following forms to an unsigned long.
    346  * 	1) A positive decimal number.
    347  *	2) A positive decimal number followed by a b (mult by 512).
    348  *	3) A positive decimal number followed by a k (mult by 1024).
    349  *	4) A positive decimal number followed by a m (mult by 1048576).
    350  *	5) A positive decimal number followed by a w (mult by sizeof int)
    351  *	6) Two or more positive decimal numbers (with/without k,b or w).
    352  *	   separated by x (also * for backwards compatibility), specifying
    353  *	   the product of the indicated values.
    354  */
    355 static u_long
    356 get_bsz(const char *val)
    357 {
    358 	u_long num, t;
    359 	char *expr;
    360 
    361 	num = strtoul(val, &expr, 0);
    362 	if (num == ULONG_MAX)			/* Overflow. */
    363 		err(1, "%s", oper);
    364 	if (expr == val)			/* No digits. */
    365 		errx(1, "%s: illegal numeric value", oper);
    366 
    367 	switch (*expr) {
    368 	case 'b':
    369 		t = num;
    370 		num *= 512;
    371 		if (t > num)
    372 			goto erange;
    373 		++expr;
    374 		break;
    375 	case 'k':
    376 		t = num;
    377 		num *= 1024;
    378 		if (t > num)
    379 			goto erange;
    380 		++expr;
    381 		break;
    382 	case 'm':
    383 		t = num;
    384 		num *= 1048576;
    385 		if (t > num)
    386 			goto erange;
    387 		++expr;
    388 		break;
    389 	case 'w':
    390 		t = num;
    391 		num *= sizeof(int);
    392 		if (t > num)
    393 			goto erange;
    394 		++expr;
    395 		break;
    396 	}
    397 
    398 	switch (*expr) {
    399 	case '\0':
    400 		break;
    401 	case '*':				/* Backward compatible. */
    402 	case 'x':
    403 		t = num;
    404 		num *= get_bsz(expr + 1);
    405 		if (t > num)
    406 erange:			errx(1, "%s: %s", oper, strerror(ERANGE));
    407 		break;
    408 	default:
    409 		errx(1, "%s: illegal numeric value", oper);
    410 	}
    411 	return (num);
    412 }
    413