Changeset 3015 for box/trunk/lib/win32
- Timestamp:
- 09/10/2011 12:24:45 (8 months ago)
- Location:
- box/trunk/lib/win32
- Files:
-
- 1 edited
- 1 copied
-
emu.h (modified) (2 diffs)
-
emu_winver.h (copied) (copied from box/trunk/lib/win32/emu.h) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/trunk/lib/win32/emu.h
r3011 r3015 1 1 // emulates unix syscalls to win32 functions 2 3 #include "emu_winver.h" 2 4 3 5 #ifdef WIN32 … … 51 53 #endif 52 54 53 // set up to include the necessary parts of Windows headers54 55 #define WIN32_LEAN_AND_MEAN56 57 #ifndef __MSVCRT_VERSION__58 #define __MSVCRT_VERSION__ 0x060159 #endif60 61 // We need WINVER at least 0x0500 to use GetFileSizeEx on Cygwin/MinGW,62 // and 0x0501 for FindFirstFile(W) for opendir/readdir.63 //64 // WIN32_WINNT versions 0x0600 (Vista) and higher enable WSAPoll() in65 // winsock2.h, whose struct pollfd conflicts with ours below, so for66 // now we just set it lower than that, to Windows XP (0x0501).67 68 #ifdef WINVER69 # if WINVER != 0x050170 // provoke a redefinition warning to track down the offender71 # define WINVER 0x050172 # error Must include emu.h before setting WINVER73 # endif74 #endif75 #define WINVER 0x050176 77 #ifdef _WIN32_WINNT78 # if _WIN32_WINNT != 0x050179 // provoke a redefinition warning to track down the offender80 # define _WIN32_WINNT 0x050181 # error Must include emu.h before setting _WIN32_WINNT82 # endif83 #endif84 #define _WIN32_WINNT 0x050185 86 55 // Windows headers 87 56 -
box/trunk/lib/win32/emu_winver.h
r3011 r3015 1 // emulates unix syscalls to win32 functions 2 3 #ifdef WIN32 4 #define EMU_STRUCT_STAT struct emu_stat 5 #define EMU_STAT emu_stat 6 #define EMU_FSTAT emu_fstat 7 #define EMU_LSTAT emu_stat 8 #else 9 #define EMU_STRUCT_STAT struct stat 10 #define EMU_STAT ::stat 11 #define EMU_FSTAT ::fstat 12 #define EMU_LSTAT ::lstat 13 #endif 14 15 #if ! defined EMU_INCLUDE && defined WIN32 16 #define EMU_INCLUDE 17 18 // Need feature detection macros below 19 #include "../common/BoxConfig.h" 20 21 // Shut up stupid new warnings. Thanks MinGW! Ever heard of "compatibility"? 22 #ifdef __MINGW32__ 23 # define __MINGW_FEATURES__ 0 24 #endif 25 26 // basic types, may be required by other headers since we 27 // don't include sys/types.h 28 29 #ifdef __MINGW32__ 30 #include <stdint.h> 31 #else // MSVC 32 typedef unsigned __int64 u_int64_t; 33 typedef unsigned __int64 uint64_t; 34 typedef __int64 int64_t; 35 typedef unsigned __int32 uint32_t; 36 typedef unsigned __int32 u_int32_t; 37 typedef __int32 int32_t; 38 typedef unsigned __int16 uint16_t; 39 typedef __int16 int16_t; 40 typedef unsigned __int8 uint8_t; 41 typedef __int8 int8_t; 42 #endif 43 44 // emulated types, present on MinGW but not MSVC or vice versa 45 46 #ifdef __MINGW32__ 47 typedef uint32_t u_int32_t; 48 #else 49 typedef unsigned int mode_t; 50 typedef unsigned int pid_t; 51 #endif 1 #ifndef _EMU_WINVER_H 2 #define _EMU_WINVER_H 52 3 53 4 // set up to include the necessary parts of Windows headers … … 84 35 #define _WIN32_WINNT 0x0501 85 36 86 // Windows headers 87 88 #include <winsock2.h> 89 #include <fcntl.h> 90 #include <sys/stat.h> 91 #include <direct.h> 92 #include <errno.h> 93 #include <io.h> 94 #include <stdlib.h> 95 #include <string.h> 96 #include <stdio.h> 97 #include <stdarg.h> 98 #include <time.h> 99 100 #include <string> 101 102 // emulated functions 103 104 #define gmtime_r( _clock, _result ) \ 105 ( *(_result) = *gmtime( (_clock) ), \ 106 (_result) ) 107 108 #define ITIMER_REAL 0 109 110 #ifdef _MSC_VER 111 // Microsoft decided to deprecate the standard POSIX functions. Great! 112 #define open(file,flags,mode) _open(file,flags,mode) 113 #define close(fd) _close(fd) 114 #define dup(fd) _dup(fd) 115 #define read(fd,buf,count) _read(fd,buf,count) 116 #define write(fd,buf,count) _write(fd,buf,count) 117 #define lseek(fd,off,whence) _lseek(fd,off,whence) 118 #define fileno(struct_file) _fileno(struct_file) 119 #endif 120 121 struct passwd { 122 char *pw_name; 123 char *pw_passwd; 124 int pw_uid; 125 int pw_gid; 126 time_t pw_change; 127 char *pw_class; 128 char *pw_gecos; 129 char *pw_dir; 130 char *pw_shell; 131 time_t pw_expire; 132 }; 133 134 extern passwd gTempPasswd; 135 inline struct passwd * getpwnam(const char * name) 136 { 137 //for the mo pretend to be root 138 gTempPasswd.pw_uid = 0; 139 gTempPasswd.pw_gid = 0; 140 141 return &gTempPasswd; 142 } 143 144 #define S_IRWXG 1 145 #define S_IRWXO 2 146 #define S_ISUID 4 147 #define S_ISGID 8 148 #define S_ISVTX 16 149 150 #ifndef __MINGW32__ 151 //not sure if these are correct 152 //S_IWRITE - writing permitted 153 //_S_IREAD - reading permitted 154 //_S_IREAD | _S_IWRITE - 155 #define S_IRUSR S_IWRITE 156 #define S_IWUSR S_IREAD 157 #define S_IRWXU (S_IREAD|S_IWRITE|S_IEXEC) 158 159 #define S_ISREG(x) (S_IFREG & x) 160 #define S_ISDIR(x) (S_IFDIR & x) 161 #endif 162 163 inline int chown(const char * Filename, u_int32_t uid, u_int32_t gid) 164 { 165 //important - this needs implementing 166 //If a large restore is required then 167 //it needs to restore files AND permissions 168 //reference AdjustTokenPrivileges 169 //GetAccountSid 170 //InitializeSecurityDescriptor 171 //SetSecurityDescriptorOwner 172 //The next function looks like the guy to use... 173 //SetFileSecurity 174 175 //indicate success 176 return 0; 177 } 178 179 // Windows and Unix owners and groups are pretty fundamentally different. 180 // Ben prefers that we kludge here rather than litter the code with #ifdefs. 181 // Pretend to be root, and pretend that set...() operations succeed. 182 inline int setegid(int) 183 { 184 return true; 185 } 186 inline int seteuid(int) 187 { 188 return true; 189 } 190 inline int setgid(int) 191 { 192 return true; 193 } 194 inline int setuid(int) 195 { 196 return true; 197 } 198 inline int getgid(void) 199 { 200 return 0; 201 } 202 inline int getuid(void) 203 { 204 return 0; 205 } 206 inline int geteuid(void) 207 { 208 return 0; 209 } 210 211 #ifndef PATH_MAX 212 #define PATH_MAX MAX_PATH 213 #endif 214 215 // MinGW provides a getopt implementation 216 #ifndef __MINGW32__ 217 #include "getopt.h" 218 #endif // !__MINGW32__ 219 220 #define timespec timeval 221 222 //win32 deals in usec not nsec - so need to ensure this follows through 223 #define tv_nsec tv_usec 224 225 #ifndef __MINGW32__ 226 typedef int socklen_t; 227 #endif 228 229 #define S_IRGRP S_IWRITE 230 #define S_IWGRP S_IREAD 231 #define S_IROTH S_IWRITE | S_IREAD 232 #define S_IWOTH S_IREAD | S_IREAD 233 234 //again need to verify these 235 #define S_IFLNK 1 236 #define S_IFSOCK 0 237 238 #define S_ISLNK(x) ( false ) 239 240 #define vsnprintf _vsnprintf 241 242 #ifndef __MINGW32__ 243 inline int strcasecmp(const char *s1, const char *s2) 244 { 245 return _stricmp(s1,s2); 246 } 247 #endif 248 249 #ifdef _DIRENT_H_ 250 #error You must not include the MinGW dirent.h! 251 #endif 252 253 struct dirent 254 { 255 char *d_name; 256 DWORD d_type; // file attributes 257 }; 258 259 struct DIR 260 { 261 HANDLE fd; // the HANDLE returned by FindFirstFile 262 WIN32_FIND_DATAW info; 263 struct dirent result; // d_name (first time null) 264 wchar_t* name; // null-terminated byte string 265 }; 266 267 DIR *opendir(const char *name); 268 struct dirent *readdir(DIR *dp); 269 int closedir(DIR *dp); 270 271 // local constant to open file exclusively without shared access 272 #define O_LOCK 0x10000 273 274 extern DWORD winerrno; /* used to report errors from openfile() */ 275 HANDLE openfile(const char *filename, int flags, int mode); 276 inline int closefile(HANDLE handle) 277 { 278 if (CloseHandle(handle) != TRUE) 279 { 280 errno = EINVAL; 281 return -1; 282 } 283 return 0; 284 } 285 286 #define LOG_DEBUG LOG_INFO 287 #define LOG_INFO 6 288 #define LOG_NOTICE LOG_INFO 289 #define LOG_WARNING 4 290 #define LOG_ERR 3 291 #define LOG_CRIT LOG_ERR 292 #define LOG_PID 0 293 #define LOG_LOCAL5 0 294 #define LOG_LOCAL6 0 295 296 void openlog (const char * daemonName, int, int); 297 void closelog(void); 298 void syslog (int loglevel, const char *fmt, ...); 299 300 #define LOG_LOCAL0 0 301 #define LOG_LOCAL1 0 302 #define LOG_LOCAL2 0 303 #define LOG_LOCAL3 0 304 #define LOG_LOCAL4 0 305 #define LOG_LOCAL5 0 306 #define LOG_LOCAL6 0 307 #define LOG_DAEMON 0 308 309 #ifndef __MINGW32__ 310 #define strtoll _strtoi64 311 #endif 312 313 inline unsigned int sleep(unsigned int secs) 314 { 315 Sleep(secs*1000); 316 return(ERROR_SUCCESS); 317 } 318 319 #define INFTIM -1 320 321 #ifndef POLLIN 322 # define POLLIN 0x1 323 #endif 324 325 #ifndef POLLERR 326 # define POLLERR 0x8 327 #endif 328 329 #ifndef POLLOUT 330 # define POLLOUT 0x4 331 #endif 332 333 #define SHUT_RDWR SD_BOTH 334 #define SHUT_RD SD_RECEIVE 335 #define SHUT_WR SD_SEND 336 337 struct pollfd 338 { 339 SOCKET fd; 340 short int events; 341 short int revents; 342 }; 343 344 inline int ioctl(SOCKET sock, int flag, int * something) 345 { 346 //indicate success 347 return 0; 348 } 349 350 inline int waitpid(pid_t pid, int *status, int) 351 { 352 return 0; 353 } 354 355 //this shouldn't be needed. 356 struct statfs 357 { 358 TCHAR f_mntonname[MAX_PATH]; 359 }; 360 361 struct emu_stat { 362 int st_dev; 363 uint64_t st_ino; 364 DWORD st_mode; 365 short st_nlink; 366 short st_uid; 367 short st_gid; 368 //_dev_t st_rdev; 369 uint64_t st_size; 370 time_t st_atime; 371 time_t st_mtime; 372 time_t st_ctime; 373 }; 374 375 // need this for conversions 376 time_t ConvertFileTimeToTime_t(FILETIME *fileTime); 377 bool ConvertTime_tToFileTime(const time_t from, FILETIME *pTo); 378 379 int emu_chdir (const char* pDirName); 380 int emu_mkdir (const char* pPathName); 381 int emu_unlink (const char* pFileName); 382 int emu_fstat (HANDLE file, struct emu_stat* st); 383 int emu_stat (const char* pName, struct emu_stat* st); 384 int emu_utimes (const char* pName, const struct timeval[]); 385 int emu_chmod (const char* pName, mode_t mode); 386 char* emu_getcwd (char* pBuffer, int BufSize); 387 int emu_rename (const char* pOldName, const char* pNewName); 388 389 #define chdir(directory) emu_chdir (directory) 390 #define mkdir(path, mode) emu_mkdir (path) 391 #define unlink(file) emu_unlink (file) 392 #define utimes(buffer, times) emu_utimes (buffer, times) 393 #define chmod(file, mode) emu_chmod (file, mode) 394 #define getcwd(buffer, size) emu_getcwd (buffer, size) 395 #define rename(oldname, newname) emu_rename (oldname, newname) 396 397 // Not safe to replace stat/fstat/lstat on mingw at least, as struct stat 398 // has a 16-bit st_ino and we need a 64-bit one. 399 // 400 // #define stat(filename, struct) emu_stat (filename, struct) 401 // #define lstat(filename, struct) emu_stat (filename, struct) 402 // #define fstat(handle, struct) emu_fstat (handle, struct) 403 // 404 // But lstat doesn't exist on Windows, so we have to provide something: 405 406 #define lstat(filename, struct) stat(filename, struct) 407 408 int statfs(const char * name, struct statfs * s); 409 410 int poll(struct pollfd *ufds, unsigned long nfds, int timeout); 411 412 struct iovec { 413 void *iov_base; /* Starting address */ 414 size_t iov_len; /* Number of bytes */ 415 }; 416 417 int readv (int filedes, const struct iovec *vector, size_t count); 418 int writev(int filedes, const struct iovec *vector, size_t count); 419 420 // The following functions are not emulations, but utilities for other 421 // parts of the code where Windows API is used or windows-specific stuff 422 // is needed, like codepage conversion. 423 424 bool EnableBackupRights( void ); 425 426 bool ConvertEncoding (const std::string& rSource, int sourceCodePage, 427 std::string& rDest, int destCodePage); 428 bool ConvertToUtf8 (const std::string& rSource, std::string& rDest, 429 int sourceCodePage); 430 bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest, 431 int destCodePage); 432 bool ConvertUtf8ToConsole(const std::string& rSource, std::string& rDest); 433 bool ConvertConsoleToUtf8(const std::string& rSource, std::string& rDest); 434 char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage); 435 bool ConvertFromWideString(const std::wstring& rInput, 436 std::string* pOutput, unsigned int codepage); 437 std::string ConvertPathToAbsoluteUnicode(const char *pFileName); 438 439 // Utility function which returns a default config file name, 440 // based on the path of the current executable. 441 std::string GetDefaultConfigFilePath(const std::string& rName); 442 443 // GetErrorMessage() returns a system error message, like strerror() 444 // but for Windows error codes. 445 std::string GetErrorMessage(DWORD errorCode); 446 447 // console_read() is a replacement for _cgetws which requires a 448 // relatively recent C runtime lib 449 int console_read(char* pBuffer, size_t BufferSize); 450 451 // Defined thus by MinGW, but missing from MSVC 452 // [http://curl.haxx.se/mail/lib-2004-11/0260.html] 453 // note: chsize() doesn't work over 2GB: 454 // [https://stat.ethz.ch/pipermail/r-devel/2005-May/033339.html] 455 #ifndef HAVE_FTRUNCATE 456 extern "C" int ftruncate(int, off_t); 457 inline int ftruncate(int __fd, off_t __length) 458 { 459 return _chsize(__fd, __length); 460 } 461 #endif 462 463 #ifdef _MSC_VER 464 /* disable certain compiler warnings to be able to actually see the show-stopper ones */ 465 #pragma warning(disable:4101) // unreferenced local variable 466 #pragma warning(disable:4244) // conversion, possible loss of data 467 #pragma warning(disable:4267) // conversion, possible loss of data 468 #pragma warning(disable:4311) // pointer truncation 469 #pragma warning(disable:4700) // uninitialized local variable used (hmmmmm...) 470 #pragma warning(disable:4805) // unsafe mix of type and type 'bool' in operation 471 #pragma warning(disable:4800) // forcing value to bool 'true' or 'false' (performance warning) 472 #pragma warning(disable:4996) // POSIX name for this item is deprecated 473 #endif // _MSC_VER 474 475 #endif // !EMU_INCLUDE && WIN32 37 #endif // _EMU_WINVER_H
Note: See TracChangeset
for help on using the changeset viewer.
