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