Home | History | Annotate | Line # | Download | only in dd
args.c revision 1.1
      1  1.1  glass /*-
      2  1.1  glass  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  glass  * All rights reserved.
      4  1.1  glass  *
      5  1.1  glass  * This code is derived from software contributed to Berkeley by
      6  1.1  glass  * Keith Muller of the University of California, San Diego and Lance
      7  1.1  glass  * Visser of Convex Computer Corporation.
      8  1.1  glass  *
      9  1.1  glass  * Redistribution and use in source and binary forms, with or without
     10  1.1  glass  * modification, are permitted provided that the following conditions
     11  1.1  glass  * are met:
     12  1.1  glass  * 1. Redistributions of source code must retain the above copyright
     13  1.1  glass  *    notice, this list of conditions and the following disclaimer.
     14  1.1  glass  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  glass  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  glass  *    documentation and/or other materials provided with the distribution.
     17  1.1  glass  * 3. All advertising materials mentioning features or use of this software
     18  1.1  glass  *    must display the following acknowledgement:
     19  1.1  glass  *	This product includes software developed by the University of
     20  1.1  glass  *	California, Berkeley and its contributors.
     21  1.1  glass  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  glass  *    may be used to endorse or promote products derived from this software
     23  1.1  glass  *    without specific prior written permission.
     24  1.1  glass  *
     25  1.1  glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  glass  * SUCH DAMAGE.
     36  1.1  glass  */
     37  1.1  glass 
     38  1.1  glass #ifndef lint
     39  1.1  glass static char sccsid[] = "@(#)args.c	5.5 (Berkeley) 7/29/91";
     40  1.1  glass #endif /* not lint */
     41  1.1  glass 
     42  1.1  glass #include <sys/types.h>
     43  1.1  glass #include <limits.h>
     44  1.1  glass #include <errno.h>
     45  1.1  glass #include <stdio.h>
     46  1.1  glass #include <stdlib.h>
     47  1.1  glass #include <string.h>
     48  1.1  glass #include "dd.h"
     49  1.1  glass #include "extern.h"
     50  1.1  glass 
     51  1.1  glass static u_long get_bsz __P((char *));
     52  1.1  glass 
     53  1.1  glass static void f_bs __P((char *));
     54  1.1  glass static void f_cbs __P((char *));
     55  1.1  glass static void f_conv __P((char *));
     56  1.1  glass static void f_count __P((char *));
     57  1.1  glass static void f_files __P((char *));
     58  1.1  glass static void f_ibs __P((char *));
     59  1.1  glass static void f_if __P((char *));
     60  1.1  glass static void f_obs __P((char *));
     61  1.1  glass static void f_of __P((char *));
     62  1.1  glass static void f_seek __P((char *));
     63  1.1  glass static void f_skip __P((char *));
     64  1.1  glass 
     65  1.1  glass static struct arg {
     66  1.1  glass 	char *name;
     67  1.1  glass 	void (*f) __P((char *));
     68  1.1  glass 	u_int set, noset;
     69  1.1  glass } args[] = {
     70  1.1  glass 	"bs",		f_bs,		C_BS,		C_BS|C_IBS|C_OBS,
     71  1.1  glass 	"cbs",		f_cbs,		C_CBS,		C_CBS,
     72  1.1  glass 	"conv",		f_conv,		0,		0,
     73  1.1  glass 	"count",	f_count,	C_COUNT,	C_COUNT,
     74  1.1  glass 	"files",	f_files,	C_FILES,	C_FILES,
     75  1.1  glass 	"ibs",		f_ibs,		C_IBS,		C_BS|C_IBS,
     76  1.1  glass 	"if",		f_if,		C_IF,		C_IF,
     77  1.1  glass 	"obs",		f_obs,		C_OBS,		C_BS|C_OBS,
     78  1.1  glass 	"of",		f_of,		C_OF,		C_OF,
     79  1.1  glass 	"seek",		f_seek,		C_SEEK,		C_SEEK,
     80  1.1  glass 	"skip",		f_skip,		C_SKIP,		C_SKIP,
     81  1.1  glass };
     82  1.1  glass 
     83  1.1  glass static char *oper;
     84  1.1  glass 
     85  1.1  glass /*
     86  1.1  glass  * args -- parse JCL syntax of dd.
     87  1.1  glass  */
     88  1.1  glass void
     89  1.1  glass jcl(argv)
     90  1.1  glass 	register char **argv;
     91  1.1  glass {
     92  1.1  glass 	register struct arg *ap;
     93  1.1  glass 	struct arg tmp;
     94  1.1  glass 	char *arg;
     95  1.1  glass 	static int c_arg __P((const void *, const void *));
     96  1.1  glass 
     97  1.1  glass 	in.dbsz = out.dbsz = 512;
     98  1.1  glass 
     99  1.1  glass 	while (oper = *++argv) {
    100  1.1  glass 		if ((arg = index(oper, '=')) == NULL)
    101  1.1  glass 			err("unknown operand %s", oper);
    102  1.1  glass 		*arg++ = '\0';
    103  1.1  glass 		if (!*arg)
    104  1.1  glass 			err("no value specified for %s", oper);
    105  1.1  glass 		tmp.name = oper;
    106  1.1  glass 		if (!(ap = (struct arg *)bsearch(&tmp, args,
    107  1.1  glass 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
    108  1.1  glass 		    c_arg)))
    109  1.1  glass 			err("unknown operand %s", tmp.name);
    110  1.1  glass 		if (ddflags & ap->noset)
    111  1.1  glass 			err("%s: illegal argument combination or already set",
    112  1.1  glass 			    tmp.name);
    113  1.1  glass 		ddflags |= ap->set;
    114  1.1  glass 		ap->f(arg);
    115  1.1  glass 	}
    116  1.1  glass 
    117  1.1  glass 	/* Final sanity checks. */
    118  1.1  glass 
    119  1.1  glass 	if (ddflags & C_BS) {
    120  1.1  glass 		/*
    121  1.1  glass 		 * Bs is turned off by any conversion -- we assume the user
    122  1.1  glass 		 * just wanted to set both the input and output block sizes
    123  1.1  glass 		 * and didn't want the bs semantics, so we don't warn.
    124  1.1  glass 		 */
    125  1.1  glass 		if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
    126  1.1  glass 			ddflags &= ~C_BS;
    127  1.1  glass 
    128  1.1  glass 		/* Bs supersedes ibs and obs. */
    129  1.1  glass 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
    130  1.1  glass 			warn("bs supersedes ibs and obs");
    131  1.1  glass 	}
    132  1.1  glass 
    133  1.1  glass 	/*
    134  1.1  glass 	 * Ascii/ebcdic and cbs implies block/unblock.
    135  1.1  glass 	 * Block/unblock requires cbs and vice-versa.
    136  1.1  glass 	 */
    137  1.1  glass 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
    138  1.1  glass 		if (!(ddflags & C_CBS))
    139  1.1  glass 			err("record operations require cbs");
    140  1.1  glass 		if (cbsz == 0)
    141  1.1  glass 			err("cbs cannot be zero");
    142  1.1  glass 		cfunc = ddflags & C_BLOCK ? block : unblock;
    143  1.1  glass 	} else if (ddflags & C_CBS) {
    144  1.1  glass 		if (ddflags & (C_ASCII|C_EBCDIC)) {
    145  1.1  glass 			if (ddflags & C_ASCII) {
    146  1.1  glass 				ddflags |= C_UNBLOCK;
    147  1.1  glass 				cfunc = unblock;
    148  1.1  glass 			} else {
    149  1.1  glass 				ddflags |= C_BLOCK;
    150  1.1  glass 				cfunc = block;
    151  1.1  glass 			}
    152  1.1  glass 		} else
    153  1.1  glass 			err("cbs meaningless if not doing record operations");
    154  1.1  glass 		if (cbsz == 0)
    155  1.1  glass 			err("cbs cannot be zero");
    156  1.1  glass 	} else
    157  1.1  glass 		cfunc = def;
    158  1.1  glass 
    159  1.1  glass 	if (in.dbsz == 0 || out.dbsz == 0)
    160  1.1  glass 		err("buffer sizes cannot be zero");
    161  1.1  glass 
    162  1.1  glass 	/*
    163  1.1  glass 	 * Read, write and seek calls take ints as arguments.  Seek sizes
    164  1.1  glass 	 * could be larger if we wanted to do it in stages or check only
    165  1.1  glass 	 * regular files, but it's probably not worth it.
    166  1.1  glass 	 */
    167  1.1  glass 	if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
    168  1.1  glass 		err("buffer sizes cannot be greater than %d", INT_MAX);
    169  1.1  glass 	if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
    170  1.1  glass 		err("seek offsets cannot be larger than %d", INT_MAX);
    171  1.1  glass }
    172  1.1  glass 
    173  1.1  glass static int
    174  1.1  glass c_arg(a, b)
    175  1.1  glass 	const void *a, *b;
    176  1.1  glass {
    177  1.1  glass 	return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
    178  1.1  glass }
    179  1.1  glass 
    180  1.1  glass static void
    181  1.1  glass f_bs(arg)
    182  1.1  glass 	char *arg;
    183  1.1  glass {
    184  1.1  glass 	in.dbsz = out.dbsz = (int)get_bsz(arg);
    185  1.1  glass }
    186  1.1  glass 
    187  1.1  glass static void
    188  1.1  glass f_cbs(arg)
    189  1.1  glass 	char *arg;
    190  1.1  glass {
    191  1.1  glass 	cbsz = (int)get_bsz(arg);
    192  1.1  glass }
    193  1.1  glass 
    194  1.1  glass static void
    195  1.1  glass f_count(arg)
    196  1.1  glass 	char *arg;
    197  1.1  glass {
    198  1.1  glass 	cpy_cnt = (u_int)get_bsz(arg);
    199  1.1  glass 	if (!cpy_cnt)
    200  1.1  glass 		terminate(0);
    201  1.1  glass }
    202  1.1  glass 
    203  1.1  glass static void
    204  1.1  glass f_files(arg)
    205  1.1  glass 	char *arg;
    206  1.1  glass {
    207  1.1  glass 	files_cnt = (int)get_bsz(arg);
    208  1.1  glass }
    209  1.1  glass 
    210  1.1  glass static void
    211  1.1  glass f_ibs(arg)
    212  1.1  glass 	char *arg;
    213  1.1  glass {
    214  1.1  glass 	if (!(ddflags & C_BS))
    215  1.1  glass 		in.dbsz = (int)get_bsz(arg);
    216  1.1  glass }
    217  1.1  glass 
    218  1.1  glass static void
    219  1.1  glass f_if(arg)
    220  1.1  glass 	char *arg;
    221  1.1  glass {
    222  1.1  glass 	in.name = arg;
    223  1.1  glass }
    224  1.1  glass 
    225  1.1  glass static void
    226  1.1  glass f_obs(arg)
    227  1.1  glass 	char *arg;
    228  1.1  glass {
    229  1.1  glass 	if (!(ddflags & C_BS))
    230  1.1  glass 		out.dbsz = (int)get_bsz(arg);
    231  1.1  glass }
    232  1.1  glass 
    233  1.1  glass static void
    234  1.1  glass f_of(arg)
    235  1.1  glass 	char *arg;
    236  1.1  glass {
    237  1.1  glass 	out.name = arg;
    238  1.1  glass }
    239  1.1  glass 
    240  1.1  glass static void
    241  1.1  glass f_seek(arg)
    242  1.1  glass 	char *arg;
    243  1.1  glass {
    244  1.1  glass 	out.offset = (u_int)get_bsz(arg);
    245  1.1  glass }
    246  1.1  glass 
    247  1.1  glass static void
    248  1.1  glass f_skip(arg)
    249  1.1  glass 	char *arg;
    250  1.1  glass {
    251  1.1  glass 	in.offset = (u_int)get_bsz(arg);
    252  1.1  glass }
    253  1.1  glass 
    254  1.1  glass static struct conv {
    255  1.1  glass 	char *name;
    256  1.1  glass 	u_int set, noset;
    257  1.1  glass 	u_char *ctab;
    258  1.1  glass } clist[] = {
    259  1.1  glass 	"ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX,
    260  1.1  glass 	"block",	C_BLOCK,	C_UNBLOCK,	NULL,
    261  1.1  glass 	"ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX,
    262  1.1  glass 	"ibm",		C_EBCDIC,	C_ASCII,	a2ibm_POSIX,
    263  1.1  glass 	"lcase",	C_LCASE,	C_UCASE,	NULL,
    264  1.1  glass 	"noerror",	C_NOERROR,	0,		NULL,
    265  1.1  glass 	"notrunc",	C_NOTRUNC,	0,		NULL,
    266  1.1  glass 	"oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V,
    267  1.1  glass 	"oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V,
    268  1.1  glass 	"oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V,
    269  1.1  glass 	"swab",		C_SWAB,		0,		NULL,
    270  1.1  glass 	"sync",		C_SYNC,		0,		NULL,
    271  1.1  glass 	"ucase",	C_UCASE,	C_LCASE,	NULL,
    272  1.1  glass 	"unblock",	C_UNBLOCK,	C_BLOCK,	NULL,
    273  1.1  glass };
    274  1.1  glass 
    275  1.1  glass static void
    276  1.1  glass f_conv(arg)
    277  1.1  glass 	char *arg;
    278  1.1  glass {
    279  1.1  glass 	register struct conv *cp;
    280  1.1  glass 	struct conv tmp;
    281  1.1  glass 	static int c_conv __P((const void *, const void *));
    282  1.1  glass 
    283  1.1  glass 	while (arg != NULL) {
    284  1.1  glass 		tmp.name = strsep(&arg, ",");
    285  1.1  glass 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
    286  1.1  glass 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
    287  1.1  glass 		    c_conv)))
    288  1.1  glass 			err("unknown conversion %s", tmp.name);
    289  1.1  glass 		if (ddflags & cp->noset)
    290  1.1  glass 			err("%s: illegal conversion combination", tmp.name);
    291  1.1  glass 		ddflags |= cp->set;
    292  1.1  glass 		if (cp->ctab)
    293  1.1  glass 			ctab = cp->ctab;
    294  1.1  glass 	}
    295  1.1  glass }
    296  1.1  glass 
    297  1.1  glass static int
    298  1.1  glass c_conv(a, b)
    299  1.1  glass 	const void *a, *b;
    300  1.1  glass {
    301  1.1  glass 	return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
    302  1.1  glass }
    303  1.1  glass 
    304  1.1  glass /*
    305  1.1  glass  * Convert an expression of the following forms to an unsigned long.
    306  1.1  glass  * 	1) A positive decimal number.
    307  1.1  glass  *	2) A positive decimal number followed by a b (mult by 512).
    308  1.1  glass  *	3) A positive decimal number followed by a k (mult by 1024).
    309  1.1  glass  *	4) A positive decimal number followed by a m (mult by 512).
    310  1.1  glass  *	5) A positive decimal number followed by a w (mult by sizeof int)
    311  1.1  glass  *	6) Two or more positive decimal numbers (with/without k,b or w).
    312  1.1  glass  *	   seperated by x (also * for backwards compatibility), specifying
    313  1.1  glass  *	   the product of the indicated values.
    314  1.1  glass  */
    315  1.1  glass static u_long
    316  1.1  glass get_bsz(val)
    317  1.1  glass 	char *val;
    318  1.1  glass {
    319  1.1  glass 	char *expr;
    320  1.1  glass 	u_long num, t;
    321  1.1  glass 
    322  1.1  glass 	num = strtoul(val, &expr, 0);
    323  1.1  glass 	if (num == ULONG_MAX)			/* Overflow. */
    324  1.1  glass 		err("%s: %s", oper, strerror(errno));
    325  1.1  glass 	if (expr == val)			/* No digits. */
    326  1.1  glass 		err("%s: illegal numeric value", oper);
    327  1.1  glass 
    328  1.1  glass 	switch(*expr) {
    329  1.1  glass 	case 'b':
    330  1.1  glass 		t = num;
    331  1.1  glass 		num *= 512;
    332  1.1  glass 		if (t > num)
    333  1.1  glass 			goto erange;
    334  1.1  glass 		++expr;
    335  1.1  glass 		break;
    336  1.1  glass 	case 'k':
    337  1.1  glass 		t = num;
    338  1.1  glass 		num *= 1024;
    339  1.1  glass 		if (t > num)
    340  1.1  glass 			goto erange;
    341  1.1  glass 		++expr;
    342  1.1  glass 		break;
    343  1.1  glass 	case 'm':
    344  1.1  glass 		t = num;
    345  1.1  glass 		num *= 1048576;
    346  1.1  glass 		if (t > num)
    347  1.1  glass 			goto erange;
    348  1.1  glass 		++expr;
    349  1.1  glass 		break;
    350  1.1  glass 	case 'w':
    351  1.1  glass 		t = num;
    352  1.1  glass 		num *= sizeof(int);
    353  1.1  glass 		if (t > num)
    354  1.1  glass 			goto erange;
    355  1.1  glass 		++expr;
    356  1.1  glass 		break;
    357  1.1  glass 	}
    358  1.1  glass 
    359  1.1  glass 	switch(*expr) {
    360  1.1  glass 		case '\0':
    361  1.1  glass 			break;
    362  1.1  glass 		case '*':			/* Backward compatible. */
    363  1.1  glass 		case 'x':
    364  1.1  glass 			t = num;
    365  1.1  glass 			num *= get_bsz(expr + 1);
    366  1.1  glass 			if (t > num)
    367  1.1  glass erange:				err("%s: %s", oper, strerror(ERANGE));
    368  1.1  glass 			break;
    369  1.1  glass 		default:
    370  1.1  glass 			err("%s: illegal numeric value", oper);
    371  1.1  glass 	}
    372  1.1  glass 	return(num);
    373  1.1  glass }
    374