]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blobdiff - src/coloredstderr.c
Rename tracked_fds_* tracked_fds_list_*.
[coloredstderr/coloredstderr.git] / src / coloredstderr.c
index 6a10654c7a41ddbd4507de2a9b3aeac67efc55a8..ccefdf34a31c9da821a58e868008a303e5310c14 100644 (file)
@@ -18,7 +18,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "config.h"
+#include <config.h>
 
 /* Must be loaded before the following headers. */
 #include "ldpreload.h"
 #include <string.h>
 #include <unistd.h>
 
+#ifdef HAVE_ERROR_H
+# include <error.h>
+#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. */