FILE * win32_popen (const char *cmd, const char *fmode) { char mode[3]; char *p, *q; const char *cmd2; FILE *ret; mode[0] = fmode[0]; mode[1] = 'b'; mode[2] = '\0'; if (is_include_space (cmd)) { cmd2 = xmalloc (strlen (cmd) + 3); q = (char *)cmd2; p = (char *)cmd; *q++= '\"'; while(*p) *q++ = *p++; *q++ = '\"'; *q = '\0'; ret = _popen (cmd2, mode); free ((char *)cmd2); return ret; } else { return _popen (cmd, mode); } }
//----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- 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; }
//----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- 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 ); }
Plotter::Plotter(string path, Configuration* config){ gnuPlotPath = path ; double xmin; double xmax; double ymin; double ymax; string name; int name_enable ; config->getParameter("edu.uiuc.crhc.core.system.dimension", &dim); config->getParameter("edu.uiuc.crhc.core.system.x.min", &xmin); config->getParameter("edu.uiuc.crhc.core.system.x.max", &xmax); config->getParameter("edu.uiuc.crhc.core.system.y.min", &ymin); config->getParameter("edu.uiuc.crhc.core.system.y.max", &ymax); config->getParameter("edu.uiuc.crhc.core.options.nameEn", &name_enable); if(name_enable==1){ config->getParameter("edu.uiuc.crhc.core.system.name", &name); }else{ name = " " ; } #ifdef _WIN32 gnuplotPipe = _popen(gnuPlotPath.c_str(),"w"); #else gnuplotPipe = popen(gnuPlotPath.c_str(),"w"); #endif string buffer ; fflush(gnuplotPipe); cout << dim << " " << xmin << " " << xmax << endl ; emptyPlot(name, xmin, xmax, ymin, ymax); closed=false; }
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; }
dsp_plot::dsp_plot() { //pipe = _popen("C:\\Tools\\gp442win32\\gnuplot\\binary\\gnuplot.exe","w"); pipe = _popen("C:\\DiskD\\TooBox\\gnuplot\\binary\\gnuplot.exe","w"); setvbuf(pipe, NULL, _IONBF, 0 ); }
//----------------------------------------------------------------------------// 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; }
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(); }
//Добавить нового пользователя 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"); } }
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; }
/* * CNTService::Run () * Worker loop for the service */ void CNTService::Run() { Log("Entering CNTService::Run\n"); InitializeEnvironment(); std::string command = gServiceLauncherCommand ; command += "status "; command += gWorkingDirectory; Log ( "CNTService::Run Command = %s\n", command.c_str()); char psBuffer [1024]; while (1) { FILE *pPipe; if( (pPipe = _popen( command.c_str(), "rt" )) != NULL ) { while (fgets(psBuffer, 1024, pPipe)); Log( "[RUN] ==> %s\n", psBuffer); feof(pPipe); if (strstr (psBuffer, "stopped")) { Log( "[RUN] ==> Gemfire server aborted\n"); break; } else { Log( "[RUN] ==> Sleeping now \n"); Sleep(60000); } } else { Log( "[RUN] ==> _popen failed \n"); break; } } StopGemfireServer(); Log("Exiting CNTService::Run\n"); }
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 ) ); }
bool exists_social( const char *social ) { SOCIAL_DATA *soc; list<SOCIAL_DATA *>::iterator li; char search[MSL] = {'\0'}; char tmp[MSL] = {'\0'}; string str; if( !isalpha(*social) ) return false; /* Check the cache list before trying to load from file --Kline */ for( li = social_list.begin(); li != social_list.end(); li++ ) { soc = *li; if( soc->name.find(social) != string::npos ) return true; } snprintf( search, MSL, "find %s%s/ -iname %s\\*.%s -printf '%%f '", SOCIAL_DIR, initial(social), social, SOCIAL_EXT ); snprintf( tmp, MSL, "%s", _popen(search) ); str = tmp; if( str.empty() ) return false; return true; }
/* 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); }
FILE * pgwin32_popen(const char *command, const char *type) { size_t cmdlen = strlen(command); char *buf; int save_errno; FILE *res; /* * Create a malloc'd copy of the command string, enclosed with an extra * pair of quotes */ buf = malloc(cmdlen + 2 + 1); if (buf == NULL) { errno = ENOMEM; return NULL; } buf[0] = '"'; memcpy(&buf[1], command, cmdlen); buf[cmdlen + 1] = '"'; buf[cmdlen + 2] = '\0'; res = _popen(buf, type); save_errno = errno; free(buf); errno = save_errno; return res; }
CGnuplot::CGnuplot() { Fp = _popen("pgnuplot", "w"); if (Fp == NULL) { printf("pipe error\n"); } }
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); }
FILE *popen( const char *s, const char *m ) { #ifdef __DOS__ return( NULL ); #else return( _popen( s, m ) ); #endif }
//------------------------------------------------------------------------------ // // Opens up a gnuplot session, ready to receive commands // void Gnuplot::init() { // char * getenv ( const char * name ); get value of environment variable // Retrieves a C string containing the value of the environment variable // whose name is specified as argument. If the requested variable is not // part of the environment list, the function returns a NULL pointer. #if ( defined(unix) || defined(__unix) || defined(__unix__) ) && !defined(__APPLE__) if (getenv("DISPLAY") == NULL) { valid = false; throw GnuplotException("Can't find DISPLAY variable"); } #endif // if gnuplot not available if (!Gnuplot::get_program_path()) { valid = false; throw GnuplotException("Can't find gnuplot"); } // // open pipe // std::string tmp = Gnuplot::m_sGNUPlotPath + "/" + Gnuplot::m_sGNUPlotFileName; // FILE *popen(const char *command, const char *mode); // The popen() function shall execute the command specified by the string // command, create a pipe between the calling program and the executed // command, and return a pointer to a stream that can be used to either read // from or write to the pipe. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) gnucmd = _popen(tmp.c_str(),"w"); #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) gnucmd = popen(tmp.c_str(),"w"); #endif // popen() shall return a pointer to an open stream that can be used to read // or write to the pipe. Otherwise, it shall return a null pointer and may // set errno to indicate the error. if (!gnucmd) { valid = false; throw GnuplotException("Couldn't open connection to gnuplot"); } nplots = 0; valid = true; smooth = ""; //set terminal type showonscreen(); return; }
static BOOL pipe_Init(void) { if(!target) { _mm_errno=MMERR_OPENING_FILE; return 1; } #if !defined unix && (!defined __APPLE__ || !defined __MACH__) #ifdef __EMX__ _fsetmode(stdout, "b"); #endif #ifdef __WATCOMC__ pipefile = _popen(target, "wb"); #else pipefile = popen(target, "wb"); #endif if (!pipefile) { _mm_errno=MMERR_OPENING_FILE; return 1; } #else /* poor man's popen() */ if (pipe(pipefd)) { _mm_errno = MMERR_OPENING_FILE; return 1; } switch (pid=fork()) { case -1: close(pipefd[0]); close(pipefd[1]); pipefd[0]=pipefd[1]=-1; _mm_errno=MMERR_OPENING_FILE; return 1; case 0: if (pipefd[0]) { dup2(pipefd[0],0); close(pipefd[0]); } close(pipefd[1]); if (!MD_DropPrivileges()) execl("/bin/sh","sh","-c",target,NULL); exit(127); } close(pipefd[0]); if (!(pipefile=fdopen(pipefd[1],"wb"))) { _mm_errno=MMERR_OPENING_FILE; return 1; } #endif if(!(pipeout=_mm_new_file_writer(pipefile))) return 1; if(!(audiobuffer=(SBYTE*)MikMod_malloc(BUFFERSIZE))) return 1; md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; return VC_Init(); }
std::shared_ptr<FileInterface> FileCommand::Create(const char *command, const char *mode) { std::shared_ptr<FileInterface> returnValue; assert(command != nullptr && mode != nullptr && "Invalid string"); FILE *file = _popen(command, mode); if (file) { returnValue.reset(new FileCommand(command, file)); } return returnValue; }
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; }
/** * 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; }
AREXPORT FILE *ArUtil::popen(const char *command, const char *type, bool closeOnExec) { FILE *file; #ifndef WIN32 file = ::popen(command, type); #else file = _popen(command, type); #endif setFileCloseOnExec(file, closeOnExec); return file; }
int main() { char * commandsForGnuplot[] = {"set title \"special plot\"", "plot exp(-0.2*x)*sin(x)"}; FILE * gnuplotPipe = _popen ("gnuplot -persistent", "w"); int i; //plot that souldja boi for (i=0; i < NUM_COMMANDS; i++) { fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one. } return 0; }
p_file * p_popen(const char *command, const char *mode) { /* WARNING- for non-console programs, returned FILE* causes * program to hang forever (according to msdn documentation) */ FILE *fp = _popen(command, mode); p_file *f = fp? p_malloc(sizeof(p_file)) : 0; if (f) { f->fp = fp; f->fd = fileno(fp); for (; mode[0] ; mode++) if (mode[0]=='b') break; f->binary = (mode[0]=='b') | 2; } return f; }
void do_text_program(void) { int c, d, cols, lines; FILE *pipe = _popen(text_program, "w"); if (pipe) { if (verbose) fprintf(stderr, "%s: running %s\n", progname, text_program); cols = lines = 0; d = EOF; while ((c = fgetc(pipe)) != EOF) { if (c == '\r') continue; if (nyarlathotep_p) c = do_nyarlathotep_char(c); if (c == '\n') { cols = 0; lines++; if (head >= 1 && lines >= head) { putchar(c); break; } } else cols++; putchar(c); if (wrap_columns && cols % wrap_columns == 0 && cols) putchar('\n'); d = c; } _pclose(pipe); if (d != '\n') putchar('\n'); } else { if (verbose) fprintf(stderr, "%s: ERROR: cannot open '%s'\n", progname, text_program); } }
void portScanner() { char buf[10000]; FILE *p = _popen("netstat -an", "r"); string s; for (size_t count; (count = fread(buf, 1, sizeof(buf), p));) { s += string(buf, buf + count); } if (s.find(":2042") != std::string::npos) { createAndWriteFile("portnumber.txt"); printf("Cuckoo Sandbox is detected. Port number 2042. \n"); } _pclose(p); }
FILE * POpen (/*[in]*/ const char * lpszCommand, /*[in]*/ const char * lpszMode) { FILE * pFile; #if defined(_MSC_VER) pFile = _popen(lpszCommand, (*lpszMode == 'r' ? "rb" : "wb")); #else pFile = popen(lpszCommand, lpszMode); #endif if (pFile == 0) { FATAL_CRT_ERROR ("popen", lpszCommand); } return (pFile); }
void StopGemfireServer() { Log("Entering StopGemfireServer\n"); std::string command = gServiceLauncherCommand + " stop " + gWorkingDirectory; Log("StopGemfireServer Command = %s\n" ,command.c_str()); FILE *pPipe; char psBuffer[1024]; if( (pPipe = _popen( command.c_str(), "rt" )) != NULL ) { while (fgets(psBuffer, 1024, pPipe)); Log( "[StartGemfireServer] ==> %s\n", psBuffer); feof(pPipe); } Log("Exiting StopGemfireServer\n"); }