From: Simon Ruderich Date: Sat, 1 Jun 2013 16:27:56 +0000 (+0200) Subject: Fix initialization if ENV_NAME_FDS was not set. X-Git-Tag: 0.1~131 X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=commitdiff_plain;h=7d4d8e0784059412a7ab3dfdfe3ad011b894a6e4 Fix initialization if ENV_NAME_FDS was not set. If ENV_NAME_FDS is not set then init_from_environment() was called for each hooked function, instead of just once. --- diff --git a/src/coloredstderr.c b/src/coloredstderr.c index f54b765..3e5dac2 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -39,6 +39,10 @@ static ssize_t (*real_write)(int, const void *, size_t); static int (*real_close)(int); static size_t (*real_fwrite)(const void *, size_t, size_t, FILE *); +/* Did we already (try to) parse the environment and setup the necessary + * variables? */ +static int initialized; + #include "constants.h" #ifdef DEBUG @@ -58,7 +62,7 @@ static int check_handle_fd(int fd) { } /* Load state from environment. Only necessary once per process. */ - if (!tracked_fds) { + if (!initialized) { init_from_environment(); } @@ -73,7 +77,7 @@ static void dup_fd(int oldfd, int newfd) { debug("%d -> %d\t\t\t[%d]\n", oldfd, newfd, getpid()); #endif - if (!tracked_fds) { + if (!initialized) { init_from_environment(); } if (tracked_fds_count == 0) { @@ -101,7 +105,7 @@ static void close_fd(int fd) { debug("%d -> .\t\t\t[%d]\n", fd, getpid()); #endif - if (!tracked_fds) { + if (!initialized) { init_from_environment(); } diff --git a/src/trackfds.h b/src/trackfds.h index 474b072..e342b12 100644 --- a/src/trackfds.h +++ b/src/trackfds.h @@ -34,6 +34,7 @@ static size_t tracked_fds_space; * ENV_NAME_FDS has the following format: Each descriptor as string followed * by a comma; there's a trailing comma. Example: "2,4,". */ static void init_from_environment(void) { + initialized = 1; tracked_fds_count = 0; const char *env = getenv(ENV_NAME_FDS);