Author | Jamozed <[email protected]> |
Date | 2020-06-26 02:17:26 |
Commit | 34337b484531502b36c515dc1b305faf537906e1 |
Parent | ca86cbe4d4875260ed5b9f0550312f8245628cc0 |
cat: Add POSIX cat
Diffstat
M | CMakeLists.txt | | | 2 | ++ |
M | README.md | | | 1 | + |
A | man/cat.1 | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
A | src/cat.c | | | 106 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 150 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1000e7e..e47c176 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,3 +9,5 @@ SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/ext) + +ADD_EXECUTABLE(cat ${CMAKE_SOURCE_DIR}/src/cat.c) diff --git a/README.md b/README.md index c6b9f75..c35798f 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ commonly found on UNIX-like systems. | Utility | Description | Standard | | ---------------- | ---------------------------------------- | -------- | +| cat | Concatenate and print files | POSIX | ## Build Instructions diff --git a/man/cat.1 b/man/cat.1 new file mode 100644 index 0000000..1752622 --- /dev/null +++ b/man/cat.1 @@ -0,0 +1,41 @@ +.TH CAT 1 2020-06-26 "OMKOV coreutils" "General Commands Manual" +.SH NAME +cat \(em concatenate and print files +.SH SYNOPSYS +\fBcat\fR [-u] [\fIfile\fR...] +.SH DESCRIPTION +Concatenate files to standard output. +.SH OPTIONS +The following options are supported: +.TP +.B -u +Disable output buffering (ignored, output is always unbuffered). +.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 input file. If no \fIfile\fR operands are specified, or +\fIfile\fR is a '\fB-\fR', \fIcat\fR will read from standard input. +.SH EXIT STATUS +The following exit values will be returned: +.TP +\ 0 +All files were processed successfully. +.TP +>0 +An error occurred. +.SH STANDARDS +The \fIcat\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/cat.c b/src/cat.c new file mode 100644 index 0000000..090eb60 --- /dev/null +++ b/src/cat.c @@ -0,0 +1,106 @@ +// cat.c, version 1.0.2 +// OMKOV coreutils implementation of POSIX cat +// 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 <stdbool.h> +#include <stdio.h> +#include <string.h> + +static char stdbuf[BUFSIZ * 16]; + +static inline int cat(const char *file); + +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 = "u"; opt.lops = lops; int o; + while ((o = optget(&opt, argv, 1)) != -1) switch (o) { + case 'u': { break; } + case 256: { help(); return 0; } + case 257: { version(); return 0; } + default: { return 1; } + } + + setvbuf(stdout, NULL, _IONBF, 0); + bool warned = false; + + if (opt.ind == argc) { cat("-"); } + else for (char **p = &argv[opt.ind]; *p; ++p) if (cat(*p)) { + warn("%s: %s: %s", argv[0], *p, serrno); warned = true; + } + + return warned; +} + +static inline int cat(const char *file) { + FILE *fi; + + if (file[0] == '-' && file[1] == 0) { fi = stdin; } + else if (!(fi = fopen(file, "r"))) { return 1; } + + for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), fi)) != 0;) { + fwrite(stdbuf, 1, c, stdout); + } + + if (fi != stdin) { fclose(fi); } return 0; +} + +static void help(void) { + puts("cat - concatenate and print files\n"); + puts("usage: cat [-u] [file...]\n"); + puts("options:"); + puts(" -u Disable output buffering (ignored)"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +static void version(void) { + puts("OMKOV coreutils cat, version 1.0.2"); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}