aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqorg11 <qorg@vxempire.xyz>2020-09-09 19:39:56 +0200
committerqorg11 <qorg@vxempire.xyz>2020-09-09 19:39:56 +0200
commitf4871118a45971814f8ae7a7b65156bb56d95a5b (patch)
treec5fdd9567f1407673ab101039fa301ad4e7c0ead
parent8eed6cca17f886ede7920eef1b98af0c784f961f (diff)
downloadk9core-f4871118a45971814f8ae7a7b65156bb56d95a5b.tar.gz
k9core-f4871118a45971814f8ae7a7b65156bb56d95a5b.zip
Added tee!
It supports all the POSIX flags! (-i and -a)
-rw-r--r--src/tee.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/tee.c b/src/tee.c
new file mode 100644
index 0000000..7fc888a
--- /dev/null
+++ 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;
+}