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