source: box/trunk/lib/win32/emu.h @ 3015

Revision 3015, 10.8 KB checked in by chris, 8 months ago (diff)

Split Win32 defines out of emu.h to enable Boxi to include them before
wx/wx.h (to set UNICODE properly) without also #including winnt.h before
UNICODE is set properly.

  • Property svn:eol-style set to native
Line 
1// emulates unix syscalls to win32 functions
2
3#include "emu_winver.h"
4
5#ifdef WIN32
6        #define EMU_STRUCT_STAT struct emu_stat
7        #define EMU_STAT  emu_stat
8        #define EMU_FSTAT emu_fstat
9        #define EMU_LSTAT emu_stat
10#else
11        #define EMU_STRUCT_STAT struct stat
12        #define EMU_STAT  ::stat
13        #define EMU_FSTAT ::fstat
14        #define EMU_LSTAT ::lstat
15#endif
16
17#if ! defined EMU_INCLUDE && defined WIN32
18#define EMU_INCLUDE
19
20// Need feature detection macros below
21#include "../common/BoxConfig.h"
22
23// Shut up stupid new warnings. Thanks MinGW! Ever heard of "compatibility"?
24#ifdef __MINGW32__
25#       define __MINGW_FEATURES__ 0
26#endif
27
28// basic types, may be required by other headers since we
29// don't include sys/types.h
30
31#ifdef __MINGW32__
32        #include <stdint.h>
33#else // MSVC
34        typedef unsigned __int64 u_int64_t;
35        typedef unsigned __int64 uint64_t;
36        typedef          __int64 int64_t;
37        typedef unsigned __int32 uint32_t;
38        typedef unsigned __int32 u_int32_t;
39        typedef          __int32 int32_t;
40        typedef unsigned __int16 uint16_t;
41        typedef          __int16 int16_t;
42        typedef unsigned __int8  uint8_t;
43        typedef          __int8  int8_t;
44#endif
45
46// emulated types, present on MinGW but not MSVC or vice versa
47
48#ifdef __MINGW32__
49        typedef uint32_t u_int32_t;
50#else
51        typedef unsigned int mode_t;
52        typedef unsigned int pid_t;
53#endif
54
55// Windows headers
56
57#include <winsock2.h>
58#include <fcntl.h>
59#include <sys/stat.h>
60#include <direct.h>
61#include <errno.h>
62#include <io.h>
63#include <stdlib.h>
64#include <string.h>
65#include <stdio.h>
66#include <stdarg.h>
67#include <time.h>
68
69#include <string>
70
71// emulated functions
72
73#define gmtime_r( _clock, _result ) \
74        ( *(_result) = *gmtime( (_clock) ), \
75        (_result) )
76
77#define ITIMER_REAL 0
78
79#ifdef _MSC_VER
80// Microsoft decided to deprecate the standard POSIX functions. Great!
81#define open(file,flags,mode) _open(file,flags,mode)
82#define close(fd)             _close(fd)
83#define dup(fd)               _dup(fd)
84#define read(fd,buf,count)    _read(fd,buf,count)
85#define write(fd,buf,count)   _write(fd,buf,count)
86#define lseek(fd,off,whence)  _lseek(fd,off,whence)
87#define fileno(struct_file)   _fileno(struct_file)
88#endif
89
90struct passwd {
91        char *pw_name;
92        char *pw_passwd;
93        int pw_uid;
94        int pw_gid;
95        time_t pw_change;
96        char *pw_class;
97        char *pw_gecos;
98        char *pw_dir;
99        char *pw_shell;
100        time_t pw_expire;
101};
102
103extern passwd gTempPasswd;
104inline struct passwd * getpwnam(const char * name)
105{
106        //for the mo pretend to be root
107        gTempPasswd.pw_uid = 0;
108        gTempPasswd.pw_gid = 0;
109
110        return &gTempPasswd;
111}
112
113#define S_IRWXG 1
114#define S_IRWXO 2
115#define S_ISUID 4
116#define S_ISGID 8
117#define S_ISVTX 16
118
119#ifndef __MINGW32__
120        //not sure if these are correct
121        //S_IWRITE -   writing permitted
122        //_S_IREAD -   reading permitted
123        //_S_IREAD | _S_IWRITE -
124        #define S_IRUSR S_IWRITE
125        #define S_IWUSR S_IREAD
126        #define S_IRWXU (S_IREAD|S_IWRITE|S_IEXEC)
127
128        #define S_ISREG(x) (S_IFREG & x)
129        #define S_ISDIR(x) (S_IFDIR & x)
130#endif
131
132inline int chown(const char * Filename, u_int32_t uid, u_int32_t gid)
133{
134        //important - this needs implementing
135        //If a large restore is required then
136        //it needs to restore files AND permissions
137        //reference AdjustTokenPrivileges
138        //GetAccountSid
139        //InitializeSecurityDescriptor
140        //SetSecurityDescriptorOwner
141        //The next function looks like the guy to use...
142        //SetFileSecurity
143
144        //indicate success
145        return 0;
146}
147
148// Windows and Unix owners and groups are pretty fundamentally different.
149// Ben prefers that we kludge here rather than litter the code with #ifdefs.
150// Pretend to be root, and pretend that set...() operations succeed.
151inline int setegid(int)
152{
153        return true;
154}
155inline int seteuid(int)
156{
157        return true;
158}
159inline int setgid(int)
160{
161        return true;
162}
163inline int setuid(int)
164{
165        return true;
166}
167inline int getgid(void)
168{
169        return 0;
170}
171inline int getuid(void)
172{
173        return 0;
174}
175inline int geteuid(void)
176{
177        return 0;
178}
179
180#ifndef PATH_MAX
181#define PATH_MAX MAX_PATH
182#endif
183
184// MinGW provides a getopt implementation
185#ifndef __MINGW32__
186#include "getopt.h"
187#endif // !__MINGW32__
188
189#define timespec timeval
190
191//win32 deals in usec not nsec - so need to ensure this follows through
192#define tv_nsec tv_usec
193
194#ifndef __MINGW32__
195        typedef int socklen_t;
196#endif
197
198#define S_IRGRP S_IWRITE
199#define S_IWGRP S_IREAD
200#define S_IROTH S_IWRITE | S_IREAD
201#define S_IWOTH S_IREAD | S_IREAD
202
203//again need to verify these
204#define S_IFLNK 1
205#define S_IFSOCK 0
206
207#define S_ISLNK(x) ( false )
208
209#define vsnprintf _vsnprintf
210
211#ifndef __MINGW32__
212inline int strcasecmp(const char *s1, const char *s2)
213{
214        return _stricmp(s1,s2);
215}
216#endif
217
218#ifdef _DIRENT_H_
219#error You must not include the MinGW dirent.h!
220#endif
221
222struct dirent
223{
224        char *d_name;
225        DWORD d_type; // file attributes
226};
227
228struct DIR
229{
230        HANDLE           fd;     // the HANDLE returned by FindFirstFile
231        WIN32_FIND_DATAW info;
232        struct dirent    result; // d_name (first time null)
233        wchar_t*         name;   // null-terminated byte string
234};
235
236DIR *opendir(const char *name);
237struct dirent *readdir(DIR *dp);
238int closedir(DIR *dp);
239
240// local constant to open file exclusively without shared access
241#define O_LOCK 0x10000
242
243extern DWORD winerrno; /* used to report errors from openfile() */
244HANDLE openfile(const char *filename, int flags, int mode);
245inline int closefile(HANDLE handle)
246{
247        if (CloseHandle(handle) != TRUE)
248        {
249                errno = EINVAL;
250                return -1;
251        }
252        return 0;
253}
254
255#define LOG_DEBUG LOG_INFO
256#define LOG_INFO 6
257#define LOG_NOTICE LOG_INFO
258#define LOG_WARNING 4
259#define LOG_ERR 3
260#define LOG_CRIT LOG_ERR
261#define LOG_PID 0
262#define LOG_LOCAL5 0
263#define LOG_LOCAL6 0
264
265void openlog (const char * daemonName, int, int);
266void closelog(void);
267void syslog  (int loglevel, const char *fmt, ...);
268
269#define LOG_LOCAL0 0
270#define LOG_LOCAL1 0
271#define LOG_LOCAL2 0
272#define LOG_LOCAL3 0
273#define LOG_LOCAL4 0
274#define LOG_LOCAL5 0
275#define LOG_LOCAL6 0
276#define LOG_DAEMON 0
277
278#ifndef __MINGW32__
279#define strtoll _strtoi64
280#endif
281
282inline unsigned int sleep(unsigned int secs)
283{
284        Sleep(secs*1000);
285        return(ERROR_SUCCESS);
286}
287
288#define INFTIM -1
289
290#ifndef POLLIN
291#       define POLLIN 0x1
292#endif
293
294#ifndef POLLERR
295#       define POLLERR 0x8
296#endif
297
298#ifndef POLLOUT
299#       define POLLOUT 0x4
300#endif
301
302#define SHUT_RDWR SD_BOTH
303#define SHUT_RD SD_RECEIVE
304#define SHUT_WR SD_SEND
305
306struct pollfd
307{
308        SOCKET fd;
309        short int events;
310        short int revents;
311};
312
313inline int ioctl(SOCKET sock, int flag,  int * something)
314{
315        //indicate success
316        return 0;
317}
318
319inline int waitpid(pid_t pid, int *status, int)
320{
321        return 0;
322}
323
324//this shouldn't be needed.
325struct statfs
326{
327        TCHAR f_mntonname[MAX_PATH];
328};
329
330struct emu_stat {
331        int st_dev;
332        uint64_t st_ino;
333        DWORD st_mode;
334        short st_nlink;
335        short st_uid;
336        short st_gid;
337        //_dev_t st_rdev;
338        uint64_t st_size;
339        time_t st_atime;
340        time_t st_mtime;
341        time_t st_ctime;
342};
343
344// need this for conversions
345time_t ConvertFileTimeToTime_t(FILETIME *fileTime);
346bool   ConvertTime_tToFileTime(const time_t from, FILETIME *pTo);
347
348int   emu_chdir  (const char* pDirName);
349int   emu_mkdir  (const char* pPathName);
350int   emu_unlink (const char* pFileName);
351int   emu_fstat  (HANDLE file,       struct emu_stat* st);
352int   emu_stat   (const char* pName, struct emu_stat* st);
353int   emu_utimes (const char* pName, const struct timeval[]);
354int   emu_chmod  (const char* pName, mode_t mode);
355char* emu_getcwd (char* pBuffer,     int BufSize);
356int   emu_rename (const char* pOldName, const char* pNewName);
357
358#define chdir(directory)         emu_chdir  (directory)
359#define mkdir(path,     mode)    emu_mkdir  (path)
360#define unlink(file)             emu_unlink (file)
361#define utimes(buffer,  times)   emu_utimes (buffer,   times)
362#define chmod(file,     mode)    emu_chmod  (file,     mode)
363#define getcwd(buffer,  size)    emu_getcwd (buffer,   size)
364#define rename(oldname, newname) emu_rename (oldname, newname)
365
366// Not safe to replace stat/fstat/lstat on mingw at least, as struct stat
367// has a 16-bit st_ino and we need a 64-bit one.
368//
369// #define stat(filename,  struct) emu_stat   (filename, struct)
370// #define lstat(filename, struct) emu_stat   (filename, struct)
371// #define fstat(handle,   struct) emu_fstat  (handle,   struct)
372//
373// But lstat doesn't exist on Windows, so we have to provide something:
374
375#define lstat(filename, struct) stat(filename, struct)
376
377int statfs(const char * name, struct statfs * s);
378
379int poll(struct pollfd *ufds, unsigned long nfds, int timeout);
380
381struct iovec {
382        void *iov_base;   /* Starting address */
383        size_t iov_len;   /* Number of bytes */
384};
385
386int readv (int filedes, const struct iovec *vector, size_t count);
387int writev(int filedes, const struct iovec *vector, size_t count);
388
389// The following functions are not emulations, but utilities for other
390// parts of the code where Windows API is used or windows-specific stuff
391// is needed, like codepage conversion.
392
393bool EnableBackupRights( void );
394
395bool ConvertEncoding (const std::string& rSource, int sourceCodePage,
396        std::string& rDest, int destCodePage);
397bool ConvertToUtf8   (const std::string& rSource, std::string& rDest, 
398        int sourceCodePage);
399bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest,
400        int destCodePage);
401bool ConvertUtf8ToConsole(const std::string& rSource, std::string& rDest);
402bool ConvertConsoleToUtf8(const std::string& rSource, std::string& rDest);
403char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage);
404bool ConvertFromWideString(const std::wstring& rInput, 
405        std::string* pOutput, unsigned int codepage);
406std::string ConvertPathToAbsoluteUnicode(const char *pFileName);
407
408// Utility function which returns a default config file name,
409// based on the path of the current executable.
410std::string GetDefaultConfigFilePath(const std::string& rName);
411
412// GetErrorMessage() returns a system error message, like strerror()
413// but for Windows error codes.
414std::string GetErrorMessage(DWORD errorCode);
415
416// console_read() is a replacement for _cgetws which requires a
417// relatively recent C runtime lib
418int console_read(char* pBuffer, size_t BufferSize);
419
420// Defined thus by MinGW, but missing from MSVC
421// [http://curl.haxx.se/mail/lib-2004-11/0260.html]
422// note: chsize() doesn't work over 2GB:
423// [https://stat.ethz.ch/pipermail/r-devel/2005-May/033339.html]
424#ifndef HAVE_FTRUNCATE
425        extern "C" int ftruncate(int, off_t); 
426        inline int ftruncate(int __fd, off_t __length) 
427        { 
428                return _chsize(__fd, __length); 
429        } 
430#endif
431
432#ifdef _MSC_VER
433        /* disable certain compiler warnings to be able to actually see the show-stopper ones */
434        #pragma warning(disable:4101)           // unreferenced local variable
435        #pragma warning(disable:4244)           // conversion, possible loss of data
436        #pragma warning(disable:4267)           // conversion, possible loss of data
437        #pragma warning(disable:4311)           // pointer truncation
438        #pragma warning(disable:4700)           // uninitialized local variable used (hmmmmm...)
439        #pragma warning(disable:4805)           // unsafe mix of type and type 'bool' in operation
440        #pragma warning(disable:4800)           // forcing value to bool 'true' or 'false' (performance warning)
441        #pragma warning(disable:4996)           // POSIX name for this item is deprecated
442#endif // _MSC_VER
443
444#endif // !EMU_INCLUDE && WIN32
Note: See TracBrowser for help on using the repository browser.