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