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.
This commit is contained in:
Brian Feldman 2000-07-01 05:36:25 +00:00
parent 5bbd55b415
commit c15c898eff
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=62311
5 changed files with 29 additions and 17 deletions

View File

@ -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");
}

View File

@ -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",

View File

@ -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 */

View File

@ -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;

View File

@ -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;
}
}