From f5be9911a211543b4fdf9d49ea4e24ad6bf7f55d Mon Sep 17 00:00:00 2001 From: fef Date: Sun, 28 Nov 2021 06:44:33 +0100 Subject: [PATCH] check printf call for error codes --- Makefile | 2 +- gaycat.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index ebba45e..c7f8b09 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -gaycat: +gaycat: gaycat.c $(CC) -O2 -o gaycat gaycat.c install: gaycat diff --git a/gaycat.c b/gaycat.c index fdd51cd..e7d8011 100644 --- a/gaycat.c +++ b/gaycat.c @@ -35,32 +35,39 @@ const char *colors[NUM_COLORS] = { char buf[4096]; -void print_line(const char *line) +int print_line(const char *line) { static int color = 0; - printf("%s%s", colors[color], line); + int ret = printf("%s%s", colors[color], line); if (++color == NUM_COLORS) color = 0; + return ret; } int main(int argc, char *argv[]) { char *line; FILE *stream; + int err; + if (argc < 2 || !strcmp(argv[1], "-")) stream = stdin; else stream = fopen(argv[1], "r"); if (!stream) { - perror(argv[0]); + perror(argv[1]); return 1; } while (1) { line = fgets(buf, sizeof(buf), stream); if (line) { - print_line(line); + err = print_line(line); + if (err < 0) { + perror(argv[0]); + return 3; + } } else { printf("\x1b[0m"); /* reset colors */ if (errno) {