check printf call for error codes
This commit is contained in:
parent
45114dc39b
commit
f5be9911a2
2 changed files with 12 additions and 5 deletions
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
||||||
gaycat:
|
gaycat: gaycat.c
|
||||||
$(CC) -O2 -o gaycat gaycat.c
|
$(CC) -O2 -o gaycat gaycat.c
|
||||||
|
|
||||||
install: gaycat
|
install: gaycat
|
||||||
|
|
15
gaycat.c
15
gaycat.c
|
@ -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…
Reference in a new issue