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() */
37 # include <sys/types.h> /* sometimes required for fd_set */
40 #ifdef HAVE_WINSOCK2_H
41 # include <winsock2.h>
42 # include <ws2tcpip.h> /* socklen_t, but doubtful */
43 # ifndef HAVE_WINSOCK_H
44 # define HAVE_WINSOCK_H
51 # define tcp_read( s, buf, len ) recv( s, buf, len, 0 )
52 # define tcp_write( s, buf, len ) send( s, buf, len, 0 )
53 # define ioctl( s, c, a ) ioctlsocket( (s), (c), (a) )
54 # define ioctl_t u_long
55 # define AC_SOCKET_INVALID ((unsigned int) ~0)
58 # define tcp_close( s ) (shutdown( s, SD_BOTH ), closesocket( s ))
60 # define tcp_close( s ) closesocket( s )
63 # define EWOULDBLOCK WSAEWOULDBLOCK
64 # define EINPROGRESS WSAEINPROGRESS
65 # define ETIMEDOUT WSAETIMEDOUT
69 # define sock_errno WSAGetLastError()
70 # define sock_errstr gras_wsa_err2string(WSAGetLastError())
72 const char *gras_wsa_err2string(int errcode);
78 # define tcp_read( s, buf, len) read( s, buf, len )
79 # define tcp_write( s, buf, len) write( s, buf, len )
80 # define sock_errno errno
81 # define sock_errstr strerror(errno)
84 # define tcp_close( s ) (shutdown( s, SHUT_RDWR ), close( s ))
86 # define tcp_close( s ) close( s )
88 #endif /* windows or unix ? */
96 #ifdef HAVE_SYS_STAT_H
97 # include <sys/stat.h>
108 #ifdef TIME_WITH_SYS_TIME
109 # include <sys/time.h>
113 # include <sys/time.h>
120 #define sleep _sleep /* else defined in stdlib.h */
128 # ifndef S_SPLINT_S /* This header drives splint into the wall */
129 # include <ucontext.h>
134 # include "xbt/context_win32.h" /* Manual reimplementation for prehistoric platforms */
138 **** string handling (parts from http://www.ijs.si/software/snprintf/)
141 /* prototype of C99 functions */
145 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
146 extern int vsnprintf(char *, size_t, const char *, va_list);
149 /* use internal functions when OS provided ones are borken */
150 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
151 extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
152 extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
153 #define snprintf portable_snprintf
154 #define vsnprintf portable_vsnprintf
157 /* prototype of GNU functions */
158 extern int asprintf (char **ptr, const char *fmt, /*args*/ ...);
159 extern int vasprintf (char **ptr, const char *fmt, va_list ap);
160 extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
161 extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
164 * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
165 * (without adding an extra check on working platforms :)
168 #ifdef PRINTF_NULL_WORKING
169 # define PRINTF_STR(a) (a)
171 # define PRINTF_STR(a) (a)?:"(null)"
175 * What we need to extract the backtrace in exception handling code
177 #ifdef HAVE_EXECINFO_H
178 # include <execinfo.h>
182 * Some debugging functions. Can't we find a better place for this??
184 void hexa_print(const char*name, unsigned char *data, int size);
185 const char *hexa_str(unsigned char *data, int size);
187 #endif /* GRAS_PORTABLE_H */