1 //////////////////////////////////////////////////////////////////////////////////////////
2 //   w32util.h        Windows porting functions
3 //////////////////////////////////////////////////////////////////////////////////////////
4 // (c) Copyright "Fish" (David B. Trout), 2005-2009. Released under the Q Public License
5 // (http://www.hercules-390.org/herclic.html) as modifications to Hercules.
6 //////////////////////////////////////////////////////////////////////////////////////////
7 
8 #ifndef _W32UTIL_H
9 #define _W32UTIL_H
10 
11 #if defined( _MSVC_ )
12 
13 #include "hercules.h"
14 
15 #ifndef _W32UTIL_C_
16   #ifndef _HUTIL_DLL_
17     #define W32_DLL_IMPORT  DLL_IMPORT
18   #else
19     #define W32_DLL_IMPORT  extern
20   #endif
21 #else
22   #define   W32_DLL_IMPORT  DLL_EXPORT
23 #endif
24 
25 //////////////////////////////////////////////////////////////////////////////////////////
26 // Translates a Win32 '[WSA]GetLastError()' value into a 'errno' value (if possible
27 // and/or if needed) that can then be used in the below 'w32_strerror' string function...
28 
29 W32_DLL_IMPORT  int  w32_trans_w32error( const DWORD dwLastError );
30 
31 //////////////////////////////////////////////////////////////////////////////////////////
32 // ("unsafe" version -- use "safer" 'w32_strerror_r' instead if possible)
33 
34 W32_DLL_IMPORT  char*  w32_strerror( int errnum );
35 
36 //////////////////////////////////////////////////////////////////////////////////////////
37 // Handles both regular 'errno' values as well as [WSA]GetLastError() values too...
38 
39 W32_DLL_IMPORT int w32_strerror_r( int errnum, char* buffer, size_t buffsize );
40 
41 //////////////////////////////////////////////////////////////////////////////////////////
42 // Return Win32 error message text associated with an error number value
43 // as returned by a call to either GetLastError() or WSAGetLastError()...
44 
45 W32_DLL_IMPORT  char*  w32_w32errmsg( int errnum, char* pszBuffer, size_t nBuffSize );
46 
47 //////////////////////////////////////////////////////////////////////////////////////////
48 // Large File Support...
49 
50 #if (_MSC_VER < 1400)
51   W32_DLL_IMPORT  __int64  w32_ftelli64 ( FILE* stream );
52   W32_DLL_IMPORT    int    w32_fseeki64 ( FILE* stream, __int64 offset, int origin );
53   W32_DLL_IMPORT    int    w32_ftrunc64 ( int fd, __int64 new_size );
54 #endif
55 
56 //////////////////////////////////////////////////////////////////////////////////////////
57 
58 #if !defined( HAVE_SOCKETPAIR )
59   W32_DLL_IMPORT int socketpair( int domain, int type, int protocol, int socket_vector[2] );
60 #endif
61 
62 #if !defined( HAVE_FORK )
63   W32_DLL_IMPORT pid_t  fork( void );
64 #endif
65 
66 #if !defined( HAVE_STRTOK_R )
67 W32_DLL_IMPORT char* strtok_r ( char* s, const char* sep, char** lasts);
68 #endif
69 
70 #if !defined( HAVE_GETTIMEOFDAY )
71   W32_DLL_IMPORT int gettimeofday ( struct timeval* pTV, void* pTZ);
72 #endif
73 
74 #if !defined( HAVE_NANOSLEEP )
75   W32_DLL_IMPORT int nanosleep ( const struct timespec* rqtp, struct timespec* rmtp );
76 #endif
77 
78 #if !defined( HAVE_USLEEP )
79   W32_DLL_IMPORT int usleep ( useconds_t  useconds );
80 #endif
81 
82 // Can't use "HAVE_SLEEP" since Win32's "Sleep" causes HAVE_SLEEP to
83 // be erroneously #defined due to autoconf AC_CHECK_FUNCS case issues...
84 
85 //#if !defined( HAVE_SLEEP )
86   W32_DLL_IMPORT unsigned sleep ( unsigned seconds );
87 //#endif
88 
89 #if !defined( HAVE_SCHED_YIELD )
90   W32_DLL_IMPORT int sched_yield ( void );
91 #endif
92 
93 #if !defined( HAVE_GETPGRP )
94   #define  getpgrp  getpid
95 #endif
96 
97 #if !defined( HAVE_SCANDIR )
98   W32_DLL_IMPORT int scandir
99   (
100     const char *dir,
101     struct dirent ***namelist,
102     int (*filter)(const struct dirent *),
103     int (*compar)(const struct dirent **, const struct dirent **)
104   );
105 #endif
106 
107 #if !defined( HAVE_ALPHASORT )
108   W32_DLL_IMPORT int alphasort ( const struct dirent **a, const struct dirent **b );
109 #endif
110 
111 #if !defined(HAVE_SYS_RESOURCE_H)
112   // Note: we only provide the absolute minimum required information
113   #define  RUSAGE_SELF       0      // Current process
114   #define  RUSAGE_CHILDREN  -1      // Children of the current process
115   struct rusage                     // Resource utilization information
116   {
117     struct timeval  ru_utime;       // User time used
118     struct timeval  ru_stime;       // System time used
119   };
120   W32_DLL_IMPORT int getrusage ( int who, struct rusage* r_usage );
121 #endif
122 
123 #if !defined(HAVE_DECL_LOGIN_NAME_MAX) || !HAVE_DECL_LOGIN_NAME_MAX
124   #define  LOGIN_NAME_MAX  UNLEN
125 #endif
126 
127 #if !defined( HAVE_GETLOGIN )
128   W32_DLL_IMPORT char* getlogin ( void );
129 #endif
130 
131 #if !defined( HAVE_GETLOGIN_R )
132   W32_DLL_IMPORT int getlogin_r ( char* name, size_t namesize );
133 #endif
134 
135 #if !defined( HAVE_REALPATH )
136   W32_DLL_IMPORT char* realpath ( const char* file_name, char* resolved_name );
137 #endif
138 
139 // The inet_aton() function converts the specified string,
140 // in the Internet standard dot notation, to a network address,
141 // and stores the address in the structure provided.
142 //
143 // The inet_aton() function returns 1 if the address is successfully converted,
144 // or 0 if the conversion failed.
145 
146 #if !defined( HAVE_INET_ATON )
147   W32_DLL_IMPORT int inet_aton( const char* cp, struct in_addr* addr );
148 #endif
149 
150 // Returns outpath as a host filesystem compatible filename path.
151 // This is a Cygwin-to-MSVC transitional period helper function.
152 // On non-Windows platforms it simply copies inpath to outpath.
153 // On Windows it converts inpath of the form "/cygdrive/x/foo.bar"
154 // to outpath in the form "x:/foo.bar" for Windows compatibility.
155 W32_DLL_IMPORT BYTE *hostpath( BYTE *outpath, const BYTE *inpath, size_t buffsize );
156 
157 // Poor man's  "fcntl( fd, F_GETFL )"...
158 // (only returns access-mode flags and not any others)
159 W32_DLL_IMPORT int get_file_accmode_flags( int fd );
160 
161 // Retrieve unique host id
162 W32_DLL_IMPORT long gethostid( void );
163 
164 // Initialize/Deinitialize sockets package...
165 W32_DLL_IMPORT int  socket_init   ( void );
166 W32_DLL_IMPORT int  socket_deinit ( void );
167 
168 // Set socket to blocking or non-blocking mode...
169 W32_DLL_IMPORT int socket_set_blocking_mode( int sfd, int blocking_mode );
170 
171 // Determine whether a file descriptor is a socket or not...
172 // (returns 1==true if it's a socket, 0==false otherwise)
173 W32_DLL_IMPORT int socket_is_socket( int sfd );
174 
175 // Set the SO_KEEPALIVE option and timeout values for a
176 // socket connection to detect when client disconnects */
177 W32_DLL_IMPORT void socket_keepalive( int sfd, int idle_time, int probe_interval, int probe_count );
178 
179 // Retrieve directory where process was loaded from...
180 // (returns >0 == success, 0 == failure)
181 W32_DLL_IMPORT int get_process_directory( char* dirbuf, size_t bufsiz );
182 
183 // Expand environment variables... (e.g. %SystemRoot%, etc); 0==success
184 W32_DLL_IMPORT int expand_environ_vars( const char* inbuff, char* outbuff, DWORD outbufsiz );
185 
186 // Initialize Hercules HOSTINFO structure
187 W32_DLL_IMPORT void w32_init_hostinfo( HOST_INFO* pHostInfo );
188 
189 W32_DLL_IMPORT int   w32_socket   ( int af, int type, int protocol );
190 W32_DLL_IMPORT void  w32_FD_SET   ( int fd, fd_set* pSet );
191 W32_DLL_IMPORT int   w32_FD_ISSET ( int fd, fd_set* pSet );
192 W32_DLL_IMPORT int   w32_select   ( int nfds,
193                      fd_set* pReadSet,
194                      fd_set* pWriteSet,
195                      fd_set* pExceptSet,
196                      const struct timeval* pTimeVal,
197                      const char* pszSourceFile,
198                      int nLineNumber
199                    );
200 
201 W32_DLL_IMPORT FILE*  w32_fdopen ( int their_fd, const char* their_mode );
202 W32_DLL_IMPORT size_t w32_fwrite ( const void* buff, size_t size, size_t count, FILE* stream );
203 W32_DLL_IMPORT int    w32_fprintf( FILE* stream, const char* format, ... );
204 W32_DLL_IMPORT int    w32_fclose ( FILE* stream );
205 W32_DLL_IMPORT int    w32_get_stdin_char ( char* pCharBuff, int wait_millisecs );
206 W32_DLL_IMPORT pid_t  w32_poor_mans_fork ( char*  pszCommandLine, int* pnWriteToChildStdinFD );
207 W32_DLL_IMPORT void   w32_set_thread_name( TID tid, char* name );
208 W32_DLL_IMPORT int    w32_hopen ( const char* path, int oflag, ... );
209 
210 //////////////////////////////////////////////////////////////////////////////////////////
211 
212 #endif // defined(_MSVC_)
213 
214 //////////////////////////////////////////////////////////////////////////////////////////
215 // Support for disabling of CRT Invalid Parameter Handler...
216 
217 #if defined( _MSVC_ ) && defined( _MSC_VER ) && ( _MSC_VER >= 1400 )
218 
219 #define DISABLE_CRT_INVALID_PARAMETER_HANDLER()   DisableInvalidParameterHandling()
220 #define ENABLE_CRT_INVALID_PARAMETER_HANDLING()   EnableInvalidParameterHandling()
221 
222 W32_DLL_IMPORT  void  DisableInvalidParameterHandling();
223 W32_DLL_IMPORT  void  EnableInvalidParameterHandling();
224 
225 #else // !defined( _MSVC_ ) || !defined( _MSC_VER ) || ( _MSC_VER < 1400 )
226 
227 #define DISABLE_CRT_INVALID_PARAMETER_HANDLER()   /* (no nothing) */
228 #define ENABLE_CRT_INVALID_PARAMETER_HANDLING()   /* (no nothing) */
229 
230 #endif // defined( _MSVC_ ) && defined( _MSC_VER ) && ( _MSC_VER >= 1400 )
231 
232 //////////////////////////////////////////////////////////////////////////////////////////
233 
234 #endif // _W32UTIL_H
235