libc.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "libc.h"
  2. /*
  3. #if _WIN32
  4. #include <windows.h>
  5. #include <bbstring.h>
  6. #if _MSC_VER
  7. #include <stdint.h>
  8. #endif
  9. #elif __APPLE__
  10. #include <TargetConditionals.h>
  11. #endif
  12. */
  13. #if _WIN32
  14. struct DIR{
  15. WIN32_FIND_DATAW ffd;
  16. HANDLE hfind;
  17. dirent entry;
  18. };
  19. static void *tmps[32];
  20. static int tmpsi;
  21. static const WCHAR *widen( const char *p ){
  22. int n=MultiByteToWideChar( CP_UTF8,0,p,-1,0,0 );
  23. WCHAR *w=(WCHAR*)malloc( n*2 );
  24. MultiByteToWideChar( CP_UTF8,0,p,-1,w,n );
  25. free(tmps[tmpsi&31]);
  26. tmps[(tmpsi++)&31]=w;
  27. return w;
  28. }
  29. static const char *narrow( const WCHAR *w ){
  30. int n=WideCharToMultiByte( CP_UTF8,0,w,-1,0,0,0,0 );
  31. char *p=(char*)malloc( n );
  32. WideCharToMultiByte( CP_UTF8,0,w,-1,p,n,0,0 );
  33. free(tmps[tmpsi&31]);
  34. tmps[(tmpsi++)&31]=p;
  35. return p;
  36. }
  37. const wchar_t *widen_utf8( const char *p ){
  38. return widen( p );
  39. }
  40. const char *narrow_utf8( const wchar_t *w ){
  41. return narrow( w );
  42. }
  43. FILE *fopen_utf8( const char *filename,const char *mode ){
  44. return _wfopen( widen( filename ),widen( mode ) );
  45. }
  46. int fputs_utf8( const char *str,FILE *stream ){
  47. return fputws( widen( str ),stream );
  48. }
  49. int remove_utf8( const char *path ){
  50. return _wremove( widen( path ) );
  51. }
  52. int rename_utf8( const char *oldpath,const char *newpath ){
  53. return _wrename( widen( oldpath ),widen( newpath ) );
  54. }
  55. int puts_utf8( const char *str ){
  56. return _putws( widen( str ) );
  57. }
  58. int setenv_utf8( const char *name,const char *value,int overwrite ){
  59. const WCHAR *wname=widen( name );
  60. if( !overwrite && _wgetenv( wname ) ) return -1;
  61. WCHAR *wbuf=(WCHAR*)malloc( (strlen(name)+strlen(value)+2)*2 );
  62. wcscpy( wbuf,wname );
  63. wcscat( wbuf,L"=" );
  64. wcscat( wbuf,widen( value ) );
  65. int n=_wputenv( wbuf );
  66. return n;
  67. }
  68. char *getenv_utf8( const char *name ){
  69. static char *p;
  70. const WCHAR *wname=widen( name );
  71. WCHAR *w=_wgetenv( wname );
  72. if( !w ) return 0;
  73. free( p );
  74. p=strdup( narrow( w ) );
  75. return p;
  76. }
  77. int system_utf8( const char *cmd ){
  78. bool inherit=false;
  79. DWORD flags=CREATE_NO_WINDOW;
  80. STARTUPINFOW si={sizeof(si)};
  81. PROCESS_INFORMATION pi={0};
  82. if( GetStdHandle( STD_OUTPUT_HANDLE ) ){
  83. inherit=true;
  84. si.dwFlags=STARTF_USESTDHANDLES;
  85. si.hStdInput=GetStdHandle( STD_INPUT_HANDLE );
  86. si.hStdOutput=GetStdHandle( STD_OUTPUT_HANDLE );
  87. si.hStdError=GetStdHandle( STD_ERROR_HANDLE );
  88. }
  89. if( GetConsoleWindow() ){
  90. flags=0;
  91. }
  92. // bbString tmp=BB_T( "cmd /S /C\"" )+BB_T( cmd )+BB_T( "\"" );
  93. const WCHAR *wopts=L"cmd /S /C\"";
  94. const WCHAR *wcmd=widen( cmd );
  95. WCHAR *wtmp=(WCHAR*)malloc( (wcslen( wopts )+wcslen( wcmd )+2)*2 );
  96. wcscpy( wtmp,wopts );
  97. wcscat( wtmp,wcmd );
  98. wcscat( wtmp,L"\"" );
  99. if( !CreateProcessW( 0,wtmp,0,0,inherit,flags,0,0,&si,&pi ) ) return -1;
  100. WaitForSingleObject( pi.hProcess,INFINITE );
  101. int res=GetExitCodeProcess( pi.hProcess,(DWORD*)&res ) ? res : -1;
  102. CloseHandle( pi.hProcess );
  103. CloseHandle( pi.hThread );
  104. return res;
  105. }
  106. char *realpath_utf8( const char *path,char *rpath ){
  107. if( !rpath ){
  108. rpath=(char*)malloc( PATH_MAX );
  109. if( realpath_utf8( path,rpath ) ) return rpath;
  110. free( rpath );
  111. return 0;
  112. }
  113. WCHAR wbuf[PATH_MAX];
  114. if( !GetFullPathNameW( widen( path ),PATH_MAX,wbuf,0 ) ) return 0;
  115. WideCharToMultiByte( CP_UTF8,0,wbuf,-1,rpath,PATH_MAX,0,0 );
  116. return rpath;
  117. }
  118. int mkdir_utf8( const char *path,int mode ){
  119. return _wmkdir( widen( path ) );
  120. }
  121. char *getcwd_utf8( char *buf,int size ){
  122. WCHAR wbuf[PATH_MAX];
  123. if( !GetCurrentDirectoryW( size,wbuf ) ) return 0;
  124. WideCharToMultiByte( CP_UTF8,0,wbuf,-1,buf,size,0,0 );
  125. return buf;
  126. }
  127. int chdir_utf8( const char *path ){
  128. return SetCurrentDirectoryW( widen( path ) ) ? 0 : -1;
  129. }
  130. int rmdir_utf8( const char *path ){
  131. return _wrmdir( widen( path ) );
  132. }
  133. int stat_utf8( const char *path,stat_t *buf ){
  134. return _wstat( widen( path ),buf );
  135. }
  136. DIR *opendir_utf8( const char *path ){
  137. const WCHAR *wpath=widen( path );
  138. WCHAR *wbuf=(WCHAR*)malloc( (wcslen(wpath)+3)*2 );
  139. wcscpy( wbuf,wpath );
  140. wcscat( wbuf,L"\\*" );
  141. DIR *dir=(DIR*)malloc( sizeof( DIR ) );
  142. memset( dir,0,sizeof(DIR) );
  143. dir->hfind=FindFirstFileW( wbuf,&dir->ffd );
  144. if( !dir->hfind ){
  145. free( dir );
  146. dir=0;
  147. }
  148. free( wbuf );
  149. return dir;
  150. }
  151. dirent *readdir_utf8( DIR *dir ){
  152. if( !dir->hfind ) return 0;
  153. const char *p=narrow( dir->ffd.cFileName );
  154. free( dir->entry.d_name );
  155. dir->entry.d_name=strdup( p );
  156. if( !FindNextFileW( dir->hfind,&dir->ffd ) ){
  157. FindClose( dir->hfind );
  158. dir->hfind=0;
  159. }
  160. return &dir->entry;
  161. }
  162. void closedir_utf8( DIR *dir ){
  163. if( dir->hfind ) FindClose( dir->hfind );
  164. free( dir->entry.d_name );
  165. free( dir );
  166. }
  167. #endif