1 /*
2  * Copyright (C) 2003-2015 FreeIPMI Core Team
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #ifndef FREEIPMI_PORTABILITY_H
20 #define FREEIPMI_PORTABILITY_H
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif /* HAVE_CONFIG_H */
25 
26 #include <stdio.h>      /* For FILE definition */
27 #include <stdlib.h>
28 #if STDC_HEADERS
29 #include <string.h>
30 #include <stdarg.h>
31 #endif /* STDC_HEADERS */
32 #include <sys/types.h>
33 #include <math.h>
34 #include <netdb.h>
35 #if HAVE_ALLOCA_H
36 #include <alloca.h>
37 #endif /* HAVE_ALLOCA_H */
38 #if TIME_WITH_SYS_TIME
39 #include <sys/time.h>
40 #include <time.h>
41 #else /* !TIME_WITH_SYS_TIME */
42 #if HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #else /* !HAVE_SYS_TIME_H */
45 #include <time.h>
46 #endif /* !HAVE_SYS_TIME_H */
47 #endif  /* !TIME_WITH_SYS_TIME */
48 
49 /* achu: I guess __func__ is the other macro people use?? */
50 #ifndef HAVE_FUNCTION_MACRO
51 #define __FUNCTION__ __func__
52 #endif
53 
54 /* achu: not on Solaris */
55 #ifndef UINT_MAX
56 #define UINT_MAX 4294967295U
57 #endif
58 
59 #if  __WORDSIZE == 64
60 #define FI_64 "%l"
61 #else
62 #define FI_64 "%ll"
63 #endif
64 
65 # if ENABLE_NLS
66 #  include <libintl.h>
67 #  define _(Text) gettext (Text)
68 # else
69 #  define textdomain(Domain)
70 #  define _(Text) Text
71 # endif
72 # define N_(Text) Text
73 
74 #if !defined(O_SYNC) && defined(O_FSYNC)
75 #define O_SYNC  O_FSYNC
76 #endif
77 
78 /* FreeBSD don't have log2(), exp10() and exp2() */
79 #ifndef HAVE_LOG2
80 /* Cygwin has a log2 macro to already handle portability */
81 #ifndef log2
82 #define log2(x)         (M_LOG2E * log ((x)))
83 #endif
84 #endif
85 #ifndef HAVE_EXP10
86 #define exp10(x)        (pow (10.0, (x)))
87 #endif
88 #ifndef HAVE_EXP2
89 #define exp2(x)         (pow (2.0, (x)))
90 #endif
91 /* uClibc may not have cbrt() */
92 #ifndef HAVE_CBRT
93 #define cbrt(x)         (pow((x), -3.0))
94 #endif
95 
96 /* FreeBSD don't have strdupa */
97 #ifndef strdupa
98 /* Duplicate S, returning an identical alloca'd string.  */
99 # define strdupa(s)                             \
100   ({                                            \
101     const char *__old = (s);                    \
102     size_t __len = strlen (__old) + 1;          \
103     char *__new = (char *) alloca (__len);      \
104     (char *) memcpy (__new, __old, __len);      \
105   })
106 #endif
107 
108 #ifndef HAVE_MEMCPY
109 static inline void*
freeipmi_memcpy(void * dest,const void * src,size_t n)110 freeipmi_memcpy (void *dest, const void *src, size_t n)
111 {
112   while (0 <= --n)
113     ((unsigned char*)dest) [n] = ((unsigned char*)src) [n];
114   return (dest);
115 }
116 #define memcpy freeipmi_memcpy
117 #endif /* HAVE_MEMCPY */
118 
119 #ifndef HAVE_MEMPCPY
120 #define mempcpy freeipmi_mempcpy
121 void *freeipmi_mempcpy (void *to, const void *from, size_t size);
122 #endif /* HAVE_MEMPCPY */
123 
124 #ifndef HAVE_MEMSET
125 static inline void*
freeipmi_memset(void * s,int c,size_t n)126 freeipmi_memset (void *s, int c, size_t n)
127 {
128   while (0 <= --n) ((unsigned char*)s) [n] = (unsigned char) c;
129   return (s);
130 }
131 #define memset freeipmi_memset
132 #endif /* HAVE_MEMSET */
133 
134 #ifndef HAVE_STRCHR
135 static inline char*
freeipmi_strchr(const char * s,int c)136 freeipmi_strchr (const char* s, int c)
137 {
138   while (*s != '\0')
139     if (*s == (char)c)
140       return s;
141     else
142       s++;
143   return (NULL);
144 }
145 # define strchr freeipmi_strchr
146 #endif /* HAVE_STRCHR */
147 
148 /* FreeBSD don't have strndup() */
149 #ifndef HAVE_STRNDUP
150 #define strndup freeipmi_strndup
151 char *freeipmi_strndup (const char *, size_t);
152 #endif
153 
154 #ifndef HAVE_STRCHRNUL
155 #define strchrnul freeipmi_strchrnul
156 char *freeipmi_strchrnul (const char *s, int c);
157 #endif /* !HAVE_STRCHRNUL */
158 
159 #ifndef HAVE_STRSEP
160 #define strsep freeipmi_strsep
161 char *freeipmi_strsep (char **stringp, const char *delim);
162 #endif /* !HAVE_STRSEP */
163 
164 #ifndef HAVE_STRISTR
165 #define stristr freeipmi_stristr
166 char *freeipmi_stristr (const char *s1, const char *s2);
167 #endif /* !HAVE_STRISTR */
168 
169 /* FreeBSD don't have getline() */
170 #ifndef HAVE_GETLINE
171 #define getline freeipmi_getline
172 ssize_t freeipmi_getline (char **buf, size_t *bufsize, FILE *fp);
173 #endif
174 
175 #ifndef HAVE_ASPRINTF
176 #define asprintf freeipmi_asprintf
177 int freeipmi_asprintf (char **strp, const char *fmt, ...);
178 #endif
179 
180 /* achu: timeradd and timersub not in solaris
181  *
182  * these definitions ripped from sys/time.h on linux.
183  */
184 #ifndef timeradd
185 # define timeradd(a, b, result)                         \
186   do {                                                  \
187     (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;       \
188     (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;    \
189     if ((result)->tv_usec >= 1000000)                   \
190       {                                                 \
191         ++(result)->tv_sec;                             \
192         (result)->tv_usec -= 1000000;                   \
193       }                                                 \
194   } while (0)
195 #endif /* timeradd */
196 
197 #ifndef timersub
198 # define timersub(a, b, result)                         \
199   do {                                                  \
200     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;       \
201     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;    \
202     if ((result)->tv_usec < 0) {                        \
203       --(result)->tv_sec;                               \
204       (result)->tv_usec += 1000000;                     \
205     }                                                   \
206   } while (0)
207 #endif /* timersub */
208 
209 #if !defined(HAVE_FUNC_GETHOSTBYNAME_R_6) && !defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
210 int freeipmi_gethostbyname_r (const char *name,
211                               struct hostent *ret,
212                               char *buf,
213                               size_t buflen,
214                               struct hostent **result,
215                               int *h_errnop);
216 #endif /* !defined(HAVE_FUNC_GETHOSTBYNAME_R_6) && !defined(HAVE_FUNC_GETHOSTBYNAME_R_5) */
217 
218 #endif /* FREEIPMI_PORTABILITY_H */
219