From 104d433088c9a8455748f594a39f9e23f080d48a Mon Sep 17 00:00:00 2001 From: misomosi Date: Tue, 7 May 2024 00:42:16 -0400 Subject: [PATCH] Add POSIX write example --- examples/write.c | 15 +++++++++++++++ pld.h | 20 +++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 examples/write.c diff --git a/examples/write.c b/examples/write.c new file mode 100644 index 0000000..9620caf --- /dev/null +++ b/examples/write.c @@ -0,0 +1,15 @@ +// PLDLOG uses write by default so redefine it to do nothing +#define PLDLOG(err, buf, len) (void)err, (void)buf, (void)len +#include "../pld.h" + +PLD("libc.so.6"); +long write(int fd, void *buf, unsigned long len) +{ + return PFN(write)(fd, buf, len); +} + +int main(void) +{ + write(1, "Hello World!\n", 13); + return 0; +} diff --git a/pld.h b/pld.h index f103b88..52ebbe6 100644 --- a/pld.h +++ b/pld.h @@ -27,10 +27,6 @@ static int PLD_Private_strlen(const char *s) #ifdef _WIN32 #include -static void *PLD_Private_ErrCode(void) -{ - return (void *)(DWORD_PTR)GetLastError(); -} static void PLD_Private_WriteString(const char *s) { DWORD dummy; @@ -48,12 +44,7 @@ static void PLD_Private_FormatError(void *code, char *s, int len, const char *na (va_list *)&arg); } #else // _WIN32 -#include #include -static void *PLD_Private_ErrCode(void) -{ - return dlerror(); -} static void PLD_Private_WriteString(const char *s) { write(2, s, PLD_Private_strlen(s)); @@ -143,10 +134,16 @@ static PLD_ProcAddress PLD_Private_dlsym(PLD_LibHandle handle, const char *name) return GetProcAddress(handle, name); } +static void *PLD_Private_ErrCode(void) +{ + return (void *)(DWORD_PTR)GetLastError(); +} + #else // _WIN32 #ifndef PLD_PRIVATE_LOG_INCLUDED #include +#include #endif typedef void *PLD_LibHandle; @@ -169,6 +166,11 @@ static PLD_ProcAddress PLD_Private_dlsym(PLD_LibHandle handle, const char *name) return dlsym(handle, name); } +static void *PLD_Private_ErrCode(void) +{ + return dlerror(); +} + #endif // _WIN32 static PLD_ProcAddress PLD_Private_dlsym_wrapper(PLD_LibHandle handle, const char *name) -- 2.49.1