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