diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-08 04:30:18 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-08 04:30:18 +0000 |
commit | e2010b31ef4137540c264f2fb69f8f2660a6f7fc (patch) | |
tree | f6dc13301edd0ff87720cbb52123d6b0832b293b /zcat.cc | |
parent | Adding upstream version 1.2~pre2. (diff) | |
download | zutils-e2010b31ef4137540c264f2fb69f8f2660a6f7fc.tar.xz zutils-e2010b31ef4137540c264f2fb69f8f2660a6f7fc.zip |
Adding upstream version 1.2~pre3.upstream/1.2_pre3
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r-- | zcat.cc | 198 |
1 files changed, 196 insertions, 2 deletions
@@ -15,6 +15,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _FILE_OFFSET_BITS 64 + +#include <cerrno> +#include <climits> +#include <csignal> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <list> +#include <string> +#include <vector> +#include <dirent.h> +#include <fcntl.h> +#include <stdint.h> +#include <unistd.h> +#include <sys/stat.h> +#if defined(__MSVCRT__) || defined(__OS2__) +#include <io.h> +#endif + +#include "arg_parser.h" +#include "rc.h" +#include "zutils.h" + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + + +namespace { + +#include "recursive.cc" + struct Cat_options { int number_lines; // 0 = no, 1 = nonblank, 2 = all @@ -58,7 +91,7 @@ public: Line_number line_number; -void show_zcat_help() +void show_help() { std::printf( "Zcat copies each given file (\"-\" means standard input), to standard\n" "output. If any given file is compressed, its decompressed content is\n" @@ -96,6 +129,41 @@ void show_zcat_help() } +int simple_extension_index( const std::string & name ) + { + for( int i = 0; i < num_formats; ++i ) + { + const std::string ext( simple_extensions[i] ); + if( name.size() > ext.size() && + name.compare( name.size() - ext.size(), ext.size(), ext ) == 0 ) + return i; + } + return -1; + } + + +int open_instream( std::string & input_filename, const bool search ) + { + int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY ); + if( infd < 0 ) + { + if( search && simple_extension_index( input_filename ) < 0 ) + { + for( int i = 0; i < num_formats; ++i ) + { + const std::string name( input_filename + + simple_extensions[format_order[i]] ); + infd = open( name.c_str(), O_RDONLY | O_BINARY ); + if( infd >= 0 ) { input_filename = name; break; } + } + } + if( infd < 0 ) + show_error2( "Can't open input file", input_filename.c_str() ); + } + return infd; + } + + int do_cat( const int infd, const int buffer_size, uint8_t * const inbuf, uint8_t * const outbuf, const std::string & input_filename, @@ -210,7 +278,133 @@ int cat( int infd, const int format_index, const std::string & input_filename, if( !good_status( children, retval == 0 ) ) retval = 1; if( retval == 0 && close( infd ) != 0 ) - { show_close_error( "data feeder" ); retval = 1; } + { show_close_error(); retval = 1; } delete[] outbuf; delete[] inbuf; return retval; } + +} // end namespace + + +int main( const int argc, const char * const argv[] ) + { + enum { format_opt = 256, verbose_opt, bz2_opt, gz_opt, lz_opt, xz_opt }; + int infd = -1; + int format_index = -1; + bool recursive = false; + std::string input_filename; + std::list< std::string > filenames; + Cat_options cat_options; + invocation_name = argv[0]; + program_name = "zcat"; + + const Arg_parser::Option options[] = + { + { 'A', "show-all", Arg_parser::no }, // cat + { 'b', "number-nonblank", Arg_parser::no }, // cat + { 'c', "stdout", Arg_parser::no }, // gzip + { 'd', "decompress", Arg_parser::no }, // gzip + { 'e', 0, Arg_parser::no }, // cat + { 'E', "show-ends", Arg_parser::no }, // cat + { 'f', "force", Arg_parser::no }, // gzip + { 'h', "help", Arg_parser::no }, + { 'l', "list", Arg_parser::no }, // gzip + { 'L', "license", Arg_parser::no }, // gzip + { 'n', "number", Arg_parser::no }, // cat + { 'N', "no-rcfile", Arg_parser::no }, + { 'q', "quiet", Arg_parser::no }, + { 'r', "recursive", Arg_parser::no }, + { 's', "squeeze-blank", Arg_parser::no }, // cat + { 't', 0, Arg_parser::no }, // cat + { 'T', "show-tabs", Arg_parser::no }, // cat + { 'v', "show-nonprinting", Arg_parser::no }, // cat + { 'V', "version", Arg_parser::no }, + { format_opt, "format", Arg_parser::yes }, + { verbose_opt, "verbose", Arg_parser::no }, + { bz2_opt, "bz2", Arg_parser::yes }, + { gz_opt, "gz", Arg_parser::yes }, + { lz_opt, "lz", Arg_parser::yes }, + { xz_opt, "xz", Arg_parser::yes }, + { 0 , 0, Arg_parser::no } }; + + const Arg_parser parser( argc, argv, options ); + if( parser.error().size() ) // bad option + { show_error( parser.error().c_str(), 0, true ); return 1; } + + maybe_process_config_file( parser ); + + int argind = 0; + for( ; argind < parser.arguments(); ++argind ) + { + const int code = parser.code( argind ); + if( !code ) break; // no more options + const char * const arg = parser.argument( argind ).c_str(); + switch( code ) + { + case 'A': cat_options.show_ends = true; + cat_options.show_nonprinting = true; + cat_options.show_tabs = true; break; + case 'b': cat_options.number_lines = 1; break; + case 'c': break; + case 'd': break; + case 'e': cat_options.show_nonprinting = true; // fall through + case 'E': cat_options.show_ends = true; break; + case 'f': break; + case 'h': show_help(); return 0; + case 'l': break; + case 'L': break; + case 'n': if( cat_options.number_lines == 0 ) + { cat_options.number_lines = 2; } break; + case 'N': break; + case 'q': verbosity = -1; break; + case 'r': recursive = true; break; + case 's': cat_options.squeeze_blank = true; break; + case 't': cat_options.show_nonprinting = true; // fall through + case 'T': cat_options.show_tabs = true; break; + case 'v': cat_options.show_nonprinting = true; break; + case 'V': show_version( "Zcat" ); return 0; + case format_opt: format_index = parse_format_type( arg ); break; + case verbose_opt: if( verbosity < 4 ) ++verbosity; break; + case bz2_opt: parse_compressor( arg, fmt_bz2, 1 ); break; + case gz_opt: parse_compressor( arg, fmt_gz, 1 ); break; + case lz_opt: parse_compressor( arg, fmt_lz, 1 ); break; + case xz_opt: parse_compressor( arg, fmt_xz, 1 ); break; + default : internal_error( "uncaught option" ); + } + } // end process options + +#if defined(__MSVCRT__) || defined(__OS2__) + setmode( STDIN_FILENO, O_BINARY ); + setmode( STDOUT_FILENO, O_BINARY ); +#endif + + for( ; argind < parser.arguments(); ++argind ) + filenames.push_back( parser.argument( argind ) ); + + if( filenames.empty() ) filenames.push_back("-"); + + int retval = 0; + bool error = false; + while( next_filename( filenames, input_filename, error, recursive ) ) + { + if( input_filename.empty() ) infd = STDIN_FILENO; + else + { + infd = open_instream( input_filename, format_index < 0 ); + if( infd < 0 ) { error = true; continue; } + } + + const int tmp = cat( infd, format_index, input_filename, cat_options ); + if( tmp > retval ) retval = tmp; + + if( input_filename.size() ) { close( infd ); infd = -1; } + } + + if( std::fclose( stdout ) != 0 ) + { + show_error( "Can't close stdout", errno ); + error = true; + } + if( error && retval == 0 ) retval = 1; + return retval; + } |