1 /*
2    Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
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  * gdef.h
20  */
21 
22 /* This header is always the first header included in source
23  * files. It takes care of some portability issues, and
24  * is a good candidate for precompilation. It requires
25  * either G_UNIX or G_WIN32 to be defined on the compiler
26  * command line, although G_UNIX may also be inferred from
27  * autoconf's HAVE_CONFIG_H.
28  */
29 
30 #ifndef G_DEF_H
31 #define G_DEF_H
32 
33 	/* Autoconf responses
34 	 */
35 	#if defined(HAVE_CONFIG_H)
36 
37 		#if ! defined(G_UNIX)
38 			#define G_UNIX
39 		#endif
40 
41 		#include <config.h>
42 
43 		#if HAVE_BUGGY_CTIME
44 			#include <time.h>
45 			#ifdef __cplusplus
46 				#include <ctime>
47 			#endif
48 		#endif
49 
50 		#if ! HAVE_GETPWNAM_R
51 			#ifdef __cplusplus
52 				#include <sys/types.h>
53 				#include <pwd.h>
54 			#endif
55 		#endif
56 
57 		#if ! HAVE_GMTIME_R || ! HAVE_LOCALTIME_R
58 			#ifdef __cplusplus
59 				#include <ctime>
60 			#endif
61 		#endif
62 
63 		#if ! HAVE_SOCKLEN_T
64 			typedef int socklen_t ;
65 		#endif
66 
67 		/* just in case, undefine if defined as zero in config.h */
68 		#if defined(USE_NO_ADMIN) && 0 == USE_NO_ADMIN
69 			#undef USE_NO_ADMIN
70 		#endif
71 		#if defined(USE_NO_AUTH) && 0 == USE_NO_AUTH
72 			#undef USE_NO_AUTH
73 		#endif
74 		#if defined(USE_NO_EXEC) && 0 == USE_NO_EXEC
75 			#undef USE_NO_EXEC
76 		#endif
77 		#if defined(USE_NO_POP) && 0 == USE_NO_POP
78 			#undef USE_NO_POP
79 		#endif
80 		#if defined(USE_SMALL_CONFIG) && 0 == USE_SMALL_CONFIG
81 			#undef USE_SMALL_CONFIG
82 		#endif
83 		#if defined(USE_SMALL_EXCEPTIONS) && 0 == USE_SMALL_EXCEPTIONS
84 			#undef USE_SMALL_EXCEPTIONS
85 		#endif
86 		#if defined(USE_IPV6) && 0 == USE_IPV6
87 			#undef USE_IPV6
88 		#endif
89 
90 	#else
91 		#define HAVE_ZLIB_H 1
92 	#endif
93 
94 	/* Check operating-system switches
95 	 */
96 	#if !defined(G_WIN32) && !defined(G_UNIX)
97 		#error invalid compilation switches - define G_WIN32 or G_UNIX
98 	#endif
99 	#if defined(G_WIN32) && defined(G_UNIX)
100 		#error invalid compilation switches - define G_WIN32 or G_UNIX
101 	#endif
102 	#if defined(__MINGW32__) && !defined(G_MINGW)
103 		#define G_MINGW 1
104 	#endif
105 
106 	/* Define supplementary o/s compilation switches
107 	 */
108 	#if defined(G_WIN32) && ! defined(G_WINDOWS)
109 		#define G_WINDOWS
110 	#endif
111 
112 	/* Define the compiler
113 	 */
114 	#if defined(_MSC_VER)
115 		#define G_COMPILER_IS_MICROSOFT 1
116 		#if (_MSC_VER <= 1200)
117 			#define G_COMPILER_IS_OLD 1
118 		#endif
119 	#endif
120 	#if defined(__GNUC__)
121 		#define G_COMPILER_IS_GNU 1
122 	#endif
123 
124 	/* Define extra microsoft header tweaks
125 	 */
126 	#ifdef G_WIN32_IE
127 		#ifndef _WIN32_IE
128 			#define _WIN32_IE 0x600
129 		#endif
130 	#endif
131 
132 	/* Include main o/s headers
133 	 */
134 	#if defined(G_WINDOWS)
135 		#if defined(G_MINGW)
136 			#define __USE_W32_SOCKETS
137 		#endif
138 		#ifndef WIN32_LEAN_AND_MEAN
139 		#define WIN32_LEAN_AND_MEAN
140 		#endif
141 		#include <windows.h>
142 		#include <winsock2.h>
143 		#include <shellapi.h>
144 		#include <direct.h>
145 		#if defined(G_MINGW)
146 			#include <sys/stat.h>
147 			#include <unistd.h>
148 			#include <stdlib.h>
149 		#endif
150 	#else
151 		#include <unistd.h>
152 		#include <sys/stat.h>
153 		#include <sys/types.h>
154 		#if ! defined(G_MINGW)
155 			#include <sys/wait.h>
156 		#endif
157 		#if ! defined(HAVE_CONFIG_H)
158 			#include <grp.h>
159 		#endif
160 	#endif
161 
162 	/* Include commonly-used system headers (good for pre-compilation)
163 	 */
164 	#ifdef __cplusplus
165 		#include <cstddef>
166 		#include <exception>
167 		#include <fstream>
168 		#include <iostream>
169 		#include <memory>
170 		#include <sstream>
171 		#include <string>
172 		#include <ctime>
173 	#else
174 		#include <stddef.h>
175 	#endif
176 
177 	/* Define Windows-style types under unix
178 	 */
179 	#if ! defined(G_WINDOWS)
180 		typedef unsigned char BOOL ;
181 		typedef unsigned int HWND ;
182 		typedef unsigned int HINSTANCE ;
183 		typedef unsigned int HANDLE ;
184 		typedef wchar_t TCHAR ;
185 	#endif
186 
187 	/* Define fixed-size types
188 	 */
189 	#if defined(G_WINDOWS)
190 		#if defined(G_COMPILER_IS_OLD)
191 			typedef unsigned int g_uint32_t ;
192 			typedef unsigned short g_uint16_t ;
193 			typedef int g_int32_t ;
194 			typedef short g_int16_t ;
195 		#else
196 			typedef UINT32 g_uint32_t ;
197 			typedef UINT16 g_uint16_t ;
198 			typedef INT32 g_int32_t ;
199 			typedef INT16 g_int16_t ;
200 		#endif
201 	#else
202 		#include <stdint.h>
203 		typedef uint32_t g_uint32_t ;
204 		typedef uint16_t g_uint16_t ;
205 		typedef int32_t g_int32_t ;
206 		typedef int16_t g_int16_t ;
207 	#endif
208 	#if __cplusplus
209 		typedef char assert_sizeof_uint16_is_2[sizeof(g_uint16_t)==2U?1:-1] ;
210 		typedef char assert_sizeof_uint32_is_4[sizeof(g_uint32_t)==4U?1:-1] ;
211 	#endif
212 
213 	/* Define missing standard types
214 	 */
215 	#if defined(G_WINDOWS)
216 		typedef int uid_t ;
217 		typedef int gid_t ;
218 		#if defined(G_MINGW)
219 			typedef int errno_t ;
220 		#endif
221 		#if defined(G_COMPILER_IS_MICROSOFT)
222 			#if defined(G_COMPILER_IS_OLD)
223 				typedef int errno_t ;
224 				typedef long LONG_PTR ;
225 				typedef char assert_sizeof_long_ptr_is_ok[sizeof(LONG_PTR)==sizeof(void*)?1:-1] ;
226 			#endif
227 			typedef SSIZE_T ssize_t ;
228 			typedef unsigned int pid_t ;
229 		#endif
230 	#endif
231 
232 	/* Pull some std types into the global namespace
233 	 */
234 	#ifdef __cplusplus
235 		#if !defined(G_COMPILER_IS_OLD)
236 			using std::size_t ;
237 		#endif
238 	#endif
239 
240 	/* Modify compiler error handling
241 	 */
242 	#if defined(G_COMPILER_IS_MICROSOFT)
243 		#pragma warning( disable : 4100 ) /* unused formal parameter */
244 		#pragma warning( disable : 4355 ) /* 'this' in initialiser list */
245 		#pragma warning( disable : 4511 ) /* cannot create default copy ctor */
246 		#pragma warning( disable : 4512 ) /* cannot create default op=() */
247 		#pragma warning( disable : 4786 ) /* truncation in debug info */
248 		#pragma warning( disable : 4275 ) /* dll-interface stuff in <complex> */
249 	#endif
250 
251 	/* Pull stuff into the std namespace
252 	 */
253 	#ifdef __cplusplus
254 		#if defined(G_COMPILER_IS_MICROSOFT)
255 		namespace std
256 		{
257 			using ::abort ;
258 			#if defined(G_COMPILER_IS_OLD)
259 				using ::size_t ;
260 				using ::tm ;
261 				using ::time_t ;
262 				using ::time ;
263 				using ::localtime ;
264 				using ::gmtime ;
265 				using ::strftime ;
266 				using ::mktime ;
267 				using ::strchr ;
268 				using ::strrchr ;
269 				using ::strlen ;
270 				using ::strcpy ;
271 				using ::strncpy ;
272 				using ::strstr ;
273 				using ::strspn ;
274 				using ::system ;
275 				using ::remove ;
276 				using ::rename ;
277 				using ::rand ;
278 				using ::srand ;
279 				using ::getenv ;
280 				using ::printf ;
281 				using ::putchar ;
282 				using ::fputs ;
283 				using ::malloc ;
284 				using ::exit ;
285 			#endif
286 		}
287 		#endif
288 	#endif
289 
290 	/* Use smaller buffers and limits if building with the uClibc run-time library.
291 	 * See glimits.h. This assumes that the uClibc header "features.h" has been
292 	 * included as a side-effect of including system headers above.
293 	 */
294 	#ifdef __UCLIBC__
295 		#ifndef G_NOT_SMALL
296 			#define G_SMALL
297 		#endif
298 	#endif
299 
300 	/* Macros to explicitly ignore unused values.
301 	 */
302 	#define G_IGNORE_RETURN(type) (void)
303 	#define G_IGNORE_PARAMETER(type,name) (void)name
304 	#define G_IGNORE_VARIABLE(name) name=name
305 
306 	/* Inline definitions of missing functions
307 	 */
308 
309 	#if defined(HAVE_CONFIG_H)
310 		#if ! HAVE_GETPWNAM_R
311 			#define G_DEF_GETPWNAM_R_INLINE 1
312 		#endif
313 		#if ! HAVE_GMTIME_R
314 			#define G_DEF_GMTIME_R_INLINE 1
315 		#endif
316 		#if ! HAVE_LOCALTIME_R
317 			#define G_DEF_LOCALTIME_R_INLINE 1
318 		#endif
319 		#if HAVE_SETGROUPS
320 			#include <grp.h>
321 		#else
322 			#define G_DEF_SETGROUPS_INLINE 1
323 		#endif
324 	#else
325 		#if defined(G_MINGW)
326 			#define G_DEF_GMTIME_R_INLINE 1
327 			#define G_DEF_LOCALTIME_R_INLINE 1
328 			#define G_DEF_LOCALTIME_S_INLINE 1
329 			#define G_DEF_GMTIME_S_INLINE 1
330 			#define G_DEF_GETENV_S_INLINE 1
331 		#endif
332 		#if defined(G_COMPILER_IS_OLD)
333 			#define G_DEF_LOCALTIME_S_INLINE 1
334 			#define G_DEF_GMTIME_S_INLINE 1
335 			#define G_DEF_GETENV_S_INLINE 1
336 			#define G_DEF_GET_WINDOW_LONG_PTR_INLINE 1
337 		#endif
338 	#endif
339 
340 	#if defined(G_DEF_GETPWNAM_R_INLINE)
341 		#ifdef __cplusplus
getpwnam_r(const char * name,struct passwd * pwd,char * buf,size_t buflen,struct passwd ** result)342 			inline int getpwnam_r( const char * name , struct passwd * pwd ,
343 				char * buf , size_t buflen , struct passwd ** result )
344 			{
345 				struct passwd * p = ::getpwnam( name ) ;
346 				if( p )
347 				{
348 					*pwd = *p ; /* let the string pointers dangle into the library storage */
349 					*result = pwd ;
350 					return 0 ;
351 				}
352 				else
353 				{
354 					*result = NULL ;
355 					return 0 ; /* or errno */
356 				}
357 			}
358 			#endif
359 	#endif
360 
361 	#if defined(G_DEF_GMTIME_R_INLINE)
362 		#ifdef __cplusplus
gmtime_r(const std::time_t * tp,struct std::tm * tm_p)363 			inline struct std::tm * gmtime_r( const std::time_t * tp , struct std::tm * tm_p )
364 			{
365 				const struct std::tm * p = std::gmtime( tp ) ;
366 				if( p == 0 ) return 0 ;
367 				*tm_p = *p ;
368 				return tm_p ;
369 			}
370 		#endif
371 	#endif
372 
373 	#if defined(G_DEF_LOCALTIME_R_INLINE)
374 		#ifdef __cplusplus
localtime_r(const std::time_t * tp,struct std::tm * tm_p)375 			inline struct std::tm * localtime_r( const std::time_t * tp , struct std::tm * tm_p )
376 			{
377 				const struct std::tm * p = std::localtime( tp ) ;
378 				if( p == 0 ) return 0 ;
379 				*tm_p = *p ;
380 				return tm_p ;
381 			}
382 		#endif
383 	#endif
384 
385 	#if defined(G_DEF_LOCALTIME_S_INLINE)
386 		#ifdef __cplusplus
localtime_s(struct std::tm * tm_p,const std::time_t * tp)387 			inline int localtime_s( struct std::tm * tm_p , const std::time_t * tp )
388 			{
389 				const errno_t e_inval = 22 ;
390 				if( tm_p == NULL ) return e_inval ;
391 				tm_p->tm_sec = tm_p->tm_min = tm_p->tm_hour = tm_p->tm_mday = tm_p->tm_mon =
392 					tm_p->tm_year = tm_p->tm_wday = tm_p->tm_yday = tm_p->tm_isdst = -1 ;
393 				if( tp == NULL || *tp < 0 ) return e_inval ;
394 				const struct std::tm * p = std::localtime( tp ) ;
395 				if( p == 0 ) return e_inval ;
396 				*tm_p = *p ;
397 				return 0 ;
398 			}
399 		#endif
400 	#endif
401 
402 	#if defined(G_DEF_GMTIME_S_INLINE)
403 		#ifdef __cplusplus
gmtime_s(struct std::tm * tm_p,const std::time_t * tp)404 			inline int gmtime_s( struct std::tm * tm_p , const std::time_t * tp )
405 			{
406 				const errno_t e_inval = 22 ;
407 				if( tm_p == NULL ) return e_inval ;
408 				tm_p->tm_sec = tm_p->tm_min = tm_p->tm_hour = tm_p->tm_mday = tm_p->tm_mon =
409 					tm_p->tm_year = tm_p->tm_wday = tm_p->tm_yday = tm_p->tm_isdst = -1 ;
410 				if( tp == NULL || *tp < 0 ) return e_inval ;
411 				const struct std::tm * p = std::gmtime( tp ) ;
412 				if( p == 0 ) return e_inval ;
413 				*tm_p = *p ;
414 				return 0 ;
415 			}
416 		#endif
417 	#endif
418 
419 	#if defined(G_DEF_SETGROUPS_INLINE)
420 		#ifdef __cplusplus
setgroups(size_t,const gid_t *)421 			inline int setgroups( size_t , const gid_t * )
422 			{
423 				return 0 ;
424 			}
425 		#endif
426 	#endif
427 
428 	#if defined(G_DEF_GETENV_S_INLINE)
429 		#ifdef __cplusplus
getenv_s(size_t * n_out,char * buffer,size_t n_in,const char * name)430 			inline errno_t getenv_s( size_t * n_out , char * buffer , size_t n_in , const char * name )
431 			{
432 				const errno_t e_inval = 22 ;
433 				const errno_t e_range = 34 ;
434 				if( n_out == NULL || name == NULL ) return e_inval ;
435 				const char * p = ::getenv( name ) ;
436 				*n_out = p ? (strlen(p) + 1U) : 0 ;
437 				if( p && *n_out > n_in ) return e_range ;
438 				if( p && buffer ) strcpy( buffer , p ) ;
439 				return 0 ;
440 			}
441 
442 		#endif
443 	#endif
444 
445 	#if defined(G_DEF_GET_WINDOW_LONG_PTR_INLINE)
446 		#ifdef __cplusplus
447 			const int GWLP_HINSTANCE = GWL_HINSTANCE ;
448 			const int GWLP_WNDPROC = GWL_WNDPROC ;
449 			const int DWLP_USER = DWL_USER ;
GetWindowLongPtr(HWND h,int id)450 			inline LONG_PTR GetWindowLongPtr( HWND h , int id )
451 			{
452 				return static_cast<LONG_PTR>(::GetWindowLong(h,id)) ;
453 			}
SetWindowLongPtr(HWND h,int id,LONG_PTR value)454 			inline LONG_PTR SetWindowLongPtr( HWND h , int id , LONG_PTR value )
455 			{
456 				return static_cast<LONG_PTR>(::SetWindowLong(h,id,static_cast<LONG>(value))) ;
457 			}
458 		#endif
459 	#endif
460 
461 #endif
462 
463