From: Adam Megacz Date: Thu, 14 Oct 2010 08:08:01 +0000 (-0700) Subject: initial commit X-Git-Url: http://git.megacz.com/?p=bicat.git;a=commitdiff_plain;h=f8271df0ee5a418bbaf81f5502efb8a3a5d0cbb3 initial commit --- f8271df0ee5a418bbaf81f5502efb8a3a5d0cbb3 diff --git a/README b/README new file mode 100644 index 0000000..b910ac2 --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +A crude UNIX tool that performs a bidirectional "cat" between +stdin/stdout on one side and a file (opened for both reading and +writing) on the other side. Usually the file is a device. diff --git a/bicat.c b/bicat.c new file mode 100644 index 0000000..bc0ce99 --- /dev/null +++ b/bicat.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include + +int main (int argc, char **argv) { + char buf[20]; + int fd; + int retval; + int numread; + fd = open(argv[1], O_RDWR); + for(;;) { + fd_set rfds; + struct timeval tv; + + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + FD_SET(fd, &rfds); + + tv.tv_sec = 5; + tv.tv_usec = 0; + + retval = select(fd+1, &rfds, NULL, NULL, &tv); + + if (retval == -1) + perror("select()"); + else if (retval) { + if (FD_ISSET(fd, &rfds)) { + numread = read(fd, buf, 1); + if (numread==0) return; + if (numread<0) perror("read()"); + write(STDOUT_FILENO, buf, numread); + } else if (FD_ISSET(STDIN_FILENO, &rfds)) { + numread = read(STDIN_FILENO, buf, 1); + if (numread==0) return; + if (numread<0) perror("read()"); + write(fd, buf, numread); + } else if (FD_ISSET(fd, &rfds)) { + perror("huh?\n"); + } + } else { + //printf("No data within five seconds.\n"); + } + } +}