3 /* portable -- header loading to write portable code */
4 /* loads much more stuff than sysdep.h since the latter is in public interface*/
6 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #ifndef GRAS_PORTABLE_H
12 #define GRAS_PORTABLE_H
14 #include "gras_config.h"
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
33 # include <netinet/in.h> /* sometimes required for #include <arpa/inet.h> */
34 # include <netinet/tcp.h> /* TCP_NODELAY */
35 # include <netdb.h> /* getprotobyname() */
36 # include <arpa/inet.h> /* inet_ntoa() */
39 #ifdef HAVE_WINSOCK2_H
40 # include <winsock2.h>
41 # include <ws2tcpip.h> /* socklen_t, but doubtful */
42 # ifndef HAVE_WINSOCK_H
43 # define HAVE_WINSOCK_H
50 # define tcp_read( s, buf, len ) recv( s, buf, len, 0 )
51 # define tcp_write( s, buf, len ) send( s, buf, len, 0 )
52 # define ioctl( s, c, a ) ioctlsocket( (s), (c), (a) )
53 # define ioctl_t u_long
54 # define AC_SOCKET_INVALID ((unsigned int) ~0)
57 # define tcp_close( s ) (shutdown( s, SD_BOTH ), closesocket( s ))
59 # define tcp_close( s ) closesocket( s )
62 # define EWOULDBLOCK WSAEWOULDBLOCK
63 # define EINPROGRESS WSAEINPROGRESS
64 # define ETIMEDOUT WSAETIMEDOUT
68 # define sock_errno WSAGetLastError()
69 # define sock_errstr gras_wsa_err2string(WSAGetLastError())
71 const char *gras_wsa_err2string(int errcode);
77 # define tcp_read( s, buf, len) read( s, buf, len )
78 # define tcp_write( s, buf, len) write( s, buf, len )
79 # define sock_errno errno
80 # define sock_errstr strerror(errno)
83 # define tcp_close( s ) (shutdown( s, SHUT_RDWR ), close( s ))
85 # define tcp_close( s ) close( s )
87 #endif /* windows or unix ? */
95 #ifdef HAVE_SYS_STAT_H
96 # include <sys/stat.h>
107 #ifdef TIME_WITH_SYS_TIME
108 # include <sys/time.h>
112 # include <sys/time.h>
119 #define sleep _sleep /* else defined in stdlib.h */
127 # ifndef S_SPLINT_S /* This header drives splint into the wall */
128 # include <ucontext.h>
133 # include "xbt/context_win32.h" /* Manual reimplementation for prehistoric platforms */
137 **** string handling (parts from http://www.ijs.si/software/snprintf/)
140 /* prototype of C99 functions */
144 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
145 extern int vsnprintf(char *, size_t, const char *, va_list);
148 /* use internal functions when OS provided ones are borken */
149 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
150 extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
151 extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
152 #define snprintf portable_snprintf
153 #define vsnprintf portable_vsnprintf
156 /* prototype of GNU functions */
157 extern int asprintf (char **ptr, const char *fmt, /*args*/ ...);
158 extern int vasprintf (char **ptr, const char *fmt, va_list ap);
159 extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
160 extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
163 * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
164 * (without adding an extra check on working platforms :)
167 #ifdef PRINTF_NULL_WORKING
168 # define PRINTF_STR(a) (a)
170 # define PRINTF_STR(a) (a)?:"(null)"
173 #endif /* GRAS_PORTABLE_H */