1 /*
2  *  $Id: platform.h,v 1.2 2009-11-24 23:53:40 sampo Exp $
3  * http://support.microsoft.com/kb/190351
4  */
5 
6 #ifndef _platform_h
7 #define _platform_h
8 
9 #include <stdlib.h>
10 
11 #ifdef MINGW
12 
13 #include <winsock2.h>
14 #include <windows.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define MS_LONG LONG
21 #define MKDIR(d,p) _mkdir(d)
22 #define GETTIMEOFDAY(tv, tz) ((tv) ? (((tv)->tv_sec = time(0)) && ((tv)->tv_usec = 0)) : -1)
23 #define GMTIME_R(secs,stm) do { struct tm* stx_tm = gmtime(&(secs)); if (stx_tm) memcpy(&stm, stx_tm, sizeof(struct tm)); } while(0)   /* *** still not thread safe */
24 //#define GMTIME_R(t, res) gmtime_r(&(t),&(res))
25 
26 #define MINGW_RW_PERM (GENERIC_READ | GENERIC_WRITE)
27 
28 #define fdstdin  (GetStdHandle(STD_INPUT_HANDLE))
29 #define fdstdout (GetStdHandle(STD_OUTPUT_HANDLE))
30 /*#define fdtype HANDLE   see zx.h */
31 #define BADFD (INVALID_HANDLE_VALUE)
32 #define closefile(x) (CloseHandle(x)?0:-1)
33 #define openfile_ro(path) zx_CreateFile((path), GENERIC_READ, FILE_SHARE_READ, 0 /*security*/, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
34 
35 HANDLE zx_CreateFile(LPCTSTR lpFileName,
36 		     DWORD dwDesiredAccess, DWORD dwShareMode,
37 		     LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
38 		     DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
39 
40   //#define read(fd, buf, siz) _read((fd),(buf),(siz))
41   //#define write(fd, buf, cnt) _write((fd),(buf),(cnt))
42 #define pipe(fds) _pipe(fds, 64*1024, _O_BINARY)
43 /*#define MKDIR(P,M)  _mkdir(P)*/
44 #define uid_t int
45 #define gid_t int
46 #define geteuid() 0
47 #define getegid() 0
48 #define getgroups(s,l) 0
49 #define stat(X,Y) zx_stat(X,Y)
50 #define openlog(a,b,c)
51 #define syslog(a,...)
52 #define closelog()
53 #define fcntl(fd,cmd,...) (-1)  /* always fail: mingw does not have fcntl(2) */
54 #define nice(x) 0
55 
56 #define F_GETFL 3
57 #define F_SETFL 4
58 #define F_SETFD 2
59 #define LOCK_EX 2
60 #define LOCK_UN 8
61 #define O_SYNC     04010000
62 #define O_NONBLOCK 04000
63 #define O_NDELAY   O_NONBLOCK
64 #define WNOHANG 1
65 
66 #ifdef WIN32CL
67 //#define intptr_t INT_PTR
68 /* The directory handling is quite different on Windows. The following
69  * posix wrappers are implemented in zxdirent.c */
70 typedef struct DIR DIR;
71 struct dirent {
72   char* d_name;
73 };
74 #define opendir zx_win23_opendir
75 #define readdir zx_win23_readdir
76 #define closedir zx_win23_closedir
77 #define rewinddir zx_win23_rewinddir
78 DIR* zx_win23_opendir(char*);
79 struct dirent* zx_win23_readdir(DIR*);
80 int zx_win23_closedir(DIR*);
81 void zx_win23_rewinddir(DIR*);
82 
83 typedef struct stack_st STACK;  /* MSVC seems to have some problem with openssl/stack.h */
84 
85 #define snprintf _snprintf
86 #define va_copy(ap2, ap) ap2 = ap
87 #define symlink(a,b) ( ERR("symlink(2) (%s => %s) not supported by Win32", (a),(b)), -1 )
88 #define unlink(a)    ( ERR("unlink(2) (%s) not supported by Win32", (a)), -1 )
89 #define rmdir(a)     ( ERR("rmdir(2) (%s) not supported by Win32", (a)), -1 )
90 #define close(fd)    ( ERR("close(2) (%s) not supported by Win32. Leaking descriptor.", (a)), -1 )
91 #define getpid()  0
92 #define geteuid() 0
93 #define getegid() 0
94 #define getgroups(s,l) 0
95 #define chdir(path) SetCurrentDirectory(path)
96 #define getcwd(b,n) "getcwd not supported on Win32"  /* *** consider GetCurrentDirectory() */
97 unsigned int sleep(unsigned int secs);
98 unsigned int alarm(unsigned int secs);
99 #else
100 #include <dirent.h>
101 #endif /* WIN32CL */
102 
103 /* Windows thread identification is a mess:
104  * - thread ID returned by GetCurrentThreadId() is like POSIX thread ID except
105  *   that almost none of the windows thread API functions accept it as an argument.
106  *   They need a handle instead.
107  * - Handle can not be obtained easily. Instead GetCurrentThread() returns a
108  *   pseudohandle (-1) that has only limited usability.
109  * - It is not clear what _beginthread() returns. Presumably a handle.
110  * In the end, we adopt keeping around the thread ID and using OpenThread(0,0,tid)
111  * to resolve it to a real thread handle when needed. */
112 #define pthread_t DWORD    /* what pthread_self() returns, or GetCurrentThreadId() */
113 #define pthread_self() GetCurrentThreadId()  /* Returns an ID, not a handle */
114 
115 /* Win32 CRITICAL SECTION based solution (supposedly faster when mutex is only used
116  * in one process, especially on uniprocessor machines). */
117 /*#define pthread_mutex_t CRITICAL_SECTION  see zxid.h */
118 #define PTHREAD_MUTEX_INITIALIZER     (0) /* All instances of MUTEX_INITIALIZER must be converted to call to pthread_mutex_init() early in main() (zxidmeta.c) */
119 #define pthread_mutex_init(mutex, ma) (InitializeCriticalSection(mutex),0) /* dsvmcall.c, dsconfig.c, io.c, dsmem.c */
120 #define pthread_mutex_destroy(mutex)  (DeleteCriticalSection(mutex),0) /* dsvmcall.c */
121 #define pthread_mutex_trylock(mutex)  (TryEnterCriticalSection(mutex)?0:-1) /* dsvm.c */
122 #define pthread_mutex_lock(mutex)     (EnterCriticalSection(mutex), 0) /* dsdbilib.c, api_mutex.c, pool.c, sg.c, io.c, shuffler.c EnterCriticalSection() */
123 #define pthread_mutex_unlock(mutex)   (LeaveCriticalSection(mutex), 0) /* dsvm.c, api_mutex.c, pool.c, sg.c, io.c, shuffler.c LeaveCriticalSection() */
124 
125 #ifdef __cplusplus
126 } // extern "C"
127 #endif
128 
129 #else
130 
131 /* ============================================================================
132  * NOT MINGW nor WIN32CL (i.e. its Unix) */
133 
134 #include <dirent.h>
135 
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139 
140 #define MKDIR(d,p) mkdir((d),(p))
141 #define GETTIMEOFDAY gettimeofday
142 #define GMTIME_R(t, res) gmtime_r(&(t),&(res))
143 
144 #define fdstdin  0  /* fileno(stdin) */
145 #define fdstdout 1  /* fileno(stdout) */
146 /*#define fdtype int   see zx.h */
147 #define BADFD (-1)
148 #define SOCKET fdtype
149 #define closefile(x) close(x)
150 #define closesocket(x) close(x)
151 #define openfile_ro(path) open((path),O_RDONLY)
152 
153 #if !defined(_UNISTD_H) && !defined(_UNISTD_H_)
154 #define _UNISTD_H 1  /* Prevent confusing double inclusion. */
155 #define _UNISTD_H_ 1 /* MacOS: Prevent confusing double inclusion. */
156 /* We do not want to include unistd.h because it does not exist on Win32.
157  * So define these here, but protect by ifndef, because unistd.h may get
158  * indirectly included first. In general we believe these Unix APIs are
159  * so standard that we do not need system includes and can cover
160  * the very few exceptions as ifdefs right in here. --Sampo */
161 int chdir(const char* path);
162 int close(int);
163 int dup(int);
164 int dup2(int,int);
165 int execl(const char *path, const char *arg, ...);
166 int fcntl(int fd, int cmd, ...);         /* Preferred */
167 int fork(void);
168 int execve(const char* f, char *const argv[], char *const envp[]);
169 char* getcwd(char* buf, size_t size);
170 int geteuid(void);
171 int getegid(void);
172 int getgroups(int,gid_t*);
173 int getpid(void);
174 int link(const char* old, const char* new);
175 int lockf(int fd, int cmd, int len);     /* Depends on current seek pos: problem in append */
176 int lseek(int fd, int offset, int whence);
177 int pipe(int fd[2]);
178 int read(int fd, void* buf, int count);
179 int rmdir(const char *pathname);
180 int getuid();
181 int getgid();
182 int setuid(int);
183 int setgid(int);
184 int setsid();
185 int symlink(const char* oldpath, const char* newpath);
186 int unlink(const char* pathname);
187 int write(int fd, void* buf, int count);
188 unsigned int sleep(unsigned int secs);
189 unsigned int alarm(unsigned int secs);
190 int fchown(int fd, uid_t owner, gid_t group);
191 int gethostname(char* name, size_t len);
192 int chroot(const char* path);
193 int nice(int inc);
194 #define F_LOCK 1
195 #define F_ULOCK 0
196 #endif
197 
198 #ifdef _GNU_SOURCE
199 #include <mcheck.h>
200 #endif
201 
202 #if defined(MACOSX) || defined(FREEBSD)
203 #include <sys/event.h>      /* for kqueue used by zxbusd */
204 #define EPOLLHUP (0)        /* *** Need to find better constant */
205 #define EPOLLERR (0)        /* *** Need to find better constant */
206 #define EPOLLOUT (EVFILT_WRITE)
207 #define EPOLLIN  (EVFILT_READ)
208 #endif
209 
210 #ifdef LINUX
211 #include <sys/epoll.h>      /* See man 4 epoll (Linux 2.6) */
212 #endif
213 #ifdef SUNOS
214 #include <sys/devpoll.h>    /* See man -s 7d poll (Solaris 8) */
215 #include <sys/poll.h>
216 #define EPOLLHUP (POLLHUP)  /* 0x010 */
217 #define EPOLLERR (POLLERR)  /* 0x008 */
218 #define EPOLLOUT (POLLOUT)  /* 0x004 */
219 #define EPOLLIN  (POLLIN)   /* 0x001 */
220 #endif
221 
222 #ifdef __cplusplus
223 } // extern "C"
224 #endif
225 
226 #endif
227 
228 #endif
229