MinGW portability fix.
[wine.git] / include / wine / port.h
blobaea28f15b3705f39b52a0167b7bc6cb2e06ee8af
1 /*
2 * Wine porting definitions
4 * Copyright 1996 Alexandre Julliard
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __WINE_WINE_PORT_H
22 #define __WINE_WINE_PORT_H
24 #ifndef __WINE_CONFIG_H
25 # error You must include config.h to use this header
26 #endif
28 #define _GNU_SOURCE /* for pread/pwrite */
29 #include <fcntl.h>
30 #include <math.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #ifdef HAVE_DIRECT_H
34 # include <direct.h>
35 #endif
36 #ifdef HAVE_IO_H
37 # include <io.h>
38 #endif
39 #ifdef HAVE_PROCESS_H
40 # include <process.h>
41 #endif
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
48 /****************************************************************
49 * Type definitions
52 #ifndef HAVE_MODE_T
53 typedef int mode_t;
54 #endif
55 #ifndef HAVE_OFF_T
56 typedef long off_t;
57 #endif
58 #ifndef HAVE_PID_T
59 typedef int pid_t;
60 #endif
61 #ifndef HAVE_SIZE_T
62 typedef unsigned int size_t;
63 #endif
64 #ifndef HAVE_SSIZE_T
65 typedef int ssize_t;
66 #endif
67 #ifndef HAVE_FSBLKCNT_T
68 typedef unsigned long fsblkcnt_t;
69 #endif
70 #ifndef HAVE_FSFILCNT_T
71 typedef unsigned long fsfilcnt_t;
72 #endif
74 #ifndef HAVE_STRUCT_STATVFS_F_BLOCKS
75 struct statvfs
77 unsigned long f_bsize;
78 unsigned long f_frsize;
79 fsblkcnt_t f_blocks;
80 fsblkcnt_t f_bfree;
81 fsblkcnt_t f_bavail;
82 fsfilcnt_t f_files;
83 fsfilcnt_t f_ffree;
84 fsfilcnt_t f_favail;
85 unsigned long f_fsid;
86 unsigned long f_flag;
87 unsigned long f_namemax;
89 #endif /* HAVE_STRUCT_STATVFS_F_BLOCKS */
92 /****************************************************************
93 * Macro definitions
96 #ifdef HAVE_DLFCN_H
97 #include <dlfcn.h>
98 #else
99 #define RTLD_LAZY 0x001
100 #define RTLD_NOW 0x002
101 #define RTLD_GLOBAL 0x100
102 #endif
104 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
105 #define ftruncate chsize
106 #endif
108 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
109 #define popen _popen
110 #endif
112 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
113 #define pclose _pclose
114 #endif
116 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
117 #define snprintf _snprintf
118 #endif
120 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
121 #define vsnprintf _vsnprintf
122 #endif
124 #ifndef S_ISLNK
125 # define S_ISLNK(mod) (0)
126 #endif
128 #ifndef S_ISSOCK
129 # define S_ISSOCK(mod) (0)
130 #endif
132 #ifndef S_ISDIR
133 # define S_ISDIR(mod) (((mod) & _S_IFMT) == _S_IFDIR)
134 #endif
136 #ifndef S_ISCHR
137 # define S_ISCHR(mod) (((mod) & _S_IFMT) == _S_IFCHR)
138 #endif
140 #ifndef S_ISFIFO
141 # define S_ISFIFO(mod) (((mod) & _S_IFMT) == _S_IFIFO)
142 #endif
144 #ifndef S_ISREG
145 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
146 #endif
148 #ifndef S_IWUSR
149 # define S_IWUSR 0
150 #endif
152 /* So we open files in 64 bit access mode on Linux */
153 #ifndef O_LARGEFILE
154 # define O_LARGEFILE 0
155 #endif
157 #ifndef O_NONBLOCK
158 # define O_NONBLOCK 0
159 #endif
161 #ifndef O_BINARY
162 # define O_BINARY 0
163 #endif
165 #if !defined(S_IXUSR) && defined(S_IEXEC)
166 # define S_IXUSR S_IEXEC
167 #endif
168 #if !defined(S_IXGRP) && defined(S_IEXEC)
169 # define S_IXGRP S_IEXEC
170 #endif
171 #if !defined(S_IXOTH) && defined(S_IEXEC)
172 # define S_IXOTH S_IEXEC
173 #endif
176 /****************************************************************
177 * Constants
180 #ifndef M_PI
181 #define M_PI 3.14159265358979323846
182 #endif
184 #ifndef M_PI_2
185 #define M_PI_2 1.570796326794896619
186 #endif
189 /* Macros to define assembler functions somewhat portably */
191 #if defined(__GNUC__) && !defined(__MINGW32__)
192 # define __ASM_GLOBAL_FUNC(name,code) \
193 __asm__( ".text\n\t" \
194 ".align 4\n\t" \
195 ".globl " __ASM_NAME(#name) "\n\t" \
196 __ASM_FUNC(#name) "\n" \
197 __ASM_NAME(#name) ":\n\t" \
198 code \
199 "\n\t.previous" );
200 #else /* defined(__GNUC__) && !defined(__MINGW32__) */
201 # define __ASM_GLOBAL_FUNC(name,code) \
202 void __asm_dummy_##name(void) { \
203 asm( ".align 4\n\t" \
204 ".globl " __ASM_NAME(#name) "\n\t" \
205 __ASM_FUNC(#name) "\n" \
206 __ASM_NAME(#name) ":\n\t" \
207 code ); \
209 #endif /* __GNUC__ */
212 /* Constructor functions */
214 #ifdef __GNUC__
215 # define DECL_GLOBAL_CONSTRUCTOR(func) \
216 static void func(void) __attribute__((constructor)); \
217 static void func(void)
218 #elif defined(__i386__)
219 # define DECL_GLOBAL_CONSTRUCTOR(func) \
220 static void __dummy_init_##func(void) { \
221 asm(".section .init,\"ax\"\n\t" \
222 "call " #func "\n\t" \
223 ".previous"); } \
224 static void func(void)
225 #elif defined(__sparc__)
226 # define DECL_GLOBAL_CONSTRUCTOR(func) \
227 static void __dummy_init_##func(void) { \
228 asm("\t.section \".init\",#alloc,#execinstr\n" \
229 "\tcall " #func "\n" \
230 "\tnop\n" \
231 "\t.section \".text\",#alloc,#execinstr\n" ); } \
232 static void func(void)
233 #else
234 # error You must define the DECL_GLOBAL_CONSTRUCTOR macro for your platform
235 #endif
238 /* Register functions */
240 #ifdef __i386__
241 #define DEFINE_REGS_ENTRYPOINT( name, fn, args, pop_args ) \
242 __ASM_GLOBAL_FUNC( name, \
243 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t" \
244 ".long " __ASM_NAME(#fn) "\n\t" \
245 ".byte " #args "," #pop_args )
246 /* FIXME: add support for other CPUs */
247 #endif /* __i386__ */
250 /****************************************************************
251 * Function definitions (only when using libwine_port)
254 #ifndef NO_LIBWINE_PORT
256 #ifndef HAVE_FSTATVFS
257 int fstatvfs( int fd, struct statvfs *buf );
258 #endif
260 #ifndef HAVE_GETOPT_LONG
261 extern char *optarg;
262 extern int optind;
263 extern int opterr;
264 extern int optopt;
265 struct option;
267 #ifndef HAVE_STRUCT_OPTION_NAME
268 struct option
270 const char *name;
271 int has_arg;
272 int *flag;
273 int val;
275 #endif
277 extern int getopt_long (int ___argc, char *const *___argv,
278 const char *__shortopts,
279 const struct option *__longopts, int *__longind);
280 extern int getopt_long_only (int ___argc, char *const *___argv,
281 const char *__shortopts,
282 const struct option *__longopts, int *__longind);
283 #endif /* HAVE_GETOPT_LONG */
285 #ifndef HAVE_GETPAGESIZE
286 size_t getpagesize(void);
287 #endif /* HAVE_GETPAGESIZE */
289 #ifndef HAVE_GETTID
290 pid_t gettid(void);
291 #endif /* HAVE_GETTID */
293 #ifndef HAVE_LSTAT
294 int lstat(const char *file_name, struct stat *buf);
295 #endif /* HAVE_LSTAT */
297 #ifndef HAVE_MEMMOVE
298 void *memmove(void *dest, const void *src, size_t len);
299 #endif /* !defined(HAVE_MEMMOVE) */
301 #ifndef HAVE_PREAD
302 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
303 #endif /* HAVE_PREAD */
305 #ifndef HAVE_PWRITE
306 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
307 #endif /* HAVE_PWRITE */
309 #ifndef HAVE_READLINK
310 int readlink( const char *path, char *buf, size_t size );
311 #endif /* HAVE_READLINK */
313 #ifndef HAVE_SIGSETJMP
314 # include <setjmp.h>
315 typedef jmp_buf sigjmp_buf;
316 int sigsetjmp( sigjmp_buf buf, int savesigs );
317 void siglongjmp( sigjmp_buf buf, int val );
318 #endif /* HAVE_SIGSETJMP */
320 #ifndef HAVE_STATVFS
321 int statvfs( const char *path, struct statvfs *buf );
322 #endif
324 #ifndef HAVE_STRNCASECMP
325 # ifndef HAVE__STRNICMP
326 int strncasecmp(const char *str1, const char *str2, size_t n);
327 # else
328 # define strncasecmp _strnicmp
329 # endif
330 #endif /* !defined(HAVE_STRNCASECMP) */
332 #ifndef HAVE_STRERROR
333 const char *strerror(int err);
334 #endif /* !defined(HAVE_STRERROR) */
336 #ifndef HAVE_STRCASECMP
337 # ifndef HAVE__STRICMP
338 int strcasecmp(const char *str1, const char *str2);
339 # else
340 # define strcasecmp _stricmp
341 # endif
342 #endif /* !defined(HAVE_STRCASECMP) */
344 #ifndef HAVE_USLEEP
345 int usleep (unsigned int useconds);
346 #endif /* !defined(HAVE_USLEEP) */
348 #ifdef __i386__
349 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
351 return memcpy( dst, src, size );
353 #else
354 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
355 #endif /* __i386__ */
357 extern int mkstemps(char *template, int suffix_len);
359 /* Process creation flags */
360 #ifndef _P_WAIT
361 # define _P_WAIT 0
362 # define _P_NOWAIT 1
363 # define _P_OVERLAY 2
364 # define _P_NOWAITO 3
365 # define _P_DETACH 4
366 #endif
367 #ifndef HAVE_SPAWNVP
368 extern int spawnvp(int mode, const char *cmdname, const char * const argv[]);
369 #endif
371 /* Interlocked functions */
373 #if defined(__i386__) && defined(__GNUC__)
375 extern inline long interlocked_cmpxchg( long *dest, long xchg, long compare )
377 long ret;
378 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
379 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
380 return ret;
383 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
385 void *ret;
386 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
387 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
388 return ret;
391 extern inline long interlocked_xchg( long *dest, long val )
393 long ret;
394 __asm__ __volatile__( "lock; xchgl %0,(%1)"
395 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
396 return ret;
399 extern inline void *interlocked_xchg_ptr( void **dest, void *val )
401 void *ret;
402 __asm__ __volatile__( "lock; xchgl %0,(%1)"
403 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
404 return ret;
407 extern inline long interlocked_xchg_add( long *dest, long incr )
409 long ret;
410 __asm__ __volatile__( "lock; xaddl %0,(%1)"
411 : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
412 return ret;
415 #else /* __i386___ && __GNUC__ */
417 extern long interlocked_cmpxchg( long *dest, long xchg, long compare );
418 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
419 extern long interlocked_xchg( long *dest, long val );
420 extern void *interlocked_xchg_ptr( void **dest, void *val );
421 extern long interlocked_xchg_add( long *dest, long incr );
423 #endif /* __i386___ && __GNUC__ */
425 #else /* NO_LIBWINE_PORT */
427 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
429 #define fstatvfs __WINE_NOT_PORTABLE(fstatvfs)
430 #define getopt_long __WINE_NOT_PORTABLE(getopt_long)
431 #define getopt_long_only __WINE_NOT_PORTABLE(getopt_long_only)
432 #define getpagesize __WINE_NOT_PORTABLE(getpagesize)
433 #define interlocked_cmpxchg __WINE_NOT_PORTABLE(interlocked_cmpxchg)
434 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
435 #define interlocked_xchg __WINE_NOT_PORTABLE(interlocked_xchg)
436 #define interlocked_xchg_ptr __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
437 #define interlocked_xchg_add __WINE_NOT_PORTABLE(interlocked_xchg_add)
438 #define lstat __WINE_NOT_PORTABLE(lstat)
439 #define memcpy_unaligned __WINE_NOT_PORTABLE(memcpy_unaligned)
440 #define memmove __WINE_NOT_PORTABLE(memmove)
441 #define pread __WINE_NOT_PORTABLE(pread)
442 #define pwrite __WINE_NOT_PORTABLE(pwrite)
443 #define spawnvp __WINE_NOT_PORTABLE(spawnvp)
444 #define statvfs __WINE_NOT_PORTABLE(statvfs)
445 #define strcasecmp __WINE_NOT_PORTABLE(strcasecmp)
446 #define strerror __WINE_NOT_PORTABLE(strerror)
447 #define strncasecmp __WINE_NOT_PORTABLE(strncasecmp)
448 #define usleep __WINE_NOT_PORTABLE(usleep)
450 #endif /* NO_LIBWINE_PORT */
452 #endif /* !defined(__WINE_WINE_PORT_H) */