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
install: gaycat

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

Loading…
Cancel
Save