Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8db663048e4996091b5fe0f372c2274efdff0460
[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 # endif
38
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
44 #  endif
45 #elif HAVE_WINSOCK_H
46 #  include <winsock.h>
47 #endif
48
49 #ifdef 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)
55
56 #       ifdef SD_BOTH
57 #               define tcp_close( s )   (shutdown( s, SD_BOTH ), closesocket( s ))
58 #       else
59 #               define tcp_close( s )           closesocket( s )
60 #       endif
61
62 #       define EWOULDBLOCK WSAEWOULDBLOCK
63 #       define EINPROGRESS WSAEINPROGRESS
64 #       define ETIMEDOUT   WSAETIMEDOUT
65
66 #       undef  sock_errno
67 #       undef  sock_errstr
68 #       define sock_errno      WSAGetLastError()
69 #       define sock_errstr     gras_wsa_err2string(WSAGetLastError())
70
71 const char *gras_wsa_err2string(int errcode);
72
73 #       define S_IRGRP 0
74 #       define S_IWGRP 0
75
76 #else
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)
81
82 #       ifdef SHUT_RDWR
83 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
84 #       else
85 #               define tcp_close( s )   close( s )
86 #       endif
87 #endif /* windows or unix ? */
88
89 /****
90  **** File handling
91  ****/
92
93 #include <fcntl.h>
94
95 #ifdef HAVE_SYS_STAT_H
96 #  include <sys/stat.h>
97 #endif
98
99 #ifndef O_BINARY
100 #  define O_BINARY 0
101 #endif
102
103 /****
104  **** Time handling
105  ****/
106
107 #ifdef TIME_WITH_SYS_TIME
108 # include <sys/time.h>
109 # include <time.h>
110 #else
111 # if HAVE_SYS_TIME_H
112 #  include <sys/time.h>
113 # else
114 #  include <time.h>
115 # endif
116 #endif
117
118 #ifdef _WIN32
119 #define sleep _sleep /* else defined in stdlib.h */
120 #endif
121
122 /****
123  **** Contexts
124  ****/
125
126 #ifdef USE_UCONTEXT
127 # ifndef S_SPLINT_S /* This header drives splint into the wall */
128 #   include <ucontext.h>
129 # endif 
130 #endif
131
132 #ifdef _WIN32
133 #  include "xbt/context_win32.h" /* Manual reimplementation for prehistoric platforms */
134 #endif
135
136 /****
137  **** string handling (parts from http://www.ijs.si/software/snprintf/)
138  ****/
139
140 /* prototype of C99 functions */
141 #ifdef HAVE_SNPRINTF
142 #include <stdio.h>
143 #else
144 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
145 extern int vsnprintf(char *, size_t, const char *, va_list);
146 #endif
147
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
154 #endif
155
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);
161
162 /*
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 :)
165  */
166
167 #ifdef PRINTF_NULL_WORKING
168 #  define PRINTF_STR(a) (a)
169 #else
170 #  define PRINTF_STR(a) (a)?:"(null)"
171 #endif
172
173 /*
174  * What we need to extract the backtrace in exception handling code
175  */
176 #ifdef HAVE_EXECINFO_H
177 #  include <execinfo.h>
178 #endif
179
180 #endif /* GRAS_PORTABLE_H */