bool BrainUtil::RunSystemCommand(const CString& cmd) { CString tempCmd = cmd; //int ret = _wsystem(tempCmd.GetBuffer()); //DATA_ASSERT(0 == ret); // Don't aseert the failure is accepted. // Get the output from the pipe FILE* pipe = _wpopen(tempCmd.GetBuffer(), _T("r")); if (!pipe) { return false; LogOut(_T("Error: failed to create the piple.")); } TCHAR buffer[256]; while(!feof(pipe)) { if(fgetws(buffer, 256, pipe) != NULL) { LogOut(buffer); } } int ret = _pclose(pipe); return 0 == ret; }
FILE *popen_utf8(const std::string &cmdline, const char *mode) { #ifdef _WIN32 // Add quotes around entire commandline per http://msdn.microsoft.com/en-us/library/96ayss4b.aspx return _wpopen(Unicode("\"" + cmdline + "\"").path(), Unicode(mode).path()); #else return popen(cmdline.c_str(), remove_b(mode).c_str()); #endif }
/** Prints packet timestaps regardless of format*/ int _tmain(int argc, _TCHAR* argv[]) { char errbuf[PCAP_ERRBUF_SIZE]; wchar_t cmd[1024]; wchar_t tshark_path[MAX_PATH]; wchar_t file_path[MAX_PATH]; if ( argc != 3 ) { wprintf(L"Prints packet timestaps regardless of format.\n"); wprintf(L"Usage:\n\t%ls <tshark path> <trace file>\n", argv[0]); return 1; } // conversion to short path name in case there are spaces if ( ! GetShortPathNameW(argv[1], tshark_path, MAX_PATH) || ! GetShortPathNameW(argv[2], file_path, MAX_PATH) ) { printf("Failed to convert paths to short form."); return 1; } // create tshark command, which will make the trace conversion and print in libpcap format to stdout if ( swprintf_s(cmd, 1024, L"%ls -r %ls -w - -F libpcap", tshark_path, file_path) < 0 ) { wprintf(L"Failed to create command\n"); return 1; } // start tshark FILE *tshark_out = _wpopen(cmd, L"rb"); if ( tshark_out == NULL ) { strerror_s(errbuf, PCAP_ERRBUF_SIZE, errno); printf("Failed run tshark: %s\n", errbuf); wprintf(L"Command: %ls", cmd); return 1; } // open stdout from tshark pcap_t *pcap = pcap_fopen_offline(tshark_out, errbuf); if ( pcap == NULL ) { printf("Error opening stream from tshark: %s\n", errbuf); return 1; } // print information about every packet int trace struct pcap_pkthdr hdr; while ( pcap_next(pcap, &hdr) ) { printf("packet: ts: %u.%06u, len: %4u, caplen: %4u\n", hdr.ts.tv_sec, hdr.ts.tv_usec, hdr.len, hdr.caplen); } // clean up pcap_close(pcap); _pclose(tshark_out); return 0; }
FILE * fsyscp_popen (const char *command, const char *mode) { FILE *f; wchar_t *commandw, modew[4]; int i; #if defined (KPSE_COMPAT_API) kpathsea kpse; #endif assert(command && mode); if (is_include_space (command)) { const char *p; char *command2, *q; command2 = xmalloc (strlen (command) + 3); p = command; q = command2; *q++ = '\"'; while (*p) *q++ = *p++; *q++ = '\"'; *q = '\0'; commandw = get_wstring_from_fsyscp(command2, commandw=NULL); free (command2); } else { commandw = get_wstring_from_fsyscp(command, commandw=NULL); } for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ f = _wpopen(commandw, modew); #if defined (KPSE_COMPAT_API) if (f != NULL) { kpse = kpse_def; if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { DEBUGF_START (); fprintf (stderr, "fsyscp_popen(%s [", command); WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), commandw, wcslen( commandw ), NULL, NULL ); #if defined(_WIN64) fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); #else fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); #endif DEBUGF_END (); } } #endif free (commandw); /* We use always binary mode on Windows */ if(f) _setmode (fileno (f), _O_BINARY); return f; }
static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); FILE **pf = newfile(L); #ifdef WIN32 wchar_t wfilename[MAX_PATH+1]; wchar_t wmode[10]; MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, MAX_PATH+1); MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, 10); *pf = _wpopen(wfilename, wmode); #else // FIXME: this should probably be patched to translate UTF-8 strings to the local filesystem encoding on other systems *pf = lua_popen(L, filename, mode); #endif return (*pf == NULL) ? pushresult(L, 0, filename) : 1; }
FILE *u8popen(const char *command, const char *mode) { wchar_t *wcommand = u8stows(command); wchar_t *wmode = u8stows(mode); FILE *fp = NULL; if(wcommand && wmode) { fp = _wpopen(wcommand, wmode); } if(wcommand) { free(wcommand); } if(wmode) { free(wmode); } return fp; }
BOOL procNmeaFile( TCHAR* fName, TCHAR* cdsOut, int cdsSize ) { TCHAR cmdBuffer[ CMDBUF ] = { 0 }; FILE* pPipe = NULL; BOOL result = TRUE; // Set up external command swprintf_s( cmdBuffer, _countof( cmdBuffer ), TEXT( "%s %s" ), TEXT( "C:\\tmp\\myTools\\hpos.exe" ), fName ); // Create pipe (read text mode) and execute external program if ( ( pPipe = _wpopen( cmdBuffer, TEXT( "rt" ) ) ) == NULL ) return FALSE; // Reset output buffer wmemset( cdsOut, 0, cdsSize ); // Read pipe until end of file, or an error occurs. // hpos outputs only one line of text while ( fgetws( cdsOut, cdsSize, pPipe ) ) { ; } // Close pipe and print return value of pPipe. if ( feof( pPipe ) ) _pclose( pPipe ); else { wprintf_s( TEXT( "Failed to read pipe for 'hpos' to the end.\n" ) ); result = FALSE; } return result; }
// all constructors call this void File::Init(const wchar_t* filename, int fileOptions) { m_filename = filename; m_options = fileOptions; if (m_filename.empty()) RuntimeError("File: filename is empty"); const auto outputPipe = (m_filename.front() == '|'); const auto inputPipe = (m_filename.back() == '|'); // translate the options string into a string for fopen() const auto reading = !!(fileOptions & fileOptionsRead); const auto writing = !!(fileOptions & fileOptionsWrite); if (!reading && !writing) RuntimeError("File: either fileOptionsRead or fileOptionsWrite must be specified"); // convert fileOptions to fopen()'s mode string wstring options = reading ? L"r" : L""; if (writing) { // if we already are reading the file, change to read/write options.clear(); options.append(L"w"); if (!outputPipe && m_filename != L"-") { options.append(L"+"); msra::files::make_intermediate_dirs(m_filename.c_str()); // writing to regular file -> also create the intermediate directories as a convenience } } if (fileOptions & fileOptionsBinary) options += L"b"; else options += L"t"; // add sequential flag to allocate big read buffer if (fileOptions & fileOptionsSequential) options += L"S"; // now open the file // Special path syntax understood here: // - "-" refers to stdin or stdout // - "|cmd" writes to a pipe // - "cmd|" reads from a pipe m_pcloseNeeded = false; m_seekable = false; if (m_filename == L"-") // stdin/stdout { if (writing && reading) RuntimeError("File: cannot specify fileOptionsRead and fileOptionsWrite at once with path '-'"); m_file = writing ? stdout : stdin; } else if (outputPipe || inputPipe) // pipe syntax { if (inputPipe && outputPipe) RuntimeError("File: pipes cannot specify fileOptionsRead and fileOptionsWrite at once"); if (inputPipe != reading) RuntimeError("File: pipes must use consistent fileOptionsRead/fileOptionsWrite"); const auto command = inputPipe ? m_filename.substr(0, m_filename.size() - 1) : m_filename.substr(1); m_file = _wpopen(command.c_str(), options.c_str()); if (!m_file) RuntimeError("File: error exexuting pipe command '%S': %s", command.c_str(), strerror(errno)); m_pcloseNeeded = true; } else attempt([=]() // regular file: use a retry loop { m_file = fopenOrDie(filename, options.c_str()); m_seekable = true; }); }