Author | Jamozed <[email protected]> |
Date | 2020-06-26 05:27:18 |
Commit | 8883cf3f18beb1969fff1085e6d64776424580f6 |
Parent | cf2d47a9c5ad467fc91a718517cb8b571eda75c1 |
tee: Add POSIX tee
Diffstat
M | CMakeLists.txt | | | 1 | + |
M | README.md | | | 1 | + |
A | man/tee.1 | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
A | src/tee.c | | | 109 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 154 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cf8706..5a5bf1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,5 +15,6 @@ ADD_EXECUTABLE(cat ${CMAKE_SOURCE_DIR}/src/cat.c) ADD_EXECUTABLE(dirname ${CMAKE_SOURCE_DIR}/src/dirname.c) ADD_EXECUTABLE(echo ${CMAKE_SOURCE_DIR}/src/echo.c) ADD_EXECUTABLE(false ${CMAKE_SOURCE_DIR}/src/false.c) +ADD_EXECUTABLE(tee ${CMAKE_SOURCE_DIR}/src/tee.c) ADD_EXECUTABLE(true ${CMAKE_SOURCE_DIR}/src/true.c) ADD_EXECUTABLE(yes ${CMAKE_SOURCE_DIR}/src/yes.c) diff --git a/README.md b/README.md index 1378735..14790ac 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ commonly found on UNIX-like systems. | dirname | Return the directory portion of a path | POSIX | | echo | Write arguments to standard output | POSIX | | false | Return false value | POSIX | +| tee | Duplicate standard input | POSIX | | true | Return true value | POSIX | | yes | Output a string repeatedly | | diff --git a/man/tee.1 b/man/tee.1 new file mode 100644 index 0000000..a27bd34 --- /dev/null +++ b/man/tee.1 @@ -0,0 +1,43 @@ +.TH TEE 1 2020-06-26 "OMKOV coreutils" "General Commands Manual" +.SH NAME +tee \(em duplicate standard input +.SH SYNOPSYS +\fBtee\fR [-ai] [\fIfile\fR...] +.SH DESCRIPTION +Copy standard input to standard output and zero or more files. +.SH OPTIONS +The following options are supported: +.TP +.B -a +Append to files. +.TP +.B -i +Ignore interrupt signals. +.TP +.B --help +Display help information. +.TP +.B --version +Display version information. +.SH OPERANDS +The following operand is supported: +.TP +.I file +A pathname of an output file. +.SH EXIT STATUS +The following exit values will be returned: +.TP +\ 0 +The standard input was successfully copied to all output files. +.TP +>0 +An error occurred. +.SH STANDARDS +The \fItee\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2") +specification. +.SH COPYRIGHT +.nf +Copyright (C) 2020, Jakob Wakeling +All rights reserved. +OMKOV Permissive Licence (https://www.omkov.net/OLPE) +.fi diff --git a/src/tee.c b/src/tee.c new file mode 100644 index 0000000..66e100a --- /dev/null +++ b/src/tee.c @@ -0,0 +1,109 @@ +// tee.c, version 1.0.1 +// OMKOV coreutils implementation of POSIX tee +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Copyright (C) 2020, Jakob Wakeling +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "error.h" +#include "optget.h" + +#include <errno.h> +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +static char stdbuf[BUFSIZ * 16]; + +static bool aflag; + +static void help(void); +static void version(void); + +int main(int argc, char *argv[]) { + lop_t lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } + }; + + opt_t opt = OPTGET_INIT; opt.str = "ai"; opt.lops = lops; int o; + while ((o = optget(&opt, argv, 1)) != -1) switch (o) { + case 'a': { aflag = true; break; } + case 'i': { signal(SIGINT, SIG_IGN); break; } + case 256: { help(); return 0; } + case 257: { version(); return 0; } + default: { return 1; } + } + + setvbuf(stdout, NULL, _IONBF, 0); + bool warned = false; + + FILE **files = (FILE **)malloc(sizeof (FILE) * (argc - opt.ind)); + if (!files) { error(1, "%s: %s", argv[0], serrno); } + + FILE **f = files; + for (char **p = &argv[opt.ind]; *p; ++p, ++f) { + *f = fopen(*p, aflag ? "w" : "a"); + if (!*f) { warn("%s: %s: %s", argv[0], *p, serrno); --f; } + } *f = NULL; + + for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), stdin)) != 0;) { + for (f = files; *f; ++f) { fwrite(stdbuf, 1, c, *f); } + fwrite(stdbuf, 1, c, stdout); + } + + for (f = files; *f; ++f) { + if (fclose(*f)) { warn("%s: %s", argv[0], serrno); } + } + + free(files); return warned; +} + +static void help(void) { + puts("tee - duplicate standard input"); + puts("usage: tee [-ai] [file...]\n"); + puts("options:"); + puts(" -a Append to files"); + puts(" -i Ignore interrupt signals"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +static void version(void) { + puts("OMKOV coreutils tee, version 1.0.1"); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}