libc.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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( GetConsoleWindow() ){
  30. flags=0;
  31. }
  32. if( !CreateProcessA( 0,(LPSTR)tmp.c_str(),0,0,inherit,flags,0,0,&si,&pi ) ) return -1;
  33. WaitForSingleObject( pi.hProcess,INFINITE );
  34. int res=GetExitCodeProcess( pi.hProcess,(DWORD*)&res ) ? res : -1;
  35. CloseHandle( pi.hProcess );
  36. CloseHandle( pi.hThread );
  37. return res;
  38. #else
  39. return system( cmd );
  40. #endif
  41. }
  42. int mkdir_( const char *path,int mode ){
  43. #if _WIN32
  44. return mkdir( path );
  45. #else
  46. return mkdir( path,0777 );
  47. #endif
  48. }
  49. int gettimeofday_( timeval *tv ){
  50. return gettimeofday( tv,0 );
  51. }