1 /* $OpenBSD: args.c,v 1.14 2003/06/11 23:42:12 deraadt 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.14 2003/06/11 23:42:12 deraadt 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 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 (in.dbsz > SIZE_T_MAX || out.dbsz > SIZE_T_MAX) 175 errx(1, "buffer sizes cannot be greater than %u", SIZE_T_MAX); 176 if (in.offset > QUAD_MAX / in.dbsz || out.offset > QUAD_MAX / out.dbsz) 177 errx(1, "seek offsets cannot be larger than %qd", QUAD_MAX); 178 } 179 180 static int 181 c_arg(const void *a, const void *b) 182 { 183 184 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name)); 185 } 186 187 static void 188 f_bs(char *arg) 189 { 190 191 in.dbsz = out.dbsz = get_bsz(arg); 192 } 193 194 static void 195 f_cbs(char *arg) 196 { 197 198 cbsz = get_bsz(arg); 199 } 200 201 static void 202 f_count(char *arg) 203 { 204 205 if ((cpy_cnt = get_bsz(arg)) == 0) 206 cpy_cnt = (size_t)-1; 207 } 208 209 static void 210 f_files(char *arg) 211 { 212 213 files_cnt = get_bsz(arg); 214 } 215 216 static void 217 f_ibs(char *arg) 218 { 219 220 if (!(ddflags & C_BS)) 221 in.dbsz = get_bsz(arg); 222 } 223 224 static void 225 f_if(char *arg) 226 { 227 228 in.name = arg; 229 } 230 231 static void 232 f_obs(char *arg) 233 { 234 235 if (!(ddflags & C_BS)) 236 out.dbsz = get_bsz(arg); 237 } 238 239 static void 240 f_of(char *arg) 241 { 242 243 out.name = arg; 244 } 245 246 static void 247 f_seek(char *arg) 248 { 249 250 out.offset = get_off(arg); 251 } 252 253 static void 254 f_skip(char *arg) 255 { 256 257 in.offset = get_off(arg); 258 } 259 260 #ifdef NO_CONV 261 /* Build a small version (i.e. for a ramdisk root) */ 262 static void 263 f_conv(char *arg) 264 { 265 errx(1, "conv option disabled"); 266 } 267 #else /* NO_CONV */ 268 269 static const struct conv { 270 char *name; 271 u_int set, noset; 272 const u_char *ctab; 273 } clist[] = { 274 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX }, 275 { "block", C_BLOCK, C_UNBLOCK, NULL }, 276 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX }, 277 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX }, 278 { "lcase", C_LCASE, C_UCASE, NULL }, 279 { "noerror", C_NOERROR, 0, NULL }, 280 { "notrunc", C_NOTRUNC, 0, NULL }, 281 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V }, 282 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V }, 283 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V }, 284 { "osync", C_OSYNC, C_BS, NULL }, 285 { "swab", C_SWAB, 0, NULL }, 286 { "sync", C_SYNC, 0, NULL }, 287 { "ucase", C_UCASE, C_LCASE, NULL }, 288 { "unblock", C_UNBLOCK, C_BLOCK, NULL }, 289 }; 290 291 static void 292 f_conv(char *arg) 293 { 294 struct conv *cp, tmp; 295 296 while (arg != NULL) { 297 tmp.name = strsep(&arg, ","); 298 if (!(cp = (struct conv *)bsearch(&tmp, clist, 299 sizeof(clist)/sizeof(struct conv), sizeof(struct conv), 300 c_conv))) 301 errx(1, "unknown conversion %s", tmp.name); 302 if (ddflags & cp->noset) 303 errx(1, "%s: illegal conversion combination", tmp.name); 304 ddflags |= cp->set; 305 if (cp->ctab) 306 ctab = cp->ctab; 307 } 308 } 309 310 static int 311 c_conv(const void *a, const void *b) 312 { 313 314 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name)); 315 } 316 317 #endif /* NO_CONV */ 318 319 /* 320 * Convert an expression of the following forms to a size_t 321 * 1) A positive decimal number. 322 * 2) A positive decimal number followed by a b (mult by 512). 323 * 3) A positive decimal number followed by a k (mult by 1024). 324 * 4) A positive decimal number followed by a m (mult by 512). 325 * 5) A positive decimal number followed by a w (mult by sizeof int) 326 * 6) Two or more positive decimal numbers (with/without k,b or w). 327 * separated by x (also * for backwards compatibility), specifying 328 * the product of the indicated values. 329 */ 330 static size_t 331 get_bsz(char *val) 332 { 333 size_t num, t; 334 char *expr; 335 336 num = strtoul(val, &expr, 0); 337 if (num == SIZE_T_MAX) /* Overflow. */ 338 err(1, "%s", oper); 339 if (expr == val) /* No digits. */ 340 errx(1, "%s: illegal numeric value", oper); 341 342 switch(*expr) { 343 case 'b': 344 t = num; 345 num *= 512; 346 if (t > num) 347 goto erange; 348 ++expr; 349 break; 350 case 'k': 351 t = num; 352 num *= 1024; 353 if (t > num) 354 goto erange; 355 ++expr; 356 break; 357 case 'm': 358 t = num; 359 num *= 1048576; 360 if (t > num) 361 goto erange; 362 ++expr; 363 break; 364 case 'w': 365 t = num; 366 num *= sizeof(int); 367 if (t > num) 368 goto erange; 369 ++expr; 370 break; 371 } 372 373 switch(*expr) { 374 case '\0': 375 break; 376 case '*': /* Backward compatible. */ 377 case 'x': 378 t = num; 379 num *= get_bsz(expr + 1); 380 if (t > num) 381 erange: errx(1, "%s: %s", oper, strerror(ERANGE)); 382 break; 383 default: 384 errx(1, "%s: illegal numeric value", oper); 385 } 386 return (num); 387 } 388 389 /* 390 * Convert an expression of the following forms to an off_t 391 * 1) A positive decimal number. 392 * 2) A positive decimal number followed by a b (mult by 512). 393 * 3) A positive decimal number followed by a k (mult by 1024). 394 * 4) A positive decimal number followed by a m (mult by 512). 395 * 5) A positive decimal number followed by a w (mult by sizeof int) 396 * 6) Two or more positive decimal numbers (with/without k,b or w). 397 * separated by x (also * for backwards compatibility), specifying 398 * the product of the indicated values. 399 */ 400 static off_t 401 get_off(char *val) 402 { 403 off_t num, t; 404 char *expr; 405 406 num = strtoq(val, &expr, 0); 407 if (num == QUAD_MAX) /* Overflow. */ 408 err(1, "%s", oper); 409 if (expr == val) /* No digits. */ 410 errx(1, "%s: illegal numeric value", oper); 411 412 switch(*expr) { 413 case 'b': 414 t = num; 415 num *= 512; 416 if (t > num) 417 goto erange; 418 ++expr; 419 break; 420 case 'k': 421 t = num; 422 num *= 1024; 423 if (t > num) 424 goto erange; 425 ++expr; 426 break; 427 case 'm': 428 t = num; 429 num *= 1048576; 430 if (t > num) 431 goto erange; 432 ++expr; 433 break; 434 case 'w': 435 t = num; 436 num *= sizeof(int); 437 if (t > num) 438 goto erange; 439 ++expr; 440 break; 441 } 442 443 switch(*expr) { 444 case '\0': 445 break; 446 case '*': /* Backward compatible. */ 447 case 'x': 448 t = num; 449 num *= get_off(expr + 1); 450 if (t > num) 451 erange: errx(1, "%s: %s", oper, strerror(ERANGE)); 452 break; 453 default: 454 errx(1, "%s: illegal numeric value", oper); 455 } 456 return (num); 457 } 458