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