Skip to content

Commit

Permalink
Merge pull request #54 from DanOlivier/master
Browse files Browse the repository at this point in the history
Always forward fputc to original fputc, with or without colors
  • Loading branch information
cehoffman committed Jul 20, 2017
2 parents ffbb85d + 86b502f commit cb9c3fd
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/stderred.c
Expand Up @@ -127,13 +127,43 @@ size_t FUNC(fwrite)(const void *data, size_t size, size_t count, FILE *stream) {
}

int FUNC(fputc)(int chr, FILE *stream) {
const unsigned char c[] = { (unsigned char)chr };
return FUNC(fwrite)(c, sizeof(char), sizeof(char), stream);
size_t result;
int fd = fileno(stream);

GET_ORIGINAL(int, fputc, int, FILE *);
GET_ORIGINAL(ssize_t, fwrite, const void*, size_t, size_t, FILE *);

if (COLORIZE(fd)) {
result = ORIGINAL(fwrite)(start_color_code, sizeof(char), start_color_code_size, stream);
if ((ssize_t)result < 0) return result;
}

result = ORIGINAL(fputc)(chr, stream);
if (COLORIZE(fd)) {
ORIGINAL(fwrite)(end_color_code, sizeof(char), end_color_code_size, stream);
}

return result;
}

int FUNC(fputc_unlocked)(int chr, FILE *stream) {
const unsigned char c[] = { (unsigned char)chr };
return FUNC(fwrite_unlocked)(c, sizeof(char), sizeof(char), stream);
size_t result;
int fd = fileno(stream);

GET_ORIGINAL(int, fputc_unlocked, int, FILE *);
GET_ORIGINAL(ssize_t, fwrite, const void*, size_t, size_t, FILE *);

if (COLORIZE(fd)) {
result = ORIGINAL(fwrite)(start_color_code, sizeof(char), start_color_code_size, stream);
if ((ssize_t)result < 0) return result;
}

result = ORIGINAL(fputc_unlocked)(chr, stream);
if (COLORIZE(fd)) {
ORIGINAL(fwrite)(end_color_code, sizeof(char), end_color_code_size, stream);
}

return result;
}

int FUNC(fputs)(const char *str, FILE *stream) {
Expand Down

0 comments on commit cb9c3fd

Please sign in to comment.