void ProcessSetArgumentList( process_t p,... ) { char * entry ; char ** args ; char ** e ; size_t size = sizeof( char * ) ; int index = 0 ; va_list list ; if( p == ProcessVoid ){ return ; } args = ( char ** )malloc( size ) ; if( args == NULL ){ _ProcessError() ; return ; } args[ index ] = p->exe ; index++ ; va_start( list,p ) ; while( 1 ){ entry = va_arg( list,char * ) ; e = ( char ** )realloc( args,( 1 + index ) * size ) ; if( e == NULL ){ free( args ) ; _ProcessError() ; va_end( list ) ; return ; }else{ args = e ; } if( entry == NULL ){ args[ index ] = NULL ; break ; }else{ args[ index ] = entry ; index++ ; } } va_end( list ) ; p->args = args ; p->str.args = ( const char * const * ) args ; }
static process_t _process( const char * path ) { process_t p ; size_t len ; if( path == NULL ){ len = 0 ; }else{ len = strlen( path ) ; } p = malloc( sizeof( struct ProcessType_t ) ) ; if( p == NULL ){ return _ProcessError() ; } if( len == 0 ){ p->exe = malloc( sizeof( char ) ) ; if( p->exe == NULL ){ free( p ) ; return _ProcessError() ; } p->exe[ 0 ] = '\0' ; }else{ p->exe = malloc( sizeof( char ) * ( len + 1 ) ) ; if( p->exe == NULL ){ free( p ) ; return _ProcessError() ; } memcpy( p->exe,path,len + 1 ) ; } p->std_io = 0 ; p->wait_status = -1 ; p->thread = NULL ; p->fd_0[ 0 ] = -1 ; p->args = NULL ; p->str.args = NULL ; p->str.timeout = -1 ; p->str.env = NULL ; p->str.user_id = -1 ; p->str.priority = 0 ; p->str.signal = SIGTERM ; p->state = ProcessHasNotStarted ; return p ; }
int ProcessExecute( const char * exe,... ) { char * entry ; char ** args ; char ** e ; size_t size = sizeof( char * ) ; int index = 1 ; va_list list ; process_t p ; if( exe == NULL ){ return -1 ; } p = _process( exe ) ; if( p == ProcessVoid ){ return -1 ; } args = p->args ; va_start( list,exe ) ; while( 1 ){ entry = va_arg( list,char * ) ; e = realloc( args,( 1 + index ) * size ) ; if( e == NULL ){ ProcessCleanUp( &p ) ; free( args ) ; va_end( list ) ; _ProcessError() ; return -1 ; }else{ args = e ; } if( entry == NULL ){ *( args + index ) = NULL ; break ; }else{ *( args + index ) = entry ; index++ ; } } va_end( list ) ; p->args = args ; p->args[ 0 ] = p->exe ; p->str.args = args ; ProcessStart( p ) ; return ProcessWaitUntilFinished( &p ) ; }
process_t Process( const char * path,... ) { char * entry ; char ** args ; char ** e ; size_t size = sizeof( char * ) ; int index = 1 ; va_list list ; process_t p ; if( path == NULL ){ return _process( NULL ) ; } p = _process( path ) ; if( p == ProcessVoid ){ return ProcessVoid ; } args = p->args ; va_start( list,path ) ; while( 1 ){ entry = va_arg( list,char * ) ; e = realloc( args,( 1 + index ) * size ) ; if( e == NULL ){ ProcessCleanUp( &p ) ; free( args ) ; va_end( list ) ; _ProcessError() ; return ProcessVoid ; }else{ args = e ; } if( entry == NULL ){ *( args + index ) = NULL ; break ; }else{ *( args + index ) = entry ; index++ ; } } va_end( list ) ; p->args = args ; p->args[ 0 ] = p->exe ; p->str.args = args ; return p ; }
static void __ProcessStartTimer( process_t p ) { p->thread = ( pthread_t * ) malloc( sizeof( pthread_t ) ) ; if( p->thread == NULL ){ _ProcessError() ; }else{ pthread_create( p->thread,NULL,__timer,( void * ) p ); } }
process_t Process( const char * path ) { process_t p ; size_t len ; if( path == NULL ){ return ProcessVoid; } len = strlen( path ) + 1 ; p = ( process_t ) malloc( sizeof( struct ProcessType_t ) ) ; if( p == NULL ){ return _ProcessError() ; } p->exe = ( char * ) malloc( sizeof( char ) * len ) ; if( p->exe == NULL ){ free( p ) ; return _ProcessError() ; } memcpy( p->exe,path,len ) ; p->std_io = 0 ; p->wait_status = -1 ; p->thread = NULL ; p->fd_0[ 0 ] = -1 ; p->args = NULL ; p->str.args = NULL ; p->str.timeout = -1 ; p->str.env = NULL ; p->str.user_id = -1 ; p->str.priority = 0 ; p->str.signal = SIGTERM ; p->state = HAS_NOT_START ; return p ; }
static inline char * __bufferExpandMemory( char * buffer,size_t new_size,size_t * buffer_size ) { char * e ; if( new_size >= *buffer_size ) { *buffer_size = new_size * FACTOR ; e = ( char * )realloc( buffer,*buffer_size ) ; if( e == NULL ){ free( buffer ) ; _ProcessError() ; return NULL ; }else{ return e ; } }else{ return buffer ; } }
size_t ProcessGetOutPut( process_t p,char ** data,int std_io ) { #define SIZE 64 #define BUFFER_SIZE 128 char * buffer ; char * e ; char buff[ SIZE ] ; size_t size = 0 ; size_t count ; size_t buffer_size = BUFFER_SIZE ; int fd ; if( p == ProcessVoid ){ return 0 ; } buffer = ( char * ) malloc( sizeof( char ) * BUFFER_SIZE ) ; if( buffer == NULL ){ _ProcessError() ; return 0 ; } if( std_io == STDOUT ){ fd = p->fd_1[ 0 ] ; }else{ fd = p->fd_2[ 0 ] ; } while( 1 ) { count = read( fd,buff,SIZE ) ; buffer = __bufferExpandMemory( buffer,size + count,&buffer_size ) ; if( buffer == NULL ){ return 0 ; }else{ memcpy( buffer + size,buff,count ) ; } size += count ; if( count < SIZE ){ break ; } } if( size > 0 ){ e = realloc( buffer,size + 1 ) ; if( e == NULL ){ free( buffer ) ; _ProcessError() ; return 0 ; }else{ e[ size ] = '\0' ; *data = e ; } } return size ; }