include: Fix the NE exe type defines.
[wine] / include / wine / port.h
1 /*
2  * Wine porting definitions
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifndef __WINE_WINE_PORT_H
22 #define __WINE_WINE_PORT_H
23
24 #ifndef __WINE_CONFIG_H
25 # error You must include config.h to use this header
26 #endif
27
28 #ifdef __WINE_BASETSD_H
29 # error You must include port.h before all other headers
30 #endif
31
32 #define _FILE_OFFSET_BITS 64
33 #define _GNU_SOURCE  /* for pread/pwrite */
34 #include <fcntl.h>
35 #include <math.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef HAVE_DIRECT_H
39 # include <direct.h>
40 #endif
41 #ifdef HAVE_IO_H
42 # include <io.h>
43 #endif
44 #ifdef HAVE_PROCESS_H
45 # include <process.h>
46 #endif
47 #include <string.h>
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52
53 /****************************************************************
54  * Type definitions
55  */
56
57 #if !defined(_MSC_VER) && !defined(__int64)
58 #  if defined(__x86_64__) || defined(_WIN64)
59 #    define __int64 long
60 #  else
61 #    define __int64 long long
62 #  endif
63 #endif
64
65 #ifndef HAVE_MODE_T
66 typedef int mode_t;
67 #endif
68 #ifndef HAVE_OFF_T
69 typedef long off_t;
70 #endif
71 #ifndef HAVE_PID_T
72 typedef int pid_t;
73 #endif
74 #ifndef HAVE_SIZE_T
75 typedef unsigned int size_t;
76 #endif
77 #ifndef HAVE_SSIZE_T
78 typedef int ssize_t;
79 #endif
80 #ifndef HAVE_FSBLKCNT_T
81 typedef unsigned long fsblkcnt_t;
82 #endif
83 #ifndef HAVE_FSFILCNT_T
84 typedef unsigned long fsfilcnt_t;
85 #endif
86
87 #ifndef HAVE_STRUCT_STATVFS_F_BLOCKS
88 struct statvfs
89 {
90     unsigned long f_bsize;
91     unsigned long f_frsize;
92     fsblkcnt_t    f_blocks;
93     fsblkcnt_t    f_bfree;
94     fsblkcnt_t    f_bavail;
95     fsfilcnt_t    f_files;
96     fsfilcnt_t    f_ffree;
97     fsfilcnt_t    f_favail;
98     unsigned long f_fsid;
99     unsigned long f_flag;
100     unsigned long f_namemax;
101 };
102 #endif /* HAVE_STRUCT_STATVFS_F_BLOCKS */
103
104
105 /****************************************************************
106  * Macro definitions
107  */
108
109 #ifdef HAVE_DLFCN_H
110 #include <dlfcn.h>
111 #else
112 #define RTLD_LAZY    0x001
113 #define RTLD_NOW     0x002
114 #define RTLD_GLOBAL  0x100
115 #endif
116
117 #ifdef HAVE_ONE_ARG_MKDIR
118 #define mkdir(path,mode) mkdir(path)
119 #endif
120
121 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
122 #define ftruncate chsize
123 #endif
124
125 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
126 #define popen _popen
127 #endif
128
129 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
130 #define pclose _pclose
131 #endif
132
133 #if !defined(HAVE_STRDUP) && defined(HAVE__STRDUP)
134 #define strdup _strdup
135 #endif
136
137 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
138 #define snprintf _snprintf
139 #endif
140
141 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
142 #define vsnprintf _vsnprintf
143 #endif
144
145 #if !defined(HAVE_STRTOLL) && defined(HAVE__STRTOI64)
146 #define strtoll _strtoi64
147 #endif
148
149 #if !defined(HAVE_STRTOULL) && defined(HAVE__STRTOUI64)
150 #define strtoull _strtoui64
151 #endif
152
153 #ifndef S_ISLNK
154 # define S_ISLNK(mod) (0)
155 #endif
156
157 #ifndef S_ISSOCK
158 # define S_ISSOCK(mod) (0)
159 #endif
160
161 #ifndef S_ISDIR
162 # define S_ISDIR(mod) (((mod) & _S_IFMT) == _S_IFDIR)
163 #endif
164
165 #ifndef S_ISCHR
166 # define S_ISCHR(mod) (((mod) & _S_IFMT) == _S_IFCHR)
167 #endif
168
169 #ifndef S_ISFIFO
170 # define S_ISFIFO(mod) (((mod) & _S_IFMT) == _S_IFIFO)
171 #endif
172
173 #ifndef S_ISREG
174 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
175 #endif
176
177 /* So we open files in 64 bit access mode on Linux */
178 #ifndef O_LARGEFILE
179 # define O_LARGEFILE 0
180 #endif
181
182 #ifndef O_NONBLOCK
183 # define O_NONBLOCK 0
184 #endif
185
186 #ifndef O_BINARY
187 # define O_BINARY 0
188 #endif
189
190
191 /****************************************************************
192  * Constants
193  */
194
195 #ifndef M_PI
196 #define M_PI 3.14159265358979323846
197 #endif
198
199 #ifndef M_PI_2
200 #define M_PI_2 1.570796326794896619
201 #endif
202
203
204 /****************************************************************
205  * Function definitions (only when using libwine_port)
206  */
207
208 #ifndef NO_LIBWINE_PORT
209
210 #ifndef HAVE_FSTATVFS
211 int fstatvfs( int fd, struct statvfs *buf );
212 #endif
213
214 #ifndef HAVE_GETOPT_LONG
215 extern char *optarg;
216 extern int optind;
217 extern int opterr;
218 extern int optopt;
219 struct option;
220
221 #ifndef HAVE_STRUCT_OPTION_NAME
222 struct option
223 {
224     const char *name;
225     int has_arg;
226     int *flag;
227     int val;
228 };
229 #endif
230
231 extern int getopt_long (int ___argc, char *const *___argv,
232                         const char *__shortopts,
233                         const struct option *__longopts, int *__longind);
234 extern int getopt_long_only (int ___argc, char *const *___argv,
235                              const char *__shortopts,
236                              const struct option *__longopts, int *__longind);
237 #endif  /* HAVE_GETOPT_LONG */
238
239 #ifndef HAVE_FFS
240 int ffs( int x );
241 #endif
242
243 #ifndef HAVE_FUTIMES
244 struct timeval;
245 int futimes(int fd, const struct timeval *tv);
246 #endif
247
248 #ifndef HAVE_GETPAGESIZE
249 size_t getpagesize(void);
250 #endif  /* HAVE_GETPAGESIZE */
251
252 #ifndef HAVE_GETTID
253 pid_t gettid(void);
254 #endif /* HAVE_GETTID */
255
256 #ifndef HAVE_ISINF
257 int isinf(double x);
258 #endif
259
260 #ifndef HAVE_ISNAN
261 int isnan(double x);
262 #endif
263
264 #ifndef HAVE_LSTAT
265 int lstat(const char *file_name, struct stat *buf);
266 #endif /* HAVE_LSTAT */
267
268 #ifndef HAVE_MEMMOVE
269 void *memmove(void *dest, const void *src, size_t len);
270 #endif /* !defined(HAVE_MEMMOVE) */
271
272 #ifndef HAVE_POLL
273 struct pollfd
274 {
275     int fd;
276     short events;
277     short revents;
278 };
279 #define POLLIN   0x01
280 #define POLLPRI  0x02
281 #define POLLOUT  0x04
282 #define POLLERR  0x08
283 #define POLLHUP  0x10
284 #define POLLNVAL 0x20
285 int poll( struct pollfd *fds, unsigned int count, int timeout );
286 #endif /* HAVE_POLL */
287
288 #ifndef HAVE_PREAD
289 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
290 #endif /* HAVE_PREAD */
291
292 #ifndef HAVE_PWRITE
293 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
294 #endif /* HAVE_PWRITE */
295
296 #ifndef HAVE_READLINK
297 int readlink( const char *path, char *buf, size_t size );
298 #endif /* HAVE_READLINK */
299
300 #ifndef HAVE_STATVFS
301 int statvfs( const char *path, struct statvfs *buf );
302 #endif
303
304 #ifndef HAVE_STRNCASECMP
305 # ifndef HAVE__STRNICMP
306 int strncasecmp(const char *str1, const char *str2, size_t n);
307 # else
308 # define strncasecmp _strnicmp
309 # endif
310 #endif /* !defined(HAVE_STRNCASECMP) */
311
312 #ifndef HAVE_STRERROR
313 const char *strerror(int err);
314 #endif /* !defined(HAVE_STRERROR) */
315
316 #ifndef HAVE_STRCASECMP
317 # ifndef HAVE__STRICMP
318 int strcasecmp(const char *str1, const char *str2);
319 # else
320 # define strcasecmp _stricmp
321 # endif
322 #endif /* !defined(HAVE_STRCASECMP) */
323
324 #ifndef HAVE_SYMLINK
325 int symlink(const char *from, const char *to);
326 #endif
327
328 #ifndef HAVE_USLEEP
329 int usleep (unsigned int useconds);
330 #endif /* !defined(HAVE_USLEEP) */
331
332 #ifdef __i386__
333 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
334 {
335     return memcpy( dst, src, size );
336 }
337 #else
338 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
339 #endif /* __i386__ */
340
341 extern int mkstemps(char *template, int suffix_len);
342
343 /* Process creation flags */
344 #ifndef _P_WAIT
345 # define _P_WAIT    0
346 # define _P_NOWAIT  1
347 # define _P_OVERLAY 2
348 # define _P_NOWAITO 3
349 # define _P_DETACH  4
350 #endif
351 #ifndef HAVE_SPAWNVP
352 extern int spawnvp(int mode, const char *cmdname, const char * const argv[]);
353 #endif
354
355 /* Interlocked functions */
356
357 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
358
359 extern inline int interlocked_cmpxchg( int *dest, int xchg, int compare );
360 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
361 extern __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare );
362 extern inline int interlocked_xchg( int *dest, int val );
363 extern inline void *interlocked_xchg_ptr( void **dest, void *val );
364 extern inline int interlocked_xchg_add( int *dest, int incr );
365
366 extern inline int interlocked_cmpxchg( int *dest, int xchg, int compare )
367 {
368     int ret;
369     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
370                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
371     return ret;
372 }
373
374 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
375 {
376     void *ret;
377 #ifdef __x86_64__
378     __asm__ __volatile__( "lock; cmpxchgq %2,(%1)"
379                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
380 #else
381     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
382                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
383 #endif
384     return ret;
385 }
386
387 extern inline int interlocked_xchg( int *dest, int val )
388 {
389     int ret;
390     __asm__ __volatile__( "lock; xchgl %0,(%1)"
391                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
392     return ret;
393 }
394
395 extern inline void *interlocked_xchg_ptr( void **dest, void *val )
396 {
397     void *ret;
398 #ifdef __x86_64__
399     __asm__ __volatile__( "lock; xchgq %0,(%1)"
400                           : "=r" (ret) :"r" (dest), "0" (val) : "memory" );
401 #else
402     __asm__ __volatile__( "lock; xchgl %0,(%1)"
403                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
404 #endif
405     return ret;
406 }
407
408 extern inline int interlocked_xchg_add( int *dest, int incr )
409 {
410     int ret;
411     __asm__ __volatile__( "lock; xaddl %0,(%1)"
412                           : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
413     return ret;
414 }
415
416 #else  /* __GNUC__ */
417
418 extern int interlocked_cmpxchg( int *dest, int xchg, int compare );
419 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
420 extern __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare );
421 extern int interlocked_xchg( int *dest, int val );
422 extern void *interlocked_xchg_ptr( void **dest, void *val );
423 extern int interlocked_xchg_add( int *dest, int incr );
424
425 #endif  /* __GNUC__ */
426
427 #else /* NO_LIBWINE_PORT */
428
429 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
430
431 #define ffs                     __WINE_NOT_PORTABLE(ffs)
432 #define fstatvfs                __WINE_NOT_PORTABLE(fstatvfs)
433 #define futimes                 __WINE_NOT_PORTABLE(futimes)
434 #define getopt_long             __WINE_NOT_PORTABLE(getopt_long)
435 #define getopt_long_only        __WINE_NOT_PORTABLE(getopt_long_only)
436 #define getpagesize             __WINE_NOT_PORTABLE(getpagesize)
437 #define interlocked_cmpxchg     __WINE_NOT_PORTABLE(interlocked_cmpxchg)
438 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
439 #define interlocked_xchg        __WINE_NOT_PORTABLE(interlocked_xchg)
440 #define interlocked_xchg_ptr    __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
441 #define interlocked_xchg_add    __WINE_NOT_PORTABLE(interlocked_xchg_add)
442 #define lstat                   __WINE_NOT_PORTABLE(lstat)
443 #define memcpy_unaligned        __WINE_NOT_PORTABLE(memcpy_unaligned)
444 #undef memmove
445 #define memmove                 __WINE_NOT_PORTABLE(memmove)
446 #define pread                   __WINE_NOT_PORTABLE(pread)
447 #define pwrite                  __WINE_NOT_PORTABLE(pwrite)
448 #define spawnvp                 __WINE_NOT_PORTABLE(spawnvp)
449 #define statvfs                 __WINE_NOT_PORTABLE(statvfs)
450 #define strcasecmp              __WINE_NOT_PORTABLE(strcasecmp)
451 #define strerror                __WINE_NOT_PORTABLE(strerror)
452 #define strncasecmp             __WINE_NOT_PORTABLE(strncasecmp)
453 #define usleep                  __WINE_NOT_PORTABLE(usleep)
454
455 #endif /* NO_LIBWINE_PORT */
456
457 #endif /* !defined(__WINE_WINE_PORT_H) */