bool stupid_get_current_work_directory(std::string & dirname) { #ifdef _MSC_VER wchar_t temp[512] = { 0 }; if (nullptr == _wgetcwd(temp, sizeof(temp) / sizeof(temp[0]))) { dirname.clear(); return(false); } else { dirname = unicode_to_utf8(std::wstring(temp)) + g_directory_separator; stupid_directory_format(dirname); return(true); } #else char temp[512] = { 0 }; if (nullptr == getcwd(temp, sizeof(temp) / sizeof(temp[0]))) { dirname.clear(); return(false); } else { dirname = ansi_to_utf8(std::string(temp)) + g_directory_separator; stupid_directory_format(dirname); return(true); } #endif // _MSC_VER }
void AsinbowAPI::executeCommandWork(const std::string& command, const FB::JSObjectPtr& callback) { char buf[4096]; FILE* pipe = popen( #ifdef _WIN32 utf8_to_ansi(command).c_str(), #else command.c_str(), #endif "r"); if (!pipe) { callback->Invoke("", FB::variant_list_of(true)("popen failed!")); return; } std::string result; int c; while ((c = fgetc(pipe)) != EOF) { result += (char)c; if (c=='\n') { #ifdef _WIN32 result = ansi_to_utf8(result); #endif callback->Invoke("", FB::variant_list_of(false)(result)); result = ""; } } pclose(pipe); callback->Invoke("", FB::variant_list_of(true)); }
void CIcqProto::setStatusMsgVar(HANDLE hContact, char* szStatusMsg, bool isAnsi) { if (szStatusMsg && szStatusMsg[0]) { if (isAnsi) { char* szStatusNote = getSettingStringUtf(hContact, DBSETTING_STATUS_NOTE, ""); wchar_t* szStatusNoteW = make_unicode_string(szStatusNote); int len = (int)wcslen(szStatusNoteW) * 3 + 1; char* szStatusNoteAnsi = (char*)alloca(len); WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, szStatusNoteW, -1, szStatusNoteAnsi, len, NULL, NULL); bool notmatch = false; for (int i=0; ;++i) { if (szStatusNoteAnsi[i] != szStatusMsg[i] && szStatusNoteAnsi[i] != '?' && szStatusMsg[i] != '?') { notmatch = true; break; } if (!szStatusNoteAnsi[i] || !szStatusMsg[i]) break; } szStatusMsg = notmatch ? ansi_to_utf8(szStatusMsg) : szStatusNote; SAFE_FREE(&szStatusNoteW); if (notmatch) SAFE_FREE(&szStatusNote); } char* oldStatusMsg = NULL; DBVARIANT dbv; if (!DBGetContactSetting(hContact, "CList", "StatusMsg", &dbv)) { switch (dbv.type) { case DBVT_UTF8: oldStatusMsg = null_strdup(dbv.pszVal); break; case DBVT_WCHAR: oldStatusMsg = make_utf8_string(dbv.pwszVal); break; } ICQFreeVariant(&dbv); } if (!oldStatusMsg || strcmp(oldStatusMsg, szStatusMsg)) setSettingStringUtf(hContact, "CList", "StatusMsg", szStatusMsg); SAFE_FREE(&oldStatusMsg); if (isAnsi) SAFE_FREE(&szStatusMsg); } else DBDeleteContactSetting(hContact, "CList", "StatusMsg"); }
bool Directory::read() { if (!is_open()) { return(false); } #ifdef _MSC_VER while (!m_eof) { const std::wstring file_name(m_file.cFileName); const DWORD file_type = m_file.dwFileAttributes; m_eof = !FindNextFileW(m_dir, &m_file); if (L"." == file_name || L".." == file_name) { continue; } if (FILE_ATTRIBUTE_DIRECTORY == (FILE_ATTRIBUTE_DIRECTORY & file_type)) { m_current_sub_path_short_name = unicode_to_utf8(file_name); m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name + g_directory_separator; m_current_sub_path_is_directory = true; } else { m_current_sub_path_short_name = unicode_to_utf8(file_name); m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name; m_current_sub_path_is_directory = false; } return(true); } #else while (nullptr != m_file) { /* * do not do like this: * struct dirent * file = m_file; * m_file = readdir(m_dir); * operate_function(file); * the behavior is undefined, the result is not expected */ const std::string d_name(m_file->d_name); #if 0 const size_t d_type = m_file->d_type; #endif // 0 m_file = readdir(m_dir); if ("." == d_name || ".." == d_name) { continue; } #if 0 /* * d_type: not supported by all filesystem */ if (DT_DIR == (DT_DIR & d_type)) { m_current_sub_path_short_name = ansi_to_utf8(d_name); m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name + g_directory_separator; m_current_sub_path_is_directory = true; return(true); } else if (DT_REG == (DT_REG & d_type)) { m_current_sub_path_short_name = ansi_to_utf8(d_name); m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name; m_current_sub_path_is_directory = false; return(true); } #else stupid_stat_t stat_buf = { 0x00 }; const std::string file_name(utf8_to_ansi(m_dir_name) + d_name); if (0 != stupid_stat(file_name.c_str(), &stat_buf)) { continue; } if (S_IFDIR == (S_IFDIR & stat_buf.st_mode)) { m_current_sub_path_short_name = ansi_to_utf8(d_name); m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name + g_directory_separator; m_current_sub_path_is_directory = true; return(true); } else if (S_IFREG == (S_IFREG & stat_buf.st_mode)) { m_current_sub_path_short_name = ansi_to_utf8(d_name); m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name; m_current_sub_path_is_directory = false; return(true); } #endif // 0 } #endif // _MSC_VER m_current_sub_path_short_name.clear(); m_current_sub_path_name.clear(); m_current_sub_path_is_directory = false; return(false); }
void CIcqProto::handleFileTransferPacket(directconnect* dc, PBYTE buf, WORD wLen) { if (wLen < 1) return; NetLog_Direct("Handling file packet"); switch (buf[0]) { case PEER_FILE_INIT: /* first packet of a file transfer */ if (dc->initialised) return; if (wLen < 19) return; buf += 5; /* id, and unknown 0 */ dc->type = DIRECTCONN_FILE; { DWORD dwFileCount; DWORD dwTotalSize; DWORD dwTransferSpeed; WORD wNickLength; int bAdded; unpackLEDWord(&buf, &dwFileCount); unpackLEDWord(&buf, &dwTotalSize); unpackLEDWord(&buf, &dwTransferSpeed); unpackLEWord(&buf, &wNickLength); dc->ft = FindExpectedFileRecv(dc->dwRemoteUin, dwTotalSize); if (dc->ft == NULL) { NetLog_Direct("Unexpected file receive"); CloseDirectConnection(dc); return; } dc->ft->dwFileCount = dwFileCount; dc->ft->dwTransferSpeed = dwTransferSpeed; dc->ft->hContact = HContactFromUIN(dc->ft->dwUin, &bAdded); dc->ft->dwBytesDone = 0; dc->ft->iCurrentFile = -1; dc->ft->fileId = -1; dc->ft->hConnection = dc->hConnection; dc->ft->dwLastNotify = GetTickCount(); dc->initialised = 1; file_sendTransferSpeed(this, dc); file_sendNick(this, dc); } BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, dc->ft, 0); break; case PEER_FILE_INIT_ACK: if (wLen < 8) return; buf++; unpackLEDWord(&buf, &dc->ft->dwTransferSpeed); /* followed by nick */ file_sendNextFile(this, dc); break; case PEER_FILE_NEXTFILE: if (wLen < 20) return; buf++; /* id */ { char *szAnsi; WORD wThisFilenameLen, wSubdirLen; BYTE isDirectory; unpackByte(&buf, &isDirectory); unpackLEWord(&buf, &wThisFilenameLen); if (wLen < 19 + wThisFilenameLen) return; SAFE_FREE(&dc->ft->szThisFile); szAnsi = (char *)_malloca(wThisFilenameLen + 1); memcpy(szAnsi, buf, wThisFilenameLen); szAnsi[wThisFilenameLen] = '\0'; dc->ft->szThisFile = ansi_to_utf8(szAnsi); buf += wThisFilenameLen; unpackLEWord(&buf, &wSubdirLen); if (wLen < 18 + wThisFilenameLen + wSubdirLen) return; SAFE_FREE(&dc->ft->szThisSubdir); szAnsi = (char *)_malloca(wSubdirLen + 1); memcpy(szAnsi, buf, wSubdirLen); szAnsi[wSubdirLen] = '\0'; dc->ft->szThisSubdir = ansi_to_utf8(szAnsi); buf += wSubdirLen; unpackLEDWord(&buf, &dc->ft->dwThisFileSize); unpackLEDWord(&buf, &dc->ft->dwThisFileDate); unpackLEDWord(&buf, &dc->ft->dwTransferSpeed); /* no cheating with paths */ if (!IsValidRelativePath(dc->ft->szThisFile) || !IsValidRelativePath(dc->ft->szThisSubdir)) { NetLog_Direct("Invalid path information"); break; } char *szFullPath = (char*)SAFE_MALLOC(strlennull(dc->ft->szSavePath)+strlennull(dc->ft->szThisSubdir)+strlennull(dc->ft->szThisFile)+3); strcpy(szFullPath, dc->ft->szSavePath); NormalizeBackslash(szFullPath); strcat(szFullPath, dc->ft->szThisSubdir); NormalizeBackslash(szFullPath); // _chdir(szFullPath); // set current dir - not very useful strcat(szFullPath, dc->ft->szThisFile); // we joined the full path to dest file SAFE_FREE(&dc->ft->szThisFile); dc->ft->szThisFile = szFullPath; dc->ft->dwFileBytesDone = 0; dc->ft->iCurrentFile++; if (isDirectory) { MakeDirUtf(dc->ft->szThisFile); dc->ft->fileId = -1; } else { /* file resume */ PROTOFILETRANSFERSTATUS pfts = {0}; file_buildProtoFileTransferStatus(dc->ft, &pfts); if (BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, dc->ft, (LPARAM)&pfts)) break; /* UI supports resume: it will call PS_FILERESUME */ dc->ft->fileId = OpenFileUtf(dc->ft->szThisFile, _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE); if (dc->ft->fileId == -1) { icq_LogMessage(LOG_ERROR, LPGEN("Your file receive has been aborted because Miranda could not open the destination file in order to write to it. You may be trying to save to a read-only folder.")); CloseDirectConnection(dc); dc->ft->hConnection = NULL; break; } } } file_sendResume(this, dc); BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, dc->ft, 0); break; case PEER_FILE_RESUME: if (dc->ft->fileId == -1 && !dc->ft->currentIsDir) return; if (wLen < 13) return; if (wLen < 17) NetLog_Direct("Warning: Received short PEER_FILE_RESUME"); buf++; { DWORD dwRestartFrom; unpackLEDWord(&buf, &dwRestartFrom); if (dwRestartFrom > dc->ft->dwThisFileSize) return; buf += 4; /* unknown. 0 */ unpackLEDWord(&buf, &dc->ft->dwTransferSpeed); buf += 4; /* unknown. 1 */ if (!dc->ft->currentIsDir) _lseek(dc->ft->fileId, dwRestartFrom, 0); dc->wantIdleTime = 1; dc->ft->dwBytesDone += dwRestartFrom; dc->ft->dwFileBytesDone += dwRestartFrom; } break; case PEER_FILE_SPEED: if (wLen < 5) return; buf++; unpackLEDWord(&buf, &dc->ft->dwTransferSpeed); dc->ft->dwLastNotify = GetTickCount(); break; case PEER_FILE_DATA: if (!dc->ft->currentIsDir) { if (dc->ft->fileId == -1) break; buf++; wLen--; _write(dc->ft->fileId, buf, wLen); } else wLen = 0; dc->ft->dwBytesDone += wLen; dc->ft->dwFileBytesDone += wLen; if (GetTickCount() > dc->ft->dwLastNotify + 500 || wLen < 2048) { PROTOFILETRANSFERSTATUS pfts; file_buildProtoFileTransferStatus(dc->ft, &pfts); BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, dc->ft, (LPARAM)&pfts); dc->ft->dwLastNotify = GetTickCount(); } if (wLen < 2048) { /* EOF */ if (!dc->ft->currentIsDir) _close(dc->ft->fileId); dc->ft->fileId = -1; if ((DWORD)dc->ft->iCurrentFile == dc->ft->dwFileCount - 1) { dc->type = DIRECTCONN_CLOSING; /* this guarantees that we won't accept any more data but that the sender is still free to closesocket() neatly */ BroadcastAck(dc->ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, dc->ft, 0); } } break; default: NetLog_Direct("Unknown file transfer packet ignored."); break; } }