diff options
author | Jakob Kaivo <jkk@ung.org> | 2023-03-04 22:58:17 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2023-03-04 22:58:17 -0500 |
commit | acf9238af56b7876048ba1178aa1b0f3949e3ffc (patch) | |
tree | 393d7cb77dbb48c2c6ca6ad8d69b8d346b116b74 /plaintext.c | |
parent | e394cafc4a2c7b57962784f3780feb8c29126668 (diff) |
Diffstat (limited to 'plaintext.c')
-rw-r--r-- | plaintext.c | 59 |
1 files changed, 59 insertions, 0 deletions
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 <ctype.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +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; +} |