void FileListBox::PopulateFileList(const std::string& path) { new_path = path; if (path.compare(path.size()-1, sizeof(PATH_SEPARATOR), PATH_SEPARATOR)) new_path += PATH_SEPARATOR; MSG_DEBUG("file", "Searching in %s\n", new_path.c_str()); FolderSearch *f = OpenFolder(new_path); // Now that we have made use of new_path, it can be freed: // clearing the list is now possible Clear(); if (f) { bool is_file = list_files; const char *name; while ((name = FolderSearchNext(f, is_file)) != NULL) { if (is_file) { // We have a file, check that it validates the list if (MatchFilter(name)) { std::string* filename = new std::string(new_path); *filename += name; MSG_DEBUG("file", "Adding file %s\n", name); AddLabelItem(false, ANSIToUTF8(new_path, name), filename, Font::FONT_MEDIUM); } else { MSG_DEBUG("file", "NOT adding file %s, invalid extension\n", name); } } else if (strcmp(name, ".")) { std::string* filename; if (!strcmp(name, "..")) { // Are we at the root? if (!strcmp(name, PATH_SEPARATOR)) break; size_t pos = new_path.find_last_of(PATH_SEPARATOR, new_path.size()-2, sizeof(PATH_SEPARATOR)); filename = new std::string(new_path.substr(0, pos+1)); } else filename = new std::string(new_path); *filename += name; MSG_DEBUG("file", "Adding directory %s\n", name); AddLabelItem(false, std::string("[") + ANSIToUTF8(new_path, name) + "]", filename, Font::FONT_MEDIUM, Font::FONT_NORMAL, c_yellow); } else MSG_DEBUG("file", "Rejecting %s\n", name); // Prepare again for searching files is_file = list_files; } CloseFolder(f); Pack(); NeedRedrawing(); } else { MSG_DEBUG("file", "Search failed?\n"); } // Store last time to drop fast clicks last_time = SDL_GetTicks(); }
void Enum_Drectory(void *cmd) { Json::FastWriter fast_writer; Json::Value root; string Base64Result; cmdinfo *pci=(cmdinfo*)cmd; if (pci->cmd_content.compare("/")==0) { string utf8_drive; //Msg("获取驱动器\r\n"); GetDirverInfo(&root); ANSIToUTF8(fast_writer.write(root).c_str(),&utf8_drive); Base64Encode(reinterpret_cast<const unsigned char*>(utf8_drive.c_str()),utf8_drive.length(),Base64Result); Replace_plus(Base64Result); SubmitResult(pci,Base64Result.c_str()); Base64Result.erase(); utf8_drive.erase(); root.clear(); } else { //Msg("获取文件夹列表\r\n"); Enum_Current_Drectory((char *)pci->cmd_content.c_str(),&root); Base64Encode(reinterpret_cast<const unsigned char*>(fast_writer.write(root).c_str()),fast_writer.write(root).length(),Base64Result); Replace_plus(Base64Result); SubmitResult(pci,Base64Result.c_str()); Base64Result.erase(); root.clear(); } }
MVMint64 MVM_proc_shell(MVMThreadContext *tc, MVMString *cmd, MVMString *cwd, MVMObject *env) { MVMint64 result = 0, spawn_result = 0; uv_process_t *process = calloc(1, sizeof(uv_process_t)); uv_process_options_t process_options = {0}; uv_stdio_container_t process_stdio[3]; int i; char * const cmdin = MVM_string_utf8_encode_C_string(tc, cmd); char * const _cwd = MVM_string_utf8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); MVMIter * const iter = (MVMIter *)MVM_iter(tc, env); char **_env = malloc((size + 1) * sizeof(char *)); #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ char * const _cmd = ANSIToUTF8(acp, getenv("ComSpec")); char *args[3]; args[0] = "/c"; { MVMint64 len = strlen(cmdin); MVMint64 i; for (i = 0; i < len; i++) if (cmdin[i] == '/') cmdin[i] = '\\'; } args[1] = cmdin; args[2] = NULL; #else char * const _cmd = "/bin/sh"; char *args[4]; args[0] = "/bin/sh"; args[1] = "-c"; args[2] = cmdin; args[3] = NULL; #endif INIT_ENV(); SPAWN(_cmd); FREE_ENV(); free(_cwd); #ifdef _WIN32 free(_cmd); #endif free(cmdin); return result; }
MVMObject * MVM_proc_getenvhash(MVMThreadContext *tc) { MVMInstance * const instance = tc->instance; MVMObject * env_hash; #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ #endif MVMuint32 pos = 0; MVMString *needle = MVM_string_ascii_decode(tc, instance->VMString, STR_WITH_LEN("=")); char *env; MVM_gc_root_temp_push(tc, (MVMCollectable **)&needle); env_hash = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_hash_type); MVM_gc_root_temp_push(tc, (MVMCollectable **)&env_hash); while ((env = environ[pos++]) != NULL) { #ifndef _WIN32 MVMString *str = MVM_string_utf8_c8_decode(tc, instance->VMString, env, strlen(env)); #else char * const _env = ANSIToUTF8(acp, env); MVMString *str = MVM_string_utf8_c8_decode(tc, instance->VMString, _env, strlen(_env)); #endif MVMuint32 index = MVM_string_index(tc, str, needle, 0); MVMString *key, *val; MVMObject *box; #ifdef _WIN32 MVM_free(_env); #endif MVM_gc_root_temp_push(tc, (MVMCollectable **)&str); key = MVM_string_substring(tc, str, 0, index); MVM_gc_root_temp_push(tc, (MVMCollectable **)&key); val = MVM_string_substring(tc, str, index + 1, -1); box = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, val); MVM_repr_bind_key_o(tc, env_hash, key, box); MVM_gc_root_temp_pop_n(tc, 2); } MVM_gc_root_temp_pop_n(tc, 2); return env_hash; }
MVMObject * MVM_proc_getenvhash(MVMThreadContext *tc) { static MVMObject *env_hash; if (!env_hash) { #ifdef _WIN32 MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ #endif MVMuint32 pos = 0; MVMString *needle = MVM_decode_C_buffer_to_string(tc, tc->instance->VMString, "=", 1, MVM_encoding_type_ascii); char *env; MVM_gc_root_temp_push(tc, (MVMCollectable **)&needle); env_hash = MVM_repr_alloc_init(tc, tc->instance->boot_types->BOOTHash); MVM_gc_root_temp_push(tc, (MVMCollectable **)&env_hash); while ((env = environ[pos++]) != NULL) { #ifndef _WIN32 MVMString *str = MVM_decode_C_buffer_to_string(tc, tc->instance->VMString, env, strlen(env), MVM_encoding_type_utf8); #else char * const _env = ANSIToUTF8(acp, env); MVMString *str = MVM_decode_C_buffer_to_string(tc, tc->instance->VMString, _env, strlen(_env), MVM_encoding_type_utf8); #endif MVMuint32 index = MVM_string_index(tc, str, needle, 0); MVMString *key, *val; #ifdef _WIN32 free(_env); #endif MVM_gc_root_temp_push(tc, (MVMCollectable **)&str); key = MVM_string_substring(tc, str, 0, index); MVM_gc_root_temp_push(tc, (MVMCollectable **)&key); val = MVM_string_substring(tc, str, index + 1, -1); MVM_repr_bind_key_boxed(tc, env_hash, key, (MVMObject *)val); MVM_gc_root_temp_pop_n(tc, 2); } MVM_gc_root_temp_pop_n(tc, 2); } return env_hash; }
MVMint64 MVM_proc_spawn(MVMThreadContext *tc, MVMString *cmd, MVMString *cwd, MVMObject *env) { MVMint64 result, spawn_result; uv_process_t process = {0}; uv_process_options_t process_options = {0}; int i; char * const cmdin = MVM_string_utf8_encode_C_string(tc, cmd); char * const _cwd = MVM_string_utf8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); MVMIter * const iter = (MVMIter *)MVM_iter(tc, env); char **_env = malloc((size + 1) * sizeof(char *)); #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ char * const _cmd = ANSIToUTF8(acp, getenv("ComSpec")); char *args[3]; args[0] = "/c"; args[1] = cmdin; args[2] = NULL; #else char * const _cmd = "/bin/sh"; char *args[4]; args[0] = "/bin/sh"; args[1] = "-c"; args[2] = cmdin; args[3] = NULL; #endif MVMROOT(tc, iter, { MVMString * const equal = MVM_string_ascii_decode(tc, tc->instance->VMString, STR_WITH_LEN("=")); MVMROOT(tc, equal, { MVMString *env_str; i = 0; while(MVM_iter_istrue(tc, iter)) { MVM_repr_shift_o(tc, (MVMObject *)iter); env_str = MVM_string_concatenate(tc, MVM_iterkey_s(tc, iter), equal); env_str = MVM_string_concatenate(tc, env_str, MVM_repr_get_str(tc, MVM_iterval(tc, iter))); _env[i++] = MVM_string_utf8_encode_C_string(tc, env_str); } _env[size] = NULL; }); });
char * ConvertMessageToUTF8(char* wText) { char * utf8RetStr = NULL; int tmpLen = 0; if(!wText) return utf8RetStr; #ifdef WIN32 utf8RetStr = ANSIToUTF8(wText); tmpLen = !utf8RetStr ? 0 : strlen(utf8RetStr); if(tmpLen == 1) { if(utf8RetStr) free(utf8RetStr); utf8RetStr = UnicodeToUTF8(wText); } #else utf8RetStr = strdup(wText); #endif return utf8RetStr; }
bool initKniPath(void) { char buff[1024]; char utf8Buff[256*3]; char* p; HWND hwnd; memset(_madaRootPath, 0, sizeof(_madaRootPath)); memset(buff, 0, sizeof(buff)); hwnd = GetModuleHandle("kni.dll"); GetModuleFileName(hwnd, buff, sizeof(buff)); //_getcwd(buff, sizeof( buff)); replaceFilePath(buff); cTrace("work dir=%s\n", buff); p = strstr(buff, "/root/bin"); if(p != NULL) { *p = 0; strcat(buff, "/root"); } else { _getcwd(buff, sizeof( buff)); replaceFilePath(buff); p = strstr(buff, "/root/lib"); if(p != NULL) { *p = 0; strcat(buff, "/root"); } else { } } memset(utf8Buff, 0, sizeof(utf8Buff)); ANSIToUTF8(buff, strlen(buff), utf8Buff, sizeof(utf8Buff)); strcpy(_madaRootPath, utf8Buff); return true; }
MVMint64 MVM_proc_shell(MVMThreadContext *tc, MVMString *cmd, MVMString *cwd, MVMObject *env, MVMObject *in, MVMObject *out, MVMObject *err, MVMint64 flags) { MVMint64 result = 0, spawn_result; uv_process_t *process = MVM_calloc(1, sizeof(uv_process_t)); uv_process_options_t process_options = {0}; uv_stdio_container_t process_stdio[3]; int i, process_still_running; char * const cmdin = MVM_string_utf8_c8_encode_C_string(tc, cmd); char * const _cwd = MVM_string_utf8_c8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); char **_env = MVM_malloc((size + 1) * sizeof(char *)); MVMIter *iter; #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ char * const _cmd = ANSIToUTF8(acp, getenv("ComSpec")); char *args[3]; args[0] = "/c"; args[1] = cmdin; args[2] = NULL; #else char * const _cmd = "/bin/sh"; char *args[4]; args[0] = "/bin/sh"; args[1] = "-c"; args[2] = cmdin; args[3] = NULL; #endif MVMROOT(tc, in, { MVMROOT(tc, out, { MVMROOT(tc, err, { iter = (MVMIter *)MVM_iter(tc, env); INIT_ENV(); }); });
bool CustomTeamsList::LoadOneTeam(const std::string &dir, const std::string &custom_team_name) { // Skip '.', '..' and hidden files if (custom_team_name[0] == '.') return true; // Is it a directory ? if (!DoesFolderExist(dir + PATH_SEPARATOR + custom_team_name)) return false; // Add the team std::string real_name = ANSIToUTF8(dir, custom_team_name); std::string error; CustomTeam *team = CustomTeam::LoadCustomTeam(dir, real_name, error); if (team) { full_list.push_back(team); return true; } std::cerr << std::endl << Format(_("Error loading team :")) << real_name <<":"<< error << std::endl; return false; }
char * xml_ansitoutf8(char* wText) { char * utf8RetStr = NULL; int tmpLen = 0; if(!wText) return utf8RetStr; if(!IsUTF8(wText)) { utf8RetStr = ANSIToUTF8(wText); tmpLen = !utf8RetStr ? 0 : strlen(utf8RetStr); if(tmpLen == 1) { if(utf8RetStr) free(utf8RetStr); utf8RetStr = UnicodeToUTF8((wchar_t *)wText); } } else { tmpLen = strlen(wText)+1; utf8RetStr = (char *)malloc(tmpLen); memcpy(utf8RetStr, wText, tmpLen); } return utf8RetStr; }
MVMObject * MVM_file_openpipe(MVMThreadContext *tc, MVMString *cmd, MVMString *cwd, MVMObject *env, MVMString *err_path) { MVMint64 spawn_result = 0; uv_process_t *process = calloc(1, sizeof(uv_process_t)); uv_process_options_t process_options = {0}; uv_stdio_container_t process_stdio[3]; int i; int status; int readable = 1; uv_pipe_t *out, *in; char * const cmdin = MVM_string_utf8_encode_C_string(tc, cmd); char * const _cwd = MVM_string_utf8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); MVMIter * const iter = (MVMIter *)MVM_iter(tc, env); char **_env = malloc((size + 1) * sizeof(char *)); #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ char * const _cmd = ANSIToUTF8(acp, getenv("ComSpec")); char *args[3]; args[0] = "/c"; { MVMint64 len = strlen(cmdin); MVMint64 i; for (i = 0; i < len; i++) if (cmdin[i] == '/') cmdin[i] = '\\'; } args[1] = cmdin; args[2] = NULL; #else char * const _cmd = "/bin/sh"; char *args[4]; args[0] = "/bin/sh"; args[1] = "-c"; args[2] = cmdin; args[3] = NULL; #endif INIT_ENV(); /* Making openpipe distinguish between :rp and :wp and all other options * is left as an excercise for the reader. readable = strncmp(cmdin, "/usr/bin/wc", 11) != 0; */ if (readable) { /* We want to read from the child's stdout. */ out = malloc(sizeof(uv_pipe_t)); uv_pipe_init(tc->loop, out, 0); uv_pipe_open(out, 0); process_stdio[0].flags = UV_INHERIT_FD; // child's stdin process_stdio[0].data.fd = 0; process_stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE; // child's stdout process_stdio[1].data.stream = (uv_stream_t*)out; } else { /* We want to print to the child's stdin. */ in = malloc(sizeof(uv_pipe_t)); uv_pipe_init(tc->loop, in, 0); uv_pipe_open(in, 1); process_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE; // child's stdin process_stdio[0].data.stream = (uv_stream_t*)in; process_stdio[1].flags = UV_INHERIT_FD; // child's stdout process_stdio[1].data.fd = 1; } process_stdio[2].flags = UV_INHERIT_FD; // child's stderr process_stdio[2].data.fd = 2; process_options.stdio = process_stdio; process_options.file = _cmd; process_options.args = args; process_options.cwd = _cwd; process_options.flags = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS | UV_PROCESS_WINDOWS_HIDE; process_options.env = _env; process_options.stdio_count = 3; process_options.exit_cb = spawn_on_exit; uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) { FREE_ENV(); free(_cwd); free(cmdin); uv_unref((uv_handle_t *)process); MVM_exception_throw_adhoc(tc, "Failed to open pipe: %d", errno); } FREE_ENV(); free(_cwd); free(cmdin); uv_unref((uv_handle_t *)process); return MVM_io_syncpipe(tc, (uv_stream_t *)(readable ? out : in), process); }
DWORD Enum_Current_Drectory(LPSTR sz_Utf8_Path,Json::Value *root) { Json::Value dir; Json::Value data; Json::Value attr; CHAR szFilePath[MAX_PATH]; CHAR szFullPath[MAX_PATH]; WIN32_FIND_DATA FindFileData; HANDLE hListFile; string szPath; string utf8_FullPath,utf8_FileName; UTF8ToANSI(sz_Utf8_Path,&szPath); if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFilePath,"%s\\*",szPath.c_str()); } else { sprintf_s(szFilePath,"%s*",szPath.c_str()); } hListFile = FindFirstFile(szFilePath,&FindFileData); if(hListFile==INVALID_HANDLE_VALUE) { Msg("开始遍历文件时出错:%d\r\n",GetLastError()); return 1; } else { do { if(lstrcmp(FindFileData.cFileName,TEXT("."))==0|| lstrcmp(FindFileData.cFileName,TEXT(".."))==0) { continue; } if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFullPath,"%s\\%s",szPath.c_str(),FindFileData.cFileName); } else { sprintf_s(szFullPath,"%s%s",szPath.c_str(),FindFileData.cFileName); } dwTotalFileNum++; utf8_FileName.erase(); utf8_FullPath.erase(); ANSIToUTF8(szFullPath,&utf8_FullPath); attr["path"]=Json::Value(utf8_FullPath.c_str()); ANSIToUTF8(FindFileData.cFileName,&utf8_FileName); data["title"]=Json::Value(utf8_FileName.c_str()); attr["title"]=Json::Value(utf8_FileName.c_str()); dir["data"]=data; dir["attr"]=attr; if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { dir["state"]=Json::Value("closed"); } else { continue; } root->append(dir); } while(FindNextFile(hListFile, &FindFileData)); FindClose(hListFile); //谨记MSDN的教导: //When the search handle is no longer needed, close it by using the FindClose function, not CloseHandle. } szPath.erase(); utf8_FullPath.erase(); utf8_FileName.erase(); return 0; }
void Enum_All_File_in_Drectory(void *cmd) { Json::FastWriter fast_writer; Json::Value dir; Json::Value file; string Base64Result; cmdinfo *pci=(cmdinfo*)cmd; CHAR szFilePath[MAX_PATH]; CHAR szFullPath[MAX_PATH]; WIN32_FIND_DATA FindFileData; HANDLE hListFile; string szPath; string utf8_FullPath,utf8_FileName; //file["title"]=Json::Value("Dir1"); //file["path"]=Json::Value("C:\\Dir1"); //file["icon"]=Json::Value("dir.png"); //file["name"]=Json::Value("Dir1"); UTF8ToANSI((char *)pci->cmd_content.c_str(),&szPath); if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFilePath,"%s\\*",szPath.c_str()); } else { sprintf_s(szFilePath,"%s*",szPath.c_str()); } hListFile = FindFirstFile(szFilePath,&FindFileData); if(hListFile==INVALID_HANDLE_VALUE) { Msg("开始遍历文件时出错:%d\r\n",GetLastError()); } else { do { if(lstrcmp(FindFileData.cFileName,TEXT("."))==0|| lstrcmp(FindFileData.cFileName,TEXT(".."))==0) { continue; } if (szPath.c_str()[szPath.length()-1]!='\\') { sprintf_s(szFullPath,"%s\\%s",szPath.c_str(),FindFileData.cFileName); } else { sprintf_s(szFullPath,"%s%s",szPath.c_str(),FindFileData.cFileName); } dwTotalFileNum++; utf8_FileName.erase(); ANSIToUTF8(FindFileData.cFileName,&utf8_FileName); file["title"]=Json::Value(utf8_FileName.c_str()); file["name"]=Json::Value(utf8_FileName.c_str()); if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { // Msg("<DIR>"); file["icon"]=Json::Value("grid_dir.png"); } else { file["icon"]=Json::Value("file.png"); } dir["dir"].append(file); } while(FindNextFile(hListFile, &FindFileData)); FindClose(hListFile); } szPath.erase(); utf8_FileName.erase(); Base64Encode(reinterpret_cast<const unsigned char*>((char *)fast_writer.write(dir).c_str()),fast_writer.write(dir).length(),Base64Result); Replace_plus(Base64Result); SubmitResult(pci,Base64Result.c_str()); Base64Result.erase(); dir.clear(); }
bool create_bob_txt() { // current directory 를 구한다. wchar_t *buf=NULL; uint32_t buflen = 0; buflen = GetCurrentDirectoryW(buflen, buf); if (0 == buflen) { print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError()); return false; } buf = (PWSTR) malloc(sizeof(WCHAR) * buflen); if (0 == GetCurrentDirectoryW(buflen, buf)) { print("err, GetCurrentDirectoryW() failed. gle = 0x%08x", GetLastError()); free(buf); return false; } // current dir \\ bob.txt 파일명 생성 wchar_t file_name[260]; if (!SUCCEEDED(StringCbPrintfW( file_name, sizeof(file_name), L"%ws\\bob.txt", buf))) { print("err, can not create file name"); free(buf); return false; } if (true == is_file_existsW(file_name)) { ::DeleteFileW(file_name); } // 파일 생성 HANDLE file_handle = CreateFileW( file_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if(file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", file_name, GetLastError()); return false; } // 파일에 데이터 쓰기 DWORD bytes_written = 0; wchar_t string_buf[1024]; if (!SUCCEEDED(StringCbPrintfW( string_buf, sizeof(string_buf), L"동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세"))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } char *pUtf8 =NULL; int len = UnicodeToUtf8(string_buf,&pUtf8); if (!WriteFile(file_handle, pUtf8, len, &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 영어로 쓰기 if (!SUCCEEDED(StringCbPrintfW( string_buf, sizeof(string_buf), L"All work and no play makes jack a dull boy."))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } free(pUtf8); *pUtf8 =NULL; len = UnicodeToUtf8(string_buf,&pUtf8); if (!WriteFile(file_handle, string_buf, wcslen(string_buf), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } char string_bufa[1024]; if (!SUCCEEDED(StringCbPrintfA( string_bufa, sizeof(string_bufa), "동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세"))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } free(pUtf8); pUtf8 =NULL; pUtf8 =ANSIToUTF8(string_bufa); len = strlen(pUtf8); if (!WriteFile(file_handle, pUtf8, len, &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } // 영어로 쓰기 if (!SUCCEEDED(StringCbPrintfA( string_bufa, sizeof(string_bufa), "All work and no play makes jack a dull boy."))) { print("err, can not create data to write."); CloseHandle(file_handle); return false; } free(pUtf8); pUtf8 =NULL; pUtf8 =ANSIToUTF8(string_bufa); len = strlen(pUtf8); if (!WriteFile(file_handle, string_bufa, strlen(string_bufa), &bytes_written, NULL)) { print("err, WriteFile() failed. gle = 0x%08x", GetLastError()); CloseHandle(file_handle); return false; } free(pUtf8); // 파일 닫기 CloseHandle(file_handle); wchar_t copy_file_name[260]; if (!SUCCEEDED(StringCbPrintfW( copy_file_name, sizeof(copy_file_name), L"%ws\\bob2.txt", buf))) { print("err, can not copy file name"); free(buf); return false; } free(buf); buf = NULL; /* BOB.txt -> BOB2.txt 파일 복사 */ if(!CopyFileW(file_name,copy_file_name,false)){ print("CopyFileW fail"); return false; } /* ReadFile()을 통한 읽기 후 출력*/ HANDLE read_file_handle = CreateFileW( copy_file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(read_file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", read_file_handle, GetLastError()); return false; } char read_buf[512]; char buf_noNull[512]; char* ansi_buf; DWORD result=0; ReadFile(read_file_handle,read_buf,sizeof(char)*512,&result,NULL); // read_buf의 글자 사이사이에 있는 NULL값 제거 ( UTF8ToANSI 에서 strlen을 사용하기 때문) int j=0; for(int i=0;i<256;i++){ if(read_buf[i] != '\0'){ buf_noNull[j++] = read_buf[i]; } } buf_noNull[j]='\0'; // 출력 ansi_buf=UTF8ToANSI(buf_noNull); printf("%s\n",ansi_buf); CloseHandle(read_file_handle); /* MMIO()을 통한 읽기 후 출력*/ read_file_handle = CreateFileW( copy_file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(read_file_handle == INVALID_HANDLE_VALUE) { print("err, CreateFile(path=%ws), gle=0x%08x", read_file_handle, GetLastError()); return false; } HANDLE file_map = CreateFileMapping( read_file_handle, NULL, PAGE_READONLY, 0, 0, NULL ); if (NULL == file_map) { print("err, CreateFileMapping(%ws) failed, gle = %u", file_name, GetLastError()); CloseHandle(read_file_handle); return false; } PCHAR file_view = (PCHAR) MapViewOfFile( file_map, FILE_MAP_READ, 0, 0, 0 ); if(file_view == NULL) { print("err, MapViewOfFile(%ws) failed, gle = %u", file_name, GetLastError()); CloseHandle(file_map); CloseHandle(read_file_handle); return false; } // read_buf의 글자 사이사이에 있는 NULL값 제거 ( UTF8ToANSI 에서 strlen을 사용하기 때문) j=0; for(int i=0;i<256;i++){ if(read_buf[i] != '\0'){ buf_noNull[j++] = file_view[i]; } } buf_noNull[j]='\0'; // 출력 ansi_buf=UTF8ToANSI(buf_noNull); printf("%s\n",ansi_buf); UnmapViewOfFile(file_view); CloseHandle(file_map); CloseHandle(read_file_handle); /* BOB.txt 파일 삭제*/ ::DeleteFileW(file_name); return true; }
MVMint64 MVM_proc_shell(MVMThreadContext *tc, MVMString *cmd, MVMString *cwd, MVMObject *env, MVMObject *in, MVMObject *out, MVMObject *err, MVMint64 flags) { MVMint64 result = 0, spawn_result; uv_process_t *process = MVM_calloc(1, sizeof(uv_process_t)); uv_process_options_t process_options = {0}; uv_stdio_container_t process_stdio[3]; int i; char * const cmdin = MVM_string_utf8_c8_encode_C_string(tc, cmd); char * const _cwd = MVM_string_utf8_c8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); MVMIter * const iter = (MVMIter *)MVM_iter(tc, env); char **_env = MVM_malloc((size + 1) * sizeof(char *)); #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ char * const _cmd = ANSIToUTF8(acp, getenv("ComSpec")); char *args[3]; args[0] = "/c"; args[1] = cmdin; args[2] = NULL; #else char * const _cmd = "/bin/sh"; char *args[4]; args[0] = "/bin/sh"; args[1] = "-c"; args[2] = cmdin; args[3] = NULL; #endif INIT_ENV(); setup_process_stdio(tc, in, process, &process_stdio[0], 0, flags, "shell"); setup_process_stdio(tc, out, process, &process_stdio[1], 1, flags >> 3, "shell"); setup_process_stdio(tc, err, process, &process_stdio[2], 2, flags >> 6, "shell"); process_options.stdio = process_stdio; process_options.file = _cmd; process_options.args = args; process_options.cwd = _cwd; process_options.flags = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS | UV_PROCESS_WINDOWS_HIDE; process_options.env = _env; process_options.stdio_count = 3; process_options.exit_cb = spawn_on_exit; if (flags & (MVM_PIPE_CAPTURE_IN | MVM_PIPE_CAPTURE_OUT | MVM_PIPE_CAPTURE_ERR)) { process->data = MVM_calloc(1, sizeof(MVMint64)); uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) result = spawn_result; } else { process->data = &result; uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) result = spawn_result; else uv_run(tc->loop, UV_RUN_DEFAULT); } FREE_ENV(); MVM_free(_cwd); #ifdef _WIN32 MVM_free(_cmd); #endif MVM_free(cmdin); uv_unref((uv_handle_t *)process); return result; }
void FileOperate(void * cmd) { cmdinfo *pci=(cmdinfo*)cmd; Json::Value json_cmd_content; Json::Reader reader; Json::FastWriter fast_writer; string Result,Base64Result; if (reader.parse(pci->cmd_content.c_str(), json_cmd_content)) { string operate=fast_writer.write(json_cmd_content["operate"]); if (operate.find("rename")!=string::npos) { string oldName,newName; PathDecode(json_cmd_content["oldName"].asString(),oldName); PathDecode(json_cmd_content["newName"].asString(),newName); //Msg("操作:重命名\r\n"); //Msg("原名称:%s\r\n",oldName.c_str()); //Msg("新名称:%s\r\n",newName.c_str()); if(!MoveFile(oldName.c_str(),newName.c_str())) { LPSTR lpBuffer=NULL; FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),LANG_NEUTRAL,(LPTSTR) & lpBuffer,0 ,NULL); if(lpBuffer==NULL) { ANSIToUTF8("Rename File Error.",&Result); } Result+=lpBuffer; LocalFree (lpBuffer); } else { ANSIToUTF8("Complete",&Result); } } if (operate.find("newdir")!=string::npos) { string newDirPath; PathDecode(json_cmd_content["newDirPath"].asString(),newDirPath); // Msg("操作:新建文件夹:%s\r\n",newDirPath.c_str()); if (!CreateDirectory(newDirPath.c_str(), NULL)) { LPSTR lpBuffer=NULL; FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),LANG_NEUTRAL,(LPTSTR) & lpBuffer,0 ,NULL); if(lpBuffer==NULL) { ANSIToUTF8("Rename File Error.",&Result); } Result+=lpBuffer; LocalFree (lpBuffer); } else { ANSIToUTF8("Complete",&Result); } } if (operate.find("delete")!=string::npos) { Json::Value filelist,dirlist; filelist=json_cmd_content["filelist"]; dirlist=json_cmd_content["dirlist"]; int iSize = filelist.size(); string temp; for (int nIndex=0;nIndex<iSize;++nIndex ) { temp.erase(); PathDecode(filelist[nIndex]["file"].asString(),temp); // Msg("文件:%s\r\n",temp.c_str()); DelFileHiddenAndReadonly(temp.c_str()); if (!DeleteFile(temp.c_str())) { LPSTR lpBuffer=NULL; FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),LANG_NEUTRAL,(LPTSTR) & lpBuffer,0 ,NULL); if(lpBuffer==NULL) { Result=Result+"删除文件\""+temp.c_str()+"\"时出错,原因:未知.\n"; } Result=Result+"删除文件\""+temp.c_str()+"\"时出错,原因:"+lpBuffer; LocalFree (lpBuffer); } else { Result=Result+"删除文件\""+temp.c_str()+"\"完成\n"; } } iSize=dirlist.size(); for (int nIndex=0;nIndex<iSize;++nIndex ) { temp.erase(); PathDecode(dirlist[nIndex]["dir"].asString(),temp); // Msg("目录:%s\r\n",temp.c_str()); if (Delete_Drectory(temp.c_str())) { Result=Result+"删除目录\""+temp.c_str()+"\"完成\n"; } else { Result=Result+"删除目录\""+temp.c_str()+"\"失败\n"; } } } if (operate.find("Copy")!=string::npos) { string temp_src,temp_dest; Json::Value src,dest; src=json_cmd_content["Source"]; dest=json_cmd_content["Destination"]; int iSize = src.size(); if (iSize!=dest.size()) { Result+="Json Error"; } for (int nIndex=0;nIndex<iSize;++nIndex ) { temp_src.erase(); temp_dest.erase(); PathDecode(src[nIndex]["src"].asString(),temp_src); PathDecode(dest[nIndex]["dest"].asString(),temp_dest); // Msg("从\"%s\"到\"%s\"\r\n",temp_src.c_str(),temp_dest.c_str()); DelFileHiddenAndReadonly(temp_src.c_str()); if(!CopyFile(temp_src.c_str(),temp_dest.c_str(),FALSE)) { LPSTR lpBuffer=NULL; FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),LANG_NEUTRAL,(LPTSTR) & lpBuffer,0 ,NULL); if(lpBuffer==NULL) { Result=Result+"复制文件\""+temp_src.c_str()+"\"时出错,原因:未知.\n"; } Result=Result+"复制文件\""+temp_src.c_str()+"\"时出错,原因:"+lpBuffer; LocalFree (lpBuffer); } else { Result=Result+"复制文件\""+temp_src.c_str()+"\"完成\n"; } } } if (operate.find("Cut")!=string::npos) { string temp_src,temp_dest; Json::Value src,dest; src=json_cmd_content["Source"]; dest=json_cmd_content["Destination"]; int iSize = src.size(); if (iSize!=dest.size()) { Result+="Json Error"; } for (int nIndex=0;nIndex<iSize;++nIndex ) { temp_src.erase(); temp_dest.erase(); PathDecode(src[nIndex]["src"].asString(),temp_src); PathDecode(dest[nIndex]["dest"].asString(),temp_dest); // Msg("从\"%s\"到\"%s\"\r\n",temp_src.c_str(),temp_dest.c_str()); DelFileHiddenAndReadonly(temp_src.c_str()); if(!MoveFile(temp_src.c_str(),temp_dest.c_str())) { LPSTR lpBuffer=NULL; FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),LANG_NEUTRAL,(LPTSTR) & lpBuffer,0 ,NULL); if(lpBuffer==NULL) { Result=Result+"移动文件\""+temp_src.c_str()+"\"时出错,原因:未知.\n"; } Result=Result+"移动文件\""+temp_src.c_str()+"\"时出错,原因:"+lpBuffer; LocalFree (lpBuffer); } else { Result=Result+"移动文件\""+temp_src.c_str()+"\"完成\n"; } } } } else { ANSIToUTF8("Json Format Error",&Result); } Base64Encode(reinterpret_cast<const unsigned char*>(Result.c_str()),Result.length(),Base64Result); Replace_plus(Base64Result); SubmitResult(pci,Base64Result.c_str()); }