Don't quote numbers in a query, as it doesn't parse correctly.
[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 _FILE_OFFSET_BITS 64
29 #define _GNU_SOURCE  /* for pread/pwrite */
30 #include <fcntl.h>
31 #include <math.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #ifdef HAVE_DIRECT_H
35 # include <direct.h>
36 #endif
37 #ifdef HAVE_IO_H
38 # include <io.h>
39 #endif
40 #ifdef HAVE_PROCESS_H
41 # include <process.h>
42 #endif
43 #include <string.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47
48
49 /****************************************************************
50  * Type definitions
51  */
52
53 #ifndef HAVE_MODE_T
54 typedef int mode_t;
55 #endif
56 #ifndef HAVE_OFF_T
57 typedef long off_t;
58 #endif
59 #ifndef HAVE_PID_T
60 typedef int pid_t;
61 #endif
62 #ifndef HAVE_SIZE_T
63 typedef unsigned int size_t;
64 #endif
65 #ifndef HAVE_SSIZE_T
66 typedef int ssize_t;
67 #endif
68 #ifndef HAVE_FSBLKCNT_T
69 typedef unsigned long fsblkcnt_t;
70 #endif
71 #ifndef HAVE_FSFILCNT_T
72 typedef unsigned long fsfilcnt_t;
73 #endif
74
75 #ifndef HAVE_STRUCT_STATVFS_F_BLOCKS
76 struct statvfs
77 {
78     unsigned long f_bsize;
79     unsigned long f_frsize;
80     fsblkcnt_t    f_blocks;
81     fsblkcnt_t    f_bfree;
82     fsblkcnt_t    f_bavail;
83     fsfilcnt_t    f_files;
84     fsfilcnt_t    f_ffree;
85     fsfilcnt_t    f_favail;
86     unsigned long f_fsid;
87     unsigned long f_flag;
88     unsigned long f_namemax;
89 };
90 #endif /* HAVE_STRUCT_STATVFS_F_BLOCKS */
91
92
93 /****************************************************************
94  * Macro definitions
95  */
96
97 #ifdef HAVE_DLFCN_H
98 #include <dlfcn.h>
99 #else
100 #define RTLD_LAZY    0x001
101 #define RTLD_NOW     0x002
102 #define RTLD_GLOBAL  0x100
103 #endif
104
105 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
106 #define ftruncate chsize
107 #endif
108
109 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
110 #define popen _popen
111 #endif
112
113 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
114 #define pclose _pclose
115 #endif
116
117 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
118 #define snprintf _snprintf
119 #endif
120
121 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
122 #define vsnprintf _vsnprintf
123 #endif
124
125 #ifndef S_ISLNK
126 # define S_ISLNK(mod) (0)
127 #endif
128
129 #ifndef S_ISSOCK
130 # define S_ISSOCK(mod) (0)
131 #endif
132
133 #ifndef S_ISDIR
134 # define S_ISDIR(mod) (((mod) & _S_IFMT) == _S_IFDIR)
135 #endif
136
137 #ifndef S_ISCHR
138 # define S_ISCHR(mod) (((mod) & _S_IFMT) == _S_IFCHR)
139 #endif
140
141 #ifndef S_ISFIFO
142 # define S_ISFIFO(mod) (((mod) & _S_IFMT) == _S_IFIFO)
143 #endif
144
145 #ifndef S_ISREG
146 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
147 #endif
148
149 #ifndef S_IWUSR
150 # define S_IWUSR 0
151 #endif
152
153 /* So we open files in 64 bit access mode on Linux */
154 #ifndef O_LARGEFILE
155 # define O_LARGEFILE 0
156 #endif
157
158 #ifndef O_NONBLOCK
159 # define O_NONBLOCK 0
160 #endif
161
162 #ifndef O_BINARY
163 # define O_BINARY 0
164 #endif
165
166 #if !defined(S_IXUSR) && defined(S_IEXEC)
167 # define S_IXUSR S_IEXEC
168 #endif
169 #if !defined(S_IXGRP) && defined(S_IEXEC)
170 # define S_IXGRP S_IEXEC
171 #endif
172 #if !defined(S_IXOTH) && defined(S_IEXEC)
173 # define S_IXOTH S_IEXEC
174 #endif
175
176
177 /****************************************************************
178  * Constants
179  */
180
181 #ifndef M_PI
182 #define M_PI 3.14159265358979323846
183 #endif
184
185 #ifndef M_PI_2
186 #define M_PI_2 1.570796326794896619
187 #endif
188
189
190 /* Macros to define assembler functions somewhat portably */
191
192 #if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__APPLE__)
193 # define __ASM_GLOBAL_FUNC(name,code) \
194       __asm__( ".text\n\t" \
195                ".align 4\n\t" \
196                ".globl " __ASM_NAME(#name) "\n\t" \
197                __ASM_FUNC(#name) "\n" \
198                __ASM_NAME(#name) ":\n\t" \
199                code \
200                "\n\t.previous" );
201 #else  /* defined(__GNUC__) && !defined(__MINGW32__) && !defined(__APPLE__)  */
202 # define __ASM_GLOBAL_FUNC(name,code) \
203       void __asm_dummy_##name(void) { \
204           asm( ".align 4\n\t" \
205                ".globl " __ASM_NAME(#name) "\n\t" \
206                __ASM_FUNC(#name) "\n" \
207                __ASM_NAME(#name) ":\n\t" \
208                code ); \
209       }
210 #endif  /* __GNUC__ */
211
212
213 /* Constructor functions */
214
215 #ifdef __GNUC__
216 # define DECL_GLOBAL_CONSTRUCTOR(func) \
217     static void func(void) __attribute__((constructor)); \
218     static void func(void)
219 #elif defined(__i386__)
220 # define DECL_GLOBAL_CONSTRUCTOR(func) \
221     static void __dummy_init_##func(void) { \
222         asm(".section .init,\"ax\"\n\t" \
223             "call " #func "\n\t" \
224             ".previous"); } \
225     static void func(void)
226 #elif defined(__sparc__)
227 # define DECL_GLOBAL_CONSTRUCTOR(func) \
228     static void __dummy_init_##func(void) { \
229         asm("\t.section \".init\",#alloc,#execinstr\n" \
230             "\tcall " #func "\n" \
231             "\tnop\n" \
232             "\t.section \".text\",#alloc,#execinstr\n" ); } \
233     static void func(void)
234 #else
235 # error You must define the DECL_GLOBAL_CONSTRUCTOR macro for your platform
236 #endif
237
238
239 /* Register functions */
240
241 #ifdef __i386__
242 #define DEFINE_REGS_ENTRYPOINT( name, args, pop_args ) \
243     __ASM_GLOBAL_FUNC( name, \
244                        "pushl %eax\n\t" \
245                        "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t" \
246                        ".long " __ASM_NAME("__regs_") #name "-.\n\t" \
247                        ".byte " #args "," #pop_args )
248 /* FIXME: add support for other CPUs */
249 #endif  /* __i386__ */
250
251
252 /****************************************************************
253  * Function definitions (only when using libwine_port)
254  */
255
256 #ifndef NO_LIBWINE_PORT
257
258 #ifndef HAVE_FSTATVFS
259 int fstatvfs( int fd, struct statvfs *buf );
260 #endif
261
262 #ifndef HAVE_GETOPT_LONG
263 extern char *optarg;
264 extern int optind;
265 extern int opterr;
266 extern int optopt;
267 struct option;
268
269 #ifndef HAVE_STRUCT_OPTION_NAME
270 struct option
271 {
272     const char *name;
273     int has_arg;
274     int *flag;
275     int val;
276 };
277 #endif
278
279 extern int getopt_long (int ___argc, char *const *___argv,
280                         const char *__shortopts,
281                         const struct option *__longopts, int *__longind);
282 extern int getopt_long_only (int ___argc, char *const *___argv,
283                              const char *__shortopts,
284                              const struct option *__longopts, int *__longind);
285 #endif  /* HAVE_GETOPT_LONG */
286
287 #ifndef HAVE_FFS
288 int ffs( int x );
289 #endif
290
291 #ifndef HAVE_FUTIMES
292 struct timeval;
293 int futimes(int fd, const struct timeval tv[2]);
294 #endif
295
296 #ifndef HAVE_GETPAGESIZE
297 size_t getpagesize(void);
298 #endif  /* HAVE_GETPAGESIZE */
299
300 #ifndef HAVE_GETTID
301 pid_t gettid(void);
302 #endif /* HAVE_GETTID */
303
304 #ifndef HAVE_LSTAT
305 int lstat(const char *file_name, struct stat *buf);
306 #endif /* HAVE_LSTAT */
307
308 #ifndef HAVE_MEMMOVE
309 void *memmove(void *dest, const void *src, size_t len);
310 #endif /* !defined(HAVE_MEMMOVE) */
311
312 #ifndef HAVE_PREAD
313 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
314 #endif /* HAVE_PREAD */
315
316 #ifndef HAVE_PWRITE
317 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
318 #endif /* HAVE_PWRITE */
319
320 #ifndef HAVE_READLINK
321 int readlink( const char *path, char *buf, size_t size );
322 #endif /* HAVE_READLINK */
323
324 #ifndef HAVE_SIGSETJMP
325 # include <setjmp.h>
326 typedef jmp_buf sigjmp_buf;
327 int sigsetjmp( sigjmp_buf buf, int savesigs );
328 void siglongjmp( sigjmp_buf buf, int val );
329 #endif /* HAVE_SIGSETJMP */
330
331 #ifndef HAVE_STATVFS
332 int statvfs( const char *path, struct statvfs *buf );
333 #endif
334
335 #ifndef HAVE_STRNCASECMP
336 # ifndef HAVE__STRNICMP
337 int strncasecmp(const char *str1, const char *str2, size_t n);
338 # else
339 # define strncasecmp _strnicmp
340 # endif
341 #endif /* !defined(HAVE_STRNCASECMP) */
342
343 #ifndef HAVE_STRERROR
344 const char *strerror(int err);
345 #endif /* !defined(HAVE_STRERROR) */
346
347 #ifndef HAVE_STRCASECMP
348 # ifndef HAVE__STRICMP
349 int strcasecmp(const char *str1, const char *str2);
350 # else
351 # define strcasecmp _stricmp
352 # endif
353 #endif /* !defined(HAVE_STRCASECMP) */
354
355 #ifndef HAVE_USLEEP
356 int usleep (unsigned int useconds);
357 #endif /* !defined(HAVE_USLEEP) */
358
359 #ifdef __i386__
360 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
361 {
362     return memcpy( dst, src, size );
363 }
364 #else
365 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
366 #endif /* __i386__ */
367
368 extern int mkstemps(char *template, int suffix_len);
369
370 /* Process creation flags */
371 #ifndef _P_WAIT
372 # define _P_WAIT    0
373 # define _P_NOWAIT  1
374 # define _P_OVERLAY 2
375 # define _P_NOWAITO 3
376 # define _P_DETACH  4
377 #endif
378 #ifndef HAVE_SPAWNVP
379 extern int spawnvp(int mode, const char *cmdname, const char * const argv[]);
380 #endif
381
382 /* Interlocked functions */
383
384 #if defined(__i386__) && defined(__GNUC__)
385
386 extern inline int interlocked_cmpxchg( int *dest, int xchg, int compare );
387 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
388 extern inline int interlocked_xchg( int *dest, int val );
389 extern inline void *interlocked_xchg_ptr( void **dest, void *val );
390 extern inline int interlocked_xchg_add( int *dest, int incr );
391
392 extern inline int interlocked_cmpxchg( int *dest, int xchg, int compare )
393 {
394     int ret;
395     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
396                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
397     return ret;
398 }
399
400 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
401 {
402     void *ret;
403     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
404                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
405     return ret;
406 }
407
408 extern inline int interlocked_xchg( int *dest, int val )
409 {
410     int ret;
411     __asm__ __volatile__( "lock; xchgl %0,(%1)"
412                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
413     return ret;
414 }
415
416 extern inline void *interlocked_xchg_ptr( void **dest, void *val )
417 {
418     void *ret;
419     __asm__ __volatile__( "lock; xchgl %0,(%1)"
420                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
421     return ret;
422 }
423
424 extern inline int interlocked_xchg_add( int *dest, int incr )
425 {
426     int ret;
427     __asm__ __volatile__( "lock; xaddl %0,(%1)"
428                           : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
429     return ret;
430 }
431
432 #else  /* __i386___ && __GNUC__ */
433
434 extern int interlocked_cmpxchg( int *dest, int xchg, int compare );
435 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
436 extern int interlocked_xchg( int *dest, int val );
437 extern void *interlocked_xchg_ptr( void **dest, void *val );
438 extern int interlocked_xchg_add( int *dest, int incr );
439
440 #endif  /* __i386___ && __GNUC__ */
441
442 #else /* NO_LIBWINE_PORT */
443
444 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
445
446 #define ffs                     __WINE_NOT_PORTABLE(ffs)
447 #define fstatvfs                __WINE_NOT_PORTABLE(fstatvfs)
448 #define futimes                 __WINE_NOT_PORTABLE(futimes)
449 #define getopt_long             __WINE_NOT_PORTABLE(getopt_long)
450 #define getopt_long_only        __WINE_NOT_PORTABLE(getopt_long_only)
451 #define getpagesize             __WINE_NOT_PORTABLE(getpagesize)
452 #define interlocked_cmpxchg     __WINE_NOT_PORTABLE(interlocked_cmpxchg)
453 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
454 #define interlocked_xchg        __WINE_NOT_PORTABLE(interlocked_xchg)
455 #define interlocked_xchg_ptr    __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
456 #define interlocked_xchg_add    __WINE_NOT_PORTABLE(interlocked_xchg_add)
457 #define lstat                   __WINE_NOT_PORTABLE(lstat)
458 #define memcpy_unaligned        __WINE_NOT_PORTABLE(memcpy_unaligned)
459 #define memmove                 __WINE_NOT_PORTABLE(memmove)
460 #define pread                   __WINE_NOT_PORTABLE(pread)
461 #define pwrite                  __WINE_NOT_PORTABLE(pwrite)
462 #define spawnvp                 __WINE_NOT_PORTABLE(spawnvp)
463 #define statvfs                 __WINE_NOT_PORTABLE(statvfs)
464 #define strcasecmp              __WINE_NOT_PORTABLE(strcasecmp)
465 #define strerror                __WINE_NOT_PORTABLE(strerror)
466 #define strncasecmp             __WINE_NOT_PORTABLE(strncasecmp)
467 #define usleep                  __WINE_NOT_PORTABLE(usleep)
468
469 #endif /* NO_LIBWINE_PORT */
470
471 #endif /* !defined(__WINE_WINE_PORT_H) */