Update the address of the Free Software Foundation.
[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 #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 /* Register functions */
214
215 #ifdef __i386__
216 #define DEFINE_REGS_ENTRYPOINT( name, args, pop_args ) \
217     __ASM_GLOBAL_FUNC( name, \
218                        "pushl %eax\n\t" \
219                        "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t" \
220                        ".long " __ASM_NAME("__regs_") #name "-.\n\t" \
221                        ".byte " #args "," #pop_args )
222 /* FIXME: add support for other CPUs */
223 #endif  /* __i386__ */
224
225
226 /****************************************************************
227  * Function definitions (only when using libwine_port)
228  */
229
230 #ifndef NO_LIBWINE_PORT
231
232 #ifndef HAVE_FSTATVFS
233 int fstatvfs( int fd, struct statvfs *buf );
234 #endif
235
236 #ifndef HAVE_GETOPT_LONG
237 extern char *optarg;
238 extern int optind;
239 extern int opterr;
240 extern int optopt;
241 struct option;
242
243 #ifndef HAVE_STRUCT_OPTION_NAME
244 struct option
245 {
246     const char *name;
247     int has_arg;
248     int *flag;
249     int val;
250 };
251 #endif
252
253 extern int getopt_long (int ___argc, char *const *___argv,
254                         const char *__shortopts,
255                         const struct option *__longopts, int *__longind);
256 extern int getopt_long_only (int ___argc, char *const *___argv,
257                              const char *__shortopts,
258                              const struct option *__longopts, int *__longind);
259 #endif  /* HAVE_GETOPT_LONG */
260
261 #ifndef HAVE_FFS
262 int ffs( int x );
263 #endif
264
265 #ifndef HAVE_FUTIMES
266 struct timeval;
267 int futimes(int fd, const struct timeval tv[2]);
268 #endif
269
270 #ifndef HAVE_GETPAGESIZE
271 size_t getpagesize(void);
272 #endif  /* HAVE_GETPAGESIZE */
273
274 #ifndef HAVE_GETTID
275 pid_t gettid(void);
276 #endif /* HAVE_GETTID */
277
278 #ifndef HAVE_LSTAT
279 int lstat(const char *file_name, struct stat *buf);
280 #endif /* HAVE_LSTAT */
281
282 #ifndef HAVE_MEMMOVE
283 void *memmove(void *dest, const void *src, size_t len);
284 #endif /* !defined(HAVE_MEMMOVE) */
285
286 #ifndef HAVE_PREAD
287 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
288 #endif /* HAVE_PREAD */
289
290 #ifndef HAVE_PWRITE
291 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
292 #endif /* HAVE_PWRITE */
293
294 #ifndef HAVE_READLINK
295 int readlink( const char *path, char *buf, size_t size );
296 #endif /* HAVE_READLINK */
297
298 #ifndef HAVE_SIGSETJMP
299 # include <setjmp.h>
300 typedef jmp_buf sigjmp_buf;
301 int sigsetjmp( sigjmp_buf buf, int savesigs );
302 void siglongjmp( sigjmp_buf buf, int val );
303 #endif /* HAVE_SIGSETJMP */
304
305 #ifndef HAVE_STATVFS
306 int statvfs( const char *path, struct statvfs *buf );
307 #endif
308
309 #ifndef HAVE_STRNCASECMP
310 # ifndef HAVE__STRNICMP
311 int strncasecmp(const char *str1, const char *str2, size_t n);
312 # else
313 # define strncasecmp _strnicmp
314 # endif
315 #endif /* !defined(HAVE_STRNCASECMP) */
316
317 #ifndef HAVE_STRERROR
318 const char *strerror(int err);
319 #endif /* !defined(HAVE_STRERROR) */
320
321 #ifndef HAVE_STRCASECMP
322 # ifndef HAVE__STRICMP
323 int strcasecmp(const char *str1, const char *str2);
324 # else
325 # define strcasecmp _stricmp
326 # endif
327 #endif /* !defined(HAVE_STRCASECMP) */
328
329 #ifndef HAVE_USLEEP
330 int usleep (unsigned int useconds);
331 #endif /* !defined(HAVE_USLEEP) */
332
333 #ifdef __i386__
334 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
335 {
336     return memcpy( dst, src, size );
337 }
338 #else
339 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
340 #endif /* __i386__ */
341
342 extern int mkstemps(char *template, int suffix_len);
343
344 /* Process creation flags */
345 #ifndef _P_WAIT
346 # define _P_WAIT    0
347 # define _P_NOWAIT  1
348 # define _P_OVERLAY 2
349 # define _P_NOWAITO 3
350 # define _P_DETACH  4
351 #endif
352 #ifndef HAVE_SPAWNVP
353 extern int spawnvp(int mode, const char *cmdname, const char * const argv[]);
354 #endif
355
356 /* Interlocked functions */
357
358 #if defined(__i386__) && defined(__GNUC__)
359
360 extern inline int interlocked_cmpxchg( int *dest, int xchg, int compare );
361 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *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     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
378                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
379     return ret;
380 }
381
382 extern inline int interlocked_xchg( int *dest, int val )
383 {
384     int ret;
385     __asm__ __volatile__( "lock; xchgl %0,(%1)"
386                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
387     return ret;
388 }
389
390 extern inline void *interlocked_xchg_ptr( void **dest, void *val )
391 {
392     void *ret;
393     __asm__ __volatile__( "lock; xchgl %0,(%1)"
394                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
395     return ret;
396 }
397
398 extern inline int interlocked_xchg_add( int *dest, int incr )
399 {
400     int ret;
401     __asm__ __volatile__( "lock; xaddl %0,(%1)"
402                           : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
403     return ret;
404 }
405
406 #else  /* __i386___ && __GNUC__ */
407
408 extern int interlocked_cmpxchg( int *dest, int xchg, int compare );
409 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
410 extern int interlocked_xchg( int *dest, int val );
411 extern void *interlocked_xchg_ptr( void **dest, void *val );
412 extern int interlocked_xchg_add( int *dest, int incr );
413
414 #endif  /* __i386___ && __GNUC__ */
415
416 #else /* NO_LIBWINE_PORT */
417
418 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
419
420 #define ffs                     __WINE_NOT_PORTABLE(ffs)
421 #define fstatvfs                __WINE_NOT_PORTABLE(fstatvfs)
422 #define futimes                 __WINE_NOT_PORTABLE(futimes)
423 #define getopt_long             __WINE_NOT_PORTABLE(getopt_long)
424 #define getopt_long_only        __WINE_NOT_PORTABLE(getopt_long_only)
425 #define getpagesize             __WINE_NOT_PORTABLE(getpagesize)
426 #define interlocked_cmpxchg     __WINE_NOT_PORTABLE(interlocked_cmpxchg)
427 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
428 #define interlocked_xchg        __WINE_NOT_PORTABLE(interlocked_xchg)
429 #define interlocked_xchg_ptr    __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
430 #define interlocked_xchg_add    __WINE_NOT_PORTABLE(interlocked_xchg_add)
431 #define lstat                   __WINE_NOT_PORTABLE(lstat)
432 #define memcpy_unaligned        __WINE_NOT_PORTABLE(memcpy_unaligned)
433 #undef memmove
434 #define memmove                 __WINE_NOT_PORTABLE(memmove)
435 #define pread                   __WINE_NOT_PORTABLE(pread)
436 #define pwrite                  __WINE_NOT_PORTABLE(pwrite)
437 #define spawnvp                 __WINE_NOT_PORTABLE(spawnvp)
438 #define statvfs                 __WINE_NOT_PORTABLE(statvfs)
439 #define strcasecmp              __WINE_NOT_PORTABLE(strcasecmp)
440 #define strerror                __WINE_NOT_PORTABLE(strerror)
441 #define strncasecmp             __WINE_NOT_PORTABLE(strncasecmp)
442 #define usleep                  __WINE_NOT_PORTABLE(usleep)
443
444 #endif /* NO_LIBWINE_PORT */
445
446 #endif /* !defined(__WINE_WINE_PORT_H) */