#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: dd.c,v 1.12 1998/08/19 01:32:44 thorpej Exp $");
#endif
#endif
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "dd.h"
#include "extern.h"
static void dd_close __P((void));
static void dd_in __P((void));
static void getfdtype __P((IO *));
static void setup __P((void));
int main __P((int, char *[]));
IO in, out;
STAT st;
void (*cfunc) __P((void));
u_long cpy_cnt;
u_int ddflags;
u_int cbsz;
u_int files_cnt = 1;
const u_char *ctab;
int
main(argc, argv)
int argc;
char *argv[];
{
jcl(argv);
setup();
(void)signal(SIGINFO, summaryx);
(void)signal(SIGINT, terminate);
(void)atexit(summary);
while (files_cnt--)
dd_in();
dd_close();
exit(0);
}
static void
setup()
{
u_int cnt;
if (in.name == NULL) {
in.name = "stdin";
in.fd = STDIN_FILENO;
} else {
in.fd = open(in.name, O_RDONLY, 0);
if (in.fd < 0)
err(1, "%s", in.name);
}
getfdtype(&in);
if (files_cnt > 1 && !(in.flags & ISTAPE))
errx(1, "files is not supported for non-tape devices");
if (out.name == NULL) {
out.fd = STDOUT_FILENO;
out.name = "stdout";
} else {
#define OFLAGS \
(O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
if (out.fd < 0) {
out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
out.flags |= NOREAD;
}
if (out.fd < 0)
err(1, "%s", out.name);
}
getfdtype(&out);
if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
err(1, "%s", "");
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)
err(1, "%s", "");
in.dbp = in.db;
out.dbp = out.db;
if (in.offset)
pos_in();
if (out.offset)
pos_out();
if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
(void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
if (ddflags & (C_LCASE|C_UCASE)) {
#ifdef NO_CONV
errx(1, "case conv and -DNO_CONV");
#else
if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
if (ddflags & C_LCASE) {
for (cnt = 0; cnt < 0377; ++cnt)
casetab[cnt] = tolower(ctab[cnt]);
} else {
for (cnt = 0; cnt < 0377; ++cnt)
casetab[cnt] = toupper(ctab[cnt]);
}
} else {
if (ddflags & C_LCASE) {
for (cnt = 0; cnt < 0377; ++cnt)
casetab[cnt] = tolower(cnt);
} else {
for (cnt = 0; cnt < 0377; ++cnt)
casetab[cnt] = toupper(cnt);
}
}
ctab = casetab;
#endif
}
(void)time(&st.start);
}
static void
getfdtype(io)
IO *io;
{
struct mtget mt;
struct stat sb;
if (fstat(io->fd, &sb))
err(1, "%s", io->name);
if (S_ISCHR(sb.st_mode))
io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
io->flags |= ISPIPE;
}
static void
dd_in()
{
int flags, n;
for (flags = ddflags;;) {
if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
return;
if (flags & C_SYNC) {
if (flags & (C_BLOCK|C_UNBLOCK))
(void)memset(in.dbp, ' ', in.dbsz);
else
(void)memset(in.dbp, 0, in.dbsz);
}
n = read(in.fd, in.dbp, in.dbsz);
if (n == 0) {
in.dbrcnt = 0;
return;
}
if (n < 0) {
if (!(flags & C_NOERROR))
err(1, "%s", in.name);
warn("%s", in.name);
summary();
if (!(in.flags & (ISPIPE|ISTAPE)) &&
lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
warn("%s", in.name);
if (!(ddflags & C_SYNC))
continue;
in.dbcnt += in.dbrcnt = in.dbsz;
++st.in_full;
} else if (n == in.dbsz) {
in.dbcnt += in.dbrcnt = n;
++st.in_full;
} else {
if (ddflags & C_SYNC)
in.dbcnt += in.dbrcnt = in.dbsz;
else
in.dbcnt += in.dbrcnt = n;
++st.in_part;
}
if (ddflags & C_BS) {
out.dbcnt = in.dbcnt;
dd_out(1);
in.dbcnt = 0;
continue;
}
if (ddflags & C_SWAB) {
if ((n = in.dbcnt) & 1) {
++st.swab;
--n;
}
swab(in.dbp, in.dbp, n);
}
in.dbp += in.dbrcnt;
(*cfunc)();
}
}
static void
dd_close()
{
if (cfunc == def)
def_close();
else if (cfunc == block)
block_close();
else if (cfunc == unblock)
unblock_close();
if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
(void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
out.dbcnt = out.dbsz;
}
if (out.dbcnt)
dd_out(1);
}
void
dd_out(force)
int force;
{
static int warned;
int cnt, n, nw;
u_char *outp;
outp = out.db;
for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
for (cnt = n;; cnt -= nw) {
nw = write(out.fd, outp, cnt);
if (nw <= 0) {
if (nw == 0)
errx(1, "%s: end of device", out.name);
if (errno != EINTR)
err(1, "%s", out.name);
nw = 0;
}
outp += nw;
st.bytes += nw;
if (nw == n) {
if (n != out.dbsz)
++st.out_part;
else
++st.out_full;
break;
}
++st.out_part;
if (nw == cnt)
break;
if (out.flags & ISCHR && !warned) {
warned = 1;
warnx("%s: short write on character device",
out.name);
}
if (out.flags & ISTAPE)
errx(1, "%s: short write on tape device", out.name);
}
if ((out.dbcnt -= n) < out.dbsz)
break;
}
if (out.dbcnt)
(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
out.dbp = out.db + out.dbcnt;
}