diff options
Diffstat (limited to '')
-rw-r--r-- | ingest.c | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/ingest.c b/ingest.c index dfc480c..b729169 100644 --- a/ingest.c +++ b/ingest.c @@ -20,47 +20,35 @@ #include <sysexits.h> #include <unistd.h> -static struct { - int local; - int remote; -} fd = { -1, STDIN_FILENO }; - int main(int argc, char *argv[]) { if (argc < 2) errx(EX_USAGE, "missing private id"); const char *path = argv[1]; - fd.local = open(path, O_WRONLY); - if (fd.local < 0) err(EX_NOINPUT, "%s", path); + int local = open(path, O_WRONLY); + if (local < 0) err(EX_NOINPUT, "%s", path); + + int remote = STDIN_FILENO; struct winsize window; - ssize_t size = read(fd.remote, &window, sizeof(window)); - if (size < 0) err(EX_IOERR, "read(%d)", fd.remote); - if ((size_t)size < sizeof(window)) errx(EX_IOERR, "short read(%d)", fd.remote); + ssize_t size = read(remote, &window, sizeof(window)); + if (size < 0) err(EX_IOERR, "read(%d)", remote); + if ((size_t)size < sizeof(window)) errx(EX_DATAERR, "short read(%d)", remote); for (;;) { - int error = ftruncate(fd.local, 0); - if (error) err(EX_IOERR, "%s", path); + ftruncate(local, 0); + lseek(local, 0, SEEK_SET); - off_t off = lseek(fd.local, 0, SEEK_SET); - if (off < 0) err(EX_IOERR, "%s", path); - - ssize_t size = write(fd.local, &window, sizeof(window)); - if (size < 0) err(EX_IOERR, "write(%d)", fd.local); - if ((size_t)size < sizeof(window)) { - errx(EX_IOERR, "short write(%d)", fd.local); - } + ssize_t size = write(local, &window, sizeof(window)); + if (size < 0) err(EX_IOERR, "write(%d)", local); char buf[4096]; - for (ssize_t totalSize = 0; totalSize < 1024 * 1024;) { - ssize_t readSize = read(fd.remote, buf, sizeof(buf)); - if (readSize < 0) err(EX_IOERR, "read(%d)", fd.remote); - if (!readSize) return EX_OK; - - ssize_t writeSize = write(fd.local, buf, readSize); - if (writeSize < 0) err(EX_IOERR, "write(%d)", fd.local); - if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", fd.local); + for (size_t totalSize = 0; totalSize < 1024 * 1024; totalSize += size) { + size = read(remote, buf, sizeof(buf)); + if (size < 0) err(EX_IOERR, "read(%d)", remote); + if (!size) return EX_OK; - totalSize += readSize; + size = write(local, buf, size); + if (size < 0) err(EX_IOERR, "write(%d)", local); } } } |