diff options
author | green <green@FreeBSD.org> | 2000-07-01 05:36:25 +0000 |
---|---|---|
committer | green <green@FreeBSD.org> | 2000-07-01 05:36:25 +0000 |
commit | 5f788961ba48b35c324803f25cdd9c5d46dcf9e7 (patch) | |
tree | e2bd810d05c1625f1047f5ab6bcc21828825df0b /bin/dd | |
parent | d65ba30aafb85a96c9826a26c7794d0a5dd9ea22 (diff) | |
download | FreeBSD-src-5f788961ba48b35c324803f25cdd9c5d46dcf9e7.zip FreeBSD-src-5f788961ba48b35c324803f25cdd9c5d46dcf9e7.tar.gz |
Various cleanups are made to reduce warnings and make code prettier :)
Also, check for ftruncate() return value and die on failure, but only
try to ftruncate() when the file is a regular file.
Diffstat (limited to 'bin/dd')
-rw-r--r-- | bin/dd/args.c | 5 | ||||
-rw-r--r-- | bin/dd/dd.c | 23 | ||||
-rw-r--r-- | bin/dd/dd.h | 5 | ||||
-rw-r--r-- | bin/dd/extern.h | 3 | ||||
-rw-r--r-- | bin/dd/position.c | 10 |
5 files changed, 29 insertions, 17 deletions
diff --git a/bin/dd/args.c b/bin/dd/args.c index 9843ddb..e118712 100644 --- a/bin/dd/args.c +++ b/bin/dd/args.c @@ -132,11 +132,12 @@ jcl(argv) * just wanted to set both the input and output block sizes * and didn't want the bs semantics, so we don't warn. */ - if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK)) + if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE | + C_UNBLOCK)) ddflags &= ~C_BS; /* Bs supersedes ibs and obs. */ - if (ddflags & C_BS && ddflags & (C_IBS|C_OBS)) + if (ddflags & C_BS && ddflags & (C_IBS | C_OBS)) warnx("bs supersedes ibs and obs"); } diff --git a/bin/dd/dd.c b/bin/dd/dd.c index 937379f..2ce90df 100644 --- a/bin/dd/dd.c +++ b/bin/dd/dd.c @@ -70,6 +70,7 @@ static const char rcsid[] = static void dd_close __P((void)); static void dd_in __P((void)); +int main __P((int, char *[])); static void getfdtype __P((IO *)); static void setup __P((void)); @@ -171,8 +172,10 @@ setup() * Truncate the output file; ignore errors because it fails on some * kinds of output files, tapes, for example. */ - if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) - (void)ftruncate(out.fd, out.offset * out.dbsz); + if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) && + out.flags & ISTRUNC) + if (ftruncate(out.fd, out.offset * out.dbsz) == -1) + err(1, "truncating %s", out.name); /* * If converting case at the same time as another conversion, build a @@ -191,10 +194,10 @@ setup() } else { if (ddflags & C_LCASE) { for (cnt = 0; cnt <= 0377; ++cnt) - casetab[cnt] = tolower(cnt); + casetab[cnt] = tolower((int)cnt); } else { for (cnt = 0; cnt <= 0377; ++cnt) - casetab[cnt] = toupper(cnt); + casetab[cnt] = toupper((int)cnt); } } ctab = casetab; @@ -213,6 +216,8 @@ getfdtype(io) if (fstat(io->fd, &sb) == -1) err(1, "%s", io->name); + if (S_ISREG(sb.st_mode)) + io->flags |= ISTRUNC; if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { if (ioctl(io->fd, FIODTYPE, &type) == -1) { err(1, "%s", io->name); @@ -251,7 +256,7 @@ dd_in() case 0: break; default: - if (st.in_full + st.in_part >= cpy_cnt) + if (st.in_full + st.in_part >= (u_quad_t)cpy_cnt) return; break; } @@ -303,7 +308,7 @@ dd_in() ++st.in_full; /* Handle full input blocks. */ - } else if (n == in.dbsz) { + } else if ((size_t)n == in.dbsz) { in.dbcnt += in.dbrcnt = n; ++st.in_full; @@ -334,7 +339,7 @@ dd_in() ++st.swab; --n; } - swab(in.dbp, in.dbp, n); + swab(in.dbp, in.dbp, (size_t)n); } in.dbp += in.dbrcnt; @@ -434,7 +439,7 @@ dd_out(force) } outp += nw; st.bytes += nw; - if (nw == n) { + if ((size_t)nw == n) { if (n != out.dbsz) ++st.out_part; else @@ -442,7 +447,7 @@ dd_out(force) break; } ++st.out_part; - if (nw == cnt) + if ((size_t)nw == cnt) break; if (out.flags & ISTAPE) errx(1, "%s: short write on tape device", diff --git a/bin/dd/dd.h b/bin/dd/dd.h index ae27a0e..183176b 100644 --- a/bin/dd/dd.h +++ b/bin/dd/dd.h @@ -48,13 +48,14 @@ typedef struct { size_t dbsz; /* buffer size */ #define ISCHR 0x01 /* character device (warn on short) */ -#define ISPIPE 0x02 /* pipe-like (not truncatable) */ +#define ISPIPE 0x02 /* pipe-like (see position.c) */ #define ISTAPE 0x04 /* tape */ #define ISSEEK 0x08 /* valid to seek on */ #define NOREAD 0x10 /* not readable */ +#define ISTRUNC 0x20 /* valid to ftruncate() */ u_int flags; - char *name; /* name */ + const char *name; /* name */ int fd; /* file descriptor */ off_t offset; /* # of blocks to skip */ diff --git a/bin/dd/extern.h b/bin/dd/extern.h index e5c982c..bdfeeb7 100644 --- a/bin/dd/extern.h +++ b/bin/dd/extern.h @@ -53,10 +53,11 @@ void summaryx __P((int)); void terminate __P((int)); void unblock __P((void)); void unblock_close __P((void)); +void bitswab __P((void *, size_t)); extern IO in, out; extern STAT st; -extern void (*cfunc)(); +extern void (*cfunc) __P((void)); extern quad_t cpy_cnt; extern size_t cbsz; extern u_int ddflags; diff --git a/bin/dd/position.c b/bin/dd/position.c index 9d3d0d8..10020d0 100644 --- a/bin/dd/position.c +++ b/bin/dd/position.c @@ -130,7 +130,7 @@ pos_out() * going to fail, but don't protect the user -- they shouldn't * have specified the seek operand. */ - if (!(out.flags & ISTAPE)) { + if (out.flags & (ISSEEK | ISPIPE)) { errno = 0; if (lseek(out.fd, out.offset * out.dbsz, SEEK_CUR) == -1 && errno != 0) @@ -166,9 +166,13 @@ pos_out() if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) err(1, "%s", out.name); - while (cnt++ < out.offset) - if ((n = write(out.fd, out.db, out.dbsz)) != out.dbsz) + while (cnt++ < out.offset) { + n = write(out.fd, out.db, out.dbsz); + if (n == -1) err(1, "%s", out.name); + if ((size_t)n != out.dbsz) + errx(1, "%s: write failure", out.name); + } break; } } |