check printf call for error codes

This commit is contained in:
anna 2021-11-28 06:44:33 +01:00
parent 45114dc39b
commit f5be9911a2
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 12 additions and 5 deletions

View file

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

View file

@ -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) {