From: Simon Ruderich Date: Sat, 1 Jun 2013 20:08:45 +0000 (+0200) Subject: Hook perror(3). X-Git-Tag: 0.1~127 X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=commitdiff_plain;h=ad50ad9c54dea706b45d63dd5fdf1e381158f83f Hook perror(3). --- diff --git a/src/coloredstderr.c b/src/coloredstderr.c index 10b83be..b16633f 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -230,6 +230,10 @@ HOOK_FILE1(int, putchar_unlocked, stdout, HOOK_FILE1(int, puts_unlocked, stdout, const char *, s) +/* perror(3) */ +HOOK_VOID1(void, perror, STDERR_FILENO, + const char *, s) + /* Hook functions which duplicate file descriptors to track them. */ diff --git a/src/hookmacros.h b/src/hookmacros.h index ffbc6b6..9e1bd75 100644 --- a/src/hookmacros.h +++ b/src/hookmacros.h @@ -30,6 +30,8 @@ DLSYM_FUNCTION(real_ ## name, #name); #define _HOOK_PRE_FD(type, name, fd) \ _HOOK_PRE(type, name) \ + _HOOK_PRE_FD_ALONE(type, name, fd) +#define _HOOK_PRE_FD_ALONE(type, name, fd) \ handle = check_handle_fd(fd); \ if (handle) { \ handle_fd_pre(fd, handle); \ @@ -42,12 +44,14 @@ } /* Save and restore the errno to make sure we return the errno of the original * function call. */ -#define _HOOK_POST_FD(fd) \ +#define _HOOK_POST_FD_ALONE(fd) \ if (handle) { \ int saved_errno = errno; \ handle_fd_post(fd, handle); \ errno = saved_errno; \ - } \ + } +#define _HOOK_POST_FD(fd) \ + _HOOK_POST_FD_ALONE(fd) \ return result; #define _HOOK_POST_FILE(file) \ if (handle) { \ @@ -58,6 +62,16 @@ return result; +#define HOOK_VOID1(type, name, fd, type1, arg1) \ + static type (*real_ ## name)(type1); \ + type name(type1 arg1) { \ + int handle; \ + DLSYM_FUNCTION(real_ ## name, #name); \ + _HOOK_PRE_FD_ALONE(type, name, fd) \ + real_ ## name(arg1); \ + _HOOK_POST_FD_ALONE(fd) \ + } + #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \ static type (*real_ ## name)(type1, type2, type3); \ type name(type1 arg1, type2 arg2, type3 arg3) { \ diff --git a/tests/example.c b/tests/example.c index 20f7310..c615211 100644 --- a/tests/example.c +++ b/tests/example.c @@ -28,6 +28,9 @@ int main(int argc, char **argv) { printf("write to stdout\n"); fflush(stdout); + errno = 0; + perror("error!"); + write(STDERR_FILENO, "write to stderr 2", 17); write(STDOUT_FILENO, "write to stdout 2", 17); diff --git a/tests/example.expected b/tests/example.expected index b44ee0b..fe7b5f2 100644 --- a/tests/example.expected +++ b/tests/example.expected @@ -1,4 +1,5 @@ >stderr>write to stderr: 1 stderr>write to stderr 2stderr> +>stderr>error!: Success +stderr>write to stderr 2stderr>