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