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