diff options
Diffstat (limited to '')
-rw-r--r-- | zgrep.cc | 46 |
1 files changed, 24 insertions, 22 deletions
@@ -18,8 +18,8 @@ void show_zgrep_help() { std::printf( "Zgrep is a front end to the grep program that allows transparent search\n" - "on any combination of compressed and non-compressed files. If any given\n" - "file is compressed, its uncompressed content is used. If a given file\n" + "on any combination of compressed and uncompressed files. If any given\n" + "file is compressed, its decompressed content is used. If a given file\n" "does not exist, and its name does not end with one of the known\n" "extensions, zgrep tries the compressed file names corresponding to the\n" "supported formats. If no files are specified, data is read from\n" @@ -51,6 +51,7 @@ void show_zgrep_help() " -L, --files-without-match only print names of files containing no matches\n" " -m, --max-count=<n> stop after <n> matches\n" " -n, --line-number print the line number of each line\n" + " -N, --no-rcfile don't read runtime configuration file\n" " -o, --only-matching show only the part of a line matching <pattern>\n" " -q, --quiet suppress all messages\n" " -r, --recursive operate recursively on directories\n" @@ -58,16 +59,20 @@ void show_zgrep_help() " -v, --invert-match select non-matching lines\n" " --verbose verbose mode (show error messages)\n" " -w, --word-regexp match only whole words\n" - " -x, --line-regexp match only whole lines\n" ); + " -x, --line-regexp match only whole lines\n" + " --bz2=<command> set compressor and options for bzip2 format\n" + " --gz=<command> set compressor and options for gzip format\n" + " --lz=<command> set compressor and options for lzip format\n" + " --xz=<command> set compressor and options for xz format\n" ); show_help_addr(); } -int zgrep_stdin( int infd, const int format_type, +int zgrep_stdin( int infd, const int format_index, const std::vector< const char * > & grep_args ) { - pid_t pid; - if( !set_data_feeder( &infd, &pid, format_type ) ) return 2; + Children children; + if( !set_data_feeder( &infd, children, format_index ) ) return 2; const pid_t grep_pid = fork(); if( grep_pid == 0 ) // child (grep) { @@ -83,28 +88,26 @@ int zgrep_stdin( int infd, const int format_type, show_exec_error( GREP ); _exit( 2 ); } - // parent - if( grep_pid < 0 ) + if( grep_pid < 0 ) // parent { show_fork_error( GREP ); return 2; } int retval = wait_for_child( grep_pid, GREP ); - if( retval != 1 ) - { if( pid ) kill( pid, SIGTERM ); } - else - if( pid && wait_for_child( pid, "data feeder" ) != 0 ) retval = 2; + + if( !good_status( children, retval == 1 ) ) retval = 2; + if( close( infd ) != 0 ) { show_close_error( "data feeder" ); return 2; } return retval; } -int zgrep_file( int infd, const int format_type, +int zgrep_file( int infd, const int format_index, const std::string & input_filename, const std::vector< const char * > & grep_args, const int grep_list_mode, const bool grep_show_name ) { - pid_t pid; - if( !set_data_feeder( &infd, &pid, format_type ) ) return 2; + Children children; + if( !set_data_feeder( &infd, children, format_index ) ) return 2; int fda[2]; // pipe from grep if( pipe( fda ) < 0 ) { show_error( "Can't create pipe", errno ); return 2; } @@ -125,10 +128,10 @@ int zgrep_file( int infd, const int format_type, show_exec_error( GREP ); _exit( 2 ); } - // parent - close( fda[1] ); - if( grep_pid < 0 ) + if( grep_pid < 0 ) // parent { show_fork_error( GREP ); return 2; } + + close( fda[1] ); enum { buffer_size = 256 }; uint8_t buffer[buffer_size]; bool line_begin = true; @@ -154,10 +157,9 @@ int zgrep_file( int infd, const int format_type, } int retval = wait_for_child( grep_pid, GREP ); - if( retval != 1 ) - { if( pid ) kill( pid, SIGTERM ); } - else - if( pid && wait_for_child( pid, "data feeder" ) != 0 ) retval = 2; + + if( !good_status( children, retval == 1 ) ) retval = 2; + if( grep_list_mode && (retval == 0) == (grep_list_mode == 1) ) std::printf( "%s\n", input_filename.c_str() ); if( close( infd ) != 0 ) |