From e83c30c158d8ece1e4b0f459faccfd5e5ae67112 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 20 Nov 2014 07:32:15 -0600 Subject: monkey patch more POSIX-like behavior out of winsock 2 Windows sockets functions look on the outside like they behave similarly to POSIX functions, but there are many subtle and glaring differences, including errors reported via WSAGetLastError, read, write, and close do not work on sockets, setsockopt takes a (char *) rather than (void *), etc. This header implements wrappers that coerce more POSIX-like behavior from these functions, making portable code easier to develop. BENEFITS: One does not necessarily need to sprinkle #ifdefs around code to handle the Windows and non-Windows behavior when porting code. CAVEATS: There may be performance implications with the 'mother-may-I' approach to determining if a descriptor is a socket or a file. The errno mappings are not 100% what one might expect compared to POSIX since there were not always good 1:1 equivalents from the WSA errors. --- include/stdio.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'include/stdio.h') diff --git a/include/stdio.h b/include/stdio.h index 989b7fc..ab17883 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -14,4 +14,17 @@ int vasprintf(char **str, const char *fmt, va_list ap); int asprintf(char **str, const char *fmt, ...); #endif +#ifdef _WIN32 +#include +#include + +static inline void +posix_perror(const char *s) +{ + fprintf(stderr, "%s: %s\n", s, strerror(errno)); +} + +#define perror(errnum) posix_perror(errnum) +#endif + #endif -- cgit v1.2.3-55-g6feb