libs/stream_message/src/startprocess.cxx
// ===========================================================================
// Returns the status of the started process (0 indicating success)
// or 1 if an error occurred while starting the process.  If 'output'
// is not null, standard output of the process is captured into that string.
//
Returns the status of the started process (0 indicating success)
or 1 if an error occurred while starting the process.  If 'output'
is not null, standard output of the process is captured into that string.
Returns the status of the started process ( 0 indicating success ) or 1 if an error occurred while starting the process . If ' output ' is not null standard output of the process is captured into that string .
int MBDriver::start_process_and_wait(const char *path, const char *const argv[], string *output)
{
    processIdType process_id;
#if _WIN32
    HANDLE hOutputRead = NULL;
    MBDriver::pid_t pid = start(path, argv, 0,
                                output != NULL ? &hOutputRead : NULL,
				process_id);
    if (output != NULL) {
#ifdef ISO_CPP_HEADERS
	ostringstream ostrm;
#else
	ostrstream ostrm;
#endif
	if (hOutputRead != NULL) {
	    CHAR lpBuffer[256];
	    DWORD nBytesRead;
	    while(TRUE)
	    {
		if (!ReadFile(hOutputRead, lpBuffer, sizeof(lpBuffer), &nBytesRead, NULL)
		    || !nBytesRead) {
		   if (GetLastError() == ERROR_BROKEN_PIPE)
		      break; 
		}
		ostrm.write(lpBuffer, nBytesRead);
	    }
	    CloseHandle(hOutputRead);
	}
#ifndef ISO_CPP_HEADERS
	ostrm << ends;
#endif
	*output = ostrm.str();
    }
#else   
    FILE *outputRead = NULL;
    const MBDriver::pid_t pid = start(path, argv, 0,
				      output != NULL ? &outputRead : NULL,
				      process_id);
    if (output != NULL) {
#ifdef ISO_CPP_HEADERS
	ostringstream ostrm;
#else
	ostrstream ostrm;
#endif
	if (outputRead != NULL) {
	    char buffer[256];
#if 0
	    long nBytesRead;
            int odescr = fileno(outputRead);
#else
	    size_t nBytesRead;
#endif
	    while (true) {
#if 0
		nBytesRead = read(odescr, buffer, sizeof(buffer));
		if (nBytesRead < 0) {
		    if (errno == EAGAIN || errno == EINTR) {
			nBytesRead = 0;
		    }
		    else {
			break;
		    }
		}
		else if (nBytesRead == 0) {
		    break; 
		}
#else
		nBytesRead = fread(buffer, 1, sizeof(buffer), outputRead);
		if (nBytesRead == 0) {
		    if (feof(outputRead)) {
		        break; 
		    }
		    if (ferror(outputRead)) {
    	    	    	break;
		    }
		}
#endif
		ostrm.write(buffer, nBytesRead);
	    }
	    fclose(outputRead);
	}
#ifndef ISO_CPP_HEADERS
	ostrm << ends;
#endif
	*output = ostrm.str();
    }
#endif  
    return wait_process(pid);
}
int MBDriver :: start_process_and_wait ( const char * path , const char * const argv [ ] , string * output ) { processIdType process_id ; #if _WIN32 HANDLE hOutputRead = NULL ; MBDriver :: pid_t pid = start ( path , argv , 0 , output != NULL ? & hOutputRead : NULL , process_id ) ; if ( output != NULL ) { #ifdef ISO_CPP_HEADERS ostringstream ostrm ; #else ostrstream ostrm ; #endif if ( hOutputRead != NULL ) { CHAR lpBuffer [ 256 ] ; DWORD nBytesRead ; while ( TRUE ) { if ( ! ReadFile ( hOutputRead , lpBuffer , sizeof ( lpBuffer ) , & nBytesRead , NULL ) || ! nBytesRead ) { if ( GetLastError ( ) == ERROR_BROKEN_PIPE ) break ; } ostrm . write ( lpBuffer , nBytesRead ) ; } CloseHandle ( hOutputRead ) ; } #ifndef ISO_CPP_HEADERS ostrm << ends ; #endif * output = ostrm . str ( ) ; } #else FILE * outputRead = NULL ; const MBDriver :: pid_t pid = start ( path , argv , 0 , output != NULL ? & outputRead : NULL , process_id ) ; if ( output != NULL ) { #ifdef ISO_CPP_HEADERS ostringstream ostrm ; #else ostrstream ostrm ; #endif if ( outputRead != NULL ) { char buffer [ 256 ] ; #if 0 long nBytesRead ; int odescr = fileno ( outputRead ) ; #else size_t nBytesRead ; #endif while ( true ) { #if 0 nBytesRead = read ( odescr , buffer , sizeof ( buffer ) ) ; if ( nBytesRead < 0 ) { if ( errno == EAGAIN || errno == EINTR ) { nBytesRead = 0 ; } else { break ; } } else if ( nBytesRead == 0 ) { break ; } #else nBytesRead = fread ( buffer , 1 , sizeof ( buffer ) , outputRead ) ; if ( nBytesRead == 0 ) { if ( feof ( outputRead ) ) { break ; } if ( ferror ( outputRead ) ) { break ; } } #endif ostrm . write ( buffer , nBytesRead ) ; } fclose ( outputRead ) ; } #ifndef ISO_CPP_HEADERS ostrm << ends ; #endif * output = ostrm . str ( ) ; } #endif return wait_process ( pid ) ; }
Returns the status of the started process (0 indicating success)
or 1 if an error occurred while starting the process.
Returns the status of the started process ( 0 indicating success ) or 1 if an error occurred while starting the process .
// Monitor pipe for input from the child. // Exit when the child exits or pipe breaks. // pipe done - normal exit path. // Copy characters read. // Redirection is complete // Unix // Monitor pipe for input from the child. // Exit when the child exits or pipe breaks. // OK -- continue // pipe done - normal exit path. // pipe done - normal exit path. // Copy characters read. // Redirection is complete // NT/Unix