diff --git a/ChangeLog b/ChangeLog index eeaf635..37da047 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2011-11-30 LRN + * msync() + * MinGW64 fixes + 2011-11-25 Nils Durner * nl_item * fix cross-platform build diff --git a/configure.in b/configure.in index 5f5b6a7..797f611 100644 --- a/configure.in +++ b/configure.in @@ -64,7 +64,7 @@ dnl Checks for libraries. dnl Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS(unistd.h sys/param.h sys/time.h time.h sys/mkdev.h sys/sysmacros.h string.h memory.h fcntl.h dirent.h sys/ndir.h ndir.h alloca.h locale.h ) -AC_CHECK_FUNCS(ftruncate) +AC_CHECK_FUNCS(ftruncate getnameinfo gettimeofday) AC_HEADER_MAJOR AC_FUNC_ALLOCA diff --git a/src/gettimeofday.c b/src/gettimeofday.c index 8b7aab6..649554a 100644 --- a/src/gettimeofday.c +++ b/src/gettimeofday.c @@ -26,6 +26,9 @@ void _win_gettimeofday(struct timeval *tp, void *tzp) { +#ifdef HAVE_GETTIMEOFDAY + gettimeofday (tp, tzp); +#else struct _timeb theTime; errno = 0; @@ -33,6 +36,7 @@ void _win_gettimeofday(struct timeval *tp, void *tzp) _ftime(&theTime); tp->tv_sec = theTime.time; tp->tv_usec = theTime.millitm * 1000; +#endif } /* end of gettimeofday.c */ diff --git a/src/include/langinfo.h b/src/include/langinfo.h index e79b6b5..618538c 100644 --- a/src/include/langinfo.h +++ b/src/include/langinfo.h @@ -25,6 +25,10 @@ #ifndef _LANGINFO_H_ #define _LANGINFO_H_ +#ifdef __cplusplus +extern "C" { +#endif + typedef int nl_item; /* Enumeration of locale items that can be queried with `nl_langinfo'. */ @@ -160,4 +164,8 @@ enum char *nl_langinfo(int item); +#ifdef __cplusplus +} +#endif + #endif //_LANGINFO_H_ diff --git a/src/include/plibc.h b/src/include/plibc.h index 7b35b83..45b0501 100644 --- a/src/include/plibc.h +++ b/src/include/plibc.h @@ -50,8 +50,8 @@ extern "C" { #include "langinfo.h" #endif -#include #include +#include #include #include #include @@ -70,7 +70,7 @@ extern "C" { /* Convert LARGE_INTEGER to double */ #define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + \ (double)((x).LowPart)) - +#ifndef __MINGW64__ struct stat64 { _dev_t st_dev; @@ -85,7 +85,7 @@ struct stat64 __time64_t st_mtime; __time64_t st_ctime; }; - +#endif typedef unsigned int sa_family_t; struct sockaddr_un { @@ -226,8 +226,13 @@ enum #define MAP_SHARED 0x1 #define MAP_PRIVATE 0x2 /* unsupported */ #define MAP_FIXED 0x10 +#define MAP_ANONYMOUS 0x20 /* unsupported */ #define MAP_FAILED ((void *)-1) +#define MS_ASYNC 1 /* sync memory asynchronously */ +#define MS_INVALIDATE 2 /* invalidate the caches */ +#define MS_SYNC 4 /* synchronous memory sync */ + struct statfs { long f_type; /* type of filesystem (see below) */ @@ -334,7 +339,7 @@ BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest); BOOL _plibc_DereferenceShortcut(char *pszShortcut); char *plibc_ChooseDir(char *pszTitle, unsigned long ulFlags); char *plibc_ChooseFile(char *pszTitle, unsigned long ulFlags); -long QueryRegistry(HKEY hMainKey, char *pszKey, char *pszSubKey, +long QueryRegistry(HKEY hMainKey, const char *pszKey, const char *pszSubKey, char *pszBuffer, long *pdLength); BOOL __win_IsHandleMarkedAsBlocking(int hHandle); @@ -406,6 +411,7 @@ size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream ); int _win_symlink(const char *path1, const char *path2); void *_win_mmap(void *start, size_t len, int access, int flags, int fd, unsigned long long offset); +int _win_msync(void *start, size_t length, int flags); int _win_munmap(void *start, size_t length); int _win_lstat(const char *path, struct stat *buf); int _win_lstat64(const char *path, struct stat64 *buf); @@ -462,10 +468,10 @@ size_t strnlen (const char *str, size_t maxlen); #endif char *stpcpy(char *dest, const char *src); char *strcasestr(const char *haystack_start, const char *needle_start); - +#ifndef __MINGW64__ #define strcasecmp(a, b) stricmp(a, b) #define strncasecmp(a, b, c) strnicmp(a, b, c) - +#endif #endif /* WINDOWS */ #ifndef WINDOWS @@ -508,6 +514,7 @@ char *strcasestr(const char *haystack_start, const char *needle_start); #define SYMLINK(a, b) symlink(a, b) #define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o) #define MKFIFO(p, m) mkfifo(p, m) + #define MSYNC(s, l, f) msync(s, l, f) #define MUNMAP(s, l) munmap(s, l) #define STRERROR(i) strerror(i) #define RANDOM() random() @@ -604,6 +611,7 @@ char *strcasestr(const char *haystack_start, const char *needle_start); #define SYMLINK(a, b) _win_symlink(a, b) #define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o) #define MKFIFO(p, m) _win_mkfifo(p, m) + #define MSYNC(s, l, f) _win_msync(s, l, f) #define MUNMAP(s, l) _win_munmap(s, l) #define STRERROR(i) _win_strerror(i) #define READLINK(p, b, s) _win_readlink(p, b, s) diff --git a/src/include/plibc_private.h b/src/include/plibc_private.h index d7c64db..08c8f95 100644 --- a/src/include/plibc_private.h +++ b/src/include/plibc_private.h @@ -49,6 +49,7 @@ typedef struct { char *pStart; HANDLE hMapping; + HANDLE hFile; } TMapping; typedef struct diff --git a/src/mmap.c b/src/mmap.c index 03d0032..669906b 100644 --- a/src/mmap.c +++ b/src/mmap.c @@ -111,6 +111,16 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd, if (! bFound) { int inserted = 0; + HANDLE hOwnFile; + + if (!DuplicateHandle (GetCurrentProcess (), hFile, GetCurrentProcess (), + &hOwnFile, 0, FALSE, DUPLICATE_SAME_ACCESS)) + { + ReleaseMutex(hMappingsLock); + SetErrnoFromWinError(GetLastError()); + CloseHandle(h); + return MAP_FAILED; + } uiIndex = 0; @@ -120,7 +130,7 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd, { pMappings[uiIndex].pStart = base; pMappings[uiIndex].hMapping = h; - + pMappings[uiIndex].hFile = hOwnFile; inserted = 1; } if (uiIndex == uiMappingsCount) @@ -137,6 +147,53 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd, return base; } +int _win_msync(void *start, size_t length, int flags) +{ + unsigned uiIndex; + /* Can't have sync and async at the same time */ + if ((flags & MS_SYNC) && (flags & MS_ASYNC)) + { + errno = EINVAL; + return -1; + } + /* Not sure what to make of it. It's either the default, or unsupported */ + if (flags & MS_INVALIDATE) + { + errno = ENOSYS; + return -1; + } + + if (FlushViewOfFile (start, length)) + { + BOOL success = TRUE; + errno = 0; + + if (flags & MS_SYNC) + { + /* Flush to the file */ + WaitForSingleObject(hMappingsLock, INFINITE); + + for(uiIndex = 0; uiIndex <= uiMappingsCount; uiIndex++) + { + if (pMappings[uiIndex].pStart == start) + { + success = FlushFileBuffers (pMappings[uiIndex].hFile); + SetErrnoFromWinError(GetLastError()); + break; + } + } + + ReleaseMutex(hMappingsLock); + } + return success ? 0 : -1; + } + else + { + SetErrnoFromWinError(GetLastError()); + return -1; + } +} + /** * @brief Unmap files from memory * @author Cygwin team @@ -158,10 +215,28 @@ int _win_munmap(void *start, size_t length) { if (pMappings[uiIndex].pStart == start) { - success = CloseHandle(pMappings[uiIndex].hMapping); - SetErrnoFromWinError(GetLastError()); + DWORD error; + + error = NO_ERROR; + + if (!CloseHandle(pMappings[uiIndex].hMapping)) + { + success = FALSE; + error = GetLastError(); + } + + if (!CloseHandle(pMappings[uiIndex].hFile)) + { + success = FALSE; + error = GetLastError(); + } + + if (error != NO_ERROR) + SetErrnoFromWinError(error); + pMappings[uiIndex].pStart = NULL; pMappings[uiIndex].hMapping = NULL; + pMappings[uiIndex].hFile = NULL; break; } diff --git a/src/printf.c b/src/printf.c index 28aa4a3..0b05091 100644 --- a/src/printf.c +++ b/src/printf.c @@ -668,7 +668,7 @@ int _win_vsprintf(char *dest, const char *format, va_list arg_ptr) return _win_vsnprintf(dest,(size_t)-1,format,arg_ptr); } -static int __fwrite(void*ptr, size_t nmemb, int fd) { +static int __fwrite(void*ptr, size_t nmemb, void *fd) { return fwrite(ptr, 1, nmemb, fd); } diff --git a/src/registry.c b/src/registry.c index dfc52f5..7e4c894 100644 --- a/src/registry.c +++ b/src/registry.c @@ -33,7 +33,7 @@ * @param pdLength receives size of returned string * @return Error code from winerror.h, ERROR_SUCCESS on success */ -long QueryRegistry(HKEY hMainKey, char *pszKey, char *pszSubKey, +long QueryRegistry(HKEY hMainKey, const char *pszKey, const char *pszSubKey, char *pszBuffer, long *pdLength) { HKEY hKey; diff --git a/src/resolv_ms.c b/src/resolv_ms.c index b3d6dfd..ac0be5c 100644 --- a/src/resolv_ms.c +++ b/src/resolv_ms.c @@ -15,6 +15,7 @@ Revision History: --*/ +#include "plibc_private.h" #include #include #include // sprintf() @@ -949,7 +950,7 @@ getaddrinfo( } - +#ifndef HAVE_GETNAMEINFO __inline int getnameinfo ( @@ -968,7 +969,7 @@ getnameinfo ( return ((*pfGetNameInfo) (sa, salen, host, hostlen, serv, servlen, flags)); } - +#endif __inline diff --git a/src/shortcut.c b/src/shortcut.c index 9a245ee..19a4883 100644 --- a/src/shortcut.c +++ b/src/shortcut.c @@ -31,6 +31,7 @@ DEFINE_OLEGUID(IID_IPersistFile, 0x0000010BL, 0, 0); #include "plibc_private.h" +#include #include #include @@ -101,8 +102,8 @@ BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest) free(pwszDest); - pFile->lpVtbl->Release(pLink); - pLink->lpVtbl->Release(pFile); + pFile->lpVtbl->Release(pFile); + pLink->lpVtbl->Release(pLink); CoUninitialize(); errno = 0;