summaryrefslogtreecommitdiffstats
path: root/zgrep.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--zgrep.cc54
1 files changed, 29 insertions, 25 deletions
diff --git a/zgrep.cc b/zgrep.cc
index 1013b5e..01c6b07 100644
--- a/zgrep.cc
+++ b/zgrep.cc
@@ -1,18 +1,18 @@
-/* Zgrep - search compressed files for a regular expression
- Copyright (C) 2010-2019 Antonio Diaz Diaz.
+/* Zgrep - search compressed files for a regular expression
+ Copyright (C) 2010-2020 Antonio Diaz Diaz.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
@@ -47,21 +47,23 @@ namespace {
void show_help()
{
- std::printf( "Zgrep is a front end to the grep program that allows transparent search\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"
+ std::printf( "zgrep is a front end to the program grep that allows transparent search\n"
+ "on any combination of compressed and uncompressed files. If any file\n"
+ "given is compressed, its decompressed content is used. If a file given\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"
- "formats supported.\n"
+ "formats supported. If a file fails to decompress, zgrep continues\n"
+ "searching the rest of the files.\n"
"\nIf a file is specified as '-', data are read from standard input,\n"
"decompressed if needed, and fed to grep. Data read from standard input\n"
"must be of the same type; all uncompressed or all in the same\n"
- "compression format.\n"
+ "compressed format.\n"
"\nIf no files are specified, recursive searches examine the current\n"
"working directory, and nonrecursive searches read standard input.\n"
- "\nThe formats supported are bzip2, gzip, lzip and xz.\n"
+ "\nThe formats supported are bzip2, gzip, lzip, and xz.\n"
"\nUsage: zgrep [options] <pattern> [files]\n"
"\nExit status is 0 if match, 1 if no match, 2 if trouble.\n"
+ "Some options only work if the grep program used supports them.\n"
"\nOptions:\n"
" --help display this help and exit\n"
" -V, --version output version information and exit\n"
@@ -87,7 +89,7 @@ void show_help()
" -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"
- " -O, --force-format=<fmt> force given format (bz2, gz, lz, xz)\n"
+ " -O, --force-format=<fmt> force the format given (bz2, gz, lz, xz)\n"
" -q, --quiet suppress all messages\n"
" -r, --recursive operate recursively on directories\n"
" -R, --dereference-recursive recursively follow symbolic links\n"
@@ -100,7 +102,7 @@ void show_help()
" --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"
- "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
+ "\nNumbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
"Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" );
show_help_addr();
}
@@ -217,15 +219,15 @@ int main( const int argc, const char * const argv[] )
enum { help_opt = 256, verbose_opt, color_opt,
bz2_opt, gz_opt, lz_opt, xz_opt };
int format_index = -1;
- int list_mode = 0; // 1 = list matches, -1 = list non matches
+ int list_mode = 0; // 1 = list matches, -1 = list non-matches
int recursive = 0; // 1 = '-r', 2 = '-R'
int show_name = -1; // tri-state bool
bool no_messages = false;
std::list< std::string > filenames;
std::vector< const char * > grep_args; // args to grep, maybe empty
std::string color_option; // needed because of optional arg
- invocation_name = argv[0];
program_name = "zgrep";
+ invocation_name = ( argc > 0 ) ? argv[0] : program_name;
const Arg_parser::Option options[] =
{
@@ -365,10 +367,10 @@ int main( const int argc, const char * const argv[] )
false, no_messages ) )
{
int infd;
- if( input_filename.empty() )
+ if( input_filename == "." )
{
if( stdin_used ) continue; else stdin_used = true;
- infd = STDIN_FILENO;
+ infd = STDIN_FILENO; input_filename = "-";
}
else
{
@@ -383,7 +385,9 @@ int main( const int argc, const char * const argv[] )
list_mode, show_name );
if( tmp == 0 || ( tmp == 2 && retval == 1 ) ) retval = tmp;
- if( input_filename.size() ) close( infd );
+ if( close( infd ) != 0 )
+ { show_file_error( input_filename.c_str(), "Error closing input file",
+ errno ); error = true; }
if( retval == 0 && verbosity < 0 ) break;
}