X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fcoloredstderr.c;h=ccefdf34a31c9da821a58e868008a303e5310c14;hb=51377106d1bd632546fa66bc27ac1a02a647f026;hp=6a10654c7a41ddbd4507de2a9b3aeac67efc55a8;hpb=7ea72c40db03659a9d468f000d59ff133aadf47e;p=coloredstderr%2Fcoloredstderr.git diff --git a/src/coloredstderr.c b/src/coloredstderr.c index 6a10654..ccefdf3 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -18,7 +18,7 @@ * along with this program. If not, see . */ -#include "config.h" +#include /* Must be loaded before the following headers. */ #include "ldpreload.h" @@ -30,6 +30,10 @@ #include #include +#ifdef HAVE_ERROR_H +# include +#endif + /* Conflicting declaration in glibc. */ #undef fwrite_unlocked @@ -62,6 +66,9 @@ static int check_handle_fd(int fd) { if (!initialized) { init_from_environment(); } + if (tracked_fds_list_count == 0) { + return 0; + } /* Never touch anything not going to a terminal - unless we are explicitly * asked to do so. */ @@ -69,9 +76,6 @@ static int check_handle_fd(int fd) { return 0; } - if (tracked_fds_count == 0) { - return 0; - } return tracked_fds_find(fd); } @@ -83,7 +87,7 @@ static void dup_fd(int oldfd, int newfd) { if (!initialized) { init_from_environment(); } - if (tracked_fds_count == 0) { + if (tracked_fds_list_count == 0) { return; } @@ -108,10 +112,10 @@ static void close_fd(int fd) { if (!initialized) { init_from_environment(); } - - if (tracked_fds_count == 0) { + if (tracked_fds_list_count == 0) { return; } + tracked_fds_remove(fd); } @@ -231,6 +235,74 @@ HOOK_FILE1(int, puts_unlocked, stdout, HOOK_VOID1(void, perror, STDERR_FILENO, const char *, s) +/* error(3) */ +#ifdef HAVE_ERROR_H +static void error_vararg(int status, int errnum, + const char *filename, unsigned int linenum, + const char *format, va_list ap) { + static const char *last_filename; + static unsigned int last_linenum; + + /* Skip this error message if requested and if there was already an error + * in the same file/line. */ + if (error_one_per_line + && filename != NULL && linenum != 0 + && filename == last_filename && linenum == last_linenum) { + return; + } + last_filename = filename; + last_linenum = linenum; + + error_message_count++; + + fflush(stdout); + + if (error_print_progname) { + error_print_progname(); + } else { + fprintf(stderr, "%s:", program_invocation_name); + } + if (filename != NULL && linenum != 0) { + fprintf(stderr, "%s:%u:", filename, linenum); + if (error_print_progname) { + fprintf(stderr, " "); + } + } + if (!error_print_progname) { + fprintf(stderr, " "); + } + + + vfprintf(stderr, format, ap); + + if (errnum != 0) { + fprintf(stderr, ": %s", strerror(errnum)); + } + + fprintf(stderr, "\n"); + + if (status != 0) { + exit(status); + } +} +void error_at_line(int status, int errnum, + const char *filename, unsigned int linenum, + const char *format, ...) { + va_list ap; + + va_start(ap, format); + error_vararg(status, errnum, filename, linenum, format, ap); + va_end(ap); +} +void error(int status, int errnum, const char *format, ...) { + va_list ap; + + va_start(ap, format); + error_vararg(status, errnum, NULL, 0, format, ap); + va_end(ap); +} +#endif + /* Hook functions which duplicate file descriptors to track them. */