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