/* Copyright (C) 2018 Causal Agent June * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include extern int winch(void); static struct { int winch; int local; int remote; } fd = { -1, -1, STDIN_FILENO }; int main(int argc, char *argv[]) { if (argc < 2) errx(EX_USAGE, "missing private id"); if (!isatty(STDERR_FILENO)) errx(EX_USAGE, "no terminal (use ssh -t)"); const char *path = argv[1]; fd.local = open(path, O_WRONLY); if (fd.local < 0) err(EX_NOINPUT, "%s", path); struct winsize window; int error = ioctl(STDERR_FILENO, TIOCGWINSZ, &window); if (error) err(EX_IOERR, "TIOCGWINSZ"); fd.winch = winch(); for (;;) { int error = ftruncate(fd.local, 0); if (error) err(EX_IOERR, "%s", path); 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); } char buf[4096]; struct pollfd fds[2] = { { .fd = fd.remote, .events = POLLIN }, { .fd = fd.winch, .events = POLLIN }, }; for (ssize_t totalSize = 0; totalSize < 1024 * 1024;) { int nfds = poll(fds, 2, -1); if (nfds < 0) { if (errno == EINTR) continue; err(EX_IOERR, "poll"); } if (fds[0].revents & POLLIN) { 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); totalSize += readSize; } if (fds[0].revents & POLLHUP) return EX_OK; if (fds[1].revents & POLLIN) { ssize_t readSize = read(fd.winch, &window, sizeof(window)); if (readSize < 0) err(EX_IOERR, "read(%d)", fd.winch); if ((size_t)readSize < sizeof(window)) { errx(EX_IOERR, "short read(%d)", fd.winch); } break; } } } }