From 767bc8ad794eec6a0b8dffaecf1e56113fcdbbcf Mon Sep 17 00:00:00 2001 From: Brian Feldman Date: Sat, 19 Jun 1999 19:49:35 +0000 Subject: [PATCH] Miscellaneous dd(1) changes: mainly fixing variable types (size_t, ssize_t, off_t, int, u_int64_t, etc.). dd(1) should now work properly with REALLY big amounts of data. --- bin/dd/args.c | 58 +++++++++++++++++++---------------------------- bin/dd/conv.c | 36 +++++++++++++++-------------- bin/dd/dd.c | 46 ++++++++++++++++++++----------------- bin/dd/dd.h | 34 ++++++++++++++------------- bin/dd/extern.h | 8 +++---- bin/dd/misc.c | 8 +++---- bin/dd/position.c | 19 +++++++++------- 7 files changed, 104 insertions(+), 105 deletions(-) diff --git a/bin/dd/args.c b/bin/dd/args.c index 0f65b3d7f3c8..b6191abdf032 100644 --- a/bin/dd/args.c +++ b/bin/dd/args.c @@ -40,7 +40,7 @@ static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; #endif static const char rcsid[] = - "$Id: args.c,v 1.13 1998/05/13 07:33:36 charnier Exp $"; + "$Id: args.c,v 1.14 1999/05/08 10:20:05 kris Exp $"; #endif /* not lint */ #include @@ -67,7 +67,7 @@ static void f_obs __P((char *)); static void f_of __P((char *)); static void f_seek __P((char *)); static void f_skip __P((char *)); -static u_long get_bsz __P((char *)); +static int64_t get_bsz __P((char *)); static struct arg { char *name; @@ -103,20 +103,20 @@ jcl(argv) while ((oper = *++argv) != NULL) { if ((oper = strdup(oper)) == NULL) - errx(1, "unable to allocate space for the argument \"%s\"", *argv); + errx(1, "unable to allocate space for the argument " + "\"%s\"", *argv); if ((arg = strchr(oper, '=')) == NULL) errx(1, "unknown operand %s", oper); *arg++ = '\0'; if (!*arg) errx(1, "no value specified for %s", oper); tmp.name = oper; - if (!(ap = (struct arg *)bsearch(&tmp, args, - sizeof(args)/sizeof(struct arg), sizeof(struct arg), - c_arg))) + if (!(ap = bsearch(&tmp, args, sizeof(args)/sizeof(struct arg), + sizeof(struct arg), c_arg))) errx(1, "unknown operand %s", tmp.name); if (ddflags & ap->noset) - errx(1, "%s: illegal argument combination or already set", - tmp.name); + errx(1, "%s: illegal argument combination or " + "already set", tmp.name); ddflags |= ap->set; ap->f(arg); } @@ -165,16 +165,6 @@ jcl(argv) if (in.dbsz == 0 || out.dbsz == 0) errx(1, "buffer sizes cannot be zero"); - - /* - * Read, write and seek calls take ints as arguments. Seek sizes - * could be larger if we wanted to do it in stages or check only - * regular files, but it's probably not worth it. - */ - if (in.dbsz > INT_MAX || out.dbsz > INT_MAX) - errx(1, "buffer sizes cannot be greater than %d", INT_MAX); - if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz) - errx(1, "seek offsets cannot be larger than %d", INT_MAX); } static int @@ -190,7 +180,7 @@ f_bs(arg) char *arg; { - in.dbsz = out.dbsz = (int)get_bsz(arg); + in.dbsz = out.dbsz = (size_t)get_bsz(arg); } static void @@ -198,7 +188,7 @@ f_cbs(arg) char *arg; { - cbsz = (int)get_bsz(arg); + cbsz = (size_t)get_bsz(arg); } static void @@ -206,7 +196,7 @@ f_count(arg) char *arg; { - cpy_cnt = (u_int)get_bsz(arg); + cpy_cnt = (size_t)get_bsz(arg); if (!cpy_cnt) terminate(0); } @@ -225,7 +215,7 @@ f_ibs(arg) { if (!(ddflags & C_BS)) - in.dbsz = (int)get_bsz(arg); + in.dbsz = (size_t)get_bsz(arg); } static void @@ -242,7 +232,7 @@ f_obs(arg) { if (!(ddflags & C_BS)) - out.dbsz = (int)get_bsz(arg); + out.dbsz = (size_t)get_bsz(arg); } static void @@ -258,7 +248,7 @@ f_seek(arg) char *arg; { - out.offset = (u_int)get_bsz(arg); + out.offset = get_bsz(arg); } static void @@ -266,7 +256,7 @@ f_skip(arg) char *arg; { - in.offset = (u_int)get_bsz(arg); + in.offset = get_bsz(arg); } static struct conv { @@ -300,9 +290,9 @@ f_conv(arg) while (arg != NULL) { tmp.name = strsep(&arg, ","); - if (!(cp = (struct conv *)bsearch(&tmp, clist, - sizeof(clist)/sizeof(struct conv), sizeof(struct conv), - c_conv))) + if (!(cp = bsearch(&tmp, clist, sizeof(clist) / + sizeof(struct conv), sizeof(struct conv), + c_conv))) errx(1, "unknown conversion %s", tmp.name); if (ddflags & cp->noset) errx(1, "%s: illegal conversion combination", tmp.name); @@ -321,7 +311,7 @@ c_conv(a, b) } /* - * Convert an expression of the following forms to an unsigned long. + * Convert an expression of the following forms to a 64-bit integer. * 1) A positive decimal number. * 2) A positive decimal number followed by a b (mult by 512). * 3) A positive decimal number followed by a k (mult by 1024). @@ -331,17 +321,15 @@ c_conv(a, b) * separated by x (also * for backwards compatibility), specifying * the product of the indicated values. */ -static u_long +static int64_t get_bsz(val) char *val; { - u_long num, t; + int64_t num, t; char *expr; - num = strtoul(val, &expr, 0); - if (num == ULONG_MAX) /* Overflow. */ - err(1, "%s", oper); - if (expr == val) /* No digits. */ + num = strtoq(val, &expr, 0); + if (num == QUAD_MAX || num < 0 || expr == val) errx(1, "%s: illegal numeric value", oper); switch(*expr) { diff --git a/bin/dd/conv.c b/bin/dd/conv.c index 84552a2c2083..d0cfe150864b 100644 --- a/bin/dd/conv.c +++ b/bin/dd/conv.c @@ -40,7 +40,7 @@ static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94"; #endif static const char rcsid[] = - "$Id: conv.c,v 1.9 1998/05/06 06:51:30 charnier Exp $"; + "$Id: conv.c,v 1.10 1998/05/13 07:33:39 charnier Exp $"; #endif /* not lint */ #include @@ -60,7 +60,7 @@ static const char rcsid[] = void def() { - int cnt; + size_t cnt; u_char *inp, *t; if ((t = ctab) != NULL) @@ -104,7 +104,8 @@ void block() { static int intrunc; - int ch, cnt, maxlen; + int ch; + size_t cnt, maxlen; u_char *inp, *outp, *t; /* @@ -114,8 +115,8 @@ block() * left empty. */ if (intrunc) { - for (inp = in.db, cnt = in.dbrcnt; - cnt && *inp++ != '\n'; --cnt); + for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt) + ; if (!cnt) { in.dbcnt = 0; in.dbp = in.db; @@ -135,12 +136,12 @@ block() for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) { maxlen = MIN(cbsz, in.dbcnt); if ((t = ctab) != NULL) - for (cnt = 0; - cnt < maxlen && (ch = *inp++) != '\n'; ++cnt) + for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n'; + ++cnt) *outp++ = t[ch]; else - for (cnt = 0; - cnt < maxlen && (ch = *inp++) != '\n'; ++cnt) + for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n'; + ++cnt) *outp++ = ch; /* * Check for short record without a newline. Reassemble the @@ -198,8 +199,8 @@ block_close() if (in.dbcnt) { ++st.trunc; memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt); - (void)memset(out.dbp + in.dbcnt, - ctab ? ctab[' '] : ' ', cbsz - in.dbcnt); + (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ', + cbsz - in.dbcnt); out.dbcnt += cbsz; } } @@ -214,7 +215,7 @@ block_close() void unblock() { - int cnt; + size_t cnt; u_char *inp, *t; /* Translation and case conversion. */ @@ -227,16 +228,16 @@ unblock() * spaces. */ for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) { - for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t); + for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t) + ; if (t >= inp) { cnt = t - inp + 1; memmove(out.dbp, inp, cnt); out.dbp += cnt; out.dbcnt += cnt; } - ++out.dbcnt; *out.dbp++ = '\n'; - if (out.dbcnt >= out.dbsz) + if (++out.dbcnt >= out.dbsz) dd_out(0); } if (in.dbcnt) @@ -247,12 +248,13 @@ unblock() void unblock_close() { - int cnt; + size_t cnt; u_char *t; if (in.dbcnt) { warnx("%s: short input record", in.name); - for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t); + for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t) + ; if (t >= in.db) { cnt = t - in.db + 1; memmove(out.dbp, in.db, cnt); diff --git a/bin/dd/dd.c b/bin/dd/dd.c index 7aac8c62e34f..5ffb7ec429d5 100644 --- a/bin/dd/dd.c +++ b/bin/dd/dd.c @@ -46,7 +46,7 @@ static char const copyright[] = static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; #endif static const char rcsid[] = - "$Id: dd.c,v 1.15 1998/05/13 07:33:47 charnier Exp $"; + "$Id: dd.c,v 1.16 1999/04/25 21:13:33 imp Exp $"; #endif /* not lint */ #include @@ -74,11 +74,11 @@ static void setup __P((void)); IO in, out; /* input/output state */ STAT st; /* statistics */ void (*cfunc) __P((void)); /* conversion function */ -u_long cpy_cnt; /* # of blocks to copy */ -u_long pending = 0; /* pending seek if sparse */ +size_t cpy_cnt; /* # of blocks to copy */ +size_t pending = 0; /* pending seek if sparse */ u_int ddflags; /* conversion options */ -u_int cbsz; /* conversion block size */ -u_int files_cnt = 1; /* # of files to copy */ +size_t cbsz; /* conversion block size */ +int files_cnt = 1; /* # of files to copy */ u_char *ctab; /* conversion table */ int @@ -112,8 +112,8 @@ setup() in.name = "stdin"; in.fd = STDIN_FILENO; } else { - in.fd = open(in.name, O_RDONLY, 0); - if (in.fd < 0) + in.fd = open(in.name, O_RDONLY); + if (in.fd == -1) err(1, "%s", in.name); } @@ -135,11 +135,11 @@ setup() * Without read we may have a problem if output also does * not support seeks. */ - if (out.fd < 0) { + if (out.fd == -1) { out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); out.flags |= NOREAD; } - if (out.fd < 0) + if (out.fd == -1) err(1, "%s", out.name); } @@ -154,8 +154,8 @@ setup() err(1, NULL); out.db = in.db; } else if ((in.db = - malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || - (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) + malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL || + (out.db = malloc(out.dbsz + cbsz)) == NULL) err(1, NULL); in.dbp = in.db; out.dbp = out.db; @@ -171,7 +171,7 @@ setup() * kinds of output files, tapes, for example. */ if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) - (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz); + (void)ftruncate(out.fd, out.offset * out.dbsz); /* * If converting case at the same time as another conversion, build a @@ -238,7 +238,7 @@ getfdtype(io) static void dd_in() { - int n; + ssize_t n; for (;;) { if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) @@ -248,11 +248,12 @@ dd_in() * Zero the buffer first if sync; If doing block operations * use spaces. */ - if (ddflags & C_SYNC) + if (ddflags & C_SYNC) { if (ddflags & (C_BLOCK|C_UNBLOCK)) memset(in.dbp, ' ', in.dbsz); else memset(in.dbp, 0, in.dbsz); + } n = read(in.fd, in.dbp, in.dbsz); if (n == 0) { @@ -261,7 +262,7 @@ dd_in() } /* Read error. */ - if (n < 0) { + if (n == -1) { /* * If noerror not specified, die. POSIX requires that * the warning message be followed by an I/O display. @@ -358,7 +359,9 @@ dd_out(force) int force; { static int warned; - int cnt, n, nw, i, sparse; + int sparse; + size_t cnt, n, i; + ssize_t nw; u_char *outp; /* @@ -396,9 +399,10 @@ dd_out(force) if (pending != 0) { if (force) pending--; - if (lseek (out.fd, pending, SEEK_CUR) == -1) - err(2, "%s: seek error creating sparse file", - out.name); + if (lseek(out.fd, (off_t)pending, + SEEK_CUR) == -1) + err(2, "%s: seek error creating" + " sparse file", out.name); if (force) write(out.fd, outp, 1); pending = 0; @@ -431,10 +435,10 @@ dd_out(force) if (out.flags & ISCHR && !warned) { warned = 1; warnx("%s: short write on character device", - out.name); + out.name); } if (out.flags & ISTAPE) - errx(1, "%s: short write on tape device", out.name); + errx(1, "%s: short write on tape device", out.name); } if ((out.dbcnt -= n) < out.dbsz) break; diff --git a/bin/dd/dd.h b/bin/dd/dd.h index 58a156868c52..6269621f2800 100644 --- a/bin/dd/dd.h +++ b/bin/dd/dd.h @@ -35,16 +35,18 @@ * SUCH DAMAGE. * * @(#)dd.h 8.3 (Berkeley) 4/2/94 - * $Id: dd.h,v 1.7 1997/10/11 20:09:05 joerg Exp $ + * $Id: dd.h,v 1.8 1998/02/11 02:23:31 asami Exp $ */ +#define uint64 u_int64_t + /* Input/output stream state. */ typedef struct { u_char *db; /* buffer address */ u_char *dbp; /* current buffer I/O address */ - u_long dbcnt; /* current buffer byte count */ - int dbrcnt; /* last read byte count */ - u_long dbsz; /* buffer size */ + size_t dbcnt, /* current buffer byte count */ + dbrcnt, /* last read byte count */ + dbsz; /* buffer size */ #define ISCHR 0x01 /* character device (warn on short) */ #define ISPIPE 0x02 /* pipe (not truncatable) */ @@ -54,22 +56,22 @@ typedef struct { char *name; /* name */ int fd; /* file descriptor */ - u_long offset; /* # of blocks to skip */ + off_t offset; /* # of blocks to skip */ - u_long f_stats; /* # of full blocks processed */ - u_long p_stats; /* # of partial blocks processed */ - u_long s_stats; /* # of odd swab blocks */ - u_long t_stats; /* # of truncations */ + uint64 f_stats, /* # of full blocks processed */ + p_stats, /* # of partial blocks processed */ + s_stats, /* # of odd swab blocks */ + t_stats; /* # of truncations */ } IO; typedef struct { - u_long in_full; /* # of full input blocks */ - u_long in_part; /* # of partial input blocks */ - u_long out_full; /* # of full output blocks */ - u_long out_part; /* # of partial output blocks */ - u_long trunc; /* # of truncated records */ - u_long swab; /* # of odd-length swab blocks */ - u_int64_t bytes; /* # of bytes written */ + uint64 in_full, /* # of full input blocks */ + in_part, /* # of partial input blocks */ + out_full, /* # of full output blocks */ + out_part, /* # of partial output blocks */ + trunc, /* # of truncated records */ + swab, /* # of odd-length swab blocks */ + bytes; /* # of bytes written */ double start; /* start time of dd */ } STAT; diff --git a/bin/dd/extern.h b/bin/dd/extern.h index 7445fee4b083..951742574c95 100644 --- a/bin/dd/extern.h +++ b/bin/dd/extern.h @@ -35,7 +35,7 @@ * SUCH DAMAGE. * * @(#)extern.h 8.3 (Berkeley) 4/2/94 - * $Id$ + * $Id: extern.h,v 1.4 1997/02/22 14:02:46 peter Exp $ */ #include @@ -57,10 +57,10 @@ void unblock_close __P((void)); extern IO in, out; extern STAT st; extern void (*cfunc)(); -extern u_long cpy_cnt; -extern u_int cbsz; +extern size_t cpy_cnt; +extern size_t cbsz; extern u_int ddflags; -extern u_int files_cnt; +extern int files_cnt; extern u_char *ctab; extern u_char a2e_32V[], a2e_POSIX[], a2ibm_32V[], a2ibm_POSIX[], e2a_32V[]; extern u_char e2a_POSIX[], l2u[], u2l[]; diff --git a/bin/dd/misc.c b/bin/dd/misc.c index 00ef7aed06c9..ac994d8e264c 100644 --- a/bin/dd/misc.c +++ b/bin/dd/misc.c @@ -40,7 +40,7 @@ static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94"; #endif static const char rcsid[] = - "$Id: misc.c,v 1.14 1998/05/13 07:33:50 charnier Exp $"; + "$Id: misc.c,v 1.15 1998/12/07 12:37:11 bde Exp $"; #endif /* not lint */ #include @@ -66,16 +66,16 @@ summary() secs = 1e-6; /* Use snprintf(3) so that we don't reenter stdio(3). */ (void)snprintf(buf, sizeof(buf), - "%lu+%lu records in\n%lu+%lu records out\n", + "%qu+%qu records in\n%qu+%qu records out\n", st.in_full, st.in_part, st.out_full, st.out_part); (void)write(STDERR_FILENO, buf, strlen(buf)); if (st.swab) { - (void)snprintf(buf, sizeof(buf), "%lu odd length swab %s\n", + (void)snprintf(buf, sizeof(buf), "%qu odd length swab %s\n", st.swab, (st.swab == 1) ? "block" : "blocks"); (void)write(STDERR_FILENO, buf, strlen(buf)); } if (st.trunc) { - (void)snprintf(buf, sizeof(buf), "%lu truncated %s\n", + (void)snprintf(buf, sizeof(buf), "%qu truncated %s\n", st.trunc, (st.trunc == 1) ? "block" : "blocks"); (void)write(STDERR_FILENO, buf, strlen(buf)); } diff --git a/bin/dd/position.c b/bin/dd/position.c index 03ffed24f896..00fd504fd467 100644 --- a/bin/dd/position.c +++ b/bin/dd/position.c @@ -40,7 +40,7 @@ static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94"; #endif static const char rcsid[] = - "$Id: position.c,v 1.7 1998/05/06 06:51:42 charnier Exp $"; + "$Id: position.c,v 1.8 1998/05/13 07:33:54 charnier Exp $"; #endif /* not lint */ #include @@ -61,11 +61,14 @@ static const char rcsid[] = void pos_in() { - int bcnt, cnt, nr, warned; + size_t bcnt; + ssize_t nr; + off_t cnt; + int warned; /* If not a character, pipe or tape device, try to seek on it. */ if (!(in.flags & (ISCHR|ISPIPE|ISTAPE))) { - if (lseek(in.fd, (off_t)in.offset * in.dbsz, SEEK_CUR) == -1) + if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1) err(1, "%s", in.name); return; } @@ -116,7 +119,8 @@ void pos_out() { struct mtop t_op; - int cnt, n; + off_t cnt; + ssize_t n; /* * If not a tape, try seeking on the file. Seeking on a pipe is @@ -124,8 +128,7 @@ pos_out() * have specified the seek operand. */ if (!(out.flags & ISTAPE)) { - if (lseek(out.fd, - (off_t)out.offset * out.dbsz, SEEK_SET) == -1) + if (lseek(out.fd, out.offset * out.dbsz, SEEK_SET) == -1) err(1, "%s", out.name); return; } @@ -135,7 +138,7 @@ pos_out() t_op.mt_op = MTFSR; t_op.mt_count = out.offset; - if (ioctl(out.fd, MTIOCTOP, &t_op) < 0) + if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) err(1, "%s", out.name); return; } @@ -145,7 +148,7 @@ pos_out() if ((n = read(out.fd, out.db, out.dbsz)) > 0) continue; - if (n < 0) + if (n == -1) err(1, "%s", out.name); /*