- doc
[oweals/gnunet.git] / src / include / plibc.h
1 /*
2      This file is part of PlibC.
3      (C) 2005, 2006, 2007, 2008, 2009, 2010 Nils Durner (and other contributing authors)
4
5            This library is free software; you can redistribute it and/or
6            modify it under the terms of the GNU Lesser General Public
7            License as published by the Free Software Foundation; either
8            version 2.1 of the License, or (at your option) any later version.
9
10            This library is distributed in the hope that it will be useful,
11            but WITHOUT ANY WARRANTY; without even the implied warranty of
12            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13            Lesser General Public License for more details.
14
15            You should have received a copy of the GNU Lesser General Public
16            License along with this library; if not, write to the Free Software
17            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 /**
21  * @file include/plibc.h
22  * @brief PlibC header
23  * @attention This file is usually not installed under Unix,
24  *            so ship it with your application
25  * @version $Revision: 84 $
26  */
27
28 #ifndef _PLIBC_H_
29 #define _PLIBC_H_
30
31 #ifndef SIGALRM
32  #define SIGALRM 14
33 #endif
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #include <stddef.h>
40
41 #ifdef Q_OS_WIN32
42  #define WINDOWS 1
43 #endif
44
45 #define HAVE_PLIBC_FD 0
46
47 #ifdef WINDOWS
48
49 #if ENABLE_NLS
50   #include "langinfo.h"
51 #endif
52
53 #include <ws2tcpip.h>
54 #include <windows.h>
55 #include <sys/types.h>
56 #include <time.h>
57 #include <stdio.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <dirent.h>
61 #include <errno.h>
62 #include <stdarg.h>
63
64 #define __BYTE_ORDER BYTE_ORDER
65 #define __BIG_ENDIAN BIG_ENDIAN
66
67 /* Conflicts with our definitions */
68 #define __G_WIN32_H__
69
70 /* Convert LARGE_INTEGER to double */
71 #define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + \
72   (double)((x).LowPart))
73 #ifndef __MINGW64__
74 struct stat64
75 {
76     _dev_t st_dev;
77     _ino_t st_ino;
78     _mode_t st_mode;
79     short st_nlink;
80     short st_uid;
81     short st_gid;
82     _dev_t st_rdev;
83     __int64 st_size;
84     __time64_t st_atime;
85     __time64_t st_mtime;
86     __time64_t st_ctime;
87 };
88 #endif
89 typedef unsigned int sa_family_t;
90
91 struct sockaddr_un {
92   short sun_family; /*AF_UNIX*/
93   char sun_path[108]; /*path name */
94 };
95
96 #ifndef pid_t
97   #define pid_t DWORD
98 #endif
99
100 #ifndef error_t
101   #define error_t int
102 #endif
103
104 #ifndef WEXITSTATUS
105         #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
106 #endif
107
108 #ifndef MSG_DONTWAIT
109   #define MSG_DONTWAIT 0
110 #endif
111
112 enum
113 {
114   _SC_PAGESIZE = 30,
115   _SC_PAGE_SIZE = 30
116 };
117
118 /* Thanks to the Cygwin project */
119 #define ENOCSI 43       /* No CSI structure available */
120 #define EL2HLT 44       /* Level 2 halted */
121 #ifndef  EDEADLK
122         #define EDEADLK 45      /* Deadlock condition */
123 #endif
124 #ifndef  ENOLCK
125         #define ENOLCK 46       /* No record locks available */
126 #endif
127 #define EBADE 50        /* Invalid exchange */
128 #define EBADR 51        /* Invalid request descriptor */
129 #define EXFULL 52       /* Exchange full */
130 #define ENOANO 53       /* No anode */
131 #define EBADRQC 54      /* Invalid request code */
132 #define EBADSLT 55      /* Invalid slot */
133 #ifndef  EDEADLOCK
134         #define EDEADLOCK EDEADLK       /* File locking deadlock error */
135 #endif
136 #define EBFONT 57       /* Bad font file fmt */
137 #define ENOSTR 60       /* Device not a stream */
138 #define ENODATA 61      /* No data (for no delay io) */
139 #define ETIME 62        /* Timer expired */
140 #define ENOSR 63        /* Out of streams resources */
141 #define ENONET 64       /* Machine is not on the network */
142 #define ENOPKG 65       /* Package not installed */
143 #define EREMOTE 66      /* The object is remote */
144 #define ENOLINK 67      /* The link has been severed */
145 #define EADV 68         /* Advertise error */
146 #define ESRMNT 69       /* Srmount error */
147 #define ECOMM 70        /* Communication error on send */
148 #define EPROTO 71       /* Protocol error */
149 #define EMULTIHOP 74    /* Multihop attempted */
150 #define ELBIN 75        /* Inode is remote (not really error) */
151 #define EDOTDOT 76      /* Cross mount point (not really error) */
152 #define EBADMSG 77      /* Trying to read unreadable message */
153 #define ENOTUNIQ 80     /* Given log. name not unique */
154 #define EBADFD 81       /* f.d. invalid for this operation */
155 #define EREMCHG 82      /* Remote address changed */
156 #define ELIBACC 83      /* Can't access a needed shared lib */
157 #define ELIBBAD 84      /* Accessing a corrupted shared lib */
158 #define ELIBSCN 85      /* .lib section in a.out corrupted */
159 #define ELIBMAX 86      /* Attempting to link in too many libs */
160 #define ELIBEXEC 87     /* Attempting to exec a shared library */
161 #ifndef  ENOSYS
162         #define ENOSYS 88       /* Function not implemented */
163 #endif
164 #define ENMFILE 89      /* No more files */
165 #ifndef  ENOTEMPTY
166         #define ENOTEMPTY 90    /* Directory not empty */
167 #endif
168 #ifndef  ENAMETOOLONG
169         #define ENAMETOOLONG 91 /* File or path name too long */
170 #endif
171 #define ELOOP 92        /* Too many symbolic links */
172 #define EOPNOTSUPP 95   /* Operation not supported on transport endpoint */
173 #define EPFNOSUPPORT 96 /* Protocol family not supported */
174 #define ECONNRESET 104  /* Connection reset by peer */
175 #define ENOBUFS 105     /* No buffer space available */
176 #define EAFNOSUPPORT 106 /* Address family not supported by protocol family */
177 #define EPROTOTYPE 107  /* Protocol wrong type for socket */
178 #define ENOTSOCK 108    /* Socket operation on non-socket */
179 #define ENOPROTOOPT 109 /* Protocol not available */
180 #define ESHUTDOWN 110   /* Can't send after socket shutdown */
181 #define ECONNREFUSED 111        /* Connection refused */
182 #define EADDRINUSE 112          /* Address already in use */
183 #define ECONNABORTED 113        /* Connection aborted */
184 #define ENETUNREACH 114         /* Network is unreachable */
185 #define ENETDOWN 115            /* Network interface is not configured */
186 #ifndef  ETIMEDOUT
187         #define ETIMEDOUT 116           /* Connection timed out */
188 #endif
189 #define EHOSTDOWN 117           /* Host is down */
190 #define EHOSTUNREACH 118        /* Host is unreachable */
191 #define EINPROGRESS 119         /* Connection already in progress */
192 #define EALREADY 120            /* Socket already connected */
193 #define EDESTADDRREQ 121        /* Destination address required */
194 #define EMSGSIZE 122            /* Message too long */
195 #define EPROTONOSUPPORT 123     /* Unknown protocol */
196 #define ESOCKTNOSUPPORT 124     /* Socket type not supported */
197 #define EADDRNOTAVAIL 125       /* Address not available */
198 #define ENETRESET 126           /* Connection aborted by network */
199 #define EISCONN 127                 /* Socket is already connected */
200 #define ENOTCONN 128            /* Socket is not connected */
201 #define ETOOMANYREFS 129        /* Too many references: cannot splice */
202 #define EPROCLIM 130            /* Too many processes */
203 #define EUSERS 131                      /* Too many users */
204 #define EDQUOT 132                      /* Disk quota exceeded */
205 #define ESTALE 133          /* Unknown error */
206 #ifndef  ENOTSUP
207         #define ENOTSUP 134                 /* Not supported */
208 #endif
209 #define ENOMEDIUM 135       /* No medium (in tape drive) */
210 #define ENOSHARE 136        /* No such host or network path */
211 #define ECASECLASH 137      /* Filename exists with different case */
212 #define EWOULDBLOCK EAGAIN      /* Operation would block */
213 #define EOVERFLOW 139 /* Value too large for defined data type */
214
215 #undef HOST_NOT_FOUND
216 #define HOST_NOT_FOUND 1
217 #undef TRY_AGAIN
218 #define TRY_AGAIN 2
219 #undef NO_RECOVERY
220 #define NO_RECOVERY 3
221 #undef NO_ADDRESS
222 #define NO_ADDRESS 4
223
224 #define PROT_READ   0x1
225 #define PROT_WRITE  0x2
226 #define MAP_SHARED  0x1
227 #define MAP_PRIVATE 0x2 /* unsupported */
228 #define MAP_FIXED   0x10
229 #define MAP_ANONYMOUS 0x20 /* unsupported */
230 #define MAP_FAILED  ((void *)-1)
231
232 #define MS_ASYNC        1       /* sync memory asynchronously */
233 #define MS_INVALIDATE   2       /* invalidate the caches */
234 #define MS_SYNC         4       /* synchronous memory sync */
235
236 struct statfs
237 {
238   long f_type;                  /* type of filesystem (see below) */
239   long f_bsize;                 /* optimal transfer block size */
240   long f_blocks;                /* total data blocks in file system */
241   long f_bfree;                 /* free blocks in fs */
242   long f_bavail;                /* free blocks avail to non-superuser */
243   long f_files;                 /* total file nodes in file system */
244   long f_ffree;                 /* free file nodes in fs */
245   long f_fsid;                  /* file system id */
246   long f_namelen;               /* maximum length of filenames */
247   long f_spare[6];              /* spare for later */
248 };
249
250 extern const struct in6_addr in6addr_any;        /* :: */
251 extern const struct in6_addr in6addr_loopback;   /* ::1 */
252
253 /* Taken from the Wine project <http://www.winehq.org>
254     /wine/include/winternl.h */
255 enum SYSTEM_INFORMATION_CLASS
256 {
257   SystemBasicInformation = 0,
258   Unknown1,
259   SystemPerformanceInformation = 2,
260   SystemTimeOfDayInformation = 3, /* was SystemTimeInformation */
261   Unknown4,
262   SystemProcessInformation = 5,
263   Unknown6,
264   Unknown7,
265   SystemProcessorPerformanceInformation = 8,
266   Unknown9,
267   Unknown10,
268   SystemDriverInformation,
269   Unknown12,
270   Unknown13,
271   Unknown14,
272   Unknown15,
273   SystemHandleList,
274   Unknown17,
275   Unknown18,
276   Unknown19,
277   Unknown20,
278   SystemCacheInformation,
279   Unknown22,
280   SystemInterruptInformation = 23,
281   SystemExceptionInformation = 33,
282   SystemRegistryQuotaInformation = 37,
283   SystemLookasideInformation = 45
284 };
285
286 typedef struct
287 {
288     LARGE_INTEGER IdleTime;
289     LARGE_INTEGER KernelTime;
290     LARGE_INTEGER UserTime;
291     LARGE_INTEGER Reserved1[2];
292     ULONG Reserved2;
293 } SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
294
295 #define sleep(secs) (Sleep(secs * 1000))
296
297 /*********************** statfs *****************************/
298 /* fake block size */
299 #define FAKED_BLOCK_SIZE 512
300
301 /* linux-compatible values for fs type */
302 #define MSDOS_SUPER_MAGIC     0x4d44
303 #define NTFS_SUPER_MAGIC      0x5346544E
304
305 /*********************** End of statfs ***********************/
306
307 #define SHUT_RDWR SD_BOTH
308
309 /* Operations for flock() */
310 #define LOCK_SH  1       /* shared lock */
311 #define LOCK_EX  2       /* exclusive lock */
312 #define LOCK_NB  4       /* or'd with one of the above to prevent
313                             blocking */
314 #define LOCK_UN  8       /* remove lock */
315
316 /* Not supported under MinGW */
317 #define S_IRGRP 0
318 #define S_IWGRP 0
319 #define S_IROTH 0
320 #define S_IXGRP 0
321 #define S_IWOTH 0
322 #define S_IXOTH 0
323 #define S_ISUID 0
324 #define S_ISGID 0
325 #define S_ISVTX 0
326 #define S_IRWXG 0
327 #define S_IRWXO 0
328
329 #define SHUT_WR SD_SEND
330 #define SHUT_RD SD_RECEIVE
331 #define SHUT_RDWR SD_BOTH
332
333 #define SIGKILL 9
334 #define SIGTERM 15
335
336 #define SetErrnoFromWinError(e) _SetErrnoFromWinError(e, __FILE__, __LINE__)
337
338 BOOL _plibc_CreateShortcut(const char *pszSrc, const char *pszDest);
339 BOOL _plibc_CreateShortcutW(const wchar_t *pwszSrc, const wchar_t *pwszDest);
340 BOOL _plibc_DereferenceShortcut(char *pszShortcut);
341 BOOL _plibc_DereferenceShortcutW(wchar_t *pwszShortcut);
342 char *plibc_ChooseDir(char *pszTitle, unsigned long ulFlags);
343 wchar_t *plibc_ChooseDirW(wchar_t *pwszTitle, unsigned long ulFlags);
344 char *plibc_ChooseFile(char *pszTitle, unsigned long ulFlags);
345 wchar_t *plibc_ChooseFileW(wchar_t *pwszTitle, unsigned long ulFlags);
346
347 long QueryRegistry(HKEY hMainKey, const char *pszKey, const char *pszSubKey,
348               char *pszBuffer, long *pdLength);
349 long QueryRegistryW(HKEY hMainKey, const wchar_t *pszKey, const wchar_t *pszSubKey,
350               wchar_t *pszBuffer, long *pdLength);
351
352 BOOL __win_IsHandleMarkedAsBlocking(int hHandle);
353 void __win_SetHandleBlockingMode(int s, BOOL bBlocking);
354 void __win_DiscardHandleBlockingMode(int s);
355 int _win_isSocketValid(int s);
356 int plibc_conv_to_win_path(const char *pszUnix, char *pszWindows);
357 int plibc_conv_to_win_pathw(const wchar_t *pszUnix, wchar_t *pwszWindows);
358
359 int plibc_conv_to_win_pathwconv(const char *pszUnix, wchar_t *pwszWindows);
360 int plibc_conv_to_win_pathwconv_ex(const char *pszUnix, wchar_t *pszWindows, int derefLinks);
361
362 unsigned plibc_get_handle_count();
363
364 typedef void (*TPanicProc) (int, char *);
365 void plibc_set_panic_proc(TPanicProc proc);
366
367 int flock(int fd, int operation);
368 int fsync(int fildes);
369 int inet_pton(int af, const char *src, void *dst);
370 int inet_pton4(const char *src, u_char *dst, int pton);
371 #if USE_IPV6
372 int inet_pton6(const char *src, u_char *dst);
373 #endif
374 int truncate(const char *fname, int distance);
375 int statfs(const char *path, struct statfs *buf);
376 const char *hstrerror(int err);
377 int mkstemp(char *tmplate);
378 char *strptime (const char *buf, const char *format, struct tm *tm);
379 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
380 struct tm *gmtime_r(const time_t *clock, struct tm *result);
381
382 int plibc_init(char *pszOrg, char *pszApp);
383 int plibc_init_utf8(char *pszOrg, char *pszApp, int utf8_mode);
384 void plibc_shutdown();
385 int plibc_initialized();
386
387 void _SetErrnoFromWinError(long lWinError, char *pszCaller, int iLine);
388 void SetErrnoFromWinsockError(long lWinError);
389 void SetHErrnoFromWinError(long lWinError);
390 void SetErrnoFromHRESULT(HRESULT hRes);
391 int GetErrnoFromWinsockError(long lWinError);
392 FILE *_win_fopen(const char *filename, const char *mode);
393 int _win_fclose(FILE *);
394 DIR *_win_opendir(const char *dirname);
395 struct dirent *_win_readdir(DIR *dirp);
396 int _win_closedir(DIR *dirp);
397 int _win_open(const char *filename, int oflag, ...);
398 #ifdef ENABLE_NLS
399 char *_win_bindtextdomain(const char *domainname, const char *dirname);
400 #endif
401 int _win_chdir(const char *path);
402 int _win_close(int fd);
403 int _win_creat(const char *path, mode_t mode);
404 char *_win_ctime(const time_t *clock);
405 char *_win_ctime_r(const time_t *clock, char *buf);
406 int _win_fstat(int handle, struct stat *buffer);
407 int _win_ftruncate(int fildes, off_t length);
408 void _win_gettimeofday(struct timeval *tp, void *tzp);
409 int _win_kill(pid_t pid, int sig);
410 int _win_pipe(int *phandles);
411 int _win_rmdir(const char *path);
412 int _win_access( const char *path, int mode );
413 int _win_chmod(const char *filename, int pmode);
414 char *realpath(const char *file_name, char *resolved_name);
415 long _win_random(void);
416 void _win_srandom(unsigned int seed);
417 int _win_remove(const char *path);
418 int _win_rename(const char *oldname, const char *newname);
419 int _win_stat(const char *path, struct stat *buffer);
420 int _win_stat64(const char *path, struct stat64 *buffer);
421 long _win_sysconf(int name);
422 int _win_unlink(const char *filename);
423 int _win_write(int fildes, const void *buf, size_t nbyte);
424 int _win_read(int fildes, void *buf, size_t nbyte);
425 size_t _win_fwrite(const void *buffer, size_t size, size_t count, FILE *stream);
426 size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream );
427 int _win_symlink(const char *path1, const char *path2);
428 void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
429                 unsigned long long offset);
430 int _win_msync(void *start, size_t length, int flags);
431 int _win_munmap(void *start, size_t length);
432 int _win_lstat(const char *path, struct stat *buf);
433 int _win_lstat64(const char *path, struct stat64 *buf);
434 int _win_readlink(const char *path, char *buf, size_t bufsize);
435 int _win_accept(int s, struct sockaddr *addr, int *addrlen);
436
437 int _win_printf(const char *format,...);
438 int _win_wprintf(const wchar_t *format, ...);
439
440 int _win_fprintf(FILE *f,const char *format,...);
441 int _win_fwprintf(FILE *f,const wchar_t *format, ...);
442
443 int _win_vprintf(const char *format, va_list ap);
444 int _win_vfwprintf(FILE *stream, const wchar_t *format, va_list arg_ptr);
445
446 int _win_vfprintf(FILE *stream, const char *format, va_list arg_ptr);
447 int _win_vwprintf(const wchar_t *format, va_list ap);
448
449 int _win_vsprintf(char *dest,const char *format, va_list arg_ptr);
450 int _win_vswprintf(wchar_t *dest, const wchar_t *format, va_list arg_ptr);
451
452 int _win_vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr);
453 int _win_vsnwprintf(wchar_t* wstr, size_t size, const wchar_t *format, va_list arg_ptr);
454
455 int _win_snprintf(char *str,size_t size,const char *format,...);
456 int _win_snwprintf(wchar_t *str, size_t size, const wchar_t *format, ...);
457
458 int _win_sprintf(char *dest,const char *format,...);
459 int _win_swprintf(wchar_t *dest, const wchar_t *format, ...);
460
461 int _win_vsscanf(const char* str, const char* format, va_list arg_ptr);
462 int _win_vswscanf(const wchar_t* wstr, const wchar_t* format, va_list arg_ptr);
463
464 int _win_sscanf(const char *str, const char *format, ...);
465 int _win_swscanf(const wchar_t *wstr, const wchar_t *format, ...);
466
467 int _win_vfscanf(FILE *stream, const char *format, va_list arg_ptr);
468 int _win_vfwscanf(FILE *stream, const wchar_t *format, va_list arg_ptr);
469
470 int _win_vscanf(const char *format, va_list arg_ptr);
471 int _win_vwscanf(const wchar_t *format, va_list arg_ptr);
472
473 int _win_scanf(const char *format, ...);
474 int _win_wscanf(const wchar_t *format, ...);
475
476 int _win_fscanf(FILE *stream, const char *format, ...);
477 int _win_fwscanf(FILE *stream, const wchar_t *format, ...);
478
479
480 pid_t _win_waitpid(pid_t pid, int *stat_loc, int options);
481 int _win_bind(int s, const struct sockaddr *name, int namelen);
482 int _win_connect(int s,const struct sockaddr *name, int namelen);
483 int _win_getpeername(int s, struct sockaddr *name,
484                 int *namelen);
485 int _win_getsockname(int s, struct sockaddr *name,
486                 int *namelen);
487 int _win_getsockopt(int s, int level, int optname, char *optval,
488                                 int *optlen);
489 int _win_listen(int s, int backlog);
490 int _win_recv(int s, char *buf, int len, int flags);
491 int _win_recvfrom(int s, void *buf, int len, int flags,
492              struct sockaddr *from, int *fromlen);
493 int _win_select(int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds,
494                 const struct timeval *tv);
495 int _win_send(int s, const char *buf, int len, int flags);
496 int _win_sendto(int s, const char *buf, int len, int flags,
497                 const struct sockaddr *to, int tolen);
498 int _win_setsockopt(int s, int level, int optname, const void *optval,
499                     int optlen);
500 int _win_shutdown(int s, int how);
501 int _win_socket(int af, int type, int protocol);
502 struct hostent *_win_gethostbyaddr(const char *addr, int len, int type);
503 struct hostent *_win_gethostbyname(const char *name);
504 struct hostent *gethostbyname2(const char *name, int af);
505 char *_win_strerror(int errnum);
506 int IsWinNT();
507 char *index(const char *s, int c);
508
509 #if !HAVE_STRNDUP
510 char *strndup (const char *s, size_t n);
511 #endif
512 #if !HAVE_STRNLEN
513 size_t strnlen (const char *str, size_t maxlen);
514 #endif
515 char *stpcpy(char *dest, const char *src);
516 char *strcasestr(const char *haystack_start, const char *needle_start);
517 #ifndef __MINGW64__
518 #define strcasecmp(a, b) stricmp(a, b)
519 #define wcscasecmp(a, b) wcsicmp(a, b)
520 #define strncasecmp(a, b, c) strnicmp(a, b, c)
521 #define wcsncasecmp(a, b, c) wcsnicmp(a, b, c)
522 #endif
523 #endif /* WINDOWS */
524
525 #ifndef WINDOWS
526  #define DIR_SEPARATOR '/'
527  #define DIR_SEPARATOR_STR "/"
528  #define PATH_SEPARATOR ':'
529  #define PATH_SEPARATOR_STR ":"
530  #define NEWLINE "\n"
531
532 #ifdef ENABLE_NLS
533  #define BINDTEXTDOMAIN(d, n) bindtextdomain(d, n)
534 #endif
535  #define CREAT(p, m) creat(p, m)
536  #define PLIBC_CTIME(c) ctime(c)
537  #define CTIME_R(c, b) ctime_r(c, b)
538  #undef FOPEN
539  #define FOPEN(f, m) fopen(f, m)
540  #define FCLOSE(f) fclose(f)
541  #define FTRUNCATE(f, l) ftruncate(f, l)
542  #define OPENDIR(d) opendir(d)
543  #define CLOSEDIR(d) closedir(d)
544  #define READDIR(d) readdir(d)
545  #define OPEN open
546  #define CHDIR(d) chdir(d)
547  #define CLOSE(f) close(f)
548  #define LSEEK(f, o, w) lseek(f, o, w)
549  #define RMDIR(f) rmdir(f)
550  #define ACCESS(p, m) access(p, m)
551  #define CHMOD(f, p) chmod(f, p)
552  #define FSTAT(h, b) fstat(h, b)
553  #define PLIBC_KILL(p, s) kill(p, s)
554  #define PIPE(h) pipe(h)
555  #define REMOVE(p) remove(p)
556  #define RENAME(o, n) rename(o, n)
557  #define STAT(p, b) stat(p, b)
558  #define STAT64(p, b) stat64(p, b)
559  #define SYSCONF(n) sysconf(n)
560  #define UNLINK(f) unlink(f)
561  #define WRITE(f, b, n) write(f, b, n)
562  #define READ(f, b, n) read(f, b, n)
563  #define GN_FREAD(b, s, c, f) fread(b, s, c, f)
564  #define GN_FWRITE(b, s, c, f) fwrite(b, s, c, f)
565  #define SYMLINK(a, b) symlink(a, b)
566  #define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o)
567  #define MKFIFO(p, m) mkfifo(p, m)
568  #define MSYNC(s, l, f) msync(s, l, f)
569  #define MUNMAP(s, l) munmap(s, l)
570  #define STRERROR(i) strerror(i)
571  #define RANDOM() random()
572  #define SRANDOM(s) srandom(s)
573  #define READLINK(p, b, s) readlink(p, b, s)
574  #define LSTAT(p, b) lstat(p, b)
575  #define LSTAT64(p, b) lstat64(p, b)
576  #define PRINTF printf
577  #define FPRINTF fprintf
578  #define VPRINTF(f, a) vprintf(f, a)
579  #define VFPRINTF(s, f, a) vfprintf(s, f, a)
580  #define VSPRINTF(d, f, a) vsprintf(d, f, a)
581  #define VSNPRINTF(str, size, fmt, a) vsnprintf(str, size, fmt, a)
582  #define _REAL_SNPRINTF snprintf
583  #define SPRINTF sprintf
584  #define VSSCANF(s, f, a) vsscanf(s, f, a)
585  #define SSCANF sscanf
586  #define VFSCANF(s, f, a) vfscanf(s, f, a)
587  #define VSCANF(f, a) vscanf(f, a)
588  #define SCANF scanf
589  #define FSCANF fscanf
590  #define WAITPID(p, s, o) waitpid(p, s, o)
591  #define ACCEPT(s, a, l) accept(s, a, l)
592  #define BIND(s, n, l) bind(s, n, l)
593  #define CONNECT(s, n, l) connect(s, n, l)
594  #define GETPEERNAME(s, n, l) getpeername(s, n, l)
595  #define GETSOCKNAME(s, n, l) getsockname(s, n, l)
596  #define GETSOCKOPT(s, l, o, v, p) getsockopt(s, l, o, v, p)
597  #define LISTEN(s, b) listen(s, b)
598  #define RECV(s, b, l, f) recv(s, b, l, f)
599  #define RECVFROM(s, b, l, f, r, o) recvfrom(s, b, l, f, r, o)
600  #define SELECT(n, r, w, e, t) select(n, r, w, e, t)
601  #define SEND(s, b, l, f) send(s, b, l, f)
602  #define SENDTO(s, b, l, f, o, n) sendto(s, b, l, f, o, n)
603  #define SETSOCKOPT(s, l, o, v, n) setsockopt(s, l, o, v, n)
604  #define SHUTDOWN(s, h) shutdown(s, h)
605  #define SOCKET(a, t, p) socket(a, t, p)
606  #define GETHOSTBYADDR(a, l, t) gethostbyname(a, l, t)
607  #define GETHOSTBYNAME(n) gethostbyname(n)
608  #define GETTIMEOFDAY(t, n) gettimeofday(t, n)
609  #define INSQUE(e, p) insque(e, p)
610  #define REMQUE(e) remque(e)
611  #define HSEARCH(i, a) hsearch(i, a)
612  #define HCREATE(n) hcreate(n)
613  #define HDESTROY() hdestroy()
614  #define HSEARCH_R(i, a, r, h) hsearch_r(i, a, r, h)
615  #define HCREATE_R(n, h) hcreate_r(n, h)
616  #define HDESTROY_R(h) hdestroy_r(h)
617  #define TSEARCH(k, r, c) tsearch(k, r, c)
618  #define TFIND(k, r, c) tfind(k, r, c)
619  #define TDELETE(k, r, c) tdelete(k, r, c)
620  #define TWALK(r, a) twalk(r, a)
621  #define TDESTROY(r, f) tdestroy(r, f)
622  #define LFIND(k, b, n, s, c) lfind(k, b, n, s, c)
623  #define LSEARCH(k, b, n, s, c) lsearch(k, b, n, s, c)
624 #else
625  #define DIR_SEPARATOR '\\'
626  #define DIR_SEPARATOR_STR "\\"
627  #define PATH_SEPARATOR ';'
628  #define PATH_SEPARATOR_STR ";"
629  #define NEWLINE "\r\n"
630
631 #ifdef ENABLE_NLS
632  #define BINDTEXTDOMAIN(d, n) _win_bindtextdomain(d, n)
633 #endif
634  #define CREAT(p, m) _win_creat(p, m)
635  #define PLIBC_CTIME(c) _win_ctime(c)
636  #define CTIME_R(c, b) _win_ctime_r(c, b)
637  #define FOPEN(f, m) _win_fopen(f, m)
638  #define FCLOSE(f) _win_fclose(f)
639  #define FTRUNCATE(f, l) _win_ftruncate(f, l)
640  #define OPENDIR(d) _win_opendir(d)
641  #define CLOSEDIR(d) _win_closedir(d)
642  #define READDIR(d) _win_readdir(d)
643  #define OPEN _win_open
644  #define CHDIR(d) _win_chdir(d)
645  #define CLOSE(f) _win_close(f)
646  #define PLIBC_KILL(p, s) _win_kill(p, s)
647  #define LSEEK(f, o, w) _win_lseek(f, o, w)
648  #define FSTAT(h, b) _win_fstat(h, b)
649  #define RMDIR(f) _win_rmdir(f)
650  #define ACCESS(p, m) _win_access(p, m)
651  #define CHMOD(f, p) _win_chmod(f, p)
652  #define PIPE(h) _win_pipe(h)
653  #define RANDOM() _win_random()
654  #define SRANDOM(s) _win_srandom(s)
655  #define REMOVE(p) _win_remove(p)
656  #define RENAME(o, n) _win_rename(o, n)
657  #define STAT(p, b) _win_stat(p, b)
658  #define STAT64(p, b) _win_stat64(p, b)
659  #define SYSCONF(n) _win_sysconf(n)
660  #define UNLINK(f) _win_unlink(f)
661  #define WRITE(f, b, n) _win_write(f, b, n)
662  #define READ(f, b, n) _win_read(f, b, n)
663  #define GN_FREAD(b, s, c, f) _win_fread(b, s, c, f)
664  #define GN_FWRITE(b, s, c, f) _win_fwrite(b, s, c, f)
665  #define SYMLINK(a, b) _win_symlink(a, b)
666  #define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o)
667  #define MKFIFO(p, m) _win_mkfifo(p, m)
668  #define MSYNC(s, l, f) _win_msync(s, l, f)
669  #define MUNMAP(s, l) _win_munmap(s, l)
670  #define STRERROR(i) _win_strerror(i)
671  #define READLINK(p, b, s) _win_readlink(p, b, s)
672  #define LSTAT(p, b) _win_lstat(p, b)
673  #define LSTAT64(p, b) _win_lstat64(p, b)
674  #define PRINTF(f, ...) _win_printf(f , __VA_ARGS__)
675  #define FPRINTF(fil, fmt, ...) _win_fprintf(fil, fmt, __VA_ARGS__)
676  #define VPRINTF(f, a) _win_vprintf(f, a)
677  #define VFPRINTF(s, f, a) _win_vfprintf(s, f, a)
678  #define VSPRINTF(d, f, a) _win_vsprintf(d, f, a)
679  #define VSNPRINTF(str, size, fmt, a) _win_vsnprintf(str, size, fmt, a)
680  #define _REAL_SNPRINTF(str, size, fmt, ...) _win_snprintf(str, size, fmt, __VA_ARGS__)
681  #define SPRINTF(d, f, ...) _win_sprintf(d, f, __VA_ARGS__)
682  #define VSSCANF(s, f, a) _win_vsscanf(s, f, a)
683  #define SSCANF(s, f, ...) _win_sscanf(s, f, __VA_ARGS__)
684  #define VFSCANF(s, f, a) _win_vfscanf(s, f, a)
685  #define VSCANF(f, a) _win_vscanf(f, a)
686  #define SCANF(f, ...) _win_scanf(f, __VA_ARGS__)
687  #define FSCANF(s, f, ...) _win_fscanf(s, f, __VA_ARGS__)
688  #define WAITPID(p, s, o) _win_waitpid(p, s, o)
689  #define ACCEPT(s, a, l) _win_accept(s, a, l)
690  #define BIND(s, n, l) _win_bind(s, n, l)
691  #define CONNECT(s, n, l) _win_connect(s, n, l)
692  #define GETPEERNAME(s, n, l) _win_getpeername(s, n, l)
693  #define GETSOCKNAME(s, n, l) _win_getsockname(s, n, l)
694  #define GETSOCKOPT(s, l, o, v, p) _win_getsockopt(s, l, o, v, p)
695  #define LISTEN(s, b) _win_listen(s, b)
696  #define RECV(s, b, l, f) _win_recv(s, b, l, f)
697  #define RECVFROM(s, b, l, f, r, o) _win_recvfrom(s, b, l, f, r, o)
698  #define SELECT(n, r, w, e, t) _win_select(n, r, w, e, t)
699  #define SEND(s, b, l, f) _win_send(s, b, l, f)
700  #define SENDTO(s, b, l, f, o, n) _win_sendto(s, b, l, f, o, n)
701  #define SETSOCKOPT(s, l, o, v, n) _win_setsockopt(s, l, o, v, n)
702  #define SHUTDOWN(s, h) _win_shutdown(s, h)
703  #define SOCKET(a, t, p) _win_socket(a, t, p)
704  #define GETHOSTBYADDR(a, l, t) _win_gethostbyname(a, l, t)
705  #define GETHOSTBYNAME(n) _win_gethostbyname(n)
706  #define GETTIMEOFDAY(t, n) _win_gettimeofday(t, n)
707  #define INSQUE(e, p) _win_insque(e, p)
708  #define REMQUE(e) _win_remque(e)
709  #define HSEARCH(i, a) _win_hsearch(i, a)
710  #define HCREATE(n) _win_hcreate(n)
711  #define HDESTROY() _win_hdestroy()
712  #define HSEARCH_R(i, a, r, h) _win_hsearch_r(i, a, r, h)
713  #define HCREATE_R(n, h) _win_hcreate_r(n, h)
714  #define HDESTROY_R(h) _win_hdestroy_r(h)
715  #define TSEARCH(k, r, c) _win_tsearch(k, r, c)
716  #define TFIND(k, r, c) _win_tfind(k, r, c)
717  #define TDELETE(k, r, c) _win_tdelete(k, r, c)
718  #define TWALK(r, a) _win_twalk(r, a)
719  #define TDESTROY(r, f) _win_tdestroy(r, f)
720  #define LFIND(k, b, n, s, c) _win_lfind(k, b, n, s, c)
721  #define LSEARCH(k, b, n, s, c) _win_lsearch(k, b, n, s, c)
722 #endif
723
724 /* search.h */
725
726 /* Prototype structure for a linked-list data structure.
727    This is the type used by the `insque' and `remque' functions.  */
728
729 struct PLIBC_SEARCH_QELEM
730   {
731     struct qelem *q_forw;
732     struct qelem *q_back;
733     char q_data[1];
734   };
735
736
737 /* Insert ELEM into a doubly-linked list, after PREV.  */
738 void _win_insque (void *__elem, void *__prev);
739
740 /* Unlink ELEM from the doubly-linked list that it is in.  */
741 void _win_remque (void *__elem);
742
743
744 /* For use with hsearch(3).  */
745 typedef int (*PLIBC_SEARCH__compar_fn_t) (__const void *, __const void *);
746
747 typedef PLIBC_SEARCH__compar_fn_t _win_comparison_fn_t;
748
749 /* Action which shall be performed in the call the hsearch.  */
750 typedef enum
751   {
752     PLIBC_SEARCH_FIND,
753     PLIBC_SEARCH_ENTER
754   }
755 PLIBC_SEARCH_ACTION;
756
757 typedef struct PLIBC_SEARCH_entry
758   {
759     char *key;
760     void *data;
761   }
762 PLIBC_SEARCH_ENTRY;
763
764 /* The reentrant version has no static variables to maintain the state.
765    Instead the interface of all functions is extended to take an argument
766    which describes the current status.  */
767 typedef struct _PLIBC_SEARCH_ENTRY
768 {
769   unsigned int used;
770   PLIBC_SEARCH_ENTRY entry;
771 }
772 _PLIBC_SEARCH_ENTRY;
773
774
775 /* Family of hash table handling functions.  The functions also
776    have reentrant counterparts ending with _r.  The non-reentrant
777    functions all work on a signle internal hashing table.  */
778
779 /* Search for entry matching ITEM.key in internal hash table.  If
780    ACTION is `FIND' return found entry or signal error by returning
781    NULL.  If ACTION is `ENTER' replace existing data (if any) with
782    ITEM.data.  */
783 PLIBC_SEARCH_ENTRY *_win_hsearch (PLIBC_SEARCH_ENTRY __item, PLIBC_SEARCH_ACTION __action);
784
785 /* Create a new hashing table which will at most contain NEL elements.  */
786 int _win_hcreate (size_t __nel);
787
788 /* Destroy current internal hashing table.  */
789 void _win_hdestroy (void);
790
791 /* Data type for reentrant functions.  */
792 struct PLIBC_SEARCH_hsearch_data
793   {
794     struct _PLIBC_SEARCH_ENTRY *table;
795     unsigned int size;
796     unsigned int filled;
797   };
798
799 /* Reentrant versions which can handle multiple hashing tables at the
800    same time.  */
801 int _win_hsearch_r (PLIBC_SEARCH_ENTRY __item, PLIBC_SEARCH_ACTION __action, PLIBC_SEARCH_ENTRY **__retval,
802           struct PLIBC_SEARCH_hsearch_data *__htab);
803 int _win_hcreate_r (size_t __nel, struct PLIBC_SEARCH_hsearch_data *__htab);
804 void _win_hdestroy_r (struct PLIBC_SEARCH_hsearch_data *__htab);
805
806
807 /* The tsearch routines are very interesting. They make many
808    assumptions about the compiler.  It assumes that the first field
809    in node must be the "key" field, which points to the datum.
810    Everything depends on that.  */
811 /* For tsearch */
812 typedef enum
813 {
814   PLIBC_SEARCH_preorder,
815   PLIBC_SEARCH_postorder,
816   PLIBC_SEARCH_endorder,
817   PLIBC_SEARCH_leaf
818 }
819 PLIBC_SEARCH_VISIT;
820
821 /* Search for an entry matching the given KEY in the tree pointed to
822    by *ROOTP and insert a new element if not found.  */
823 void *_win_tsearch (__const void *__key, void **__rootp,
824           PLIBC_SEARCH__compar_fn_t __compar);
825
826 /* Search for an entry matching the given KEY in the tree pointed to
827    by *ROOTP.  If no matching entry is available return NULL.  */
828 void *_win_tfind (__const void *__key, void *__const *__rootp,
829         PLIBC_SEARCH__compar_fn_t __compar);
830
831 /* Remove the element matching KEY from the tree pointed to by *ROOTP.  */
832 void *_win_tdelete (__const void *__restrict __key,
833           void **__restrict __rootp,
834           PLIBC_SEARCH__compar_fn_t __compar);
835
836 typedef void (*PLIBC_SEARCH__action_fn_t) (__const void *__nodep, PLIBC_SEARCH_VISIT __value,
837              int __level);
838
839 /* Walk through the whole tree and call the ACTION callback for every node
840    or leaf.  */
841 void _win_twalk (__const void *__root, PLIBC_SEARCH__action_fn_t __action);
842
843 /* Callback type for function to free a tree node.  If the keys are atomic
844    data this function should do nothing.  */
845 typedef void (*PLIBC_SEARCH__free_fn_t) (void *__nodep);
846
847 /* Destroy the whole tree, call FREEFCT for each node or leaf.  */
848 void _win_tdestroy (void *__root, PLIBC_SEARCH__free_fn_t __freefct);
849
850
851 /* Perform linear search for KEY by comparing by COMPAR in an array
852    [BASE,BASE+NMEMB*SIZE).  */
853 void *_win_lfind (__const void *__key, __const void *__base,
854         size_t *__nmemb, size_t __size, PLIBC_SEARCH__compar_fn_t __compar);
855
856 /* Perform linear search for KEY by comparing by COMPAR function in
857    array [BASE,BASE+NMEMB*SIZE) and insert entry if not found.  */
858 void *_win_lsearch (__const void *__key, void *__base,
859           size_t *__nmemb, size_t __size, PLIBC_SEARCH__compar_fn_t __compar);
860
861
862 #ifdef __cplusplus
863 }
864 #endif
865
866
867 #endif //_PLIBC_H_
868
869 /* end of plibc.h */