Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent everything (possibly breaking all branches, but for the last time)
[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 "xbt/misc.h"
15 /* 
16  * win32 or win64 (__WIN32 is defined for win32 and win64 applications, __TOS_WIN__ is defined by xlC). 
17 */
18 #ifdef _WIN32
19 # include "win32/config.h"
20 # include <windows.h>
21 #else
22 #  include "gras_config.h"
23 #endif
24
25 /* Load this asap to make sure that GNU_SOURCE is defined on need when stdio gets loaded by some random system header */
26 #ifdef HAVE_GETLINE
27 #  ifndef _GNU_SOUCE
28 #    define _GNU_SOURCE
29 #  endif
30 #  include <stdio.h>
31 #endif
32
33 #include <stdarg.h>
34
35 #ifdef HAVE_ERRNO_H
36 #  include <errno.h>
37 #endif
38
39 #ifdef HAVE_UNISTD_H
40 #  include <unistd.h>
41 #endif
42
43 /****
44  **** Networking 
45  ****/
46
47
48 #ifdef HAVE_SYS_SOCKET_H
49 #  include <sys/socket.h>
50 #  include <netinet/in.h>       /* sometimes required for #include <arpa/inet.h> */
51 #  include <netinet/tcp.h>      /* TCP_NODELAY */
52 #  include <netdb.h>            /* getprotobyname() */
53 #  include <arpa/inet.h>        /* inet_ntoa() */
54 #  include <sys/types.h>        /* sometimes required for fd_set */
55 # endif
56
57
58 #ifndef HAVE_WINSOCK_H
59 #       define tcp_read( s, buf, len)   read( s, buf, len )
60 #       define tcp_write( s, buf, len)  write( s, buf, len )
61 #       define sock_errno        errno
62 #       define sock_errstr(err)  strerror(err)
63
64 #       ifdef SHUT_RDWR
65 #               define tcp_close( s )   (shutdown( s, SHUT_RDWR ), close( s ))
66 #       else
67 #               define tcp_close( s )   close( s )
68 #       endif
69 #endif /* windows or unix ? */
70
71 /****
72  **** File handling
73  ****/
74
75 #include <fcntl.h>
76
77 #ifdef HAVE_SYS_STAT_H
78 #  include <sys/stat.h>
79 #endif
80
81 #ifndef O_BINARY
82 #  define O_BINARY 0
83 #endif
84
85 /****
86  **** Time handling
87  ****/
88
89 #ifdef TIME_WITH_SYS_TIME
90 # include <sys/time.h>
91 # include <time.h>
92 #else
93 # if HAVE_SYS_TIME_H
94 #  include <sys/time.h>
95 # else
96 #  include <time.h>
97 # endif
98 #endif
99
100 /****
101  **** Signals
102  ****/
103 #ifdef HAVE_SIGNAL_H
104 # include <signal.h>
105 #endif
106
107 /****
108  **** string handling (parts from http://www.ijs.si/software/snprintf/)
109  ****/
110
111 /* prototype of C99 functions */
112 #if defined(HAVE_SNPRINTF)
113 #include <stdio.h>
114 #else
115 XBT_PUBLIC(int) snprintf(char *, size_t, const char *, /*args */ ...);
116 XBT_PUBLIC(int) vsnprintf(char *, size_t, const char *, va_list);
117 #endif
118
119
120 /* use internal functions when OS provided ones are borken */
121 #if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
122      extern int portable_snprintf(char *str, size_t str_m, const char *fmt,
123                                   /*args */ ...);
124      extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt,
125                                    va_list ap);
126 #define snprintf  portable_snprintf
127 #define vsnprintf portable_vsnprintf
128 #endif
129
130 /* prototype of GNU functions  */
131 #if (defined(__GNUC__) && !defined(__cplusplus))
132      extern int asprintf(char **ptr, const char *fmt, /*args */ ...);
133      extern int vasprintf(char **ptr, const char *fmt, va_list ap);
134 #endif
135
136      extern int asnprintf(char **ptr, size_t str_m, const char *fmt,    /*args */
137                           ...);
138      extern int vasnprintf(char **ptr, size_t str_m, const char *fmt,
139                            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 */