Home | History | Annotate | Line # | Download | only in dd
      1 /*	$NetBSD: args.c,v 1.43 2024/01/26 07:10:04 mlelstv 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. 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/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
     40 #else
     41 __RCSID("$NetBSD: args.c,v 1.43 2024/01/26 07:10:04 mlelstv Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/time.h>
     47 
     48 #ifndef NO_IOFLAG
     49 #include <fcntl.h>
     50 #endif /* NO_IOFLAG */
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <limits.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 
     58 #include "dd.h"
     59 #include "extern.h"
     60 
     61 static int	c_arg(const void *, const void *);
     62 
     63 #ifdef NO_MSGFMT
     64 static void	f_msgfmt(char *) __dead;
     65 #else
     66 static void	f_msgfmt(char *);
     67 #endif /* NO_MSGFMT */
     68 
     69 #ifdef NO_CONV
     70 static void	f_conv(char *) __dead;
     71 #else
     72 static void	f_conv(char *);
     73 static int	c_conv(const void *, const void *);
     74 #endif /* NO_CONV */
     75 
     76 #ifdef NO_IOFLAG
     77 static void	f_iflag(char *) __dead;
     78 static void	f_oflag(char *) __dead;
     79 #else
     80 static void	f_iflag(char *);
     81 static void	f_oflag(char *);
     82 static u_int	f_ioflag(char *, u_int);
     83 static int	c_ioflag(const void *, const void *);
     84 #endif /* NO_IOFLAG */
     85 
     86 static void	f_bs(char *);
     87 static void	f_cbs(char *);
     88 static void	f_count(char *);
     89 static void	f_files(char *);
     90 static void	f_ibs(char *);
     91 static void	f_if(char *);
     92 static void	f_obs(char *);
     93 static void	f_of(char *);
     94 static void	f_seek(char *);
     95 static void	f_skip(char *);
     96 static void	f_progress(char *);
     97 
     98 static const struct arg {
     99 	const char *name;
    100 	void (*f)(char *);
    101 	u_int set, noset;
    102 } args[] = {
    103      /* the array needs to be sorted by the first column so
    104 	bsearch() can be used to find commands quickly */
    105 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
    106 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
    107 	{ "conv",	f_conv,		0,	 0 },
    108 	{ "count",	f_count,	C_COUNT, C_COUNT },
    109 	{ "files",	f_files,	C_FILES, C_FILES },
    110 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
    111 	{ "if",		f_if,		C_IF,	 C_IF },
    112 	{ "iflag",	f_iflag,	C_IFLAG, C_IFLAG },
    113 	{ "iseek",	f_skip,		C_SKIP,	 C_SKIP },
    114 	{ "msgfmt",	f_msgfmt,	0,	 0 },
    115 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
    116 	{ "of",		f_of,		C_OF,	 C_OF },
    117 	{ "oflag",	f_oflag,	C_OFLAG, C_OFLAG },
    118 	{ "oseek",	f_seek,		C_SEEK,	 C_SEEK },
    119 	{ "progress",	f_progress,	0,	 0 },
    120 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
    121 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
    122 };
    123 
    124 /*
    125  * args -- parse JCL syntax of dd.
    126  */
    127 void
    128 jcl(char **argv)
    129 {
    130 	const struct arg *ap;
    131 	struct arg tmp;
    132 	char *oper, *arg;
    133 
    134 	in.dbsz = out.dbsz = 512;
    135 
    136 	while ((oper = *++argv) != NULL) {
    137 		if ((oper = strdup(oper)) == NULL) {
    138 			errx(EXIT_FAILURE,
    139 			    "unable to allocate space for the argument %s",
    140 			    *argv);
    141 			/* NOTREACHED */
    142 		}
    143 		if ((arg = strchr(oper, '=')) == NULL) {
    144 			errx(EXIT_FAILURE, "unknown operand %s", oper);
    145 			/* NOTREACHED */
    146 		}
    147 		*arg++ = '\0';
    148 		if (!*arg) {
    149 			errx(EXIT_FAILURE, "no value specified for %s", oper);
    150 			/* NOTREACHED */
    151 		}
    152 		tmp.name = oper;
    153 		if (!(ap = bsearch(&tmp, args,
    154 		    __arraycount(args), sizeof(*args), c_arg))) {
    155 			errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
    156 			/* NOTREACHED */
    157 		}
    158 		if (ddflags & ap->noset) {
    159 			errx(EXIT_FAILURE,
    160 			    "%s: illegal argument combination or already set",
    161 			    tmp.name);
    162 			/* NOTREACHED */
    163 		}
    164 		ddflags |= ap->set;
    165 		ap->f(arg);
    166 	}
    167 
    168 	/* Final sanity checks. */
    169 
    170 	if (ddflags & C_BS) {
    171 		/*
    172 		 * Bs is turned off by any conversion -- we assume the user
    173 		 * just wanted to set both the input and output block sizes
    174 		 * and didn't want the bs semantics, so we don't warn.
    175 		 */
    176 		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
    177 		    C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
    178 			ddflags &= ~C_BS;
    179 			ddflags |= C_IBS|C_OBS;
    180 		}
    181 
    182 		/* Bs supersedes ibs and obs. */
    183 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
    184 			warnx("bs supersedes ibs and obs");
    185 	}
    186 
    187 	/*
    188 	 * Ascii/ebcdic and cbs implies block/unblock.
    189 	 * Block/unblock requires cbs and vice-versa.
    190 	 */
    191 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
    192 		if (!(ddflags & C_CBS)) {
    193 			errx(EXIT_FAILURE, "record operations require cbs");
    194 			/* NOTREACHED */
    195 		}
    196 		cfunc = ddflags & C_BLOCK ? block : unblock;
    197 	} else if (ddflags & C_CBS) {
    198 		if (ddflags & (C_ASCII|C_EBCDIC)) {
    199 			if (ddflags & C_ASCII) {
    200 				ddflags |= C_UNBLOCK;
    201 				cfunc = unblock;
    202 			} else {
    203 				ddflags |= C_BLOCK;
    204 				cfunc = block;
    205 			}
    206 		} else {
    207 			errx(EXIT_FAILURE,
    208 			    "cbs meaningless if not doing record operations");
    209 			/* NOTREACHED */
    210 		}
    211 	} else
    212 		cfunc = def;
    213 
    214 	/* Read, write and seek calls take off_t as arguments.
    215 	 *
    216 	 * The following check is not done because an off_t is a quad
    217 	 *  for current NetBSD implementations.
    218 	 *
    219 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
    220 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
    221 	 */
    222 }
    223 
    224 static int
    225 c_arg(const void *a, const void *b)
    226 {
    227 
    228 	return (strcmp(((const struct arg *)a)->name,
    229 	    ((const struct arg *)b)->name));
    230 }
    231 
    232 static void
    233 f_bs(char *arg)
    234 {
    235 
    236 	in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
    237 }
    238 
    239 static void
    240 f_cbs(char *arg)
    241 {
    242 
    243 	cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
    244 }
    245 
    246 static void
    247 f_count(char *arg)
    248 {
    249 
    250 	cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
    251 	if (!cpy_cnt) {
    252 		summary();
    253 		exit(0);
    254 	}
    255 }
    256 
    257 static void
    258 f_files(char *arg)
    259 {
    260 
    261 	files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
    262 	if (!files_cnt) {
    263 		summary();
    264 		exit(0);
    265 	}
    266 }
    267 
    268 static void
    269 f_ibs(char *arg)
    270 {
    271 
    272 	if (!(ddflags & C_BS))
    273 		in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
    274 }
    275 
    276 static void
    277 f_if(char *arg)
    278 {
    279 
    280 	in.name = arg;
    281 }
    282 
    283 #ifdef NO_MSGFMT
    284 /* Build a small version (i.e. for a ramdisk root) */
    285 static void
    286 f_msgfmt(char *arg)
    287 {
    288 
    289 	errx(EXIT_FAILURE, "msgfmt option disabled");
    290 	/* NOTREACHED */
    291 }
    292 #else	/* NO_MSGFMT */
    293 static void
    294 f_msgfmt(char *arg)
    295 {
    296 
    297 	/*
    298 	 * If the format string is not valid, dd_write_msg() will print
    299 	 * an error and exit.
    300 	 */
    301 	dd_write_msg(arg, 0);
    302 
    303 	msgfmt = arg;
    304 }
    305 #endif	/* NO_MSGFMT */
    306 
    307 static void
    308 f_obs(char *arg)
    309 {
    310 
    311 	if (!(ddflags & C_BS))
    312 		out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
    313 }
    314 
    315 static void
    316 f_of(char *arg)
    317 {
    318 
    319 	out.name = arg;
    320 }
    321 
    322 static void
    323 f_seek(char *arg)
    324 {
    325 
    326 	out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
    327 }
    328 
    329 static void
    330 f_skip(char *arg)
    331 {
    332 
    333 	in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
    334 }
    335 
    336 static void
    337 f_progress(char *arg)
    338 {
    339 
    340 	progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
    341 }
    342 
    343 #ifdef	NO_CONV
    344 /* Build a small version (i.e. for a ramdisk root) */
    345 static void
    346 f_conv(char *arg)
    347 {
    348 
    349 	errx(EXIT_FAILURE, "conv option disabled");
    350 	/* NOTREACHED */
    351 }
    352 #else	/* NO_CONV */
    353 
    354 static const struct conv {
    355 	const char *name;
    356 	u_int set, noset;
    357 	const u_char *ctab;
    358 } clist[] = {
    359 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
    360 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
    361 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
    362 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm },
    363 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
    364 	{ "noerror",	C_NOERROR,	0,		NULL },
    365 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
    366 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
    367 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
    368 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm },
    369 	{ "osync",	C_OSYNC,	C_BS,		NULL },
    370 	{ "sparse",	C_SPARSE,	0,		NULL },
    371 	{ "swab",	C_SWAB,		0,		NULL },
    372 	{ "sync",	C_SYNC,		0,		NULL },
    373 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
    374 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
    375 	/* If you add items to this table, be sure to add the
    376 	 * conversions to the C_BS check in the jcl routine above.
    377 	 */
    378 };
    379 
    380 static void
    381 f_conv(char *arg)
    382 {
    383 	const struct conv *cp;
    384 	struct conv tmp;
    385 
    386 	while (arg != NULL) {
    387 		tmp.name = strsep(&arg, ",");
    388 		if (!(cp = bsearch(&tmp, clist,
    389 		    __arraycount(clist), sizeof(*clist), c_conv))) {
    390 			errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
    391 			/* NOTREACHED */
    392 		}
    393 		if (ddflags & cp->noset) {
    394 			errx(EXIT_FAILURE,
    395 			    "%s: illegal conversion combination", tmp.name);
    396 			/* NOTREACHED */
    397 		}
    398 		ddflags |= cp->set;
    399 		if (cp->ctab)
    400 			ctab = cp->ctab;
    401 	}
    402 }
    403 
    404 static int
    405 c_conv(const void *a, const void *b)
    406 {
    407 
    408 	return (strcmp(((const struct conv *)a)->name,
    409 	    ((const struct conv *)b)->name));
    410 }
    411 
    412 #endif	/* NO_CONV */
    413 
    414 static void
    415 f_iflag(char *arg)
    416 {
    417 /* Build a small version (i.e. for a ramdisk root) */
    418 #ifdef	NO_IOFLAG
    419 	errx(EXIT_FAILURE, "iflag option disabled");
    420 	/* NOTREACHED */
    421 #else
    422 	iflag = f_ioflag(arg, C_IFLAG);
    423 	return;
    424 #endif
    425 }
    426 
    427 static void
    428 f_oflag(char *arg)
    429 {
    430 /* Build a small version (i.e. for a ramdisk root) */
    431 #ifdef	NO_IOFLAG
    432 	errx(EXIT_FAILURE, "oflag option disabled");
    433 	/* NOTREACHED */
    434 #else
    435 	oflag = f_ioflag(arg, C_OFLAG);
    436 	return;
    437 #endif
    438 }
    439 
    440 #ifndef	NO_IOFLAG
    441 static const struct ioflag {
    442 	const char *name;
    443 	u_int set;
    444 	u_int allowed;
    445 } olist[] = {
    446      /* the array needs to be sorted by the first column so
    447 	bsearch() can be used to find commands quickly */
    448 	{ "alt_io",	O_ALT_IO,	C_IFLAG|C_OFLAG	},
    449 	{ "append",	O_APPEND,	C_OFLAG		},
    450 	{ "async",	O_ASYNC,	C_IFLAG|C_OFLAG	},
    451 	{ "cloexec",	O_CLOEXEC,	C_IFLAG|C_OFLAG	},
    452 	{ "creat",	O_CREAT,	C_OFLAG		},
    453 	{ "direct",	O_DIRECT,	C_IFLAG|C_OFLAG	},
    454 	{ "directory",	O_DIRECTORY,	C_NONE		},
    455 	{ "dsync",	O_DSYNC,	C_OFLAG		},
    456 	{ "excl",	O_EXCL,		C_OFLAG		},
    457 	{ "exlock",	O_EXLOCK,	C_IFLAG|C_OFLAG	},
    458 	{ "noctty",	O_NOCTTY,	C_IFLAG|C_OFLAG	},
    459 	{ "nofollow",	O_NOFOLLOW,	C_IFLAG|C_OFLAG	},
    460 	{ "nonblock",	O_NONBLOCK,	C_IFLAG|C_OFLAG	},
    461 	{ "nosigpipe",	O_NOSIGPIPE,	C_IFLAG|C_OFLAG	},
    462 	{ "rdonly",	O_RDONLY,	C_IFLAG		},
    463 	{ "rdwr",	O_RDWR,		C_IFLAG		},
    464 	{ "rsync",	O_RSYNC,	C_IFLAG		},
    465 	{ "shlock",	O_SHLOCK,	C_IFLAG|C_OFLAG	},
    466 	{ "sync",	O_SYNC,		C_IFLAG|C_OFLAG	},
    467 	{ "trunc",	O_TRUNC,	C_OFLAG		},
    468 	{ "wronly",	O_WRONLY,	C_OFLAG		},
    469 };
    470 
    471 static u_int
    472 f_ioflag(char *arg, u_int flagtype)
    473 {
    474 	u_int ioflag = 0;
    475 	const struct ioflag *cp;
    476 	struct ioflag tmp;
    477 	const char *flagstr = (flagtype == C_IFLAG) ? "iflag" : "oflag";
    478 
    479 	while (arg != NULL) {
    480 		tmp.name = strsep(&arg, ",");
    481 		if (!(cp = bsearch(&tmp, olist,
    482 		    __arraycount(olist), sizeof(*olist), c_ioflag))) {
    483 			errx(EXIT_FAILURE, "unknown %s %s", flagstr, tmp.name);
    484 			/* NOTREACHED */
    485 		}
    486 
    487 		if ((cp->set & O_ACCMODE) && (flagtype == C_OFLAG)) {
    488 			warnx("rdonly, rdwr and wronly are ignored for oflag");
    489 			continue;
    490 		}
    491 
    492 		if ((cp->allowed & flagtype) == 0) {
    493 			warnx("%s set for %s but makes no sense",
    494 			      cp->name, flagstr);
    495 		}
    496 
    497 		ioflag |= cp->set;
    498 	}
    499 
    500 
    501 	return ioflag;
    502 }
    503 
    504 static int
    505 c_ioflag(const void *a, const void *b)
    506 {
    507 
    508 	return (strcmp(((const struct ioflag *)a)->name,
    509 	    ((const struct ioflag *)b)->name));
    510 }
    511 #endif	/* NO_IOFLAG */
    512