From b5ff856f47d8e54d12c568462a06351633c29610 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 8 Nov 2011 17:29:16 -0500 Subject: Windows --- src/win32/anope_windows.h | 146 ++++++++++++++++++------------------ src/win32/dir/dir.cpp | 47 ++++++++++++ src/win32/dir/dir.h | 26 +++++++ src/win32/dl/dl.cpp | 30 ++++++++ src/win32/dl/dl.h | 13 ++++ src/win32/pipe/pipe.cpp | 61 +++++++++++++++ src/win32/pipe/pipe.h | 8 ++ src/win32/pthread/pthread.cpp | 117 +++++++++++++++++++++++++++++ src/win32/pthread/pthread.h | 34 +++++++++ src/win32/sigaction/sigaction.cpp | 60 +++++++-------- src/win32/sigaction/sigaction.h | 56 +++++++------- src/win32/socket.cpp | 152 ++++++++++++++++++++++++++++++++++++++ src/win32/socket.h | 32 ++++++++ src/win32/vsvars32.bat | 66 ----------------- src/win32/windows.cpp | 104 +++++--------------------- 15 files changed, 671 insertions(+), 281 deletions(-) create mode 100644 src/win32/dir/dir.cpp create mode 100644 src/win32/dir/dir.h create mode 100644 src/win32/dl/dl.cpp create mode 100644 src/win32/dl/dl.h create mode 100644 src/win32/pipe/pipe.cpp create mode 100644 src/win32/pipe/pipe.h create mode 100644 src/win32/pthread/pthread.cpp create mode 100644 src/win32/pthread/pthread.h create mode 100644 src/win32/socket.cpp create mode 100644 src/win32/socket.h delete mode 100644 src/win32/vsvars32.bat (limited to 'src/win32') diff --git a/src/win32/anope_windows.h b/src/win32/anope_windows.h index a631a4a70..cd5215298 100644 --- a/src/win32/anope_windows.h +++ b/src/win32/anope_windows.h @@ -1,71 +1,75 @@ - /* POSIX emulation layer for Windows. - * - * Copyright (C) 2008-2011 Robin Burchell - * Copyright (C) 2008-2011 Anope Team - * - * Please read COPYING and README for further details. - * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. - */ - - #ifndef WINDOWS_H - #define WINDOWS_H - #ifdef _WIN32 - -#include -#include -#include -#include -#include -#include -#ifdef MODULE_COMPILE -# define CoreExport __declspec(dllimport) -# define DllExport __declspec(dllexport) -#else -# define CoreExport __declspec(dllexport) -# define DllExport __declspec(dllimport) -#endif -/* We have our own inet_pton and inet_ntop (Windows XP doesn't have its own) */ -#define inet_pton inet_pton_ -#define inet_ntop inet_ntop_ -#define setenv(x, y, z) SetEnvironmentVariable(x, y) -#define unsetenv(x) SetEnvironmentVariable(x, NULL) -#define MARK_DEPRECATED -#if GETTEXT_FOUND -/* Undefine some functions libintl defines */ -# undef snprintf -# undef vsnprintf -# undef printf -#endif -#define snprintf _snprintf -/* VS2008 hates having this define before its own */ -#define vsnprintf _vsnprintf -#define stat _stat -#define getcwd(x, y) GetCurrentDirectory(y, x) -#define getpid GetCurrentProcessId -#define S_ISREG(x) ((x) & _S_IFREG) -#ifdef EINPROGRESS -# undef EINPROGRESS -#endif -#define EINPROGRESS WSAEWOULDBLOCK - -#include "sigaction/sigaction.h" - -namespace Anope -{ - class string; -} - -extern CoreExport void OnStartup(); -extern CoreExport void OnShutdown(); -extern CoreExport USHORT WindowsGetLanguage(const char *lang); -extern CoreExport int inet_pton(int af, const char *src, void *dst); -extern CoreExport const char *inet_ntop(int af, const void *src, char *dst, size_t size); -extern CoreExport int gettimeofday(timeval *tv, void *); -extern CoreExport Anope::string GetWindowsVersion(); -extern CoreExport bool SupportedWindowsVersion(); -extern int mkstemp(char *input); - - #endif // _WIN32 - #endif // WINDOWS_H + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Robin Burchell + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + + #ifndef WINDOWS_H + #define WINDOWS_H + #ifdef _WIN32 + +#include +#include +#include +#include +#include + +#ifdef MODULE_COMPILE +# define CoreExport __declspec(dllimport) +# define DllExport __declspec(dllexport) +#else +# define CoreExport __declspec(dllexport) +# define DllExport __declspec(dllimport) +#endif + +#define MARK_DEPRECATED + +#if GETTEXT_FOUND +/* Undefine some functions libintl defines */ +# undef snprintf +# undef vsnprintf +# undef printf +#endif + +#define snprintf _snprintf +/* VS2008 hates having this define before its own */ +#define vsnprintf _vsnprintf + +#define stat _stat +#define S_ISREG(x) ((x) & _S_IFREG) + +#ifdef EINPROGRESS +# undef EINPROGRESS +#endif +#define EINPROGRESS WSAEWOULDBLOCK + +#include "socket.h" +#include "dir/dir.h" +#include "dl/dl.h" +#include "pipe/pipe.h" +#include "pthread/pthread.h" +#include "sigaction/sigaction.h" + +namespace Anope +{ + class string; +} + +extern CoreExport void OnStartup(); +extern CoreExport void OnShutdown(); +extern CoreExport USHORT WindowsGetLanguage(const char *lang); +extern CoreExport int gettimeofday(timeval *tv, void *); +extern CoreExport Anope::string GetWindowsVersion(); +extern CoreExport bool SupportedWindowsVersion(); +extern int setenv(const char *name, const char *value, int overwrite); +extern int unsetenv(const char *name); +extern int mkstemp(char *input); +extern void getcwd(char *buf, size_t sz); + +#endif // _WIN32 +#endif // WINDOWS_H diff --git a/src/win32/dir/dir.cpp b/src/win32/dir/dir.cpp new file mode 100644 index 000000000..28a54be98 --- /dev/null +++ b/src/win32/dir/dir.cpp @@ -0,0 +1,47 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#include "dir.h" +#include + +DIR *opendir(const char *path) +{ + char real_path[MAX_PATH]; + _snprintf(real_path, sizeof(real_path), "%s/*", path); + + DIR *d = new DIR(); + d->handle = FindFirstFile(real_path, &d->data); + d->read_first = false; + + if (d->handle == INVALID_HANDLE_VALUE) + { + delete d; + return NULL; + } + + return d; +} + +dirent *readdir(DIR *d) +{ + if (d->read_first == false) + d->read_first = true; + else if (!FindNextFile(d->handle, &d->data)) + return NULL; + + d->ent.d_ino = 1; + d->ent.d_name = d->data.cFileName; + + return &d->ent; +} + +int closedir(DIR *d) +{ + FindClose(d->handle); + delete d; + return 0; +} diff --git a/src/win32/dir/dir.h b/src/win32/dir/dir.h new file mode 100644 index 000000000..8e04c01cc --- /dev/null +++ b/src/win32/dir/dir.h @@ -0,0 +1,26 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#include + +struct dirent +{ + int d_ino; + char *d_name; +}; + +struct DIR +{ + dirent ent; + HANDLE handle; + WIN32_FIND_DATA data; + bool read_first; +}; + +DIR *opendir(const char *); +dirent *readdir(DIR *); +int closedir(DIR *); diff --git a/src/win32/dl/dl.cpp b/src/win32/dl/dl.cpp new file mode 100644 index 000000000..8d76e1f0d --- /dev/null +++ b/src/win32/dl/dl.cpp @@ -0,0 +1,30 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#include "services.h" + +void *dlopen(const char *filename, int) +{ + return LoadLibrary(filename); +} + +char *dlerror(void) +{ + static Anope::string err = Anope::LastError(); + SetLastError(0); + return err.empty() ? NULL : const_cast(err.c_str()); +} + +void *dlsym(void *handle, const char *symbol) +{ + return GetProcAddress(reinterpret_cast(handle), symbol); +} + +int dlclose(void *handle) +{ + return !FreeLibrary(reinterpret_cast(handle)); +} diff --git a/src/win32/dl/dl.h b/src/win32/dl/dl.h new file mode 100644 index 000000000..686611594 --- /dev/null +++ b/src/win32/dl/dl.h @@ -0,0 +1,13 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#define RTLD_LAZY 0 + +extern void *dlopen(const char *filename, int); +extern char *dlerror(void); +extern void *dlsym(void *handle, const char *symbol); +extern int dlclose(void *handle); diff --git a/src/win32/pipe/pipe.cpp b/src/win32/pipe/pipe.cpp new file mode 100644 index 000000000..94e204d9a --- /dev/null +++ b/src/win32/pipe/pipe.cpp @@ -0,0 +1,61 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#include "services.h" + +int pipe(int fds[2]) +{ + sockaddrs localhost; + + localhost.pton(AF_INET, "127.0.0.1"); + + int cfd = socket(AF_INET, SOCK_STREAM, 0), lfd = socket(AF_INET, SOCK_STREAM, 0); + if (cfd == -1 || lfd == -1) + { + close(cfd); + close(lfd); + return -1; + } + + if (bind(lfd, &localhost.sa, localhost.size()) == -1) + { + close(cfd); + close(lfd); + return -1; + } + + if (listen(lfd, 1) == -1) + { + close(cfd); + close(lfd); + return -1; + } + + sockaddrs lfd_addr; + socklen_t sz = sizeof(lfd_addr); + getsockname(lfd, &lfd_addr.sa, &sz); + + if (connect(cfd, &lfd_addr.sa, lfd_addr.size())) + { + close(cfd); + close(lfd); + return -1; + } + + int afd = accept(lfd, NULL, NULL); + if (afd == -1) + { + close(cfd); + return -1; + } + + fds[0] = cfd; + fds[1] = afd; + + return 0; +} + diff --git a/src/win32/pipe/pipe.h b/src/win32/pipe/pipe.h new file mode 100644 index 000000000..1e8538cfb --- /dev/null +++ b/src/win32/pipe/pipe.h @@ -0,0 +1,8 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +extern int pipe(int fds[2]); diff --git a/src/win32/pthread/pthread.cpp b/src/win32/pthread/pthread.cpp new file mode 100644 index 000000000..8c1da7555 --- /dev/null +++ b/src/win32/pthread/pthread.cpp @@ -0,0 +1,117 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#include "pthread.h" + +struct ThreadInfo +{ + void *(*entry)(void *); + void *param; +}; + +static DWORD WINAPI entry_point(void *parameter) +{ + ThreadInfo *ti = static_cast(parameter); + ti->entry(ti->param); + delete ti; + return 0; +} + +int pthread_attr_init(pthread_attr_t *) +{ + /* No need for this */ + return 0; +} + +int pthread_attr_setdetachstate(pthread_attr_t *, int) +{ + /* No need for this */ + return 0; +} + +int pthread_create(pthread_t *thread, const pthread_attr_t *, void *(*entry)(void *), void *param) +{ + ThreadInfo *ti = new ThreadInfo; + ti->entry = entry; + ti->param = param; + + *thread = CreateThread(NULL, 0, entry_point, ti, 0, NULL); + if (!*thread) + { + delete ti; + return -1; + } + + return 0; +} + +int pthread_join(pthread_t thread, void **) +{ + if (WaitForSingleObject(thread, INFINITE) == WAIT_FAILED) + return -1; + return 0; +} + +void pthread_exit(int i) +{ + ExitThread(i); +} + +int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *) +{ + InitializeCriticalSection(mutex); + return 0; +} + +int pthread_mutex_destroy(pthread_mutex_t *mutex) +{ + DeleteCriticalSection(mutex); + return 0; +} + +int pthread_mutex_lock(pthread_mutex_t *mutex) +{ + EnterCriticalSection(mutex); + return 0; +} + +int pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + return !TryEnterCriticalSection(mutex); +} + +int pthread_mutex_unlock(pthread_mutex_t *mutex) +{ + LeaveCriticalSection(mutex); + return 0; +} + +int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *) +{ + *cond = CreateEvent(NULL, false, false, NULL); + if (*cond == NULL) + return -1; + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) +{ + return !CloseHandle(*cond); +} + +int pthread_cond_signal(pthread_cond_t *cond) +{ + return !PulseEvent(*cond); +} + +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) +{ + LeaveCriticalSection(mutex); + WaitForSingleObject(*cond, INFINITE); + EnterCriticalSection(mutex); + return 0; +} diff --git a/src/win32/pthread/pthread.h b/src/win32/pthread/pthread.h new file mode 100644 index 000000000..5ec0775ea --- /dev/null +++ b/src/win32/pthread/pthread.h @@ -0,0 +1,34 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#include + +typedef HANDLE pthread_t; +typedef CRITICAL_SECTION pthread_mutex_t; +typedef HANDLE pthread_cond_t; +typedef int pthread_attr_t; +typedef void pthread_mutexattr_t; +typedef void pthread_condattr_t; + +#define PTHREAD_CREATE_JOINABLE 0 + +extern int pthread_attr_init(pthread_attr_t *); +extern int pthread_attr_setdetachstate(pthread_attr_t *, int); +extern int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); +extern int pthread_join(pthread_t, void **); +extern void pthread_exit(int); + +extern int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +extern int pthread_mutex_destroy(pthread_mutex_t *); +extern int pthread_mutex_lock(pthread_mutex_t *); +extern int pthread_mutex_trylock(pthread_mutex_t *); +extern int pthread_mutex_unlock(pthread_mutex_t *); + +extern int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); +extern int pthread_cond_destroy(pthread_cond_t *); +extern int pthread_cond_signal(pthread_cond_t *); +extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); diff --git a/src/win32/sigaction/sigaction.cpp b/src/win32/sigaction/sigaction.cpp index f65ffaa84..e79187461 100644 --- a/src/win32/sigaction/sigaction.cpp +++ b/src/win32/sigaction/sigaction.cpp @@ -1,31 +1,31 @@ - /* POSIX emulation layer for Windows. - * - * Copyright (C) 2008-2011 Anope Team - * - * Please read COPYING and README for further details. - * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. - */ - - #include - #include "sigaction.h" - #include - - int sigaction(int sig, struct sigaction *action, struct sigaction *old) - { - if (sig == -1) - return 0; - if (old == NULL) - { - if (signal(sig, SIG_DFL) == SIG_ERR) - return -1; - } - else - { - if (signal(sig, action->sa_handler) == SIG_ERR) - return -1; - } - return 0; - } + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + + #include + #include "sigaction.h" + #include + + int sigaction(int sig, struct sigaction *action, struct sigaction *old) + { + if (sig == -1) + return 0; + if (old == NULL) + { + if (signal(sig, SIG_DFL) == SIG_ERR) + return -1; + } + else + { + if (signal(sig, action->sa_handler) == SIG_ERR) + return -1; + } + return 0; + } \ No newline at end of file diff --git a/src/win32/sigaction/sigaction.h b/src/win32/sigaction/sigaction.h index 366965f8b..96d67e8e9 100644 --- a/src/win32/sigaction/sigaction.h +++ b/src/win32/sigaction/sigaction.h @@ -1,28 +1,28 @@ - /* POSIX emulation layer for Windows. - * - * Copyright (C) 2008-2011 Anope Team - * - * Please read COPYING and README for further details. - * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. - */ - - #define sigemptyset(x) memset((x), 0, sizeof(*(x))) - - #ifndef SIGHUP - # define SIGHUP -1 - #endif - #ifndef SIGPIPE - # define SIGPIPE -1 - #endif - - struct sigaction - { - void (*sa_handler)(int); - int sa_flags; - int sa_mask; - }; - - extern int sigaction(int, struct sigaction *, struct sigaction *); - + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + + #define sigemptyset(x) memset((x), 0, sizeof(*(x))) + + #ifndef SIGHUP + # define SIGHUP -1 + #endif + #ifndef SIGPIPE + # define SIGPIPE -1 + #endif + + struct sigaction + { + void (*sa_handler)(int); + int sa_flags; + int sa_mask; + }; + + extern int sigaction(int, struct sigaction *, struct sigaction *); + diff --git a/src/win32/socket.cpp b/src/win32/socket.cpp new file mode 100644 index 000000000..42140ed8b --- /dev/null +++ b/src/win32/socket.cpp @@ -0,0 +1,152 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#define WIN32_NO_OVERRIDE +#include "services.h" + +inline static bool is_socket(int fd) +{ + int optval; + socklen_t optlen = sizeof(optval); + return getsockopt(fd, SOL_SOCKET, SO_TYPE, reinterpret_cast(&optval), &optlen) == 0; +} + +int read(int fd, char *buf, size_t count) +{ + if (is_socket(fd)) + return recv(fd, buf, count, 0); + else + return _read(fd, buf, count); +} + +int write(int fd, const char *buf, size_t count) +{ + if (is_socket(fd)) + return send(fd, buf, count, 0); + else + return _write(fd, buf, count); +} + +int windows_close(int fd) +{ + if (is_socket(fd)) + return closesocket(fd); + else + return close(fd); +} + +int windows_accept(int fd, struct sockaddr *addr, int *addrlen) +{ + int i = accept(fd, addr, addrlen); + if (i == INVALID_SOCKET) + return -1; + return i; +} + +/** This is inet_pton, but it works on Windows + * @param af The protocol type, AF_INET or AF_INET6 + * @param src The address + * @param dst Struct to put results in + * @return 1 on sucess, -1 on error + */ +int windows_inet_pton(int af, const char *src, void *dst) +{ + int address_length; + sockaddr_storage sa; + sockaddr_in *sin = reinterpret_cast(&sa); + sockaddr_in6 *sin6 = reinterpret_cast(&sa); + + switch (af) + { + case AF_INET: + address_length = sizeof(sockaddr_in); + break; + case AF_INET6: + address_length = sizeof(sockaddr_in6); + break; + default: + return -1; + } + + if (!WSAStringToAddress(static_cast(const_cast(src)), af, NULL, reinterpret_cast(&sa), &address_length)) + { + switch (af) + { + case AF_INET: + memcpy(dst, &sin->sin_addr, sizeof(in_addr)); + break; + case AF_INET6: + memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr)); + break; + } + return 1; + } + + return 0; +} + +/** This is inet_ntop, but it works on Windows + * @param af The protocol type, AF_INET or AF_INET6 + * @param src Network address structure + * @param dst After converting put it here + * @param size sizeof the dest + * @return dst + */ +const char *windows_inet_ntop(int af, const void *src, char *dst, size_t size) +{ + int address_length; + DWORD string_length = size; + sockaddr_storage sa; + sockaddr_in *sin = reinterpret_cast(&sa); + sockaddr_in6 *sin6 = reinterpret_cast(&sa); + + memset(&sa, 0, sizeof(sa)); + + switch (af) + { + case AF_INET: + address_length = sizeof(sockaddr_in); + sin->sin_family = af; + memcpy(&sin->sin_addr, src, sizeof(in_addr)); + break; + case AF_INET6: + address_length = sizeof(sockaddr_in6); + sin6->sin6_family = af; + memcpy(&sin6->sin6_addr, src, sizeof(in6_addr)); + break; + default: + return NULL; + } + + if (!WSAAddressToString(reinterpret_cast(&sa), address_length, NULL, dst, &string_length)) + return dst; + + return NULL; +} + +int fcntl(int fd, int cmd, int arg) +{ + if (cmd == F_GETFL) + { + return 0; + } + else if (cmd == F_SETFL) + { + if (arg & O_NONBLOCK) + { + unsigned long opt = 1; + return ioctlsocket(fd, FIONBIO, &opt); + } + else + { + unsigned long opt = 0; + return ioctlsocket(fd, FIONBIO, &opt); + } + } + + return -1; +} diff --git a/src/win32/socket.h b/src/win32/socket.h new file mode 100644 index 000000000..106b9310e --- /dev/null +++ b/src/win32/socket.h @@ -0,0 +1,32 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team + * + * Please read COPYING and README for further details. + */ + +#define read read_not_used +#define write write_not_used +#include +#undef read +#undef write + +#define F_GETFL 0 +#define F_SETFL 1 + +#define O_NONBLOCK 1 + +extern CoreExport int read(int fd, char *buf, size_t count); +extern CoreExport int write(int fd, const char *buf, size_t count); +extern CoreExport int windows_close(int fd); +extern CoreExport int windows_accept(int fd, struct sockaddr *addr, int *addrlen); +extern CoreExport int windows_inet_pton(int af, const char *src, void *dst); +extern CoreExport const char *windows_inet_ntop(int af, const void *src, char *dst, size_t size); +extern CoreExport int fcntl(int fd, int cmd, int arg); + +#ifndef WIN32_NO_OVERRIDE +# define close windows_close +# define accept windows_accept +# define inet_pton windows_inet_pton +# define inet_ntop windows_inet_ntop +#endif diff --git a/src/win32/vsvars32.bat b/src/win32/vsvars32.bat deleted file mode 100644 index f1aee215e..000000000 --- a/src/win32/vsvars32.bat +++ /dev/null @@ -1,66 +0,0 @@ -@SET VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8 -@SET VCINSTALLDIR=%VSINSTALLDIR%\VC -@SET FrameworkDir=C:\WINDOWS\Microsoft.NET\Framework -@SET FrameworkVersion=v2.0.50727 -@SET FrameworkSDKDir=%VSINSTALLDIR%\SDK\v2.0 -@SET Framework35Version=v3.5 -@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR -@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR - -@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools. - -@call :GetWindowsSdkDir - -@if not "%WindowsSdkDir%" == "" ( - set "PATH=%WindowsSdkDir%bin;%PATH%" - set "INCLUDE=%WindowsSdkDir%include;%INCLUDE%" - set "LIB=%WindowsSdkDir%lib;%LIB%" -) - -@rem -@rem Root of Visual Studio IDE installed files. -@rem -@set DevEnvDir=%VSINSTALLDIR%\Common7\IDE - -@set PATH=%VSINSTALLDIR%\Common7\IDE;%VCINSTALLDIR%\BIN;%VSINSTALLDIR%\Common7\Tools;C:\WINDOWS\Microsoft.NET\Framework\v3.5;%VSINSTALLDIR%\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;%VCINSTALLDIR%\VCPackages;%PATH% -@set INCLUDE=%VCINSTALLDIR%\INCLUDE;%INCLUDE% -@set LIB=%VCINSTALLDIR%\LIB;%VSINSTALLDIR%\SDK\v2.0\lib;%LIB% -@set LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v3.5;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;%VCINSTALLDIR%\LIB;%LIBPATH% - -@goto end - -:GetWindowsSdkDir -@call :GetWindowsSdk2008DirHelper HKLM > nul 2>&1 -@if errorlevel 1 call :GetWindowsSdk2003DirHelper HKLM > nul 2>&1 -@if errorlevel 1 call :GetWindowsSdk2008DirHelper HKCU > nul 2>&1 -@if errorlevel 1 call :GetWindowsSdk2003DirHelper HKCU > nul 2>&1 -@if errorlevel 1 set WindowsSdkDir=%VCINSTALLDIR%\PlatformSDK\ -@exit /B 0 - -:GetWindowsSdk2008DirHelper -@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\Microsoft SDKs\Windows" /v "CurrentInstallFolder"') DO ( - if "%%i"=="CurrentInstallFolder" ( - SET "WindowsSdkDir=%%k" - ) -) -@if "%WindowsSdkDir%"=="" exit /B 1 -@exit /B 0 - -:GetWindowsSdk2003DirHelper -@for /F "tokens=1,2,3*" %%i in ('reg query "%1\SOFTWARE\Microsoft\MicrosoftSDK\InstalledSDKs\D2FF9F89-8AA2-4373-8A31-C838BF4DBBE1" /v "Install Dir"') DO ( - if "%%i %%j"=="Install Dir" ( - SET "WindowsSdkDir=%%l" - ) -) -@if "%WindowsSdkDir%"=="" exit /B 1 -@exit /B 0 - -:error_no_VSINSTALLDIR -@echo ERROR: VSINSTALLDIR variable is not set. -@goto end - -:error_no_VCINSTALLDIR -@echo ERROR: VCINSTALLDIR variable is not set. -@goto end - -:end diff --git a/src/win32/windows.cpp b/src/win32/windows.cpp index 60edaffbc..139a17736 100644 --- a/src/win32/windows.cpp +++ b/src/win32/windows.cpp @@ -12,13 +12,11 @@ #ifdef _WIN32 #include "services.h" -struct WindowsLanguage +static struct WindowsLanguage { const char *languageName; USHORT windowsLanguageName; -}; - -WindowsLanguage WindowsLanguages[] = { +} WindowsLanguages[] = { {"ca_ES", LANG_CATALAN}, {"de_DE", LANG_GERMAN}, {"el_GR", LANG_GREEK}, @@ -34,7 +32,7 @@ WindowsLanguage WindowsLanguages[] = { {NULL, 0} }; -WSADATA wsa; +static WSADATA wsa; void OnStartup() { @@ -55,87 +53,6 @@ USHORT WindowsGetLanguage(const char *lang) return LANG_NEUTRAL; } -/** This is inet_pton, but it works on Windows - * @param af The protocol type, AF_INET or AF_INET6 - * @param src The address - * @param dst Struct to put results in - * @return 1 on sucess, -1 on error - */ -int inet_pton(int af, const char *src, void *dst) -{ - int address_length; - sockaddr_storage sa; - sockaddr_in *sin = reinterpret_cast(&sa); - sockaddr_in6 *sin6 = reinterpret_cast(&sa); - - switch (af) - { - case AF_INET: - address_length = sizeof(sockaddr_in); - break; - case AF_INET6: - address_length = sizeof(sockaddr_in6); - break; - default: - return -1; - } - - if (!WSAStringToAddress(static_cast(const_cast(src)), af, NULL, reinterpret_cast(&sa), &address_length)) - { - switch (af) - { - case AF_INET: - memcpy(dst, &sin->sin_addr, sizeof(in_addr)); - break; - case AF_INET6: - memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr)); - break; - } - return 1; - } - - return 0; -} - -/** This is inet_ntop, but it works on Windows - * @param af The protocol type, AF_INET or AF_INET6 - * @param src Network address structure - * @param dst After converting put it here - * @param size sizeof the dest - * @return dst - */ -const char *inet_ntop(int af, const void *src, char *dst, size_t size) -{ - int address_length; - DWORD string_length = size; - sockaddr_storage sa; - sockaddr_in *sin = reinterpret_cast(&sa); - sockaddr_in6 *sin6 = reinterpret_cast(&sa); - - memset(&sa, 0, sizeof(sa)); - - switch (af) - { - case AF_INET: - address_length = sizeof(sockaddr_in); - sin->sin_family = af; - memcpy(&sin->sin_addr, src, sizeof(in_addr)); - break; - case AF_INET6: - address_length = sizeof(sockaddr_in6); - sin6->sin6_family = af; - memcpy(&sin6->sin6_addr, src, sizeof(in6_addr)); - break; - default: - return NULL; - } - - if (!WSAAddressToString(reinterpret_cast(&sa), address_length, NULL, dst, &string_length)) - return dst; - - return NULL; -} - /** Like gettimeofday(), but it works on Windows. * @param tv A timeval struct * @param tz Should be NULL, it is not used @@ -302,6 +219,16 @@ bool SupportedWindowsVersion() return false; } +int setenv(const char *name, const char *value, int overwrite) +{ + return SetEnvironmentVariable(name, value); +} + +int unsetenv(const char *name) +{ + return SetEnvironmentVariable(name, NULL); +} + int mkstemp(char *input) { input = _mktemp(input); @@ -315,4 +242,9 @@ int mkstemp(char *input) return fd; } +void getcwd(char *buf, size_t sz) +{ + GetCurrentDirectory(sz, buf); +} + #endif -- cgit