10
Filter the output of a command
- /*
- * Runs ps -e and lists processes matching argument.
- *
- * popen
- * pclose
- * strstr
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #if defined( LINUX )
- #include <string.h>
- #endif
- #define PS "/bin/ps -e"
- int main( int argc, char *argv[] ) {
- FILE *pfd;
- char *proc;
- char line[4096];
- switch ( argc ) {
- case 2:
- proc = argv[1];
- break;
- default:
- usage: fprintf( stderr, "%s procname\n", argv[0] );
- exit( 1 );
- }
- if ( (pfd = popen( PS, "r" )) == NULL ) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- exit( 1 );
- }
- while ( fgets( line, sizeof(line), pfd ) != NULL )
- if ( strstr( line, proc ) )
- fputs( line, stdout );
- pclose( pfd );
- exit( 0 );
- }
Comments