libc.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "libc.h"
  2. #if _WIN32
  3. #include <windows.h>
  4. #include <bbstring.h>
  5. #endif
  6. void setenv_( const char *name,const char *value,int overwrite ){
  7. #if _WIN32
  8. if( !overwrite && getenv( name ) ) return;
  9. bbString tmp=bbString( name )+BB_T( "=" )+bbString( value );
  10. putenv( tmp.c_str() );
  11. #else
  12. setenv( name,value,overwrite );
  13. #endif
  14. }
  15. int system_( const char *cmd ){
  16. #if _WIN32
  17. bool inherit=false;
  18. DWORD flags=CREATE_NO_WINDOW;
  19. STARTUPINFOA si={sizeof(si)};
  20. PROCESS_INFORMATION pi={0};
  21. bbString tmp=BB_T( "cmd /S /C\"" )+BB_T( cmd )+BB_T( "\"" );
  22. if( GetStdHandle( STD_OUTPUT_HANDLE ) ){
  23. inherit=true;
  24. si.dwFlags=STARTF_USESTDHANDLES;
  25. si.hStdInput=GetStdHandle( STD_INPUT_HANDLE );
  26. si.hStdOutput=GetStdHandle( STD_OUTPUT_HANDLE );
  27. si.hStdError=GetStdHandle( STD_ERROR_HANDLE );
  28. }
  29. if( !CreateProcessA( 0,(LPSTR)tmp.c_str(),0,0,inherit,flags,0,0,&si,&pi ) ) return -1;
  30. WaitForSingleObject( pi.hProcess,INFINITE );
  31. int res=GetExitCodeProcess( pi.hProcess,(DWORD*)&res ) ? res : -1;
  32. CloseHandle( pi.hProcess );
  33. CloseHandle( pi.hThread );
  34. return res;
  35. #else
  36. return system( cmd );
  37. #endif
  38. }
  39. int mkdir_( const char *path,int mode ){
  40. #if _WIN32
  41. return mkdir( path );
  42. #else
  43. return mkdir( path,0777 );
  44. #endif
  45. }
  46. int gettimeofday_( timeval *tv ){
  47. return gettimeofday( tv,0 );
  48. }