Check for and use chsize instead of ftruncate if present.
[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 <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
42
43
44 /****************************************************************
45  * Type definitions
46  */
47
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
63
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;
70 };
71 #endif /* !defined(HAVE_GETNETBYADDR) && !defined(HAVE_GETNETBYNAME) */
72
73 #if !defined(HAVE_GETPROTOBYNAME) && !defined(HAVE_GETPROTOBYNUMBER)
74 struct  protoent {
75   char  *p_name;
76   char **p_aliases;
77   int    p_proto;
78 };
79 #endif /* !defined(HAVE_GETPROTOBYNAME) && !defined(HAVE_GETPROTOBYNUMBER) */
80
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 */
88 };
89 # else /* defined(__BEOS__) */
90 struct statfs;
91 # endif /* defined(__BEOS__) */
92 #endif /* !defined(HAVE_STATFS) */
93
94
95 /****************************************************************
96  * Macro definitions
97  */
98
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
106
107 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
108 #define ftruncate chsize
109 #endif
110
111 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
112 #define popen _popen
113 #endif
114
115 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
116 #define pclose _pclose
117 #endif
118
119 #ifndef S_ISLNK
120 # define S_ISLNK(mod) (0)
121 #endif /* S_ISLNK */
122
123 /* So we open files in 64 bit access mode on Linux */
124 #ifndef O_LARGEFILE
125 # define O_LARGEFILE 0
126 #endif
127
128 /* Macros to define assembler functions somewhat portably */
129
130 #ifdef NEED_UNDERSCORE_PREFIX
131 # define __ASM_NAME(name) "_" name
132 #else
133 # define __ASM_NAME(name) name
134 #endif
135
136 #ifdef NEED_TYPE_IN_DEF
137 # define __ASM_FUNC(name) ".def " __ASM_NAME(name) "; .scl 2; .type 32; .endef"
138 #else
139 # define __ASM_FUNC(name) ".type " __ASM_NAME(name) ",@function"
140 #endif
141
142 #ifdef __GNUC__
143 # define __ASM_GLOBAL_FUNC(name,code) \
144       __asm__( ".align 4\n\t" \
145                ".globl " __ASM_NAME(#name) "\n\t" \
146                __ASM_FUNC(#name) "\n" \
147                __ASM_NAME(#name) ":\n\t" \
148                code );
149 #else  /* __GNUC__ */
150 # define __ASM_GLOBAL_FUNC(name,code) \
151       void __asm_dummy_##name(void) { \
152           asm( ".align 4\n\t" \
153                ".globl " __ASM_NAME(#name) "\n\t" \
154                __ASM_FUNC(#name) "\n" \
155                __ASM_NAME(#name) ":\n\t" \
156                code ); \
157       }
158 #endif  /* __GNUC__ */
159
160
161 /* Macros to access unaligned or wrong-endian WORDs and DWORDs. */
162
163 #define PUT_WORD(ptr, w)  (*(WORD *)(ptr) = (w))
164 #define GET_WORD(ptr)     (*(WORD *)(ptr))
165 #define PUT_DWORD(ptr, d) (*(DWORD *)(ptr) = (d))
166 #define GET_DWORD(ptr)    (*(DWORD *)(ptr))
167
168 #define PUT_LE_WORD(ptr, w) \
169         do { ((BYTE *)(ptr))[0] = LOBYTE(w); \
170              ((BYTE *)(ptr))[1] = HIBYTE(w); } while (0)
171 #define GET_LE_WORD(ptr) \
172         MAKEWORD( ((BYTE *)(ptr))[0], \
173                   ((BYTE *)(ptr))[1] )
174 #define PUT_LE_DWORD(ptr, d) \
175         do { PUT_LE_WORD(&((WORD *)(ptr))[0], LOWORD(d)); \
176              PUT_LE_WORD(&((WORD *)(ptr))[1], HIWORD(d)); } while (0)
177 #define GET_LE_DWORD(ptr) \
178         ((DWORD)MAKELONG( GET_LE_WORD(&((WORD *)(ptr))[0]), \
179                           GET_LE_WORD(&((WORD *)(ptr))[1]) ))
180
181 #define PUT_BE_WORD(ptr, w) \
182         do { ((BYTE *)(ptr))[1] = LOBYTE(w); \
183              ((BYTE *)(ptr))[0] = HIBYTE(w); } while (0)
184 #define GET_BE_WORD(ptr) \
185         MAKEWORD( ((BYTE *)(ptr))[1], \
186                   ((BYTE *)(ptr))[0] )
187 #define PUT_BE_DWORD(ptr, d) \
188         do { PUT_BE_WORD(&((WORD *)(ptr))[1], LOWORD(d)); \
189              PUT_BE_WORD(&((WORD *)(ptr))[0], HIWORD(d)); } while (0)
190 #define GET_BE_DWORD(ptr) \
191         ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
192                           GET_BE_WORD(&((WORD *)(ptr))[0]) ))
193
194 #if defined(ALLOW_UNALIGNED_ACCESS)
195 #define PUT_UA_WORD(ptr, w)  PUT_WORD(ptr, w)
196 #define GET_UA_WORD(ptr)     GET_WORD(ptr)
197 #define PUT_UA_DWORD(ptr, d) PUT_DWORD(ptr, d)
198 #define GET_UA_DWORD(ptr)    GET_DWORD(ptr)
199 #elif defined(WORDS_BIGENDIAN)
200 #define PUT_UA_WORD(ptr, w)  PUT_BE_WORD(ptr, w)
201 #define GET_UA_WORD(ptr)     GET_BE_WORD(ptr)
202 #define PUT_UA_DWORD(ptr, d) PUT_BE_DWORD(ptr, d)
203 #define GET_UA_DWORD(ptr)    GET_BE_DWORD(ptr)
204 #else
205 #define PUT_UA_WORD(ptr, w)  PUT_LE_WORD(ptr, w)
206 #define GET_UA_WORD(ptr)     GET_LE_WORD(ptr)
207 #define PUT_UA_DWORD(ptr, d) PUT_LE_DWORD(ptr, d)
208 #define GET_UA_DWORD(ptr)    GET_LE_DWORD(ptr)
209 #endif
210
211
212 /****************************************************************
213  * Function definitions (only when using libwine)
214  */
215
216 #ifndef NO_LIBWINE
217
218 #if !defined(HAVE_CLONE) && defined(linux)
219 int clone(int (*fn)(void *arg), void *stack, int flags, void *arg);
220 #endif /* !defined(HAVE_CLONE) && defined(linux) */
221
222 #ifndef HAVE_GETNETBYADDR
223 struct netent *getnetbyaddr(unsigned long net, int type);
224 #endif /* defined(HAVE_GETNETBYNAME) */
225
226 #ifndef HAVE_GETNETBYNAME
227 struct netent *getnetbyname(const char *name);
228 #endif /* defined(HAVE_GETNETBYNAME) */
229
230 #ifndef HAVE_GETPAGESIZE
231 size_t getpagesize(void);
232 #endif  /* HAVE_GETPAGESIZE */
233
234 #ifndef HAVE_GETPROTOBYNAME
235 struct protoent *getprotobyname(const char *name);
236 #endif /* !defined(HAVE_GETPROTOBYNAME) */
237
238 #ifndef HAVE_GETPROTOBYNUMBER
239 struct protoent *getprotobynumber(int proto);
240 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
241
242 #ifndef HAVE_GETSERVBYPORT
243 struct servent *getservbyport(int port, const char *proto);
244 #endif /* !defined(HAVE_GETSERVBYPORT) */
245
246 #ifndef HAVE_GETSOCKOPT
247 int getsockopt(int socket, int level, int option_name, void *option_value, size_t *option_len);
248 #endif /* !defined(HAVE_GETSOCKOPT) */
249
250 #ifndef HAVE_INET_NETWORK
251 unsigned long inet_network(const char *cp);
252 #endif /* !defined(HAVE_INET_NETWORK) */
253
254 #ifndef HAVE_LSTAT
255 int lstat(const char *file_name, struct stat *buf);
256 #endif /* HAVE_LSTAT */
257
258 #ifndef HAVE_MEMMOVE
259 void *memmove(void *dest, const void *src, unsigned int len);
260 #endif /* !defined(HAVE_MEMMOVE) */
261
262 #ifndef HAVE_PREAD
263 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
264 #endif /* HAVE_PREAD */
265
266 #ifndef HAVE_PWRITE
267 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
268 #endif /* HAVE_PWRITE */
269
270 #ifndef HAVE_STATFS
271 int statfs(const char *name, struct statfs *info);
272 #endif /* !defined(HAVE_STATFS) */
273
274 #ifndef HAVE_STRNCASECMP
275 # ifndef HAVE__STRNICMP
276 int strncasecmp(const char *str1, const char *str2, size_t n);
277 # else
278 # define strncasecmp _strnicmp
279 # endif
280 #endif /* !defined(HAVE_STRNCASECMP) */
281
282 #ifndef HAVE_STRERROR
283 const char *strerror(int err);
284 #endif /* !defined(HAVE_STRERROR) */
285
286 #ifndef HAVE_STRCASECMP
287 # ifndef HAVE__STRICMP
288 int strcasecmp(const char *str1, const char *str2);
289 # else
290 # define strcasecmp _stricmp
291 # endif
292 #endif /* !defined(HAVE_STRCASECMP) */
293
294 #ifndef HAVE_USLEEP
295 int usleep (unsigned int useconds);
296 #endif /* !defined(HAVE_USLEEP) */
297
298 /* Interlocked functions */
299
300 #if defined(__i386__) && defined(__GNUC__)
301
302 inline static long interlocked_cmpxchg( long *dest, long xchg, long compare )
303 {
304     long ret;
305     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
306                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
307     return ret;
308 }
309
310 inline static void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
311 {
312     void *ret;
313     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
314                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
315     return ret;
316 }
317
318 inline static long interlocked_xchg( long *dest, long val )
319 {
320     long ret;
321     __asm__ __volatile__( "lock; xchgl %0,(%1)"
322                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
323     return ret;
324 }
325
326 inline static void *interlocked_xchg_ptr( void **dest, void *val )
327 {
328     void *ret;
329     __asm__ __volatile__( "lock; xchgl %0,(%1)"
330                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
331     return ret;
332 }
333
334 inline static long interlocked_xchg_add( long *dest, long incr )
335 {
336     long ret;
337     __asm__ __volatile__( "lock; xaddl %0,(%1)"
338                           : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
339     return ret;
340 }
341
342 #else  /* __i386___ && __GNUC__ */
343
344 extern long interlocked_cmpxchg( long *dest, long xchg, long compare );
345 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
346 extern long interlocked_xchg( long *dest, long val );
347 extern void *interlocked_xchg_ptr( void **dest, void *val );
348 extern long interlocked_xchg_add( long *dest, long incr );
349
350 #endif  /* __i386___ && __GNUC__ */
351
352 #else /* NO_LIBWINE */
353
354 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
355
356 #define clone             __WINE_NOT_PORTABLE(clone)
357 #define getnetbyaddr      __WINE_NOT_PORTABLE(getnetbyaddr)
358 #define getnetbyname      __WINE_NOT_PORTABLE(getnetbyname)
359 #define getpagesize       __WINE_NOT_PORTABLE(getpagesize)
360 #define getprotobyname    __WINE_NOT_PORTABLE(getprotobyname)
361 #define getprotobynumber  __WINE_NOT_PORTABLE(getprotobynumber)
362 #define getservbyport     __WINE_NOT_PORTABLE(getservbyport)
363 #define getsockopt        __WINE_NOT_PORTABLE(getsockopt)
364 #define inet_network      __WINE_NOT_PORTABLE(inet_network)
365 #define lstat             __WINE_NOT_PORTABLE(lstat)
366 #define memmove           __WINE_NOT_PORTABLE(memmove)
367 #define pread             __WINE_NOT_PORTABLE(pread)
368 #define pwrite            __WINE_NOT_PORTABLE(pwrite)
369 #define statfs            __WINE_NOT_PORTABLE(statfs)
370 #define strcasecmp        __WINE_NOT_PORTABLE(strcasecmp)
371 #define strerror          __WINE_NOT_PORTABLE(strerror)
372 #define strncasecmp       __WINE_NOT_PORTABLE(strncasecmp)
373 #define usleep            __WINE_NOT_PORTABLE(usleep)
374
375 #endif /* NO_LIBWINE */
376
377 #endif /* !defined(__WINE_WINE_PORT_H) */