k9core

Unnamed repository
Log | Files | Refs | LICENSE

commit f4871118a45971814f8ae7a7b65156bb56d95a5b
parent 8eed6cca17f886ede7920eef1b98af0c784f961f
Author: qorg11 <qorg@vxempire.xyz>
Date:   Wed,  9 Sep 2020 19:39:56 +0200

Added tee!

It supports all the POSIX flags! (-i and -a)

Diffstat:
Asrc/tee.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+), 0 deletions(-)

diff --git a/src/tee.c b/src/tee.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <signal.h> +#include <errno.h> +#include <string.h> + +int +tee(int fd) +{ + if(fd == -1) + { + fprintf(stderr,"%s\n",strerror(errno)); + return 1; + } + char buf[8192]; + int read_bytes = 0; + while((read_bytes = read(0,buf,8192)) > 0) + write(fd,buf,read_bytes); + return 1; +} + +int +main(int argc, char *argv[]) +{ + int c; + int append = 0; + int ignore_signt = 0; + int fd = 0; + int FLAGS = O_WRONLY; /* POSIX is a bit dumb, god knows why + this is not the same as O_RDWR */ + while((c = getopt(argc, argv,"ai")) != -1) + { + switch(c) + { + case 'a': append = 1; break; + case 'i': ignore_signt = 1; break; + } + } + if(argc == optind) + { + if(ignore_signt) + signal(SIGINT,SIG_IGN); + tee(1); + } + else + { + if(argv[argc-1][0] == '-') + fd = 1; + if(append) + FLAGS = O_RDWR|O_APPEND; + + fd = open(argv[argc-1],FLAGS); + if(ignore_signt) + signal(SIGINT,SIG_IGN); + if(tee(fd) == 1) + return -1; + } + return 0; +}