#include <sys/types.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 <unistd.h>
#include "dd.h"
#include "extern.h"
static void dd_close(void);
static void dd_in(void);
static void getfdtype(IO *);
static void setup(void);
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
IO in, out;
STAT st;
void (*cfunc)(void);
size_t cpy_cnt;
u_int ddflags;
size_t cbsz;
size_t files_cnt = 1;
const u_char *ctab;
int
main(int argc, char *argv[])
{
jcl(argv);
setup();
(void)signal(SIGINFO, sig_summary);
(void)signal(SIGINT, sig_terminate);
atexit(exit_summary);
if (cpy_cnt != (size_t)-1) {
while (files_cnt--)
dd_in();
}
dd_close();
exit(0);
}
static void
setup(void)
{
if (in.name == NULL) {
in.name = "stdin";
in.fd = STDIN_FILENO;
} else {
in.fd = open(in.name, O_RDONLY);
if (in.fd == -1)
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 == -1) {
out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
out.flags |= NOREAD;
}
if (out.fd == -1)
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, "input buffer");
out.db = in.db;
} else {
in.db = malloc(MAXIMUM(in.dbsz, cbsz) + cbsz);
if (in.db == NULL)
err(1, "input buffer");
out.db = malloc(out.dbsz + cbsz);
if (out.db == NULL)
err(1, "output buffer");
}
in.dbp = in.db;
out.dbp = out.db;
if (in.offset)
pos_in();
if (out.offset)
pos_out();
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
(void)ftruncate(out.fd, out.offset * out.dbsz);
if (ddflags & (C_LCASE|C_UCASE)) {
#ifdef NO_CONV
errx(1, "case conv and -DNO_CONV");
#else
u_int cnt;
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
}
clock_gettime(CLOCK_MONOTONIC, &st.start);
}
static void
getfdtype(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;
if (S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode))
io->flags |= ISPIPE;
}
static void
swapbytes(void *v, size_t len)
{
unsigned char *p = v;
unsigned char t;
while (len > 1) {
t = p[0];
p[0] = p[1];
p[1] = t;
p += 2;
len -= 2;
}
}
static void
dd_in(void)
{
ssize_t n;
for (;;) {
if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
return;
if (ddflags & C_SYNC) {
if (ddflags & (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 == -1) {
if (!(ddflags & C_NOERROR))
err(1, "%s", in.name);
warn("%s", in.name);
sig_summary(0);
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.dbrcnt) & 1) {
++st.swab;
--n;
}
swapbytes(in.dbp, n);
}
in.dbp += in.dbrcnt;
(*cfunc)();
}
}
static void
dd_close(void)
{
if (cfunc == def)
def_close();
else if (cfunc == block)
block_close();
else if (cfunc == unblock)
unblock_close();
if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
if (ddflags & (C_BLOCK|C_UNBLOCK))
memset(out.dbp, ' ', out.dbsz - out.dbcnt);
else
memset(out.dbp, 0, out.dbsz - out.dbcnt);
out.dbcnt = out.dbsz;
}
if (out.dbcnt)
dd_out(1);
if (ddflags & C_FSYNC) {
if (fsync(out.fd) == -1)
err(1, "fsync %s", out.name);
}
}
void
dd_out(int force)
{
static int warned;
size_t cnt, n;
ssize_t 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)
errx(1, "%s: end of device", out.name);
if (nw == -1) {
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;
}