From acf9238af56b7876048ba1178aa1b0f3949e3ffc Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 4 Mar 2023 22:58:17 -0500 Subject: quick and dirty implementation --- plaintext.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 plaintext.c (limited to 'plaintext.c') diff --git a/plaintext.c b/plaintext.c new file mode 100644 index 0000000..a107317 --- /dev/null +++ b/plaintext.c @@ -0,0 +1,59 @@ +#define _POSIX_C_SOURCE 2 +#include +#include +#include +#include + +static int plaintext(const char *path) +{ + FILE *f = stdin; + if (path && strcmp(path, "-") != 0) { + f = fopen(path, "rb"); + if (f == NULL) { + perror(path); + return 1; + } + } + + int c = 0; + while ((c = fgetc(f)) != EOF) { + if (c == '\033') { + c = fgetc(f); /* read '[' */ + c = '\033'; + while (c != EOF && ! (0x40 <= c && c <= 0x7e)) { + c = fgetc(f); + } + continue; + } + if (iscntrl(c) && !isspace(c)) { + continue; + } + putchar(c); + } + + if (f != stdin) { + fclose(f); + } + return 0; +} + +int main(int argc, char *argv[]) +{ + int c; + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + return 1; + } + } + + if (optind >= argc) { + return plaintext(NULL); + } + + int r = 0; + for (int i = optind; i < argc; i++) { + r |= plaintext(argv[i]); + } + return r; +} -- cgit v1.2.1