Try<std::string> shell(const std::string& fmt, const T&... t) { const Try<std::string> command = strings::internal::format(fmt, t...); if (command.isError()) { return Error(command.error()); } FILE* file; std::ostringstream stdoutstr; if ((file = _popen(command.get().c_str(), "r")) == nullptr) { return Error("Failed to run '" + command.get() + "'"); } char line[1024]; // NOTE(vinod): Ideally the if and while loops should be interchanged. But // we get a broken pipe error if we don't read the output and simply close. while (fgets(line, sizeof(line), file) != nullptr) { stdoutstr << line; } if (ferror(file) != 0) { _pclose(file); // Ignoring result since we already have an error. return Error("Error reading output of '" + command.get() + "'"); } int status; if ((status = _pclose(file)) == -1) { return Error("Failed to get status of '" + command.get() + "'"); } return stdoutstr.str(); }
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; }
std::vector<std::string> ExecuteCommandAndReturn(const std::string &szCommand) { std::vector<std::string> ret; FILE *fp; /* Open the command for reading. */ #ifdef WIN32 fp = _popen(szCommand.c_str(), "r"); #else fp = popen(szCommand.c_str(), "r"); #endif if (fp != NULL) { char path[1035]; /* Read the output a line at a time - output it. */ while (fgets(path, sizeof(path)-1, fp) != NULL) { ret.push_back(path); } /* close */ #ifdef WIN32 _pclose(fp); #else pclose(fp); #endif } return ret; }
//----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- bool CVProjectUtil::RunStudiomdl( const std::string &file ) { const std::string game( Prefix() + "/game/" + Game() ); const std::string src( ModelSrc( file ) ); const std::string cmd( std::string( "studiomdl -game \"" ) + game + std::string( "\" \"" ) + src + "\"" ); FILE *cmdp( _popen( cmd.c_str(), "rt" ) ); if ( !cmdp ) { merr << "Couldn't execute studiomdl command: " << cmd << std::endl; return false; } char buf[ BUFSIZ ]; while ( !feof( cmdp ) ) { if ( fgets( buf, BUFSIZ, cmdp ) == NULL ) { break; } minfo << "studiomdl: " << buf; } minfo << std::endl; _pclose( cmdp ); return true; }
int main( void ) { char psBuffer[128]; FILE *pPipe; /* Run DIR so that it writes its output to a pipe. Open this * pipe with read text attribute so that we can read it * like a text file. */ if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL ) exit( 1 ); else printf("have\n"); /* Read pipe until end of file. */ while( !feof( pPipe ) ) { if( fgets( psBuffer, 128, pPipe ) != NULL ) printf( psBuffer ); } /* Close pipe and print return value of pPipe. */ printf( "\nProcess returned %d\n", _pclose( pPipe ) ); }
RTC::ReturnCode_t Gnu3DPlot::onDeactivated(RTC::UniqueId ec_id) { fprintf(m_gp, "exit\n"); // gnuplotの終了 fflush(m_gp); _pclose(m_gp); // パイプを閉じる return RTC::RTC_OK; }
std::string query(std::string url) { #ifdef _MSC_VER // Windows FILE *f = _popen(("C:\\bin\\curl.exe -s -k \"" + url + "\"").c_str(), "r"); #else // Linux, MacOS, Cygwin FILE *f = popen(("curl -s -k \"" + url + "\"").c_str(), "r"); #endif if (f == NULL || url.size() >= BUF_SIZE) { perror("error!"); } char buf[BUF_SIZE]; std::string xml; while (!feof(f)) { if (fgets(buf, 1024, f) == NULL) { break; } xml += (std::string)(buf); } #ifdef _MSC_VER _pclose(f); #else pclose(f); #endif return xml; }
/* Fetch the sha of a library in the folder 'path' and return it in 'res'. */ void getCommit(const char *path, char* res){ /* Buffers */ char buffer[250]; char sha[42]; /* Command to fetch sha */ sprintf(buffer, "cd %s && git rev-parse HEAD", path); /* Read command result */ FILE *fp; #ifdef __linux__ fp = popen(buffer, "r"); #elif _WIN32 fp = _popen(buffer, "r"); #else ModelicaFormatError("Unsupported operating system\n"); #endif if (fp == NULL) { ModelicaFormatError("Failed to call git command\n"); } if (fgets(sha, sizeof(sha)-1, fp) == NULL){ ModelicaFormatError("Path %s does not exist or git cannot be called\n", path); } #ifdef __linux__ pclose(fp); #else _pclose(fp); #endif /* Return result */ strcpy(res,sha); }
static int io_file_close(lua_State *L, IOFileUD *iof) { int ok; if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_FILE) { ok = (fclose(iof->fp) == 0); } else if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_PIPE) { int stat = -1; #if LJ_TARGET_POSIX stat = pclose(iof->fp); #elif LJ_TARGET_WINDOWS && !defined(WP8) stat = _pclose(iof->fp); #else lua_assert(0); return 0; #endif #if LJ_52 iof->fp = NULL; return luaL_execresult(L, stat); #else ok = (stat != -1); #endif } else { lua_assert((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_STDF); setnilV(L->top++); lua_pushliteral(L, "cannot close standard file"); return 2; } iof->fp = NULL; return luaL_fileresult(L, ok, NULL); }
// File Destructor // closes the file // Note: this does not check for errors. Use Flush() before closing a file you are writing. File::~File(void) { if (m_pcloseNeeded) _pclose(m_file); else if (m_file != stdin && m_file != stdout && m_file != stderr) fclose(m_file); // (since destructors may not throw, we ignore the return code here) }
int os_popen(const std::string& cmd, std::string& ret) { ret.clear(); FILE* in = NULL; #ifndef _WIN32 #define _popen popen #define _pclose pclose #endif in = _popen(cmd.c_str(), "r"); if(in==NULL) { return -1; } char buf[4096]; size_t read; do { read=fread(buf, 1, sizeof(buf), in); if(read>0) { ret.append(buf, buf+read); } } while(read==sizeof(buf)); return _pclose(in); }
//----------------------------------------------------------------------------// int _execPipe(const char* cmd, std::string * err) { #ifdef __unix__ FILE* pipe = popen(cmd, "r"); #else FILE* pipe = _popen(cmd, "r"); #endif if (!pipe) { (*err) += "Could not execute command"; (*err) += std::string(cmd); return 1; } char buffer[128]; std::string result = ""; while(!feof(pipe)) { if(fgets(buffer, 128, pipe) != NULL) result += buffer; } #ifdef __unix__ int exit_status = pclose(pipe); #else int exit_status = _pclose(pipe); #endif if (exit_status) { (*err) += result; } return exit_status; }
static void pipe_Exit(void) { #if defined unix || (defined __APPLE__ && defined __MACH__) int pstat; pid_t pid2; #endif VC_Exit(); MikMod_free(audiobuffer); if(pipeout) { _mm_delete_file_writer(pipeout); pipeout=NULL; } if(pipefile) { #if !defined unix && (!defined __APPLE__ || !defined __MACH__) #ifdef __WATCOMC__ _pclose(pipefile); #else pclose(pipefile); #endif #ifdef __EMX__ _fsetmode(stdout,"t"); #endif #else fclose(pipefile); do { pid2=waitpid(pid,&pstat,0); } while (pid2==-1 && errno==EINTR); #endif pipefile=NULL; } }
int p_fclose(p_file *file) { int flag = (file->binary&2)? _pclose(file->fp) : fclose(file->fp); p_free(file); return flag; }
//----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- std::string CVProjectUtil::ConvertMaterialSrcToVtf( const std::string &file ) { const std::string game( Prefix() + "/game/" + Game() ); const std::string src( MaterialSrc( file ) ); const std::string cmd( std::string( "vtex -mkdir -nopause -game \"" ) + game + std::string( "\" \"" ) + src + "\"" ); FILE *vtex( _popen( cmd.c_str(), "rt" ) ); if ( !vtex ) { merr << "Couldn't execute vtex command: " << cmd << std::endl; return false; } char buf[ BUFSIZ ]; while ( !feof( vtex ) ) { if ( fgets( buf, BUFSIZ, vtex ) == NULL ) { break; } minfo << "vtex: " << buf; } minfo << std::endl; _pclose( vtex ); return MaterialDst( src ); }
//Добавить нового пользователя void addNewUser(char* username, char* password) { char command[1024]; memset(command,0,1024); strcat_s(command,"echo '"); strcat_s(command,username); strcat_s(command,"\n"); strcat_s(command,password); strcat_s(command,"\n"); strcat_s(command,"cd ls who kill logout\n"); strcat_s(command,retCurrentDirectory()); strcat_s(command,"' >> usersandpasswords.txt"); FILE *pPipe; /* Run DIR so that it writes its output to a pipe. Open this * pipe with read text attribute so that we can read it * like a text file. */ if( (pPipe = _popen( command, "rt" )) == NULL ) exit( 1 ); /* Close pipe and print return value of pPipe. */ if (feof( pPipe)) { printf( "\nProcess returned %d\n", _pclose( pPipe ) ); } else { printf( "Error: Failed to read the pipe to the end.\n"); } }
/// Dtor. virtual ~fptrostream () { #ifdef _MSC_VER _pclose(fptr_); #else pclose(fptr_); #endif }
int pclose( FILE *f ) { #ifdef __DOS__ return( 0 ); #else return( _pclose( f ) ); #endif }
void closeFileAuto(AutoFile* seqFile) { if (!seqFile) return; if (seqFile->pid) _pclose(seqFile->file); else fclose(seqFile->file); }
/** 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; }
void fclose_comp (FILE *fp, int32 ispipe) { if (ispipe) { #if (SPEC_CPU_WINDOWS) _pclose (fp); #else pclose (fp); #endif } else fclose (fp); }
void fclose_comp (FILE *fp, int32 ispipe) { if (ispipe) { #if (WIN32) _pclose (fp); #else pclose (fp); #endif } else fclose (fp); }
/** * Wykonywanie komend systemowych z przechwytywaniem wyniku * @param cmd - komenda do wykonania * @returns std::string - wynik wykonania */ std::string exec(char* cmd) { FILE* pipe = _popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; std::string result = ""; while(!feof(pipe)) { if(fgets(buffer, 128, pipe) != NULL) result += buffer; } _pclose(pipe); return result; }
string content::filemagic() { #ifdef HAVE_LIBMAGIC static bool magic_init=false; static bool magic_bad = false; static magic_t mt; if(magic_bad) return string(""); if(magic_init==false){ magic_init=true; mt = magic_open(MAGIC_NONE); if(magic_load(mt,NULL)==-1){ magic_bad = true; return string(""); } } const char *ret_ = magic_file(mt,tempfile_path.c_str()); string ret(ret_ ? ret_ : ""); #elif _MSC_VER char cmd[1024]; char buf[1024]; string ret; snprintf(cmd,sizeof(cmd),"file -b -z '%s'",tempfile_path.c_str()); FILE *f = _popen(cmd,"r"); while(!feof(f)){ if(fgets(buf,sizeof(buf),f)) ret += buf; } _pclose(f); /* Remove the newlines */ #else char cmd[1024]; char buf[1024]; string ret; snprintf(cmd,sizeof(cmd),"file -b -z '%s'",tempfile_path.c_str()); FILE *f = popen(cmd,"r"); while(!feof(f)){ if(fgets(buf,sizeof(buf),f)) ret += buf; } pclose(f); /* Remove the newlines */ #endif /* Remove newlines and invalid characters */ for(string::iterator cc = ret.begin(); cc!=ret.end(); cc++){ if(!isprint(*cc)){ *cc = ' '; } } while(ret.size()>0 && ret[ret.size()-1]=='\n'){ ret.erase(ret.size()-1); } return ret; }
//------------------------------------------------------------------------------ // // Destructor: needed to delete temporary files // Gnuplot::~Gnuplot() { // remove_tmpfiles(); // A stream opened by popen() should be closed by pclose() #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) if (_pclose(gnucmd) == -1) #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) if (pclose(gnucmd) == -1) #endif throw GnuplotException("Problem closing communication to gnuplot"); }
void sfcloseout(CSOUND *csound) { OPARMS *O = csound->oparms; int nb; alloc_globals(csound); if (!STA(osfopen)) return; if ((nb = (O->outbufsamps - STA(outbufrem)) * sizeof(MYFLT)) > 0) { /* flush outbuffer */ csound->nrecs++; csound->audtran(csound, STA(outbuf), nb); } if (STA(pipdevout) == 2 && (!STA(isfopen) || STA(pipdevin) != 2)) { /* close only if not open for input too */ csound->rtclose_callback(csound); } if (STA(pipdevout) == 2) goto report; if (STA(outfile) != NULL) { if (!STA(pipdevout) && O->outformat != AE_VORBIS) sf_command(STA(outfile), SFC_UPDATE_HEADER_NOW, NULL, 0); sf_close(STA(outfile)); STA(outfile) = NULL; } #ifdef PIPES if (STA(pout) != NULL) { _pclose(STA(pout)); STA(pout) = NULL; } #endif report: if (STA(pipdevout) == 2) { csound->Message(csound, "%"PRIi32" %d %s%lu%s%s\n", csound->nrecs, O->outbufsamps, Str("sample blks of "), (unsigned long)sizeof(MYFLT)*8,Str("-bit floats written to "), STA(sfoutname)); } else { csound->Message(csound, Str("%"PRIi32" %d sample blks of %s written to %s"), O->outbufsamps, O->outbufsamps * O->sfsampsize, getstrformat(O->outformat), STA(sfoutname)); if (O->sfheader == 0) csound->Message(csound, Str(" (raw)\n")); else csound->Message(csound, " (%s)\n", type2string(O->filetyp)); } STA(osfopen) = 0; }
void close() { // Close the pipe manually! if (pipe != NULL) { #if LOOM_PLATFORM == LOOM_PLATFORM_WIN32 _pclose(pipe); #elif LOOM_PLATFORM == LOOM_PLATFORM_OSX pclose(pipe); #else // Do nothing, there is no pipe to close! #endif } }
// File Destructor // closes the file // Note: this does not check for errors when the File corresponds to pipe stream. In this case, use Flush() before closing a file you are writing. File::~File(void) { if (m_pcloseNeeded) { // TODO: Check for error code and throw if !std::uncaught_exception() _pclose(m_file); } else if (m_file != stdin && m_file != stdout && m_file != stderr) { int rc = fclose(m_file); if ((rc != 0) && !std::uncaught_exception()) RuntimeError("File: failed to close file at %S", m_filename.c_str()); } }
static int __stdcall getData(void *param) { System* self = (System*)param; // Zero out the buffer memset(self->buffer, 0, BUFFER_LENGTH); while (true) { // Get the next character! int receivedChar = fgetc(self->pipe); // If getting the last character set the EOF flag, break out of the loop immedietly if (feof(self->pipe)) break; // Make sure we are getting actual characters if (receivedChar < 0) continue; if (receivedChar != 10 && receivedChar != 13) { // The received character is not a new line character, and we have room for it. Add it to the buffer self->buffer[strlen(self->buffer)] = static_cast<char>(receivedChar); } if (receivedChar == 10 || receivedChar == 13 || strlen(self->buffer) >= BUFFER_LENGTH) { // Either we hit a new line, or the buffer is full. send back the data! self->_OnDataDelegate.pushArgument(self->buffer); self->_OnDataDelegate.invoke(); // Zero out the buffer to prepare for the next packet of data memset(self->buffer, 0, BUFFER_LENGTH); } } // The Loop finished, which means we hit an EOF (The pipe is finished giving back data) close it! #if LOOM_PLATFORM == LOOM_PLATFORM_WIN32 _pclose(self->pipe); #elif LOOM_PLATFORM == LOOM_PLATFORM_OSX pclose(self->pipe); #else // Do nothing, there is no pipe to close! #endif // After the pipe is closed, invoke the onFinish delegate self->_OnFinishDelegate.invoke(); return 0; }
std::wstring TestUtils::exec(LPCTSTR szCmd) { FILE* pipe = _tpopen(szCmd, _T("rt")); if (!pipe) return L"ERROR"; wchar_t buffer[4096]; std::wstring result; while(!feof(pipe)) { if(fgetws(buffer, 4096, pipe) != NULL) result += buffer; } wtrim(result); _pclose(pipe); return result; }