+static ssize_t (*libc_write)(int fd, const void *buf, size_t count);
+static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL;
+static struct cgit_filter *current_write_filter = NULL;
+
+void cgit_init_filters(void)
+{
+ libc_write = dlsym(RTLD_NEXT, "write");
+ if (!libc_write)
+ die("Could not locate libc's write function");
+}
+
+ssize_t write(int fd, const void *buf, size_t count)
+{
+ if (fd != STDOUT_FILENO || !filter_write)
+ return libc_write(fd, buf, count);
+ return filter_write(current_write_filter, buf, count);
+}
+
+static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
+{
+ /* We want to avoid buggy nested patterns. */
+ assert(filter_write == NULL);
+ assert(current_write_filter == NULL);
+ current_write_filter = filter;
+ filter_write = new_write;
+}
+
+static inline void unhook_write(void)
+{
+ assert(filter_write != NULL);
+ assert(current_write_filter != NULL);
+ filter_write = NULL;
+ current_write_filter = NULL;
+}
+