"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "zcat.cc" between
zutils-1.8.tar.lz and zutils-1.9.tar.lz

About: Zutils is a collection of utilities able to deal with any combination of compressed and non-compressed files transparently. The utilities zcat, zcmp, zdiff, zgrep and ztest supports the compressors bzip2, gzip, lzip and xz.

zcat.cc  (zutils-1.8.tar.lz):zcat.cc  (zutils-1.9.tar.lz)
/* Zcat - decompress and concatenate files to standard output /* Zcat - decompress and concatenate files to standard output
Copyright (C) 2010-2019 Antonio Diaz Diaz. Copyright (C) 2010-2020 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#include <cerrno> #include <cerrno>
#include <climits> #include <climits>
#include <csignal> #include <csignal>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
skipping to change at line 90 skipping to change at line 90
{ {
std::memcpy( buf, str.c_str(), str.size() ); std::memcpy( buf, str.c_str(), str.size() );
return str.size(); return str.size();
} }
}; };
Line_number line_number; Line_number line_number;
void show_help() void show_help()
{ {
std::printf( "Zcat copies each given file to standard output. If any given fil std::printf( "zcat copies each file argument to standard output in sequence. I
e is\n" f any\n"
"compressed, its decompressed content is used. If a given file do "file given is compressed, its decompressed content is copied. If
es not\n" a file\n"
"exist, and its name does not end with one of the known extension "given does not exist, and its name does not end with one of the
s, zcat\n" known\n"
"tries the compressed file names corresponding to the formats sup "extensions, zcat tries the compressed file names corresponding t
ported.\n" o the\n"
"formats supported. If a file fails to decompress, zcat continues
copying the\n"
"rest of the files.\n"
"\nIf a file is specified as '-', data are read from standard inp ut,\n" "\nIf a file is specified as '-', data are read from standard inp ut,\n"
"decompressed if needed, and sent to standard output. Data read f rom\n" "decompressed if needed, and sent to standard output. Data read f rom\n"
"standard input must be of the same type; all uncompressed or all in the\n" "standard input must be of the same type; all uncompressed or all in the\n"
"same compression format.\n" "same compressed format.\n"
"\nIf no files are specified, recursive searches examine the curr ent\n" "\nIf no files are specified, recursive searches examine the curr ent\n"
"working directory, and nonrecursive searches read standard input .\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: zcat [options] [files]\n" "\nUsage: zcat [options] [files]\n"
"\nExit status is 0 if no errors occurred, non-zero otherwise.\n" "\nExit status is 0 if no errors occurred, 1 otherwise.\n"
"\nOptions:\n" "\nOptions:\n"
" -h, --help display this help and exit\n" " -h, --help display this help and exit\n"
" -V, --version output version information and ex it\n" " -V, --version output version information and ex it\n"
" -A, --show-all equivalent to '-vET'\n" " -A, --show-all equivalent to '-vET'\n"
" -b, --number-nonblank number nonblank output lines\n" " -b, --number-nonblank number nonblank output lines\n"
" -e equivalent to '-vE'\n" " -e equivalent to '-vE'\n"
" -E, --show-ends display '$' at end of each line\n " " -E, --show-ends display '$' at end of each line\n "
" -M, --format=<list> process only the formats in <list >\n" " -M, --format=<list> process only the formats in <list >\n"
" -n, --number number all output lines\n" " -n, --number number all output lines\n"
" -N, --no-rcfile don't read runtime configuration file\n" " -N, --no-rcfile don't read runtime configuration file\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" " -q, --quiet suppress all messages\n"
" -r, --recursive operate recursively on directorie s\n" " -r, --recursive operate recursively on directorie s\n"
" -R, --dereference-recursive recursively follow symbolic links \n" " -R, --dereference-recursive recursively follow symbolic links \n"
" -s, --squeeze-blank never more than one single blank line\n" " -s, --squeeze-blank never more than one single blank line\n"
" -t equivalent to '-vT'\n" " -t equivalent to '-vT'\n"
" -T, --show-tabs display TAB characters as '^I'\n" " -T, --show-tabs display TAB characters as '^I'\n"
" -v, --show-nonprinting use '^' and 'M-' notation, except for LF and TAB\n" " -v, --show-nonprinting use '^' and 'M-' notation, except for LF and TAB\n"
" --verbose verbose mode (show error messages )\n" " --verbose verbose mode (show error messages )\n"
" --bz2=<command> set compressor and options for bz ip2 format\n" " --bz2=<command> set compressor and options for bz ip2 format\n"
" --gz=<command> set compressor and options for gz ip format\n" " --gz=<command> set compressor and options for gz ip format\n"
" --lz=<command> set compressor and options for lz ip format\n" " --lz=<command> set compressor and options for lz ip format\n"
" --xz=<command> set compressor and options for xz format\n" ); " --xz=<command> set compressor and options for xz format\n" );
show_help_addr(); show_help_addr();
} }
int do_cat( const int infd, const int buffer_size, bool do_cat( const int infd, const int buffer_size,
uint8_t * const inbuf, uint8_t * const outbuf, uint8_t * const inbuf, uint8_t * const outbuf,
const std::string & input_filename, const std::string & input_filename,
const Cat_options & cat_options ) const Cat_options & cat_options )
{ {
static int at_bol = 1; // at begin of line. 0 = false, 1 = true, static int at_bol = 1; // at begin of line. 0 = false, 1 = true,
// 2 = at begin of second blank line. // 2 = at begin of second blank line.
int inpos = 0; // positions in buffers int inpos = 0; // positions in buffers
int outpos = 0; int outpos = 0;
int rd = -1; // bytes read by the last readblock int rd = -1; // bytes read by the last readblock
unsigned char c; unsigned char c;
while( true ) while( true )
{ {
do { do {
if( outpos >= buffer_size ) if( outpos >= buffer_size )
{ {
if( writeblock( STDOUT_FILENO, outbuf, outpos ) != outpos ) if( writeblock( STDOUT_FILENO, outbuf, outpos ) != outpos )
{ show_error( "Write error", errno ); return 1; } { show_error( "Write error", errno ); return false; }
outpos = 0; outpos = 0;
} }
if( inpos > rd ) // inbuf is empty if( inpos > rd ) // inbuf is empty
{ {
rd = readblock( infd, inbuf, buffer_size ); rd = readblock( infd, inbuf, buffer_size );
if( rd != buffer_size && errno ) if( rd != buffer_size && errno )
{ {
show_file_error( input_filename.c_str(), "Read error", errno ); show_file_error( input_filename.c_str(), "Read error", errno );
return 1; return false;
} }
if( rd == 0 ) if( rd == 0 )
{ {
if( writeblock( STDOUT_FILENO, outbuf, outpos ) != outpos ) if( writeblock( STDOUT_FILENO, outbuf, outpos ) != outpos )
{ show_error( "Write error", errno ); return 1; } { show_error( "Write error", errno ); return false; }
outpos = 0; outpos = 0;
return 0; return true;
} }
inpos = 0; inpos = 0;
inbuf[rd] = '\n'; // sentinel newline inbuf[rd] = '\n'; // sentinel newline
} }
else // a real newline was found else // a real newline was found
{ {
if( at_bol > 1 ) if( at_bol > 1 )
{ {
if( cat_options.squeeze_blank ) { c = inbuf[inpos++]; continue; } if( cat_options.squeeze_blank ) { c = inbuf[inpos++]; continue; }
} }
skipping to change at line 224 skipping to change at line 226
while( c != '\n' ) while( c != '\n' )
{ {
if( c == '\t' && cat_options.show_tabs ) if( c == '\t' && cat_options.show_tabs )
{ c += 64; outbuf[outpos++] = '^'; } { c += 64; outbuf[outpos++] = '^'; }
outbuf[outpos++] = c; outbuf[outpos++] = c;
c = inbuf[inpos++]; c = inbuf[inpos++];
} }
} }
} }
int cat( int infd, const int format_index, const std::string & input_filename, bool cat( int infd, const int format_index, const std::string & input_filename,
const Cat_options & cat_options ) const Cat_options & cat_options )
{ {
enum { buffer_size = 4096, outbuf_size = (5 * buffer_size) + 256 + 1 }; enum { buffer_size = 4096, outbuf_size = (5 * buffer_size) + 256 + 1 };
// buffer with space for sentinel newline at the end // input buffer with space for sentinel newline at the end
uint8_t * const inbuf = new uint8_t[buffer_size+1]; uint8_t * const inbuf = new uint8_t[buffer_size+1];
// buffer with space for character quoting, 255-digit line number, // output buffer with space for character quoting, 255-digit line number,
// worst case flushing respect to inbuf, and a canary byte. // worst case flushing respect to inbuf, and a canary byte.
uint8_t * const outbuf = new uint8_t[outbuf_size]; uint8_t * const outbuf = new uint8_t[outbuf_size];
outbuf[outbuf_size-1] = 0; outbuf[outbuf_size-1] = 0;
int retval = 0;
Children children; Children children;
if( !set_data_feeder( input_filename, &infd, children, format_index ) ) bool error = false;
retval = 1;
else if( !set_data_feeder( input_filename, &infd, children, format_index ) ||
retval = do_cat( infd, buffer_size, inbuf, outbuf, !do_cat( infd, buffer_size, inbuf, outbuf, input_filename, cat_options ) )
input_filename, cat_options ); error = true;
if( !good_status( children, !error ) ) error = true;
if( !good_status( children, retval == 0 ) ) retval = 1; if( !error && close( infd ) != 0 ) { show_close_error(); error = true; }
if( outbuf[outbuf_size-1] != 0 ) internal_error( "buffer overflow." );
if( retval == 0 && close( infd ) != 0 )
{ show_close_error(); retval = 1; }
if( outbuf[outbuf_size-1] != 0 )
internal_error( "buffer overflow." );
delete[] outbuf; delete[] inbuf; delete[] outbuf; delete[] inbuf;
return retval; return !error;
} }
} // end namespace } // end namespace
int main( const int argc, const char * const argv[] ) int main( const int argc, const char * const argv[] )
{ {
enum { verbose_opt = 256, bz2_opt, gz_opt, lz_opt, xz_opt }; enum { verbose_opt = 256, bz2_opt, gz_opt, lz_opt, xz_opt };
int format_index = -1; int format_index = -1;
int recursive = 0; // 1 = '-r', 2 = '-R' int recursive = 0; // 1 = '-r', 2 = '-R'
std::list< std::string > filenames; std::list< std::string > filenames;
Cat_options cat_options; Cat_options cat_options;
invocation_name = argv[0];
program_name = "zcat"; program_name = "zcat";
invocation_name = ( argc > 0 ) ? argv[0] : program_name;
const Arg_parser::Option options[] = const Arg_parser::Option options[] =
{ {
{ 'A', "show-all", Arg_parser::no }, // cat { 'A', "show-all", Arg_parser::no }, // cat
{ 'b', "number-nonblank", Arg_parser::no }, // cat { 'b', "number-nonblank", Arg_parser::no }, // cat
{ 'c', "stdout", Arg_parser::no }, // gzip { 'c', "stdout", Arg_parser::no }, // gzip
{ 'd', "decompress", Arg_parser::no }, // gzip { 'd', "decompress", Arg_parser::no }, // gzip
{ 'e', 0, Arg_parser::no }, // cat { 'e', 0, Arg_parser::no }, // cat
{ 'E', "show-ends", Arg_parser::no }, // cat { 'E', "show-ends", Arg_parser::no }, // cat
{ 'f', "force", Arg_parser::no }, // gzip { 'f', "force", Arg_parser::no }, // gzip
skipping to change at line 354 skipping to change at line 351
setmode( STDIN_FILENO, O_BINARY ); setmode( STDIN_FILENO, O_BINARY );
setmode( STDOUT_FILENO, O_BINARY ); setmode( STDOUT_FILENO, O_BINARY );
#endif #endif
for( ; argind < parser.arguments(); ++argind ) for( ; argind < parser.arguments(); ++argind )
filenames.push_back( parser.argument( argind ) ); filenames.push_back( parser.argument( argind ) );
if( filenames.empty() ) filenames.push_back( recursive ? "." : "-" ); if( filenames.empty() ) filenames.push_back( recursive ? "." : "-" );
std::string input_filename; std::string input_filename;
int retval = 0;
bool error = false; bool error = false;
bool stdin_used = false; bool stdin_used = false;
while( next_filename( filenames, input_filename, error, recursive ) ) while( next_filename( filenames, input_filename, error, recursive ) )
{ {
int infd; int infd;
if( input_filename.empty() ) if( input_filename == "." )
{ {
if( stdin_used ) continue; else stdin_used = true; if( stdin_used ) continue; else stdin_used = true;
infd = STDIN_FILENO; infd = STDIN_FILENO; input_filename = "-";
} }
else else
{ {
infd = open_instream( input_filename, format_index < 0 ); infd = open_instream( input_filename, format_index < 0 );
if( infd < 0 ) { error = true; continue; } if( infd < 0 ) { error = true; continue; }
} }
const int tmp = cat( infd, format_index, input_filename, cat_options ); if( !cat( infd, format_index, input_filename, cat_options ) ) error = true;
if( tmp > retval ) 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( std::fclose( stdout ) != 0 ) if( std::fclose( stdout ) != 0 )
{ {
show_error( "Error closing stdout", errno ); show_error( "Error closing stdout", errno );
error = true; error = true;
} }
if( error && retval == 0 ) retval = 1; return error;
return retval;
} }
 End of changes. 27 change blocks. 
60 lines changed or deleted 57 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)