Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement functions SD_workstation_route_get_list, SD_workstation_route_get_size...
[simgrid.git] / src / portable.h
1 /* $Id$ */
2
3 /* portable -- header loading to write portable code                         */
4 /* loads much more stuff than sysdep.h since the latter is in public interface*/
5
6 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                   */
7
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. */
10
11 #ifndef GRAS_PORTABLE_H
12 #define GRAS_PORTABLE_H
13
14 #include "gras_config.h"
15
16 #include <stdarg.h>
17
18 #ifdef HAVE_ERRNO_H
19 #  include <errno.h>
20 #endif
21
22 #ifdef HAVE_UNISTD_H
23 #  include <unistd.h>
24 #endif
25
26 /****
27  **** Networking 
28  ****/
29
30
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 */
38 # endif
39
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
45 #  endif
46 #elif HAVE_WINSOCK_H
47 #  include <winsock.h>
48 #endif
49
50 #ifdef 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)
56
57 #       ifdef SD_BOTH
58 #               define tcp_close( s )   (shutdown( s, SD_BOTH ), closesocket( s ))
59 #       else
60 #               define tcp_close( s )           closesocket( s )
61 #       endif
62
63 #       define EWOULDBLOCK WSAEWOULDBLOCK
64 #       define EINPROGRESS WSAEINPROGRESS
65 #       define ETIMEDOUT   WSAETIMEDOUT
66
67 #       undef  sock_errno
68 #       undef  sock_errstr
69 #       define sock_errno      WSAGetLastError()
70 #       define sock_errstr     gras_wsa_err2string(WSAGetLastError())
71
72 const char *gras_wsa_err2string(int errcode);
73
74 #       define S_IRGRP 0
75 #       define S_IWGRP 0
76
77 #else
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)
82
83 #       ifdef SHUT_RDWR
84 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
85 #       else
86 #               define tcp_close( s )   close( s )
87 #       endif
88 #endif /* windows or unix ? */
89
90 /****
91  **** File handling
92  ****/
93
94 #include <fcntl.h>
95
96 #ifdef HAVE_SYS_STAT_H
97 #  include <sys/stat.h>
98 #endif
99
100 #ifndef O_BINARY
101 #  define O_BINARY 0
102 #endif
103
104 /****
105  **** Time handling
106  ****/
107
108 #ifdef TIME_WITH_SYS_TIME
109 # include <sys/time.h>
110 # include <time.h>
111 #else
112 # if HAVE_SYS_TIME_H
113 #  include <sys/time.h>
114 # else
115 #  include <time.h>
116 # endif
117 #endif
118
119 #ifdef _WIN32
120 #define sleep _sleep /* else defined in stdlib.h */
121 #endif
122
123 /****
124  **** Contexts
125  ****/
126
127 #ifdef USE_UCONTEXT
128 # ifndef S_SPLINT_S /* This header drives splint into the wall */
129 #   include <ucontext.h>
130 # endif 
131 #endif
132
133 #ifdef _WIN32
134 #  include "xbt/context_win32.h" /* Manual reimplementation for prehistoric platforms */
135 #endif
136
137 /****
138  **** string handling (parts from http://www.ijs.si/software/snprintf/)
139  ****/
140
141 /* prototype of C99 functions */
142 #ifdef HAVE_SNPRINTF
143 #include <stdio.h>
144 #else
145 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
146 extern int vsnprintf(char *, size_t, const char *, va_list);
147 #endif
148
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
155 #endif
156
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);
162
163 /*
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 :)
166  */
167
168 #ifdef PRINTF_NULL_WORKING
169 #  define PRINTF_STR(a) (a)
170 #else
171 #  define PRINTF_STR(a) (a)?:"(null)"
172 #endif
173
174 /*
175  * What we need to extract the backtrace in exception handling code
176  */
177 #ifdef HAVE_EXECINFO_H
178 #  include <execinfo.h>
179 #endif
180
181 /*
182  * Some debugging functions. Can't we find a better place for this??
183  */
184 void hexa_print(const char*name, unsigned char *data, int size);
185 const char *hexa_str(unsigned char *data, int size);
186
187 #endif /* GRAS_PORTABLE_H */