1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #ifndef lint 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 #include <sys/param.h> 40 41 #include <ctype.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <inttypes.h> 45 #include <limits.h> 46 #include <signal.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include "dd.h" 51 #include "extern.h" 52 53 static int c_arg(const void *, const void *); 54 static int c_conv(const void *, const void *); 55 static int c_iflag(const void *, const void *); 56 static int c_oflag(const void *, const void *); 57 static void f_bs(char *); 58 static void f_cbs(char *); 59 static void f_conv(char *); 60 static void f_count(char *); 61 static void f_files(char *); 62 static void f_fillchar(char *); 63 static void f_ibs(char *); 64 static void f_if(char *); 65 static void f_iflag(char *); 66 static void f_obs(char *); 67 static void f_of(char *); 68 static void f_oflag(char *); 69 static void f_seek(char *); 70 static void f_skip(char *); 71 static void f_speed(char *); 72 static void f_status(char *); 73 static uintmax_t get_num(const char *); 74 static off_t get_off_t(const char *); 75 76 static const struct arg { 77 const char *name; 78 void (*f)(char *); 79 uint64_t set, noset; 80 } args[] = { 81 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC }, 82 { "cbs", f_cbs, C_CBS, C_CBS }, 83 { "conv", f_conv, 0, 0 }, 84 { "count", f_count, C_COUNT, C_COUNT }, 85 { "files", f_files, C_FILES, C_FILES }, 86 { "fillchar", f_fillchar, C_FILL, C_FILL }, 87 { "ibs", f_ibs, C_IBS, C_BS|C_IBS }, 88 { "if", f_if, C_IF, C_IF }, 89 { "iflag", f_iflag, 0, 0 }, 90 { "iseek", f_skip, C_SKIP, C_SKIP }, 91 { "obs", f_obs, C_OBS, C_BS|C_OBS }, 92 { "of", f_of, C_OF, C_OF }, 93 { "oflag", f_oflag, 0, 0 }, 94 { "oseek", f_seek, C_SEEK, C_SEEK }, 95 { "seek", f_seek, C_SEEK, C_SEEK }, 96 { "skip", f_skip, C_SKIP, C_SKIP }, 97 { "speed", f_speed, 0, 0 }, 98 { "status", f_status, C_STATUS,C_STATUS }, 99 }; 100 101 static char *oper; 102 103 /* 104 * args -- parse JCL syntax of dd. 105 */ 106 void 107 jcl(char **argv) 108 { 109 struct arg *ap, tmp; 110 char *arg; 111 112 in.dbsz = out.dbsz = 512; 113 114 while ((oper = *++argv) != NULL) { 115 if ((oper = strdup(oper)) == NULL) 116 errx(1, "unable to allocate space for the argument \"%s\"", *argv); 117 if ((arg = strchr(oper, '=')) == NULL) 118 errx(1, "unknown operand %s", oper); 119 *arg++ = '\0'; 120 if (!*arg) 121 errx(1, "no value specified for %s", oper); 122 tmp.name = oper; 123 if (!(ap = (struct arg *)bsearch(&tmp, args, 124 sizeof(args)/sizeof(struct arg), sizeof(struct arg), 125 c_arg))) 126 errx(1, "unknown operand %s", tmp.name); 127 if (ddflags & ap->noset) 128 errx(1, "%s: illegal argument combination or already set", 129 tmp.name); 130 ddflags |= ap->set; 131 ap->f(arg); 132 } 133 134 /* Final sanity checks. */ 135 136 if (ddflags & C_BS) { 137 /* 138 * Bs is turned off by any conversion -- we assume the user 139 * just wanted to set both the input and output block sizes 140 * and didn't want the bs semantics, so we don't warn. 141 */ 142 if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE | 143 C_UNBLOCK)) 144 ddflags &= ~C_BS; 145 146 /* Bs supersedes ibs and obs. */ 147 if (ddflags & C_BS && ddflags & (C_IBS | C_OBS)) 148 warnx("bs supersedes ibs and obs"); 149 } 150 151 /* 152 * Ascii/ebcdic and cbs implies block/unblock. 153 * Block/unblock requires cbs and vice-versa. 154 */ 155 if (ddflags & (C_BLOCK | C_UNBLOCK)) { 156 if (!(ddflags & C_CBS)) 157 errx(1, "record operations require cbs"); 158 if (cbsz == 0) 159 errx(1, "cbs cannot be zero"); 160 cfunc = ddflags & C_BLOCK ? block : unblock; 161 } else if (ddflags & C_CBS) { 162 if (ddflags & (C_ASCII | C_EBCDIC)) { 163 if (ddflags & C_ASCII) { 164 ddflags |= C_UNBLOCK; 165 cfunc = unblock; 166 } else { 167 ddflags |= C_BLOCK; 168 cfunc = block; 169 } 170 } else 171 errx(1, "cbs meaningless if not doing record operations"); 172 } else 173 cfunc = def; 174 } 175 176 static int 177 c_arg(const void *a, const void *b) 178 { 179 180 return (strcmp(((const struct arg *)a)->name, 181 ((const struct arg *)b)->name)); 182 } 183 184 static void 185 f_bs(char *arg) 186 { 187 uintmax_t res; 188 189 res = get_num(arg); 190 if (res < 1 || res > SSIZE_MAX) 191 errx(1, "bs must be between 1 and %zd", (ssize_t)SSIZE_MAX); 192 in.dbsz = out.dbsz = (size_t)res; 193 } 194 195 static void 196 f_cbs(char *arg) 197 { 198 uintmax_t res; 199 200 res = get_num(arg); 201 if (res < 1 || res > SSIZE_MAX) 202 errx(1, "cbs must be between 1 and %zd", (ssize_t)SSIZE_MAX); 203 cbsz = (size_t)res; 204 } 205 206 static void 207 f_count(char *arg) 208 { 209 uintmax_t res; 210 211 res = get_num(arg); 212 if (res == UINTMAX_MAX) 213 errc(1, ERANGE, "%s", oper); 214 if (res == 0) 215 cpy_cnt = UINTMAX_MAX; 216 else 217 cpy_cnt = res; 218 } 219 220 static void 221 f_files(char *arg) 222 { 223 224 files_cnt = get_num(arg); 225 if (files_cnt < 1) 226 errx(1, "files must be between 1 and %zu", SIZE_MAX); 227 } 228 229 static void 230 f_fillchar(char *arg) 231 { 232 233 if (strlen(arg) != 1) 234 errx(1, "need exactly one fill char"); 235 236 fill_char = arg[0]; 237 } 238 239 static void 240 f_ibs(char *arg) 241 { 242 uintmax_t res; 243 244 if (!(ddflags & C_BS)) { 245 res = get_num(arg); 246 if (res < 1 || res > SSIZE_MAX) 247 errx(1, "ibs must be between 1 and %zd", 248 (ssize_t)SSIZE_MAX); 249 in.dbsz = (size_t)res; 250 } 251 } 252 253 static void 254 f_if(char *arg) 255 { 256 257 in.name = arg; 258 } 259 260 static const struct iflag { 261 const char *name; 262 uint64_t set, noset; 263 } ilist[] = { 264 { "direct", C_IDIRECT, 0 }, 265 { "fullblock", C_IFULLBLOCK, C_SYNC }, 266 }; 267 268 static void 269 f_iflag(char *arg) 270 { 271 struct iflag *ip, tmp; 272 273 while (arg != NULL) { 274 tmp.name = strsep(&arg, ","); 275 ip = bsearch(&tmp, ilist, nitems(ilist), sizeof(struct iflag), 276 c_iflag); 277 if (ip == NULL) 278 errx(1, "unknown iflag %s", tmp.name); 279 if (ddflags & ip->noset) 280 errx(1, "%s: illegal conversion combination", tmp.name); 281 ddflags |= ip->set; 282 } 283 } 284 285 static int 286 c_iflag(const void *a, const void *b) 287 { 288 289 return (strcmp(((const struct iflag *)a)->name, 290 ((const struct iflag *)b)->name)); 291 } 292 293 static void 294 f_obs(char *arg) 295 { 296 uintmax_t res; 297 298 if (!(ddflags & C_BS)) { 299 res = get_num(arg); 300 if (res < 1 || res > SSIZE_MAX) 301 errx(1, "obs must be between 1 and %zd", 302 (ssize_t)SSIZE_MAX); 303 out.dbsz = (size_t)res; 304 } 305 } 306 307 static void 308 f_of(char *arg) 309 { 310 311 out.name = arg; 312 } 313 314 static void 315 f_seek(char *arg) 316 { 317 318 out.offset = get_off_t(arg); 319 } 320 321 static void 322 f_skip(char *arg) 323 { 324 325 in.offset = get_off_t(arg); 326 } 327 328 static void 329 f_speed(char *arg) 330 { 331 332 speed = get_num(arg); 333 } 334 335 static void 336 f_status(char *arg) 337 { 338 339 if (strcmp(arg, "none") == 0) 340 ddflags |= C_NOINFO; 341 else if (strcmp(arg, "noxfer") == 0) 342 ddflags |= C_NOXFER; 343 else if (strcmp(arg, "progress") == 0) 344 ddflags |= C_PROGRESS; 345 else 346 errx(1, "unknown status %s", arg); 347 } 348 349 static const struct conv { 350 const char *name; 351 uint64_t set, noset; 352 const u_char *ctab; 353 } clist[] = { 354 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX }, 355 { "block", C_BLOCK, C_UNBLOCK, NULL }, 356 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX }, 357 { "fdatasync", C_FDATASYNC, 0, NULL }, 358 { "fsync", C_FSYNC, 0, NULL }, 359 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX }, 360 { "lcase", C_LCASE, C_UCASE, NULL }, 361 { "noerror", C_NOERROR, 0, NULL }, 362 { "notrunc", C_NOTRUNC, 0, NULL }, 363 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V }, 364 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V }, 365 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V }, 366 { "osync", C_OSYNC, C_BS, NULL }, 367 { "pareven", C_PAREVEN, C_PARODD|C_PARSET|C_PARNONE, NULL}, 368 { "parnone", C_PARNONE, C_PARODD|C_PARSET|C_PAREVEN, NULL}, 369 { "parodd", C_PARODD, C_PAREVEN|C_PARSET|C_PARNONE, NULL}, 370 { "parset", C_PARSET, C_PARODD|C_PAREVEN|C_PARNONE, NULL}, 371 { "sparse", C_SPARSE, 0, NULL }, 372 { "swab", C_SWAB, 0, NULL }, 373 { "sync", C_SYNC, C_IFULLBLOCK, NULL }, 374 { "ucase", C_UCASE, C_LCASE, NULL }, 375 { "unblock", C_UNBLOCK, C_BLOCK, NULL }, 376 }; 377 378 static void 379 f_conv(char *arg) 380 { 381 struct conv *cp, tmp; 382 383 while (arg != NULL) { 384 tmp.name = strsep(&arg, ","); 385 cp = bsearch(&tmp, clist, nitems(clist), sizeof(struct conv), 386 c_conv); 387 if (cp == NULL) 388 errx(1, "unknown conversion %s", tmp.name); 389 if (ddflags & cp->noset) 390 errx(1, "%s: illegal conversion combination", tmp.name); 391 ddflags |= cp->set; 392 if (cp->ctab) 393 ctab = cp->ctab; 394 } 395 } 396 397 static int 398 c_conv(const void *a, const void *b) 399 { 400 401 return (strcmp(((const struct conv *)a)->name, 402 ((const struct conv *)b)->name)); 403 } 404 405 static const struct oflag { 406 const char *name; 407 uint64_t set; 408 } olist[] = { 409 { "direct", C_ODIRECT }, 410 { "fsync", C_OFSYNC }, 411 { "sync", C_OFSYNC }, 412 }; 413 414 static void 415 f_oflag(char *arg) 416 { 417 struct oflag *op, tmp; 418 419 while (arg != NULL) { 420 tmp.name = strsep(&arg, ","); 421 op = bsearch(&tmp, olist, nitems(olist), sizeof(struct oflag), 422 c_oflag); 423 if (op == NULL) 424 errx(1, "unknown open flag %s", tmp.name); 425 ddflags |= op->set; 426 } 427 } 428 429 static int 430 c_oflag(const void *a, const void *b) 431 { 432 433 return (strcmp(((const struct oflag *)a)->name, 434 ((const struct oflag *)b)->name)); 435 } 436 437 static intmax_t 438 postfix_to_mult(const char expr) 439 { 440 intmax_t mult; 441 442 mult = 0; 443 switch (expr) { 444 case 'B': 445 case 'b': 446 mult = 512; 447 break; 448 case 'K': 449 case 'k': 450 mult = 1 << 10; 451 break; 452 case 'M': 453 case 'm': 454 mult = 1 << 20; 455 break; 456 case 'G': 457 case 'g': 458 mult = 1 << 30; 459 break; 460 case 'T': 461 case 't': 462 mult = (uintmax_t)1 << 40; 463 break; 464 case 'P': 465 case 'p': 466 mult = (uintmax_t)1 << 50; 467 break; 468 case 'W': 469 case 'w': 470 mult = sizeof(int); 471 break; 472 } 473 474 return (mult); 475 } 476 477 /* 478 * Convert an expression of the following forms to a uintmax_t. 479 * 1) A positive decimal number. 480 * 2) A positive decimal number followed by a 'b' or 'B' (mult by 512). 481 * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). 482 * 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20). 483 * 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30). 484 * 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40). 485 * 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50). 486 * 8) A positive decimal number followed by a 'w' or 'W' (mult by sizeof int). 487 * 9) Two or more positive decimal numbers (with/without [BbKkMmGgWw]) 488 * separated by 'x' or 'X' (also '*' for backwards compatibility), 489 * specifying the product of the indicated values. 490 */ 491 static uintmax_t 492 get_num(const char *val) 493 { 494 uintmax_t num, mult, prevnum; 495 char *expr; 496 497 errno = 0; 498 num = strtoumax(val, &expr, 0); 499 if (expr == val) /* No valid digits. */ 500 errx(1, "%s: invalid numeric value", oper); 501 if (errno != 0) 502 err(1, "%s", oper); 503 504 mult = postfix_to_mult(*expr); 505 506 if (mult != 0) { 507 prevnum = num; 508 num *= mult; 509 /* Check for overflow. */ 510 if (num / mult != prevnum) 511 goto erange; 512 expr++; 513 } 514 515 switch (*expr) { 516 case '\0': 517 break; 518 case '*': /* Backward compatible. */ 519 case 'X': 520 case 'x': 521 mult = get_num(expr + 1); 522 prevnum = num; 523 num *= mult; 524 if (num / mult == prevnum) 525 break; 526 erange: 527 errx(1, "%s: %s", oper, strerror(ERANGE)); 528 default: 529 errx(1, "%s: illegal numeric value", oper); 530 } 531 return (num); 532 } 533 534 /* 535 * Convert an expression of the following forms to an off_t. This is the 536 * same as get_num(), but it uses signed numbers. 537 * 538 * The major problem here is that an off_t may not necessarily be a intmax_t. 539 */ 540 static off_t 541 get_off_t(const char *val) 542 { 543 intmax_t num, mult, prevnum; 544 char *expr; 545 546 errno = 0; 547 num = strtoimax(val, &expr, 0); 548 if (expr == val) /* No valid digits. */ 549 errx(1, "%s: invalid numeric value", oper); 550 if (errno != 0) 551 err(1, "%s", oper); 552 553 mult = postfix_to_mult(*expr); 554 555 if (mult != 0) { 556 prevnum = num; 557 num *= mult; 558 /* Check for overflow. */ 559 if ((prevnum > 0) != (num > 0) || num / mult != prevnum) 560 goto erange; 561 expr++; 562 } 563 564 switch (*expr) { 565 case '\0': 566 break; 567 case '*': /* Backward compatible. */ 568 case 'X': 569 case 'x': 570 mult = (intmax_t)get_off_t(expr + 1); 571 prevnum = num; 572 num *= mult; 573 if ((prevnum > 0) == (num > 0) && num / mult == prevnum) 574 break; 575 erange: 576 errx(1, "%s: %s", oper, strerror(ERANGE)); 577 default: 578 errx(1, "%s: illegal numeric value", oper); 579 } 580 return (num); 581 } 582