From 549fa98bdb771079219df2af0ce56bb8f379f992 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 29 Jul 2024 10:53:58 -0600 Subject: [PATCH] npc(1): interpret all retvals --- src/npc.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/npc.c b/src/npc.c index 2e256b0..b35aba4 100644 --- a/src/npc.c +++ b/src/npc.c @@ -17,17 +17,23 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ -#include /* fprintf(3), fputs(3), getc(3), putc(3), stdin, stdout, - * EOF */ -#include /* EXIT_FAILURE */ +#include /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin, + * stdout, EOF */ +#include /* EX_IOERR, EX_OK, EX_USAGE */ #include /* getopt(3) */ -#include /* EX_OK, EX_USAGE */ char *program_name = "npc"; +static int +ioerr(char *argv0, int err) { + perror(argv0); + + return EX_IOERR; +} + static int usage(char *argv0) { - fprintf(stderr, "Usage: %s [-et]\n", argv0); + (void)fprintf(stderr, "Usage: %s [-et]\n", argv0); return EX_USAGE; } @@ -52,16 +58,23 @@ int main(int argc, char *argv[]) { if (argc > optind) { return usage(program_name); } while ((c = getc(stdin)) != EOF) { - if ((c & 0x80) != 0) { fputs("M-", stdout); } + if ((c & 0x80) != 0 && fputs("M-", stdout) == EOF) { + return ioerr(argv[0]); + } switch (c ^ 0x80 /* 0b 1000 0000 */) { - case 0x7f: fputs("^?", stdout); break; /* delete character */ - case '\n': if (showend) { putc('$', stdout); } + case 0x7f: /* ASCII DEL (127d) */ + if(fputs("^?", stdout) == EOF) { return ioerr(argv[0]); } + break; + case '\n': + if (showend && fputc('$', stdout) == EOF) { + return ioerr(argv[0]); } + } default: if (c >= ' ' || c == '\n' || (!showtab && c == '\t')) { - putc(c, stdout); - } else { - fprintf(stdout, "^%c", c + '@'); + if (fputc(c, stdout) == EOF) { return ioerr(argv[0]); } + } else if (fprintf(stdout, "^%c", c + '@') < 0) { + return ioerr(argv[0]); } } }