Added varargs support for 16-bit entry points.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #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 #include <string.h>
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43
44
45 /****************************************************************
46  * Type definitions
47  */
48
49 #ifndef HAVE_MODE_T
50 typedef int mode_t;
51 #endif
52 #ifndef HAVE_OFF_T
53 typedef long off_t;
54 #endif
55 #ifndef HAVE_PID_T
56 typedef int pid_t;
57 #endif
58 #ifndef HAVE_SIZE_T
59 typedef unsigned int size_t;
60 #endif
61 #ifndef HAVE_SSIZE_T
62 typedef int ssize_t;
63 #endif
64
65 #ifndef HAVE_STATFS
66 # ifdef __BEOS__
67 #  define HAVE_STRUCT_STATFS_F_BFREE
68 struct statfs {
69   long   f_bsize;  /* block_size */
70   long   f_blocks; /* total_blocks */
71   long   f_bfree;  /* free_blocks */
72 };
73 # else /* defined(__BEOS__) */
74 struct statfs;
75 # endif /* defined(__BEOS__) */
76 #endif /* !defined(HAVE_STATFS) */
77
78
79 /****************************************************************
80  * Macro definitions
81  */
82
83 #ifdef HAVE_DLFCN_H
84 #include <dlfcn.h>
85 #else
86 #define RTLD_LAZY    0x001
87 #define RTLD_NOW     0x002
88 #define RTLD_GLOBAL  0x100
89 #endif
90
91 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
92 #define ftruncate chsize
93 #endif
94
95 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
96 #define popen _popen
97 #endif
98
99 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
100 #define pclose _pclose
101 #endif
102
103 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
104 #define snprintf _snprintf
105 #endif
106
107 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
108 #define vsnprintf _vsnprintf
109 #endif
110
111 #ifndef S_ISLNK
112 # define S_ISLNK(mod) (0)
113 #endif /* S_ISLNK */
114
115 /* So we open files in 64 bit access mode on Linux */
116 #ifndef O_LARGEFILE
117 # define O_LARGEFILE 0
118 #endif
119
120
121 /****************************************************************
122  * Constants
123  */
124
125 #ifndef M_PI
126 #define M_PI 3.14159265358979323846
127 #endif
128
129 #ifndef M_PI_2
130 #define M_PI_2 1.570796326794896619
131 #endif
132
133
134 /* Macros to define assembler functions somewhat portably */
135
136 #ifdef __GNUC__
137 # define __ASM_GLOBAL_FUNC(name,code) \
138       __asm__( ".align 4\n\t" \
139                ".globl " __ASM_NAME(#name) "\n\t" \
140                __ASM_FUNC(#name) "\n" \
141                __ASM_NAME(#name) ":\n\t" \
142                code );
143 #else  /* __GNUC__ */
144 # define __ASM_GLOBAL_FUNC(name,code) \
145       void __asm_dummy_##name(void) { \
146           asm( ".align 4\n\t" \
147                ".globl " __ASM_NAME(#name) "\n\t" \
148                __ASM_FUNC(#name) "\n" \
149                __ASM_NAME(#name) ":\n\t" \
150                code ); \
151       }
152 #endif  /* __GNUC__ */
153
154
155 /* Constructor functions */
156
157 #ifdef __GNUC__
158 # define DECL_GLOBAL_CONSTRUCTOR(func) \
159     static void func(void) __attribute__((constructor)); \
160     static void func(void)
161 #elif defined(__i386__)
162 # define DECL_GLOBAL_CONSTRUCTOR(func) \
163     static void __dummy_init_##func(void) { \
164         asm(".section .init,\"ax\"\n\t" \
165             "call " #func "\n\t" \
166             ".previous"); } \
167     static void func(void)
168 #elif defined(__sparc__)
169 # define DECL_GLOBAL_CONSTRUCTOR(func) \
170     static void __dummy_init_##func(void) { \
171         asm("\t.section \".init\",#alloc,#execinstr\n" \
172             "\tcall " #func "\n" \
173             "\tnop\n" \
174             "\t.section \".text\",#alloc,#execinstr\n" ); } \
175     static void func(void)
176 #else
177 # error You must define the DECL_GLOBAL_CONSTRUCTOR macro for your platform
178 #endif
179
180
181 /****************************************************************
182  * Function definitions (only when using libwine_port)
183  */
184
185 #ifndef NO_LIBWINE_PORT
186
187 #ifndef HAVE_GETOPT_LONG
188 extern char *optarg;
189 extern int optind;
190 extern int opterr;
191 extern int optopt;
192 struct option;
193
194 #ifndef HAVE_STRUCT_OPTION_NAME
195 struct option
196 {
197     const char *name;
198     int has_arg;
199     int *flag;
200     int val;
201 };
202 #endif
203
204 extern int getopt_long (int ___argc, char *const *___argv,
205                         const char *__shortopts,
206                         const struct option *__longopts, int *__longind);
207 extern int getopt_long_only (int ___argc, char *const *___argv,
208                              const char *__shortopts,
209                              const struct option *__longopts, int *__longind);
210 #endif  /* HAVE_GETOPT_LONG */
211
212 #ifndef HAVE_GETPAGESIZE
213 size_t getpagesize(void);
214 #endif  /* HAVE_GETPAGESIZE */
215
216 #ifndef HAVE_LSTAT
217 int lstat(const char *file_name, struct stat *buf);
218 #endif /* HAVE_LSTAT */
219
220 #ifndef HAVE_MEMMOVE
221 void *memmove(void *dest, const void *src, size_t len);
222 #endif /* !defined(HAVE_MEMMOVE) */
223
224 #ifndef HAVE_PREAD
225 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
226 #endif /* HAVE_PREAD */
227
228 #ifndef HAVE_PWRITE
229 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
230 #endif /* HAVE_PWRITE */
231
232 #ifndef HAVE_STATFS
233 int statfs(const char *name, struct statfs *info);
234 #endif /* !defined(HAVE_STATFS) */
235
236 #ifndef HAVE_STRNCASECMP
237 # ifndef HAVE__STRNICMP
238 int strncasecmp(const char *str1, const char *str2, size_t n);
239 # else
240 # define strncasecmp _strnicmp
241 # endif
242 #endif /* !defined(HAVE_STRNCASECMP) */
243
244 #ifndef HAVE_STRERROR
245 const char *strerror(int err);
246 #endif /* !defined(HAVE_STRERROR) */
247
248 #ifndef HAVE_STRCASECMP
249 # ifndef HAVE__STRICMP
250 int strcasecmp(const char *str1, const char *str2);
251 # else
252 # define strcasecmp _stricmp
253 # endif
254 #endif /* !defined(HAVE_STRCASECMP) */
255
256 #ifndef HAVE_USLEEP
257 int usleep (unsigned int useconds);
258 #endif /* !defined(HAVE_USLEEP) */
259
260 #ifdef __i386__
261 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
262 {
263     return memcpy( dst, src, size );
264 }
265 #else
266 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
267 #endif /* __i386__ */
268
269 extern int mkstemps(char *template, int suffix_len);
270
271 /* Process creation flags */
272 #ifndef _P_WAIT
273 # define _P_WAIT    0
274 # define _P_NOWAIT  1
275 # define _P_OVERLAY 2
276 # define _P_NOWAITO 3
277 # define _P_DETACH  4
278 #endif
279 extern int spawnvp(int mode, const char *cmdname, char *const argv[]);
280
281 /* Interlocked functions */
282
283 #if defined(__i386__) && defined(__GNUC__)
284
285 extern inline long interlocked_cmpxchg( long *dest, long xchg, long compare )
286 {
287     long ret;
288     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
289                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
290     return ret;
291 }
292
293 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
294 {
295     void *ret;
296     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
297                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
298     return ret;
299 }
300
301 extern inline long interlocked_xchg( long *dest, long val )
302 {
303     long ret;
304     __asm__ __volatile__( "lock; xchgl %0,(%1)"
305                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
306     return ret;
307 }
308
309 extern inline void *interlocked_xchg_ptr( void **dest, void *val )
310 {
311     void *ret;
312     __asm__ __volatile__( "lock; xchgl %0,(%1)"
313                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
314     return ret;
315 }
316
317 extern inline long interlocked_xchg_add( long *dest, long incr )
318 {
319     long ret;
320     __asm__ __volatile__( "lock; xaddl %0,(%1)"
321                           : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
322     return ret;
323 }
324
325 #else  /* __i386___ && __GNUC__ */
326
327 extern long interlocked_cmpxchg( long *dest, long xchg, long compare );
328 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
329 extern long interlocked_xchg( long *dest, long val );
330 extern void *interlocked_xchg_ptr( void **dest, void *val );
331 extern long interlocked_xchg_add( long *dest, long incr );
332
333 #endif  /* __i386___ && __GNUC__ */
334
335 #else /* NO_LIBWINE_PORT */
336
337 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
338
339 #define getopt_long             __WINE_NOT_PORTABLE(getopt_long)
340 #define getopt_long_only        __WINE_NOT_PORTABLE(getopt_long_only)
341 #define getpagesize             __WINE_NOT_PORTABLE(getpagesize)
342 #define interlocked_cmpxchg     __WINE_NOT_PORTABLE(interlocked_cmpxchg)
343 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
344 #define interlocked_xchg        __WINE_NOT_PORTABLE(interlocked_xchg)
345 #define interlocked_xchg_ptr    __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
346 #define interlocked_xchg_add    __WINE_NOT_PORTABLE(interlocked_xchg_add)
347 #define lstat                   __WINE_NOT_PORTABLE(lstat)
348 #define memcpy_unaligned        __WINE_NOT_PORTABLE(memcpy_unaligned)
349 #define memmove                 __WINE_NOT_PORTABLE(memmove)
350 #define pread                   __WINE_NOT_PORTABLE(pread)
351 #define pwrite                  __WINE_NOT_PORTABLE(pwrite)
352 #define spawnvp                 __WINE_NOT_PORTABLE(spawnvp)
353 #define statfs                  __WINE_NOT_PORTABLE(statfs)
354 #define strcasecmp              __WINE_NOT_PORTABLE(strcasecmp)
355 #define strerror                __WINE_NOT_PORTABLE(strerror)
356 #define strncasecmp             __WINE_NOT_PORTABLE(strncasecmp)
357 #define usleep                  __WINE_NOT_PORTABLE(usleep)
358
359 #endif /* NO_LIBWINE_PORT */
360
361 #endif /* !defined(__WINE_WINE_PORT_H) */