diff --git a/src/include/port.h b/src/include/port.h index d2409a43e33..818b7c7baef 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -306,6 +306,33 @@ extern bool rmtree(const char *path, bool rmtopdir); #if defined(WIN32) && !defined(__CYGWIN__) +/* + * We want the 64-bit variant of lseek(). + * + * For Visual Studio, this must be after to avoid messing up its + * lseek() and _lseeki64() function declarations. + * + * For MinGW there is already a macro, so we have to undefine it (depending on + * _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to + * count on that being set). + */ +#undef lseek +#define lseek(a,b,c) _lseeki64((a),(b),(c)) + +/* + * We want the 64-bit variant of chsize(). It sets errno and also returns it, + * so convert non-zero result to -1 to match POSIX. + * + * Prevent MinGW from declaring functions, and undefine its macro before we + * define our own. + */ +#ifndef _MSC_VER +#define FTRUNCATE_DEFINED +#include +#undef ftruncate +#endif +#define ftruncate(a,b) (_chsize_s((a),(b)) == 0 ? 0 : -1) + /* * open() and fopen() replacements to allow deletion of open files and * passing of other special options. diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h index d9794fa8727..7900272e8d1 100644 --- a/src/include/port/win32_port.h +++ b/src/include/port/win32_port.h @@ -79,8 +79,6 @@ /* Must be here to avoid conflicting with prototype in windows.h */ #define mkdir(a,b) mkdir(a) -#define ftruncate(a,b) chsize(a,b) - /* Windows doesn't have fsync() as such, use _commit() */ #define fsync(fd) _commit(fd) diff --git a/src/pl/plperl/plperl_system.h b/src/pl/plperl/plperl_system.h index f2e7f1873dc..57355ac1ed4 100644 --- a/src/pl/plperl/plperl_system.h +++ b/src/pl/plperl/plperl_system.h @@ -103,6 +103,7 @@ #undef fstat #undef kill #undef listen +#undef lseek #undef lstat #undef mkdir #undef open