Detect snprintf && _snprintf, use _snprintf on stupid platforms
[wine/multimedia.git] / include / wine / port.h
blobc4110f311922960ea30245eccd202783ab04288e
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 <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_DIRECT_H
33 # include <direct.h>
34 #endif
35 #ifdef HAVE_IO_H
36 # include <io.h>
37 #endif
38 #include <string.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
44 /****************************************************************
45 * Type definitions
48 #ifndef HAVE_MODE_T
49 typedef int mode_t;
50 #endif
51 #ifndef HAVE_OFF_T
52 typedef long off_t;
53 #endif
54 #ifndef HAVE_PID_T
55 typedef int pid_t;
56 #endif
57 #ifndef HAVE_SIZE_T
58 typedef unsigned int size_t;
59 #endif
60 #ifndef HAVE_SSIZE_T
61 typedef int ssize_t;
62 #endif
64 #if !defined(HAVE_GETNETBYADDR) && !defined(HAVE_GETNETBYNAME)
65 struct netent {
66 char *n_name;
67 char **n_aliases;
68 int n_addrtype;
69 unsigned long n_net;
71 #endif /* !defined(HAVE_GETNETBYADDR) && !defined(HAVE_GETNETBYNAME) */
73 #if !defined(HAVE_GETPROTOBYNAME) && !defined(HAVE_GETPROTOBYNUMBER)
74 struct protoent {
75 char *p_name;
76 char **p_aliases;
77 int p_proto;
79 #endif /* !defined(HAVE_GETPROTOBYNAME) && !defined(HAVE_GETPROTOBYNUMBER) */
81 #ifndef HAVE_STATFS
82 # ifdef __BEOS__
83 # define STATFS_HAS_BFREE
84 struct statfs {
85 long f_bsize; /* block_size */
86 long f_blocks; /* total_blocks */
87 long f_bfree; /* free_blocks */
89 # else /* defined(__BEOS__) */
90 struct statfs;
91 # endif /* defined(__BEOS__) */
92 #endif /* !defined(HAVE_STATFS) */
95 /****************************************************************
96 * Macro definitions
99 #ifdef HAVE_DLFCN_H
100 #include <dlfcn.h>
101 #else
102 #define RTLD_LAZY 0x001
103 #define RTLD_NOW 0x002
104 #define RTLD_GLOBAL 0x100
105 #endif
107 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
108 #define ftruncate chsize
109 #endif
111 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
112 #define popen _popen
113 #endif
115 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
116 #define pclose _pclose
117 #endif
119 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
120 #define snprintf _snprintf
121 #endif
123 #ifndef S_ISLNK
124 # define S_ISLNK(mod) (0)
125 #endif /* S_ISLNK */
127 /* So we open files in 64 bit access mode on Linux */
128 #ifndef O_LARGEFILE
129 # define O_LARGEFILE 0
130 #endif
132 /* Macros to define assembler functions somewhat portably */
134 #ifdef NEED_UNDERSCORE_PREFIX
135 # define __ASM_NAME(name) "_" name
136 #else
137 # define __ASM_NAME(name) name
138 #endif
140 #ifdef NEED_TYPE_IN_DEF
141 # define __ASM_FUNC(name) ".def " __ASM_NAME(name) "; .scl 2; .type 32; .endef"
142 #else
143 # define __ASM_FUNC(name) ".type " __ASM_NAME(name) ",@function"
144 #endif
146 #ifdef __GNUC__
147 # define __ASM_GLOBAL_FUNC(name,code) \
148 __asm__( ".align 4\n\t" \
149 ".globl " __ASM_NAME(#name) "\n\t" \
150 __ASM_FUNC(#name) "\n" \
151 __ASM_NAME(#name) ":\n\t" \
152 code );
153 #else /* __GNUC__ */
154 # define __ASM_GLOBAL_FUNC(name,code) \
155 void __asm_dummy_##name(void) { \
156 asm( ".align 4\n\t" \
157 ".globl " __ASM_NAME(#name) "\n\t" \
158 __ASM_FUNC(#name) "\n" \
159 __ASM_NAME(#name) ":\n\t" \
160 code ); \
162 #endif /* __GNUC__ */
165 /* Macros to access unaligned or wrong-endian WORDs and DWORDs. */
167 #define PUT_WORD(ptr, w) (*(WORD *)(ptr) = (w))
168 #define GET_WORD(ptr) (*(WORD *)(ptr))
169 #define PUT_DWORD(ptr, d) (*(DWORD *)(ptr) = (d))
170 #define GET_DWORD(ptr) (*(DWORD *)(ptr))
172 #define PUT_LE_WORD(ptr, w) \
173 do { ((BYTE *)(ptr))[0] = LOBYTE(w); \
174 ((BYTE *)(ptr))[1] = HIBYTE(w); } while (0)
175 #define GET_LE_WORD(ptr) \
176 MAKEWORD( ((BYTE *)(ptr))[0], \
177 ((BYTE *)(ptr))[1] )
178 #define PUT_LE_DWORD(ptr, d) \
179 do { PUT_LE_WORD(&((WORD *)(ptr))[0], LOWORD(d)); \
180 PUT_LE_WORD(&((WORD *)(ptr))[1], HIWORD(d)); } while (0)
181 #define GET_LE_DWORD(ptr) \
182 ((DWORD)MAKELONG( GET_LE_WORD(&((WORD *)(ptr))[0]), \
183 GET_LE_WORD(&((WORD *)(ptr))[1]) ))
185 #define PUT_BE_WORD(ptr, w) \
186 do { ((BYTE *)(ptr))[1] = LOBYTE(w); \
187 ((BYTE *)(ptr))[0] = HIBYTE(w); } while (0)
188 #define GET_BE_WORD(ptr) \
189 MAKEWORD( ((BYTE *)(ptr))[1], \
190 ((BYTE *)(ptr))[0] )
191 #define PUT_BE_DWORD(ptr, d) \
192 do { PUT_BE_WORD(&((WORD *)(ptr))[1], LOWORD(d)); \
193 PUT_BE_WORD(&((WORD *)(ptr))[0], HIWORD(d)); } while (0)
194 #define GET_BE_DWORD(ptr) \
195 ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
196 GET_BE_WORD(&((WORD *)(ptr))[0]) ))
198 #if defined(ALLOW_UNALIGNED_ACCESS)
199 #define PUT_UA_WORD(ptr, w) PUT_WORD(ptr, w)
200 #define GET_UA_WORD(ptr) GET_WORD(ptr)
201 #define PUT_UA_DWORD(ptr, d) PUT_DWORD(ptr, d)
202 #define GET_UA_DWORD(ptr) GET_DWORD(ptr)
203 #elif defined(WORDS_BIGENDIAN)
204 #define PUT_UA_WORD(ptr, w) PUT_BE_WORD(ptr, w)
205 #define GET_UA_WORD(ptr) GET_BE_WORD(ptr)
206 #define PUT_UA_DWORD(ptr, d) PUT_BE_DWORD(ptr, d)
207 #define GET_UA_DWORD(ptr) GET_BE_DWORD(ptr)
208 #else
209 #define PUT_UA_WORD(ptr, w) PUT_LE_WORD(ptr, w)
210 #define GET_UA_WORD(ptr) GET_LE_WORD(ptr)
211 #define PUT_UA_DWORD(ptr, d) PUT_LE_DWORD(ptr, d)
212 #define GET_UA_DWORD(ptr) GET_LE_DWORD(ptr)
213 #endif
216 /****************************************************************
217 * Function definitions (only when using libwine)
220 #ifndef NO_LIBWINE
222 #if !defined(HAVE_CLONE) && defined(linux)
223 int clone(int (*fn)(void *arg), void *stack, int flags, void *arg);
224 #endif /* !defined(HAVE_CLONE) && defined(linux) */
226 #ifndef HAVE_GETNETBYADDR
227 struct netent *getnetbyaddr(unsigned long net, int type);
228 #endif /* defined(HAVE_GETNETBYNAME) */
230 #ifndef HAVE_GETNETBYNAME
231 struct netent *getnetbyname(const char *name);
232 #endif /* defined(HAVE_GETNETBYNAME) */
234 #ifndef HAVE_GETPAGESIZE
235 size_t getpagesize(void);
236 #endif /* HAVE_GETPAGESIZE */
238 #ifndef HAVE_GETPROTOBYNAME
239 struct protoent *getprotobyname(const char *name);
240 #endif /* !defined(HAVE_GETPROTOBYNAME) */
242 #ifndef HAVE_GETPROTOBYNUMBER
243 struct protoent *getprotobynumber(int proto);
244 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
246 #ifndef HAVE_GETSERVBYPORT
247 struct servent *getservbyport(int port, const char *proto);
248 #endif /* !defined(HAVE_GETSERVBYPORT) */
250 #ifndef HAVE_GETSOCKOPT
251 int getsockopt(int socket, int level, int option_name, void *option_value, size_t *option_len);
252 #endif /* !defined(HAVE_GETSOCKOPT) */
254 #ifndef HAVE_INET_NETWORK
255 unsigned long inet_network(const char *cp);
256 #endif /* !defined(HAVE_INET_NETWORK) */
258 #ifndef HAVE_LSTAT
259 int lstat(const char *file_name, struct stat *buf);
260 #endif /* HAVE_LSTAT */
262 #ifndef HAVE_MEMMOVE
263 void *memmove(void *dest, const void *src, unsigned int len);
264 #endif /* !defined(HAVE_MEMMOVE) */
266 #ifndef HAVE_PREAD
267 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
268 #endif /* HAVE_PREAD */
270 #ifndef HAVE_PWRITE
271 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
272 #endif /* HAVE_PWRITE */
274 #ifndef HAVE_STATFS
275 int statfs(const char *name, struct statfs *info);
276 #endif /* !defined(HAVE_STATFS) */
278 #ifndef HAVE_STRNCASECMP
279 # ifndef HAVE__STRNICMP
280 int strncasecmp(const char *str1, const char *str2, size_t n);
281 # else
282 # define strncasecmp _strnicmp
283 # endif
284 #endif /* !defined(HAVE_STRNCASECMP) */
286 #ifndef HAVE_STRERROR
287 const char *strerror(int err);
288 #endif /* !defined(HAVE_STRERROR) */
290 #ifndef HAVE_STRCASECMP
291 # ifndef HAVE__STRICMP
292 int strcasecmp(const char *str1, const char *str2);
293 # else
294 # define strcasecmp _stricmp
295 # endif
296 #endif /* !defined(HAVE_STRCASECMP) */
298 #ifndef HAVE_USLEEP
299 int usleep (unsigned int useconds);
300 #endif /* !defined(HAVE_USLEEP) */
302 /* Interlocked functions */
304 #if defined(__i386__) && defined(__GNUC__)
306 inline static long interlocked_cmpxchg( long *dest, long xchg, long compare )
308 long ret;
309 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
310 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
311 return ret;
314 inline static void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
316 void *ret;
317 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
318 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
319 return ret;
322 inline static long interlocked_xchg( long *dest, long val )
324 long ret;
325 __asm__ __volatile__( "lock; xchgl %0,(%1)"
326 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
327 return ret;
330 inline static void *interlocked_xchg_ptr( void **dest, void *val )
332 void *ret;
333 __asm__ __volatile__( "lock; xchgl %0,(%1)"
334 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
335 return ret;
338 inline static long interlocked_xchg_add( long *dest, long incr )
340 long ret;
341 __asm__ __volatile__( "lock; xaddl %0,(%1)"
342 : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
343 return ret;
346 #else /* __i386___ && __GNUC__ */
348 extern long interlocked_cmpxchg( long *dest, long xchg, long compare );
349 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
350 extern long interlocked_xchg( long *dest, long val );
351 extern void *interlocked_xchg_ptr( void **dest, void *val );
352 extern long interlocked_xchg_add( long *dest, long incr );
354 #endif /* __i386___ && __GNUC__ */
356 #else /* NO_LIBWINE */
358 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
360 #define clone __WINE_NOT_PORTABLE(clone)
361 #define getnetbyaddr __WINE_NOT_PORTABLE(getnetbyaddr)
362 #define getnetbyname __WINE_NOT_PORTABLE(getnetbyname)
363 #define getpagesize __WINE_NOT_PORTABLE(getpagesize)
364 #define getprotobyname __WINE_NOT_PORTABLE(getprotobyname)
365 #define getprotobynumber __WINE_NOT_PORTABLE(getprotobynumber)
366 #define getservbyport __WINE_NOT_PORTABLE(getservbyport)
367 #define getsockopt __WINE_NOT_PORTABLE(getsockopt)
368 #define inet_network __WINE_NOT_PORTABLE(inet_network)
369 #define lstat __WINE_NOT_PORTABLE(lstat)
370 #define memmove __WINE_NOT_PORTABLE(memmove)
371 #define pread __WINE_NOT_PORTABLE(pread)
372 #define pwrite __WINE_NOT_PORTABLE(pwrite)
373 #define statfs __WINE_NOT_PORTABLE(statfs)
374 #define strcasecmp __WINE_NOT_PORTABLE(strcasecmp)
375 #define strerror __WINE_NOT_PORTABLE(strerror)
376 #define strncasecmp __WINE_NOT_PORTABLE(strncasecmp)
377 #define usleep __WINE_NOT_PORTABLE(usleep)
379 #endif /* NO_LIBWINE */
381 #endif /* !defined(__WINE_WINE_PORT_H) */