Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
finish the convertion of tests to TESH
[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 /* 
15  * win32 or win64 (__WIN32 is defined for win32 and win64 applications, __TOS_WIN__ is defined by xlC). 
16 */ 
17 #ifdef _WIN32
18 # include "win32/config.h"
19 # include <windows.h>
20 #else
21 #  include "gras_config.h"
22 #endif
23
24 /* Load this asap to make sure that GNU_SOURCE is defined on need when stdio gets loaded by some random system header */
25 #ifdef HAVE_GETLINE
26 #  define _GNU_SOURCE
27 #  include <stdio.h>
28 #endif
29
30 #include <stdarg.h>
31
32 #ifdef HAVE_ERRNO_H
33 #  include <errno.h>
34 #endif
35
36 #ifdef HAVE_UNISTD_H
37 #  include <unistd.h>
38 #endif
39
40 /****
41  **** Networking 
42  ****/
43
44
45 #ifdef HAVE_SYS_SOCKET_H
46 #  include <sys/socket.h>
47 #  include <netinet/in.h>   /* sometimes required for #include <arpa/inet.h> */
48 #  include <netinet/tcp.h>  /* TCP_NODELAY */
49 #  include <netdb.h>        /* getprotobyname() */
50 #  include <arpa/inet.h>    /* inet_ntoa() */
51 #  include <sys/types.h>    /* sometimes required for fd_set */
52 # endif
53
54
55 #ifndef HAVE_WINSOCK_H
56 #       define tcp_read( s, buf, len)   read( s, buf, len )
57 #       define tcp_write( s, buf, len)  write( s, buf, len )
58 #       define sock_errno        errno
59 #       define sock_errstr(err)  strerror(err)
60
61 #       ifdef SHUT_RDWR
62 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
63 #       else
64 #               define tcp_close( s )   close( s )
65 #       endif
66 #endif /* windows or unix ? */
67
68 /****
69  **** File handling
70  ****/
71
72 #include <fcntl.h>
73
74 #ifdef HAVE_SYS_STAT_H
75 #  include <sys/stat.h>
76 #endif
77
78 #ifndef O_BINARY
79 #  define O_BINARY 0
80 #endif
81
82 /****
83  **** Time handling
84  ****/
85
86 #ifdef TIME_WITH_SYS_TIME
87 # include <sys/time.h>
88 # include <time.h>
89 #else
90 # if HAVE_SYS_TIME_H
91 #  include <sys/time.h>
92 # else
93 #  include <time.h>
94 # endif
95 #endif
96
97 /****
98  **** Signals
99  ****/
100 #ifdef HAVE_SIGNAL_H
101 # include <signal.h>
102 #endif
103
104 /****
105  **** string handling (parts from http://www.ijs.si/software/snprintf/)
106  ****/
107
108 /* prototype of C99 functions */
109 #ifdef HAVE_SNPRINTF
110 #include <stdio.h>
111 #else
112 #  if (defined(_MSC_VER) && defined(DLL_EXPORT))
113      __declspec(dllexport) int snprintf(char *, size_t, const char *, /*args*/ ...);
114      __declspec(dllexport)  int vsnprintf(char *, size_t, const char *, va_list);
115 #  elif (defined(_MSC_VER) && !defined(DLL_EXPORT) && !defined(DLL_STATIC) )
116      __declspec(dllimport) int snprintf(char *, size_t, const char *, /*args*/ ...);
117      __declspec(dllimport)  int vsnprintf(char *, size_t, const char *, va_list);
118 #  else
119      extern int snprintf(char *, size_t, const char *, /*args*/ ...);
120      extern int vsnprintf(char *, size_t, const char *, va_list);
121 #  endif
122
123 #endif
124
125 /* use internal functions when OS provided ones are borken */
126 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
127 extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
128 extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
129 #define snprintf  portable_snprintf
130 #define vsnprintf portable_vsnprintf
131 #endif
132
133 /* prototype of GNU functions  */
134 #ifndef __BORLANDC__
135 extern int asprintf  (char **ptr, const char *fmt, /*args*/ ...);
136 extern int vasprintf (char **ptr, const char *fmt, va_list ap);
137 #endif
138 extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
139 extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
140
141 /*
142  * That's needed to protect solaris's printf from ever seing NULL associated to a %s format
143  * (without adding an extra check on working platforms :)
144  */
145
146 #ifdef PRINTF_NULL_WORKING
147 #  define PRINTF_STR(a) (a)
148 #else
149 #  define PRINTF_STR(a) (a)?:"(null)"
150 #endif
151
152 /*
153  * What we need to extract the backtrace in exception handling code
154  */
155 #ifdef HAVE_EXECINFO_H
156 #  include <execinfo.h>
157 #endif
158
159 /****
160  **** Some debugging functions. Can't we find a better place for this??
161  ****/
162 void hexa_print(const char*name, unsigned char *data, int size);
163 const char *hexa_str(unsigned char *data, int size, int downside);
164
165 #endif /* GRAS_PORTABLE_H */