- distribute peers equally among island nodes on SuperMUC
[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 off_t _win_lseek(int fildes, off_t offset, int whence);
562 int _win_ftruncate(int fildes, off_t length);
563 void _win_gettimeofday(struct timeval *tp, void *tzp);
564 int _win_kill(pid_t pid, int sig);
565 int _win_pipe(int *phandles);
566 intptr_t _win_mkfifo(const char *path, mode_t mode);
567 int _win_rmdir(const char *path);
568 int _win_access( const char *path, int mode );
569 int _win_chmod(const char *filename, int pmode);
570 char *realpath(const char *file_name, char *resolved_name);
571 long _win_random(void);
572 void _win_srandom(unsigned int seed);
573 int _win_remove(const char *path);
574 int _win_rename(const char *oldname, const char *newname);
575 int _win_stat(const char *path, struct stat *buffer);
576 int _win_stat64(const char *path, struct stat64 *buffer);
577 long _win_sysconf(int name);
578 int _win_unlink(const char *filename);
579 int _win_write(int fildes, const void *buf, size_t nbyte);
580 int _win_read(int fildes, void *buf, size_t nbyte);
581 size_t _win_fwrite(const void *buffer, size_t size, size_t count, FILE *stream);
582 size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream );
583 int _win_symlink(const char *path1, const char *path2);
584 void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
585                 unsigned long long offset);
586 int _win_msync(void *start, size_t length, int flags);
587 int _win_munmap(void *start, size_t length);
588 int _win_lstat(const char *path, struct stat *buf);
589 int _win_lstat64(const char *path, struct stat64 *buf);
590 int _win_readlink(const char *path, char *buf, size_t bufsize);
591 int _win_accept(int s, struct sockaddr *addr, int *addrlen);
592
593 int _win_printf(const char *format,...);
594 int _win_wprintf(const wchar_t *format, ...);
595
596 int _win_fprintf(FILE *f,const char *format,...);
597 int _win_fwprintf(FILE *f,const wchar_t *format, ...);
598
599 int _win_vprintf(const char *format, va_list ap);
600 int _win_vfwprintf(FILE *stream, const wchar_t *format, va_list arg_ptr);
601
602 int _win_vfprintf(FILE *stream, const char *format, va_list arg_ptr);
603 int _win_vwprintf(const wchar_t *format, va_list ap);
604
605 int _win_vsprintf(char *dest,const char *format, va_list arg_ptr);
606 int _win_vswprintf(wchar_t *dest, const wchar_t *format, va_list arg_ptr);
607
608 int _win_vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr);
609 int _win_vsnwprintf(wchar_t* wstr, size_t size, const wchar_t *format, va_list arg_ptr);
610
611 int _win_snprintf(char *str,size_t size,const char *format,...);
612 int _win_snwprintf(wchar_t *str, size_t size, const wchar_t *format, ...);
613
614 int _win_sprintf(char *dest,const char *format,...);
615 int _win_swprintf(wchar_t *dest, const wchar_t *format, ...);
616
617 int _win_vsscanf(const char* str, const char* format, va_list arg_ptr);
618 int _win_vswscanf(const wchar_t* wstr, const wchar_t* format, va_list arg_ptr);
619
620 int _win_sscanf(const char *str, const char *format, ...);
621 int _win_swscanf(const wchar_t *wstr, const wchar_t *format, ...);
622
623 int _win_vfscanf(FILE *stream, const char *format, va_list arg_ptr);
624 int _win_vfwscanf(FILE *stream, const wchar_t *format, va_list arg_ptr);
625
626 int _win_vscanf(const char *format, va_list arg_ptr);
627 int _win_vwscanf(const wchar_t *format, va_list arg_ptr);
628
629 int _win_scanf(const char *format, ...);
630 int _win_wscanf(const wchar_t *format, ...);
631
632 int _win_fscanf(FILE *stream, const char *format, ...);
633 int _win_fwscanf(FILE *stream, const wchar_t *format, ...);
634
635
636 pid_t _win_waitpid(pid_t pid, int *stat_loc, int options);
637 int _win_bind(int s, const struct sockaddr *name, int namelen);
638 int _win_connect(int s,const struct sockaddr *name, int namelen);
639 int _win_getpeername(int s, struct sockaddr *name,
640                 int *namelen);
641 int _win_getsockname(int s, struct sockaddr *name,
642                 int *namelen);
643 int _win_getsockopt(int s, int level, int optname, char *optval,
644                                 int *optlen);
645 int _win_listen(int s, int backlog);
646 int _win_recv(int s, char *buf, int len, int flags);
647 int _win_recvfrom(int s, void *buf, int len, int flags,
648              struct sockaddr *from, int *fromlen);
649 int _win_select(int max_fd, fd_set * rfds, fd_set * wfds, fd_set * efds,
650                 const struct timeval *tv);
651 int _win_send(int s, const char *buf, int len, int flags);
652 int _win_sendto(int s, const char *buf, int len, int flags,
653                 const struct sockaddr *to, int tolen);
654 int _win_setsockopt(int s, int level, int optname, const void *optval,
655                     int optlen);
656 int _win_shutdown(int s, int how);
657 int _win_socket(int af, int type, int protocol);
658 struct hostent *_win_gethostbyaddr(const char *addr, int len, int type);
659 struct hostent *_win_gethostbyname(const char *name);
660 struct hostent *gethostbyname2(const char *name, int af);
661 char *_win_strerror(int errnum);
662 int IsWinNT();
663 char *index(const char *s, int c);
664
665 #if !HAVE_STRNDUP
666 char *strndup (const char *s, size_t n);
667 #endif
668 #if !HAVE_STRNLEN && (!defined(__MINGW64_VERSION_MAJOR) || !defined(_INC_STRING))
669 size_t strnlen (const char *str, size_t maxlen);
670 #endif
671 #if !HAVE_STRTOK_R
672 char *strtok_r (char *ptr, const char *sep, char **end);
673 #endif
674 char *stpcpy(char *dest, const char *src);
675 char *strcasestr(const char *haystack_start, const char *needle_start);
676 #ifndef __MINGW64_VERSION_MAJOR
677 #define strcasecmp(a, b) stricmp(a, b)
678 #define strncasecmp(a, b, c) strnicmp(a, b, c)
679 #endif
680 #ifndef wcscasecmp
681 #define wcscasecmp(a, b) wcsicmp(a, b)
682 #endif
683 #ifndef wcsncasecmp
684 #define wcsncasecmp(a, b, c) wcsnicmp(a, b, c)
685 #endif
686 #endif /* WINDOWS */
687
688 #ifndef WINDOWS
689  #define DIR_SEPARATOR '/'
690  #define DIR_SEPARATOR_STR "/"
691  #define PATH_SEPARATOR ':'
692  #define PATH_SEPARATOR_STR ":"
693  #define NEWLINE "\n"
694
695 #ifdef ENABLE_NLS
696  #define BINDTEXTDOMAIN(d, n) bindtextdomain(d, n)
697 #endif
698  #define CREAT(p, m) creat(p, m)
699  #define PLIBC_CTIME(c) ctime(c)
700  #define CTIME_R(c, b) ctime_r(c, b)
701  #undef FOPEN
702  #define FOPEN(f, m) fopen(f, m)
703  #define FCLOSE(f) fclose(f)
704  #define FTRUNCATE(f, l) ftruncate(f, l)
705  #define OPENDIR(d) opendir(d)
706  #define CLOSEDIR(d) closedir(d)
707  #define READDIR(d) readdir(d)
708  #define OPEN open
709  #define CHDIR(d) chdir(d)
710  #define CLOSE(f) close(f)
711  #define LSEEK(f, o, w) lseek(f, o, w)
712  #define RMDIR(f) rmdir(f)
713  #define ACCESS(p, m) access(p, m)
714  #define CHMOD(f, p) chmod(f, p)
715  #define FSTAT(h, b) fstat(h, b)
716  #define PLIBC_KILL(p, s) kill(p, s)
717  #define PIPE(h) pipe(h)
718  #define REMOVE(p) remove(p)
719  #define RENAME(o, n) rename(o, n)
720  #define STAT(p, b) stat(p, b)
721  #define STAT64(p, b) stat64(p, b)
722  #define SYSCONF(n) sysconf(n)
723  #define UNLINK(f) unlink(f)
724  #define WRITE(f, b, n) write(f, b, n)
725  #define READ(f, b, n) read(f, b, n)
726  #define GN_FREAD(b, s, c, f) fread(b, s, c, f)
727  #define GN_FWRITE(b, s, c, f) fwrite(b, s, c, f)
728  #define SYMLINK(a, b) symlink(a, b)
729  #define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o)
730  #define MKFIFO(p, m) mkfifo(p, m)
731  #define MSYNC(s, l, f) msync(s, l, f)
732  #define MUNMAP(s, l) munmap(s, l)
733  #define STRERROR(i) strerror(i)
734  #define RANDOM() random()
735  #define SRANDOM(s) srandom(s)
736  #define READLINK(p, b, s) readlink(p, b, s)
737  #define LSTAT(p, b) lstat(p, b)
738  #define LSTAT64(p, b) lstat64(p, b)
739  #define PRINTF printf
740  #define FPRINTF fprintf
741  #define VPRINTF(f, a) vprintf(f, a)
742  #define VFPRINTF(s, f, a) vfprintf(s, f, a)
743  #define VSPRINTF(d, f, a) vsprintf(d, f, a)
744  #define VSNPRINTF(str, size, fmt, a) vsnprintf(str, size, fmt, a)
745  #define _REAL_SNPRINTF snprintf
746  #define SPRINTF sprintf
747  #define VSSCANF(s, f, a) vsscanf(s, f, a)
748  #define SSCANF sscanf
749  #define VFSCANF(s, f, a) vfscanf(s, f, a)
750  #define VSCANF(f, a) vscanf(f, a)
751  #define SCANF scanf
752  #define FSCANF fscanf
753  #define WAITPID(p, s, o) waitpid(p, s, o)
754  #define ACCEPT(s, a, l) accept(s, a, l)
755  #define BIND(s, n, l) bind(s, n, l)
756  #define CONNECT(s, n, l) connect(s, n, l)
757  #define GETPEERNAME(s, n, l) getpeername(s, n, l)
758  #define GETSOCKNAME(s, n, l) getsockname(s, n, l)
759  #define GETSOCKOPT(s, l, o, v, p) getsockopt(s, l, o, v, p)
760  #define LISTEN(s, b) listen(s, b)
761  #define RECV(s, b, l, f) recv(s, b, l, f)
762  #define RECVFROM(s, b, l, f, r, o) recvfrom(s, b, l, f, r, o)
763  #define SELECT(n, r, w, e, t) select(n, r, w, e, t)
764  #define SEND(s, b, l, f) send(s, b, l, f)
765  #define SENDTO(s, b, l, f, o, n) sendto(s, b, l, f, o, n)
766  #define SETSOCKOPT(s, l, o, v, n) setsockopt(s, l, o, v, n)
767  #define SHUTDOWN(s, h) shutdown(s, h)
768  #define SOCKET(a, t, p) socket(a, t, p)
769  #define GETHOSTBYADDR(a, l, t) gethostbyaddr(a, l, t)
770  #define GETHOSTBYNAME(n) gethostbyname(n)
771  #define GETTIMEOFDAY(t, n) gettimeofday(t, n)
772  #define INSQUE(e, p) insque(e, p)
773  #define REMQUE(e) remque(e)
774  #define HSEARCH(i, a) hsearch(i, a)
775  #define HCREATE(n) hcreate(n)
776  #define HDESTROY() hdestroy()
777  #define HSEARCH_R(i, a, r, h) hsearch_r(i, a, r, h)
778  #define HCREATE_R(n, h) hcreate_r(n, h)
779  #define HDESTROY_R(h) hdestroy_r(h)
780  #define TSEARCH(k, r, c) tsearch(k, r, c)
781  #define TFIND(k, r, c) tfind(k, r, c)
782  #define TDELETE(k, r, c) tdelete(k, r, c)
783  #define TWALK(r, a) twalk(r, a)
784  #define TDESTROY(r, f) tdestroy(r, f)
785  #define LFIND(k, b, n, s, c) lfind(k, b, n, s, c)
786  #define LSEARCH(k, b, n, s, c) lsearch(k, b, n, s, c)
787 #else
788  #define DIR_SEPARATOR '\\'
789  #define DIR_SEPARATOR_STR "\\"
790  #define PATH_SEPARATOR ';'
791  #define PATH_SEPARATOR_STR ";"
792  #define NEWLINE "\r\n"
793
794 #ifdef ENABLE_NLS
795  #define BINDTEXTDOMAIN(d, n) _win_bindtextdomain(d, n)
796 #endif
797  #define CREAT(p, m) _win_creat(p, m)
798  #define PLIBC_CTIME(c) _win_ctime(c)
799  #define CTIME_R(c, b) _win_ctime_r(c, b)
800  #define FOPEN(f, m) _win_fopen(f, m)
801  #define FCLOSE(f) _win_fclose(f)
802  #define FTRUNCATE(f, l) _win_ftruncate(f, l)
803  #define OPENDIR(d) _win_opendir(d)
804  #define CLOSEDIR(d) _win_closedir(d)
805  #define READDIR(d) _win_readdir(d)
806  #define OPEN _win_open
807  #define CHDIR(d) _win_chdir(d)
808  #define CLOSE(f) _win_close(f)
809  #define PLIBC_KILL(p, s) _win_kill(p, s)
810  #define LSEEK(f, o, w) _win_lseek(f, o, w)
811  #define FSTAT(h, b) _win_fstat(h, b)
812  #define RMDIR(f) _win_rmdir(f)
813  #define ACCESS(p, m) _win_access(p, m)
814  #define CHMOD(f, p) _win_chmod(f, p)
815  #define PIPE(h) _win_pipe(h)
816  #define RANDOM() _win_random()
817  #define SRANDOM(s) _win_srandom(s)
818  #define REMOVE(p) _win_remove(p)
819  #define RENAME(o, n) _win_rename(o, n)
820  #define STAT(p, b) _win_stat(p, b)
821  #define STAT64(p, b) _win_stat64(p, b)
822  #define SYSCONF(n) _win_sysconf(n)
823  #define UNLINK(f) _win_unlink(f)
824  #define WRITE(f, b, n) _win_write(f, b, n)
825  #define READ(f, b, n) _win_read(f, b, n)
826  #define GN_FREAD(b, s, c, f) _win_fread(b, s, c, f)
827  #define GN_FWRITE(b, s, c, f) _win_fwrite(b, s, c, f)
828  #define SYMLINK(a, b) _win_symlink(a, b)
829  #define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o)
830  #define MKFIFO(p, m) _win_mkfifo(p, m)
831  #define MSYNC(s, l, f) _win_msync(s, l, f)
832  #define MUNMAP(s, l) _win_munmap(s, l)
833  #define STRERROR(i) _win_strerror(i)
834  #define READLINK(p, b, s) _win_readlink(p, b, s)
835  #define LSTAT(p, b) _win_lstat(p, b)
836  #define LSTAT64(p, b) _win_lstat64(p, b)
837  #define PRINTF(f, ...) _win_printf(f , __VA_ARGS__)
838  #define FPRINTF(fil, fmt, ...) _win_fprintf(fil, fmt, __VA_ARGS__)
839  #define VPRINTF(f, a) _win_vprintf(f, a)
840  #define VFPRINTF(s, f, a) _win_vfprintf(s, f, a)
841  #define VSPRINTF(d, f, a) _win_vsprintf(d, f, a)
842  #define VSNPRINTF(str, size, fmt, a) _win_vsnprintf(str, size, fmt, a)
843  #define _REAL_SNPRINTF(str, size, fmt, ...) _win_snprintf(str, size, fmt, __VA_ARGS__)
844  #define SPRINTF(d, f, ...) _win_sprintf(d, f, __VA_ARGS__)
845  #define VSSCANF(s, f, a) _win_vsscanf(s, f, a)
846  #define SSCANF(s, f, ...) _win_sscanf(s, f, __VA_ARGS__)
847  #define VFSCANF(s, f, a) _win_vfscanf(s, f, a)
848  #define VSCANF(f, a) _win_vscanf(f, a)
849  #define SCANF(f, ...) _win_scanf(f, __VA_ARGS__)
850  #define FSCANF(s, f, ...) _win_fscanf(s, f, __VA_ARGS__)
851  #define WAITPID(p, s, o) _win_waitpid(p, s, o)
852  #define ACCEPT(s, a, l) _win_accept(s, a, l)
853  #define BIND(s, n, l) _win_bind(s, n, l)
854  #define CONNECT(s, n, l) _win_connect(s, n, l)
855  #define GETPEERNAME(s, n, l) _win_getpeername(s, n, l)
856  #define GETSOCKNAME(s, n, l) _win_getsockname(s, n, l)
857  #define GETSOCKOPT(s, l, o, v, p) _win_getsockopt(s, l, o, v, p)
858  #define LISTEN(s, b) _win_listen(s, b)
859  #define RECV(s, b, l, f) _win_recv(s, b, l, f)
860  #define RECVFROM(s, b, l, f, r, o) _win_recvfrom(s, b, l, f, r, o)
861  #define SELECT(n, r, w, e, t) _win_select(n, r, w, e, t)
862  #define SEND(s, b, l, f) _win_send(s, b, l, f)
863  #define SENDTO(s, b, l, f, o, n) _win_sendto(s, b, l, f, o, n)
864  #define SETSOCKOPT(s, l, o, v, n) _win_setsockopt(s, l, o, v, n)
865  #define SHUTDOWN(s, h) _win_shutdown(s, h)
866  #define SOCKET(a, t, p) _win_socket(a, t, p)
867  #define GETHOSTBYADDR(a, l, t) _win_gethostbyaddr(a, l, t)
868  #define GETHOSTBYNAME(n) _win_gethostbyname(n)
869  #define GETTIMEOFDAY(t, n) _win_gettimeofday(t, n)
870  #define INSQUE(e, p) _win_insque(e, p)
871  #define REMQUE(e) _win_remque(e)
872  #define HSEARCH(i, a) _win_hsearch(i, a)
873  #define HCREATE(n) _win_hcreate(n)
874  #define HDESTROY() _win_hdestroy()
875  #define HSEARCH_R(i, a, r, h) _win_hsearch_r(i, a, r, h)
876  #define HCREATE_R(n, h) _win_hcreate_r(n, h)
877  #define HDESTROY_R(h) _win_hdestroy_r(h)
878  #define TSEARCH(k, r, c) _win_tsearch(k, r, c)
879  #define TFIND(k, r, c) _win_tfind(k, r, c)
880  #define TDELETE(k, r, c) _win_tdelete(k, r, c)
881  #define TWALK(r, a) _win_twalk(r, a)
882  #define TDESTROY(r, f) _win_tdestroy(r, f)
883  #define LFIND(k, b, n, s, c) _win_lfind(k, b, n, s, c)
884  #define LSEARCH(k, b, n, s, c) _win_lsearch(k, b, n, s, c)
885 #endif
886
887 /* search.h */
888
889 /* Prototype structure for a linked-list data structure.
890    This is the type used by the `insque' and `remque' functions.  */
891
892 struct PLIBC_SEARCH_QELEM
893   {
894     struct qelem *q_forw;
895     struct qelem *q_back;
896     char q_data[1];
897   };
898
899
900 /* Insert ELEM into a doubly-linked list, after PREV.  */
901 void _win_insque (void *__elem, void *__prev);
902
903 /* Unlink ELEM from the doubly-linked list that it is in.  */
904 void _win_remque (void *__elem);
905
906
907 /* For use with hsearch(3).  */
908 typedef int (*PLIBC_SEARCH__compar_fn_t) (__const void *, __const void *);
909
910 typedef PLIBC_SEARCH__compar_fn_t _win_comparison_fn_t;
911
912 /* Action which shall be performed in the call the hsearch.  */
913 typedef enum
914   {
915     PLIBC_SEARCH_FIND,
916     PLIBC_SEARCH_ENTER
917   }
918 PLIBC_SEARCH_ACTION;
919
920 typedef struct PLIBC_SEARCH_entry
921   {
922     char *key;
923     void *data;
924   }
925 PLIBC_SEARCH_ENTRY;
926
927 /* The reentrant version has no static variables to maintain the state.
928    Instead the interface of all functions is extended to take an argument
929    which describes the current status.  */
930 typedef struct _PLIBC_SEARCH_ENTRY
931 {
932   unsigned int used;
933   PLIBC_SEARCH_ENTRY entry;
934 }
935 _PLIBC_SEARCH_ENTRY;
936
937
938 /* Family of hash table handling functions.  The functions also
939    have reentrant counterparts ending with _r.  The non-reentrant
940    functions all work on a signle internal hashing table.  */
941
942 /* Search for entry matching ITEM.key in internal hash table.  If
943    ACTION is `FIND' return found entry or signal error by returning
944    NULL.  If ACTION is `ENTER' replace existing data (if any) with
945    ITEM.data.  */
946 PLIBC_SEARCH_ENTRY *_win_hsearch (PLIBC_SEARCH_ENTRY __item, PLIBC_SEARCH_ACTION __action);
947
948 /* Create a new hashing table which will at most contain NEL elements.  */
949 int _win_hcreate (size_t __nel);
950
951 /* Destroy current internal hashing table.  */
952 void _win_hdestroy (void);
953
954 /* Data type for reentrant functions.  */
955 struct PLIBC_SEARCH_hsearch_data
956   {
957     struct _PLIBC_SEARCH_ENTRY *table;
958     unsigned int size;
959     unsigned int filled;
960   };
961
962 /* Reentrant versions which can handle multiple hashing tables at the
963    same time.  */
964 int _win_hsearch_r (PLIBC_SEARCH_ENTRY __item, PLIBC_SEARCH_ACTION __action, PLIBC_SEARCH_ENTRY **__retval,
965           struct PLIBC_SEARCH_hsearch_data *__htab);
966 int _win_hcreate_r (size_t __nel, struct PLIBC_SEARCH_hsearch_data *__htab);
967 void _win_hdestroy_r (struct PLIBC_SEARCH_hsearch_data *__htab);
968
969
970 /* The tsearch routines are very interesting. They make many
971    assumptions about the compiler.  It assumes that the first field
972    in node must be the "key" field, which points to the datum.
973    Everything depends on that.  */
974 /* For tsearch */
975 typedef enum
976 {
977   PLIBC_SEARCH_preorder,
978   PLIBC_SEARCH_postorder,
979   PLIBC_SEARCH_endorder,
980   PLIBC_SEARCH_leaf
981 }
982 PLIBC_SEARCH_VISIT;
983
984 /* Search for an entry matching the given KEY in the tree pointed to
985    by *ROOTP and insert a new element if not found.  */
986 void *_win_tsearch (__const void *__key, void **__rootp,
987           PLIBC_SEARCH__compar_fn_t __compar);
988
989 /* Search for an entry matching the given KEY in the tree pointed to
990    by *ROOTP.  If no matching entry is available return NULL.  */
991 void *_win_tfind (__const void *__key, void *__const *__rootp,
992         PLIBC_SEARCH__compar_fn_t __compar);
993
994 /* Remove the element matching KEY from the tree pointed to by *ROOTP.  */
995 void *_win_tdelete (__const void *__restrict __key,
996           void **__restrict __rootp,
997           PLIBC_SEARCH__compar_fn_t __compar);
998
999 typedef void (*PLIBC_SEARCH__action_fn_t) (__const void *__nodep, PLIBC_SEARCH_VISIT __value,
1000              int __level);
1001
1002 /* Walk through the whole tree and call the ACTION callback for every node
1003    or leaf.  */
1004 void _win_twalk (__const void *__root, PLIBC_SEARCH__action_fn_t __action);
1005
1006 /* Callback type for function to free a tree node.  If the keys are atomic
1007    data this function should do nothing.  */
1008 typedef void (*PLIBC_SEARCH__free_fn_t) (void *__nodep);
1009
1010 /* Destroy the whole tree, call FREEFCT for each node or leaf.  */
1011 void _win_tdestroy (void *__root, PLIBC_SEARCH__free_fn_t __freefct);
1012
1013
1014 /* Perform linear search for KEY by comparing by COMPAR in an array
1015    [BASE,BASE+NMEMB*SIZE).  */
1016 void *_win_lfind (__const void *__key, __const void *__base,
1017         size_t *__nmemb, size_t __size, PLIBC_SEARCH__compar_fn_t __compar);
1018
1019 /* Perform linear search for KEY by comparing by COMPAR function in
1020    array [BASE,BASE+NMEMB*SIZE) and insert entry if not found.  */
1021 void *_win_lsearch (__const void *__key, void *__base,
1022           size_t *__nmemb, size_t __size, PLIBC_SEARCH__compar_fn_t __compar);
1023
1024
1025 #ifdef __cplusplus
1026 }
1027 #endif
1028
1029
1030 #endif //_PLIBC_H_
1031
1032 /* end of plibc.h */