args.c (273734) | args.c (273743) |
---|---|
1/*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Keith Muller of the University of California, San Diego and Lance 7 * Visser of Convex Computer Corporation. 8 * --- 23 unchanged lines hidden (view full) --- 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; 37#endif 38#endif /* not lint */ 39#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Keith Muller of the University of California, San Diego and Lance 7 * Visser of Convex Computer Corporation. 8 * --- 23 unchanged lines hidden (view full) --- 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; 37#endif 38#endif /* not lint */ 39#include <sys/cdefs.h> |
40__FBSDID("$FreeBSD: head/bin/dd/args.c 273734 2014-10-27 11:38:17Z pi $"); | 40__FBSDID("$FreeBSD: head/bin/dd/args.c 273743 2014-10-27 17:39:37Z pi $"); |
41 42#include <sys/types.h> 43 | 41 42#include <sys/types.h> 43 |
44#include <ctype.h> | |
45#include <err.h> 46#include <errno.h> 47#include <inttypes.h> 48#include <limits.h> 49#include <signal.h> 50#include <stdlib.h> 51#include <string.h> 52 --- 114 unchanged lines hidden (view full) --- 167 } else 168 cfunc = def; 169 170 /* 171 * Bail out if the calculation of a file offset would overflow. 172 */ 173 if (in.offset > OFF_MAX / (ssize_t)in.dbsz || 174 out.offset > OFF_MAX / (ssize_t)out.dbsz) | 44#include <err.h> 45#include <errno.h> 46#include <inttypes.h> 47#include <limits.h> 48#include <signal.h> 49#include <stdlib.h> 50#include <string.h> 51 --- 114 unchanged lines hidden (view full) --- 166 } else 167 cfunc = def; 168 169 /* 170 * Bail out if the calculation of a file offset would overflow. 171 */ 172 if (in.offset > OFF_MAX / (ssize_t)in.dbsz || 173 out.offset > OFF_MAX / (ssize_t)out.dbsz) |
175 errx(1, "seek offsets cannot be larger than %jd", OFF_MAX); | 174 errx(1, "seek offsets cannot be larger than %jd", 175 (intmax_t)OFF_MAX); |
176} 177 178static int 179c_arg(const void *a, const void *b) 180{ 181 182 return (strcmp(((const struct arg *)a)->name, 183 ((const struct arg *)b)->name)); 184} 185 186static void 187f_bs(char *arg) 188{ | 176} 177 178static int 179c_arg(const void *a, const void *b) 180{ 181 182 return (strcmp(((const struct arg *)a)->name, 183 ((const struct arg *)b)->name)); 184} 185 186static void 187f_bs(char *arg) 188{ |
189 uintmax_t res; |
|
189 | 190 |
190 in.dbsz = out.dbsz = get_num(arg); 191 if (out.dbsz < 1 || out.dbsz > SSIZE_MAX) 192 errx(1, "bs must be between 1 and %jd", SSIZE_MAX); | 191 res = get_num(arg); 192 if (res < 1 || res > SSIZE_MAX) 193 errx(1, "bs must be between 1 and %jd", (intmax_t)SSIZE_MAX); 194 in.dbsz = out.dbsz = (size_t)res; |
193} 194 195static void 196f_cbs(char *arg) 197{ | 195} 196 197static void 198f_cbs(char *arg) 199{ |
200 uintmax_t res; |
|
198 | 201 |
199 cbsz = get_num(arg); 200 if (cbsz < 1 || cbsz > SSIZE_MAX) 201 errx(1, "cbs must be between 1 and %jd", SSIZE_MAX); | 202 res = get_num(arg); 203 if (res < 1 || res > SSIZE_MAX) 204 errx(1, "cbs must be between 1 and %jd", (intmax_t)SSIZE_MAX); 205 cbsz = (size_t)res; |
202} 203 204static void 205f_count(char *arg) 206{ | 206} 207 208static void 209f_count(char *arg) 210{ |
211 intmax_t res; |
|
207 | 212 |
208 cpy_cnt = get_num(arg); 209 if (cpy_cnt == SIZE_MAX) 210 errc(1, ERANGE, "%s", oper); 211 if (cpy_cnt == 0) 212 cpy_cnt = -1; | 213 res = (intmax_t)get_num(arg); 214 if (res < 0) 215 errx(1, "count cannot be negative"); 216 if (res == 0) 217 cpy_cnt = (uintmax_t)-1; 218 else 219 cpy_cnt = (uintmax_t)res; |
213} 214 215static void 216f_files(char *arg) 217{ 218 219 files_cnt = get_num(arg); 220 if (files_cnt < 1) | 220} 221 222static void 223f_files(char *arg) 224{ 225 226 files_cnt = get_num(arg); 227 if (files_cnt < 1) |
221 errx(1, "files must be between 1 and %ju", SIZE_MAX); | 228 errx(1, "files must be between 1 and %jd", (uintmax_t)-1); |
222} 223 224static void 225f_fillchar(char *arg) 226{ 227 228 if (strlen(arg) != 1) 229 errx(1, "need exactly one fill char"); 230 231 fill_char = arg[0]; 232} 233 234static void 235f_ibs(char *arg) 236{ | 229} 230 231static void 232f_fillchar(char *arg) 233{ 234 235 if (strlen(arg) != 1) 236 errx(1, "need exactly one fill char"); 237 238 fill_char = arg[0]; 239} 240 241static void 242f_ibs(char *arg) 243{ |
244 uintmax_t res; |
|
237 238 if (!(ddflags & C_BS)) { | 245 246 if (!(ddflags & C_BS)) { |
239 in.dbsz = get_num(arg); 240 if (in.dbsz < 1 || in.dbsz > SSIZE_MAX) 241 errx(1, "ibs must be between 1 and %ju", SSIZE_MAX); | 247 res = get_num(arg); 248 if (res < 1 || res > SSIZE_MAX) 249 errx(1, "ibs must be between 1 and %jd", 250 (intmax_t)SSIZE_MAX); 251 in.dbsz = (size_t)res; |
242 } 243} 244 245static void 246f_if(char *arg) 247{ 248 249 in.name = arg; 250} 251 252static void 253f_obs(char *arg) 254{ | 252 } 253} 254 255static void 256f_if(char *arg) 257{ 258 259 in.name = arg; 260} 261 262static void 263f_obs(char *arg) 264{ |
265 uintmax_t res; |
|
255 256 if (!(ddflags & C_BS)) { | 266 267 if (!(ddflags & C_BS)) { |
257 out.dbsz = get_num(arg); 258 if (out.dbsz < 1 || out.dbsz > SSIZE_MAX) 259 errx(1, "obs must be between 1 and %jd", SSIZE_MAX); | 268 res = get_num(arg); 269 if (res < 1 || res > SSIZE_MAX) 270 errx(1, "obs must be between 1 and %jd", 271 (intmax_t)SSIZE_MAX); 272 out.dbsz = (size_t)res; |
260 } 261} 262 263static void 264f_of(char *arg) 265{ 266 267 out.name = arg; --- 92 unchanged lines hidden (view full) --- 360 * specifying the product of the indicated values. 361 */ 362static uintmax_t 363get_num(const char *val) 364{ 365 uintmax_t num, mult, prevnum; 366 char *expr; 367 | 273 } 274} 275 276static void 277f_of(char *arg) 278{ 279 280 out.name = arg; --- 92 unchanged lines hidden (view full) --- 373 * specifying the product of the indicated values. 374 */ 375static uintmax_t 376get_num(const char *val) 377{ 378 uintmax_t num, mult, prevnum; 379 char *expr; 380 |
368 while (isspace(val[0])) 369 val++; 370 371 if (val[0] == '-') 372 errx(1, "%s: cannot be negative", oper); 373 | |
374 errno = 0; | 381 errno = 0; |
375 num = strtoull(val, &expr, 0); | 382 num = strtouq(val, &expr, 0); |
376 if (errno != 0) /* Overflow or underflow. */ 377 err(1, "%s", oper); | 383 if (errno != 0) /* Overflow or underflow. */ 384 err(1, "%s", oper); |
378 | 385 |
379 if (expr == val) /* No valid digits. */ 380 errx(1, "%s: illegal numeric value", oper); 381 382 mult = 0; 383 switch (*expr) { 384 case 'B': 385 case 'b': 386 mult = 512; --- 120 unchanged lines hidden --- | 386 if (expr == val) /* No valid digits. */ 387 errx(1, "%s: illegal numeric value", oper); 388 389 mult = 0; 390 switch (*expr) { 391 case 'B': 392 case 'b': 393 mult = 512; --- 120 unchanged lines hidden --- |