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