Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
These free's are buggy.
[simgrid.git] / src / portable.h
1 /* portable -- header loading to write portable code                         */
2 /* loads much more stuff than sysdep.h since the latter is in public interface*/
3
4 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
5  * All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8   * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef GRAS_PORTABLE_H
11 #define GRAS_PORTABLE_H
12
13 #include "gras_config.h"
14 #include "xbt/misc.h"
15 /* 
16  * win32 or win64 (__XBT_WIN32 is defined for win32 and win64 applications, __TOS_WIN__ is defined by xlC).
17 */
18 #ifdef _XBT_WIN32
19 # include "win32/config.h"
20 # include <windows.h>
21 #endif
22
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #ifdef HAVE_ERRNO_H
27 #  include <errno.h>
28 #endif
29
30 #ifdef HAVE_UNISTD_H
31 #  include <unistd.h>
32 #endif
33
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
36 #endif
37 #ifdef HAVE_SYS_SYSCTL_H
38 # include <sys/sysctl.h>
39 #endif
40
41 /****
42  **** Networking 
43  ****/
44
45
46 #ifdef HAVE_SYS_SOCKET_H
47 #  include <sys/socket.h>
48 #  include <netinet/in.h>       /* sometimes required for #include <arpa/inet.h> */
49 #  include <netinet/tcp.h>      /* TCP_NODELAY */
50 #  include <netdb.h>            /* getprotobyname() */
51 #  include <arpa/inet.h>        /* inet_ntoa() */
52 #  include <sys/types.h>        /* sometimes required for fd_set */
53 # endif
54
55
56 #ifndef HAVE_WINSOCK_H
57 #       define tcp_read( s, buf, len)   read( s, buf, len )
58 #       define tcp_write( s, buf, len)  write( s, buf, len )
59 #       define sock_errno        errno
60 #       define sock_errstr(err)  strerror(err)
61
62 #       ifdef SHUT_RDWR
63 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
64 #       else
65 #               define tcp_close( s )   close( s )
66 #       endif
67 #endif                          /* windows or unix ? */
68
69 /****
70  **** File handling
71  ****/
72
73 #include <fcntl.h>
74
75 #ifdef HAVE_SYS_STAT_H
76 #  include <sys/stat.h>
77 #endif
78
79 #ifndef O_BINARY
80 #  define O_BINARY 0
81 #endif
82
83 /****
84  **** Time handling
85  ****/
86
87 #ifdef TIME_WITH_SYS_TIME
88 # include <sys/time.h>
89 # include <time.h>
90 #else
91 # if HAVE_SYS_TIME_H
92 #  include <sys/time.h>
93 # else
94 #  include <time.h>
95 # endif
96 #endif
97
98 /****
99  **** Signals
100  ****/
101 #ifdef HAVE_SIGNAL_H
102 # include <signal.h>
103 #endif
104
105 /****
106  **** string handling (parts from http://www.ijs.si/software/snprintf/)
107  ****/
108
109 /* prototype of C99 functions */
110 #if defined(HAVE_SNPRINTF)
111 #include <stdio.h>
112 #else
113 XBT_PUBLIC(int) snprintf(char *, size_t, const char *, /*args */ ...);
114 XBT_PUBLIC(int) vsnprintf(char *, size_t, const char *, va_list);
115 #endif
116
117
118 /* use internal functions when OS provided ones are borken */
119 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
120 extern int portable_snprintf(char *str, size_t str_m, const char *fmt,
121                              /*args */ ...);
122 extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt,
123                               va_list ap);
124 #define snprintf  portable_snprintf
125 #define vsnprintf portable_vsnprintf
126 #endif
127
128 /* prototype of GNU functions  */
129 #if (defined(__GNUC__) && !defined(__cplusplus))
130 extern int asprintf(char **ptr, const char *fmt, /*args */ ...);
131 extern int vasprintf(char **ptr, const char *fmt, va_list ap);
132 #endif
133
134 extern int asnprintf(char **ptr, size_t str_m, const char *fmt, /*args */
135                      ...);
136 extern int vasnprintf(char **ptr, size_t str_m, const char *fmt,
137                       va_list ap);
138
139 /*
140  * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
141  * (without adding an extra check on working platforms :)
142  */
143
144 #ifdef PRINTF_NULL_WORKING
145 #  define PRINTF_STR(a) (a)
146 #else
147 #  define PRINTF_STR(a) (a)?:"(null)"
148 #endif
149
150 /*
151  * What we need to extract the backtrace in exception handling code
152  */
153 #ifdef HAVE_EXECINFO_H
154 #  include <execinfo.h>
155 #endif
156
157 /****
158  **** Some debugging functions. Can't we find a better place for this??
159  ****/
160 void hexa_print(const char *name, unsigned char *data, int size);
161 const char *hexa_str(unsigned char *data, int size, int downside);
162
163 #endif                          /* GRAS_PORTABLE_H */