check printf call for error codes

main
anna 2 years ago
parent 45114dc39b
commit f5be9911a2
Signed by: fef
GPG Key ID: EC22E476DC2D3D84

@ -1,4 +1,4 @@
gaycat: gaycat: gaycat.c
$(CC) -O2 -o gaycat gaycat.c $(CC) -O2 -o gaycat gaycat.c
install: gaycat install: gaycat

@ -35,32 +35,39 @@ const char *colors[NUM_COLORS] = {
char buf[4096]; char buf[4096];
void print_line(const char *line) int print_line(const char *line)
{ {
static int color = 0; static int color = 0;
printf("%s%s", colors[color], line); int ret = printf("%s%s", colors[color], line);
if (++color == NUM_COLORS) if (++color == NUM_COLORS)
color = 0; color = 0;
return ret;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *line; char *line;
FILE *stream; FILE *stream;
int err;
if (argc < 2 || !strcmp(argv[1], "-")) if (argc < 2 || !strcmp(argv[1], "-"))
stream = stdin; stream = stdin;
else else
stream = fopen(argv[1], "r"); stream = fopen(argv[1], "r");
if (!stream) { if (!stream) {
perror(argv[0]); perror(argv[1]);
return 1; return 1;
} }
while (1) { while (1) {
line = fgets(buf, sizeof(buf), stream); line = fgets(buf, sizeof(buf), stream);
if (line) { if (line) {
print_line(line); err = print_line(line);
if (err < 0) {
perror(argv[0]);
return 3;
}
} else { } else {
printf("\x1b[0m"); /* reset colors */ printf("\x1b[0m"); /* reset colors */
if (errno) { if (errno) {

Loading…
Cancel
Save