Author | Jamozed <[email protected]> |
Date | 2021-01-03 01:02:38 |
Commit | ca214639145b349fd7ba5bd02eb0bcd8b02746f1 |
Parent | 115414703d62d040a71a3145289d118557367a47 |
wc: Add POSIX wc
Diffstat
M | CMakeLists.txt | | | 1 | + |
A | src/wc.c | | | 146 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 147 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c8c3b54..b103212 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,4 +44,5 @@ ADD_EXECUTABLE(true ${PROJECT_SOURCE_DIR}/src/true.c) ADD_EXECUTABLE(tty ${PROJECT_SOURCE_DIR}/src/tty.c) ADD_EXECUTABLE(uname ${PROJECT_SOURCE_DIR}/src/uname.c) ADD_EXECUTABLE(unlink ${PROJECT_SOURCE_DIR}/src/unlink.c) +ADD_EXECUTABLE(wc ${PROJECT_SOURCE_DIR}/src/wc.c) ADD_EXECUTABLE(yes ${PROJECT_SOURCE_DIR}/src/yes.c) diff --git a/src/wc.c b/src/wc.c new file mode 100644 index 0000000..aff65f9 --- /dev/null +++ b/src/wc.c @@ -0,0 +1,146 @@ +// wc.c, version 0.1.0 +// OMKOV coreutils implementation of POSIX wc +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +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. +*/ + +/* + TODO Handle multi-byte characters according to locale. + TODO Improve word counting algorithm. +*/ + +#include "lib/error.h" +#include "lib/optget.h" + +#include <ctype.h> +#include <locale.h> +#include <stdbool.h> +#include <stdio.h> + +#define VERSION "0.1.0" + +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } +}; + +static bool cflag, lflag, mflag, wflag; +static size_t ctotal, ltotal, mtotal, wtotal; + +static inline int wc(const char *file); +static inline void report(size_t c, size_t l, size_t m, size_t w, const char *f); + +static void hlp(void); +static void ver(void); + +int main(int ac, char *av[]) { A0 = av[0]; + struct opt opt = OPTGET_INIT; opt.str = "clmw"; opt.lops = lops; + for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { + case 'c': { cflag = true; break; } + case 'l': { lflag = true; break; } + case 'm': { mflag = true; break; } + case 'w': { wflag = true; break; } + case 256: { hlp(); return 0; } + case 257: { ver(); return 0; } + default: { return 1; } + } + + char *lc = setlocale(LC_ALL, ""); + printf("%s\n", lc); + + // If no options specified, use default format + if (!cflag && !lflag && !mflag && !wflag) { cflag = lflag = wflag = true; } + bool warned = false; + + if (opt.ind == ac) { wc(NULL); } + else for (char **p = &av[opt.ind]; *p; ++p) if (wc(*p)) { + warn("%s: %s", *p, serr()); warned = true; + } + + if ((ac - opt.ind) > 1) { report(ctotal, ltotal, mtotal, wtotal, "total"); } + + return warned; +} + +/* + Count the number of bytes, characters, words and lines in a file. + If the file path given is NULL or "-", then use stdin. +*/ +static inline int wc(const char *file) { + FILE *fi; size_t ccount = 0, lcount = 0, mcount = 0, wcount = 0; + bool wordflag = false; + + if (!file || (file[0] == '-' && file[1] == 0)) { fi = stdin; } + else if (!(fi = fopen(file, "r"))) { return 1; } + + for (int c; (c = fgetc(fi)) != EOF;) { + ++ccount; ++mcount; + if (c == '\n') { ++lcount; } + if (isspace(c)) { if (!wordflag) { ++wcount; } wordflag = true; } + else { wordflag = false; } + } + + report(ccount, lcount, mcount, wcount, file); + ctotal += ccount; ltotal += lcount; mtotal += mcount; wtotal += wcount; + + if (fi != stdin) { fclose(fi); } return 0; +} + +/* Report the appropriate metrics */ +static inline void report(size_t c, size_t l, size_t m, size_t w, const char *f) { + if (lflag) { printf("%zu ", l); } + if (wflag) { printf("%zu ", w); } + if (mflag) { printf("%zu ", m); } + if (cflag) { printf("%zu ", c); } + if (f) { fputs(f, stdout); } fputc('\n', stdout); return; +} + +/* Print help information */ +static void hlp(void) { + puts("wc - word, line, and byte or character count\n"); + puts("usage: wc [-clmw] [file...]\n"); + puts("options:"); + puts(" -c Print the number of bytes in the file"); + puts(" -l Print the number of newlines in the file"); + puts(" -m Print the number of characters in the file"); + puts(" -w Print the number of words in the file"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +/* Print version information */ +static void ver(void) { + puts("OMKOV coreutils wc, version " VERSION); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}