From 2944f70ee33de24b023ad1edb04246e4b71eef17 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 22 Sep 2020 18:16:38 -0700 Subject: *: initial commit wye is an input-oriented tee variant The name is derived from the plumbing name for a directional tee, or "Y" intersection. --- Makefile.am | 1 + README | 12 ++++++++ bootstrap | 3 ++ configure.ac | 16 ++++++++++ src/Makefile.am | 2 ++ src/wye.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 Makefile.am create mode 100644 README create mode 100755 bootstrap create mode 100644 configure.ac create mode 100644 src/Makefile.am create mode 100644 src/wye.c diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..af437a6 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/README b/README new file mode 100644 index 0000000..e57b6c3 --- /dev/null +++ b/README @@ -0,0 +1,12 @@ +`wye` is an input variant of the `tee` utility + +Usage is like `tee`: wye [FILE]... + +wye always consumes from its stdin, the optional files specified are +read-multiplexed as alternative inputs to stdin. All reads are +performed using PIPE_BUF sized buffers which are the atomic units for +unix pipes, any read data is immediately written to stdout. + +Someone should try get this command added upstream in GNU Coreutils +upstream, and give me credit for the name. This implementation is a +quick and dirty hack and not particularly robust. diff --git a/bootstrap b/bootstrap new file mode 100755 index 0000000..c5a7472 --- /dev/null +++ b/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh + +autoreconf --install diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..8cdfc3e --- /dev/null +++ b/configure.ac @@ -0,0 +1,16 @@ +AC_INIT([wye], [1.0], [vcaputo@pengaru.com]) +AM_INIT_AUTOMAKE([-Wall -Werror foreign]) +AC_PROG_CC +AM_PROG_CC_C_O +AM_PROG_AR +AC_PROG_RANLIB +AM_SILENT_RULES([yes]) + +CFLAGS="$CFLAGS -Wall" + +AC_CONFIG_FILES([ + Makefile + src/Makefile +]) + +AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..8490c6e --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,2 @@ +bin_PROGRAMS = wye +wye_SOURCES = wye.c diff --git a/src/wye.c b/src/wye.c new file mode 100644 index 0000000..17cbc3b --- /dev/null +++ b/src/wye.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2020 - Vito Caputo - + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published + * by the Free Software Foundation. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int n = argc; + struct pollfd *pfds; + + assert(argc >= 1); + + pfds = calloc(n, sizeof(*pfds)); + if (!pfds) { + fprintf(stderr, "Unable to allocate struct pollfd array: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } + + for (int i = 0; i < n; i++) { + if (i > 0) { + const char *path = argv[i]; + + pfds[i].fd = open(path, O_RDONLY); + if (pfds[i].fd == -1) { + fprintf(stderr, "Unable to open \"%s\": %s\n", + path, strerror(errno)); + return EXIT_FAILURE; + } + } + pfds[i].events = POLLIN; + } + + while (poll(pfds, n, -1) > 0) { + for (int i = 0; i < n; i++) { + char buf[PIPE_BUF]; + ssize_t rr, rw; + + if (pfds[i].revents & POLLHUP) { + fprintf(stderr, "fd %i hangup, quitting\n", + pfds[i].fd); + return EXIT_SUCCESS; + } + + if (!(pfds[i].revents & POLLIN)) + continue; + + rr = read(pfds[i].fd, buf, sizeof(buf)); + if (rr < 0) { + fprintf(stderr, "Error reading fd %i: %s\n", + pfds[i].fd, strerror(errno)); + return EXIT_FAILURE; + } + + rw = write(1, buf, rr); + if (rw < 0) { + fprintf(stderr, "Error writing: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } + + if (rw != rr) { + fprintf(stderr, "Short write\n"); + return EXIT_FAILURE; + } + } + } + + return EXIT_FAILURE; +} -- cgit v1.2.3