-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathw32util.h
296 lines (225 loc) · 12.1 KB
/
w32util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* W32UTIL.H (c) Copyright "Fish" (David B. Trout), 2005-2012 */
/* Windows porting functions */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
#ifndef _W32UTIL_H
#define _W32UTIL_H
#if defined( _MSVC_ )
#include "hercules.h"
#ifndef _W32UTIL_C_
#ifndef _HUTIL_DLL_
#define W32_DLL_IMPORT DLL_IMPORT
#else
#define W32_DLL_IMPORT extern
#endif
#else
#define W32_DLL_IMPORT DLL_EXPORT
#endif
//////////////////////////////////////////////////////////////////////////////////////////
// Determine whether process is running "elevated" or not.
// (returns 1==true running elevated, 0==false otherwise)
W32_DLL_IMPORT int is_elevated();
//////////////////////////////////////////////////////////////////////////////////////////
// Translates a Win32 '[WSA]GetLastError()' value into a 'errno' value (if possible
// and/or if needed) that can then be used in the below 'w32_strerror' string function...
W32_DLL_IMPORT int w32_trans_w32error( const DWORD dwLastError );
//////////////////////////////////////////////////////////////////////////////////////////
// ("unsafe" version -- use "safer" 'w32_strerror_r' instead if possible)
W32_DLL_IMPORT char* w32_strerror( int errnum );
//////////////////////////////////////////////////////////////////////////////////////////
// Handles both regular 'errno' values as well as [WSA]GetLastError() values too...
W32_DLL_IMPORT int w32_strerror_r( int errnum, char* buffer, size_t buffsize );
//////////////////////////////////////////////////////////////////////////////////////////
// Return Win32 error message text associated with an error number value
// as returned by a call to either GetLastError() or WSAGetLastError()...
W32_DLL_IMPORT char* w32_w32errmsg( int errnum, char* pszBuffer, size_t nBuffSize );
//////////////////////////////////////////////////////////////////////////////////////////
// Large File Support...
#if (_MSC_VER < VS2005)
W32_DLL_IMPORT __int64 w32_ftelli64 ( FILE* stream );
W32_DLL_IMPORT int w32_fseeki64 ( FILE* stream, __int64 offset, int origin );
W32_DLL_IMPORT int w32_ftrunc64 ( int fd, __int64 new_size );
#endif
//////////////////////////////////////////////////////////////////////////////////////////
#if !defined( HAVE_SOCKETPAIR )
W32_DLL_IMPORT int socketpair( int domain, int type, int protocol, int socket_vector[2] );
#endif
#if !defined( HAVE_FORK )
W32_DLL_IMPORT pid_t fork( void );
#endif
#if !defined( HAVE_STRTOK_R )
W32_DLL_IMPORT char* strtok_r ( char* s, const char* sep, char** lasts);
#endif
#define BILLION 1000000000
#define MILLION 1000000
#if !defined( HAVE_GETTIMEOFDAY )
#define DEF_GTOD_RESYNC_SECS 30 // (host clock resync freq)
#define MAX_GTOD_RESOLUTION BILLION // (nanosecond resolution)
#define MIN_GTOD_RESOLUTION MILLION // (microsecond resolution)
typedef int clockid_t;
W32_DLL_IMPORT int clock_gettime ( clockid_t clk_id, struct timespec* ts );
W32_DLL_IMPORT int gettimeofday ( struct timeval* pTV, void* pTZ );
#endif
#if !defined( HAVE_NANOSLEEP )
W32_DLL_IMPORT int nanosleep ( const struct timespec* rqtp, struct timespec* rmtp );
#endif
#if !defined( HAVE_USLEEP )
W32_DLL_IMPORT int usleep ( useconds_t useconds );
#endif
// Can't use "HAVE_SLEEP" since Win32's "Sleep" causes HAVE_SLEEP to
// be erroneously #defined due to autoconf AC_CHECK_FUNCS case issues...
//#if !defined( HAVE_SLEEP )
W32_DLL_IMPORT unsigned sleep ( unsigned seconds );
//#endif
#if !defined( HAVE_SCHED_YIELD )
W32_DLL_IMPORT int sched_yield ( void );
#endif
#if !defined( HAVE_GETPGRP )
#define getpgrp getpid
#endif
#if !defined( HAVE_SCANDIR )
W32_DLL_IMPORT int scandir
(
const char *dir,
struct dirent ***namelist,
int (*filter)( struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **)
);
#endif
#if !defined( HAVE_ALPHASORT )
W32_DLL_IMPORT int alphasort ( const struct dirent **a, const struct dirent **b );
#endif
#if !defined(HAVE_SYS_RESOURCE_H)
// Note: We only provide the absolute minimum required information;
// RUSAGE_BOTH not defined as RUSAGE_BOTH is deprecated.
#define RUSAGE_SELF ( 0) // Current process
#define RUSAGE_CHILDREN (-1) // Children of the current process
#define RUSAGE_THREAD ( 1) // Current thread
struct rusage // Resource utilization information
{
struct timeval ru_utime; // User time used
struct timeval ru_stime; // System time used
};
W32_DLL_IMPORT int getrusage ( int who, struct rusage* r_usage );
W32_DLL_IMPORT int pthread_getcpuclockid ( TID tid, clockid_t* clk_id );
#define _POSIX_THREAD_CPUTIME (1) // Indicate pthread_getcpuclockid() presence
#endif
#if !defined(HAVE_DECL_LOGIN_NAME_MAX) || !HAVE_DECL_LOGIN_NAME_MAX
#define LOGIN_NAME_MAX UNLEN
#endif
#if !defined( HAVE_GETLOGIN )
W32_DLL_IMPORT char* getlogin ( void );
#endif
#if !defined( HAVE_GETLOGIN_R )
W32_DLL_IMPORT int getlogin_r ( char* name, size_t namesize );
#endif
#if !defined( HAVE_REALPATH )
W32_DLL_IMPORT char* realpath ( const char* file_name, char* resolved_name );
#endif
// The inet_aton() function converts the specified string,
// in the Internet standard dot notation, to a network address,
// and stores the address in the structure provided.
//
// The inet_aton() function returns 1 if the address is successfully converted,
// or 0 if the conversion failed.
#if !defined( HAVE_INET_ATON )
W32_DLL_IMPORT int inet_aton( const char* cp, struct in_addr* addr );
#endif
// Returns outpath as a host filesystem compatible filename path.
// This is a Cygwin-to-MSVC transitional period helper function.
// On non-Windows platforms it simply copies inpath to outpath.
// On Windows it converts inpath of the form "/cygdrive/x/foo.bar"
// to outpath in the form "x:/foo.bar" for Windows compatibility.
W32_DLL_IMPORT BYTE *hostpath( BYTE *outpath, const BYTE *inpath, size_t buffsize );
// Poor man's "fcntl( fd, F_GETFL )"...
// (only returns access-mode flags and not any others)
W32_DLL_IMPORT int get_file_accmode_flags( int fd );
// Retrieve unique host id
W32_DLL_IMPORT long gethostid( void );
// Initialize/Deinitialize sockets package...
W32_DLL_IMPORT int socket_init ( void );
W32_DLL_IMPORT int socket_deinit ( void );
// Set socket to blocking or non-blocking mode...
W32_DLL_IMPORT int socket_set_blocking_mode( int sfd, int blocking_mode );
// Determine whether a file descriptor is a socket or not...
// (returns 1==true if it's a socket, 0==false otherwise)
W32_DLL_IMPORT int socket_is_socket( int sfd );
// Set the SO_KEEPALIVE option and timeout values for a
// socket connection to detect when client disconnects.
// Returns 0==success, +1==warning(*), -1==failure.
// (*) Warning failure means function only partially
// succeeded (not all requested values were set)
W32_DLL_IMPORT int set_socket_keepalive( int sfd, int idle_time, int probe_interval, int probe_count );
// Function to retrieve keepalive values. 0==success, -1=failure
W32_DLL_IMPORT int get_socket_keepalive( int sfd, int* idle_time, int* probe_interval, int* probe_count );
// Retrieve directory where process was loaded from...
// (returns >0 == success, 0 == failure)
W32_DLL_IMPORT int get_process_directory( char* dirbuf, size_t bufsiz );
// Expand environment variables... (e.g. %SystemRoot%, etc); 0==success
W32_DLL_IMPORT int expand_environ_vars( const char* inbuff, char* outbuff, DWORD outbufsiz );
// Initialize Hercules HOSTINFO structure
W32_DLL_IMPORT void w32_init_hostinfo( HOST_INFO* pHostInfo );
W32_DLL_IMPORT int w32_socket ( int af, int type, int protocol );
W32_DLL_IMPORT int w32_accept ( int lsock, struct sockaddr* addr, int* addrlen );
W32_DLL_IMPORT int w32_close_socket( int fd );
W32_DLL_IMPORT void w32_FD_SET ( int fd, fd_set* pSet );
W32_DLL_IMPORT int w32_FD_ISSET ( int fd, fd_set* pSet );
// PROGRAMMING NOTE: w32_select can only be used with sockets, not files.
W32_DLL_IMPORT int w32_select ( int nfds,
fd_set* pReadSet,
fd_set* pWriteSet,
fd_set* pExceptSet,
const struct timeval* pTimeVal,
const char* pszSourceFile,
int nLineNumber
);
// PROGRAMMING NOTE: w32_pselect can only be used with sockets, not files.
// PROGRAMMING NOTE: w32_pselect does not support the sigmask parameter.
typedef unsigned long sigset_t;
W32_DLL_IMPORT int w32_pselect ( int nfds,
fd_set* pReadSet,
fd_set* pWriteSet,
fd_set* pExceptSet,
const struct timespec* pTimeout,
const sigset_t* pSigmask,
const char* pszSourceFile,
int nLineNumber
);
W32_DLL_IMPORT unsigned w32_if_nametoindex ( const char* ifname );
W32_DLL_IMPORT const char* w32_inet_ntop ( int af, const void* src, char* dst, socklen_t size );
W32_DLL_IMPORT int w32_inet_pton ( int af, const char* src, void* dst );
W32_DLL_IMPORT FILE* w32_fdopen ( int their_fd, const char* their_mode );
W32_DLL_IMPORT size_t w32_fwrite ( const void* buff, size_t size, size_t count, FILE* stream );
W32_DLL_IMPORT int w32_vsnprintf( char *bfr, size_t cnt, const char *fmt, va_list vargs ) ATTR_PRINTF(3,0);
W32_DLL_IMPORT int w32_snprintf ( char *bfr, size_t cnt, const char *fmt, ... ) ATTR_PRINTF(3,4);
W32_DLL_IMPORT int w32_fprintf( FILE* stream, const char* format, ... ) ATTR_PRINTF(2,3);
W32_DLL_IMPORT int w32_fclose ( FILE* stream );
W32_DLL_IMPORT int w32_get_stdin_char ( char* pCharBuff, int wait_millisecs );
W32_DLL_IMPORT pid_t w32_poor_mans_fork ( char* pszCommandLine, int* pnWriteToChildStdinFD );
W32_DLL_IMPORT void w32_set_thread_name( TID tid, const char* name );
W32_DLL_IMPORT unsigned long w32_hpagesize();
W32_DLL_IMPORT int w32_mlock (const void *addr, size_t len);
W32_DLL_IMPORT int w32_munlock (const void *addr, size_t len);
// Hercules low-level file open...
W32_DLL_IMPORT int w32_hopen ( const char* path, int oflag, ... );
//////////////////////////////////////////////////////////////////////////////////////////
// Windows implementation of basename and dirname functions
W32_DLL_IMPORT char* w32_basename( const char* path );
W32_DLL_IMPORT char* w32_dirname( const char* path );
W32_DLL_IMPORT char* w32_strcasestr( const char* haystack, const char* needle );
#endif // defined(_MSVC_)
//////////////////////////////////////////////////////////////////////////////////////////
// Support for disabling of CRT Invalid Parameter Handler...
#if defined( _MSVC_ ) && defined( _MSC_VER ) && ( _MSC_VER >= VS2005 )
#define DISABLE_CRT_INVALID_PARAMETER_HANDLER() DisableInvalidParameterHandling()
#define ENABLE_CRT_INVALID_PARAMETER_HANDLING() EnableInvalidParameterHandling()
W32_DLL_IMPORT void DisableInvalidParameterHandling();
W32_DLL_IMPORT void EnableInvalidParameterHandling();
#else // !defined( _MSVC_ ) || !defined( _MSC_VER ) || ( _MSC_VER < VS2005 )
#define DISABLE_CRT_INVALID_PARAMETER_HANDLER() /* (no nothing) */
#define ENABLE_CRT_INVALID_PARAMETER_HANDLING() /* (no nothing) */
#endif // defined( _MSVC_ ) && defined( _MSC_VER ) && ( _MSC_VER >= VS2005 )
//////////////////////////////////////////////////////////////////////////////////////////
#endif // _W32UTIL_H