summaryrefslogtreecommitdiffstats
path: root/zutils.cc
diff options
context:
space:
mode:
Diffstat (limited to 'zutils.cc')
-rw-r--r--zutils.cc151
1 files changed, 88 insertions, 63 deletions
diff --git a/zutils.cc b/zutils.cc
index 1324f89..798da03 100644
--- a/zutils.cc
+++ b/zutils.cc
@@ -1,5 +1,5 @@
/* Zutils - Utilities dealing with compressed files
- Copyright (C) 2009, 2010, 2011 Antonio Diaz Diaz.
+ Copyright (C) 2009, 2010, 2011, 2012, 2013 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
@@ -35,10 +35,20 @@ const char * util_name = program_name;
int verbosity = 0;
+int get_format_type( const std::string & arg )
+ {
+ for( int i = 0; i < num_formats; ++i )
+ if( arg == format_names[i] )
+ return i;
+ show_error( "Bad argument for '--format' option." );
+ std::exit( 1 );
+ }
+
+
// Returns the number of bytes really read.
// If (returned value < size) and (errno == 0), means EOF was reached.
//
-int readblock( const int fd, uint8_t * const buf, const int size ) throw()
+int readblock( const int fd, uint8_t * const buf, const int size )
{
int rest = size;
errno = 0;
@@ -47,7 +57,7 @@ int readblock( const int fd, uint8_t * const buf, const int size ) throw()
errno = 0;
const int n = read( fd, buf + size - rest, rest );
if( n > 0 ) rest -= n;
- else if( n == 0 ) break;
+ else if( n == 0 ) break; // EOF
else if( errno != EINTR && errno != EAGAIN ) break;
}
return ( rest > 0 ) ? size - rest : size;
@@ -57,7 +67,7 @@ int readblock( const int fd, uint8_t * const buf, const int size ) throw()
// Returns the number of bytes really written.
// If (returned value < size), it is always an error.
//
-int writeblock( const int fd, const uint8_t * const buf, const int size ) throw()
+int writeblock( const int fd, const uint8_t * const buf, const int size )
{
int rest = size;
errno = 0;
@@ -66,7 +76,7 @@ int writeblock( const int fd, const uint8_t * const buf, const int size ) throw(
errno = 0;
const int n = write( fd, buf + size - rest, rest );
if( n > 0 ) rest -= n;
- else if( errno && errno != EINTR && errno != EAGAIN ) break;
+ else if( n < 0 && errno != EINTR && errno != EAGAIN ) break;
}
return ( rest > 0 ) ? size - rest : size;
}
@@ -75,7 +85,7 @@ int writeblock( const int fd, const uint8_t * const buf, const int size ) throw(
bool feed_data( const int infd, const int outfd,
const uint8_t * magic_data, const int magic_size )
{
- if( writeblock( outfd, magic_data, magic_size ) != magic_size )
+ if( magic_size && writeblock( outfd, magic_data, magic_size ) != magic_size )
{ show_error( "Write error", errno ); return false; }
enum { buffer_size = 4096 };
uint8_t buffer[buffer_size];
@@ -92,15 +102,16 @@ bool feed_data( const int infd, const int outfd,
}
-bool set_data_feeder( int * const infdp, pid_t * const pidp )
+bool set_data_feeder( int * const infdp, pid_t * const pidp,
+ const int format_type )
{
- std::string file_type;
- const uint8_t * magic_data;
- int magic_size;
- const bool compressed =
- test_format( *infdp, file_type, &magic_data, &magic_size );
+ const uint8_t * magic_data = 0;
+ int magic_size = 0;
+ const char * const decompressor_name = ( format_type >= 0 ) ?
+ decompressor_names[format_type] :
+ test_format( *infdp, &magic_data, &magic_size );
- if( compressed ) // compressed with `file_type'
+ if( decompressor_name ) // compressed
{
int fda[2]; // pipe from feeder
int fda2[2]; // pipe from decompressor
@@ -118,12 +129,13 @@ bool set_data_feeder( int * const infdp, pid_t * const pidp )
dup2( fda2[1], STDOUT_FILENO ) >= 0 &&
close( fda[0] ) == 0 && close( fda[1] ) == 0 &&
close( fda2[0] ) == 0 && close( fda2[1] ) == 0 )
- execlp( file_type.c_str(), file_type.c_str(), "-cdfq", (char *)0 );
- show_exec_error( file_type.c_str() );
+ execlp( decompressor_name, decompressor_name,
+ (verbosity >= 0) ? "-d" : "-dq", (char *)0 );
+ show_exec_error( decompressor_name );
_exit( 2 );
}
if( pid2 < 0 )
- { show_fork_error( file_type.c_str() ); _exit( 2 ); }
+ { show_fork_error( decompressor_name ); _exit( 2 ); }
if( close( fda[0] ) != 0 ||
close( fda2[0] ) != 0 || close( fda2[1] ) != 0 ||
@@ -131,7 +143,7 @@ bool set_data_feeder( int * const infdp, pid_t * const pidp )
_exit( 2 );
if( close( fda[1] ) != 0 )
{ show_close_error( "decompressor feeder" ); _exit( 2 ); }
- _exit( wait_for_child( pid2, file_type.c_str() ) );
+ _exit( wait_for_child( pid2, decompressor_name ) );
}
// parent
close( fda[0] ); close( fda[1] ); close( fda2[1] );
@@ -166,28 +178,27 @@ bool set_data_feeder( int * const infdp, pid_t * const pidp )
}
-void show_help_addr() throw()
+void show_help_addr()
{
- std::printf( "\nReport bugs to zutils-bug@nongnu.org\n" );
- std::printf( "Zutils home page: http://www.nongnu.org/zutils/zutils.html\n" );
+ std::printf( "\nReport bugs to zutils-bug@nongnu.org\n"
+ "Zutils home page: http://www.nongnu.org/zutils/zutils.html\n" );
}
-void show_version( const char * const Util_name ) throw()
+void show_version( const char * const Util_name )
{
if( !Util_name || !*Util_name )
std::printf( "%s %s\n", Program_name, PROGVERSION );
else
std::printf( "%s (%s) %s\n", Util_name, program_name, PROGVERSION );
std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
- std::printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" );
- std::printf( "This is free software: you are free to change and redistribute it.\n" );
- std::printf( "There is NO WARRANTY, to the extent permitted by law.\n" );
+ std::printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
+ "This is free software: you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n" );
}
-void show_error( const char * const msg, const int errcode,
- const bool help ) throw()
+void show_error( const char * const msg, const int errcode, const bool help )
{
if( verbosity >= 0 )
{
@@ -198,22 +209,22 @@ void show_error( const char * const msg, const int errcode,
std::fprintf( stderr, ": %s", std::strerror( errcode ) );
std::fprintf( stderr, "\n" );
}
- if( help && invocation_name && invocation_name[0] )
- std::fprintf( stderr, "Try `%s --help' for more information.\n",
+ if( help )
+ std::fprintf( stderr, "Try '%s --help' for more information.\n",
invocation_name );
}
}
-void show_error2( const char * const msg, const char * const name ) throw()
+void show_error2( const char * const msg, const char * const name )
{
if( verbosity >= 0 )
- std::fprintf( stderr, "%s: %s `%s': %s.\n",
+ std::fprintf( stderr, "%s: %s '%s': %s.\n",
util_name, msg, name, std::strerror( errno ) );
}
-void show_close_error( const char * const prog_name ) throw()
+void show_close_error( const char * const prog_name )
{
if( verbosity >= 0 )
std::fprintf( stderr, "%s: Can't close output of %s: %s.\n",
@@ -221,18 +232,18 @@ void show_close_error( const char * const prog_name ) throw()
}
-void show_exec_error( const char * const prog_name ) throw()
+void show_exec_error( const char * const prog_name )
{
if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Can't exec `%s': %s.\n",
+ std::fprintf( stderr, "%s: Can't exec '%s': %s.\n",
util_name, prog_name, std::strerror( errno ) );
}
-void show_fork_error( const char * const prog_name ) throw()
+void show_fork_error( const char * const prog_name )
{
if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Can't fork `%s': %s.\n",
+ std::fprintf( stderr, "%s: Can't fork '%s': %s.\n",
util_name, prog_name, std::strerror( errno ) );
}
@@ -245,21 +256,13 @@ void internal_error( const char * const msg )
}
-unsigned char xdigit( const int value ) throw()
- {
- if( value >= 0 && value <= 9 ) return '0' + value;
- if( value >= 10 && value <= 15 ) return 'A' + ( value - 10 );
- return 0;
- }
-
-
-bool test_format( const int infd, std::string & file_type,
- const uint8_t ** const magic_datap, int * const magic_sizep )
+const char * test_format( const int infd,
+ const uint8_t ** const magic_datap,
+ int * const magic_sizep )
{
enum { buf_size = 5 };
static uint8_t buf[buf_size];
int i = 0;
- file_type.clear();
if( readblock( infd, buf, 1 ) == 1 )
{
++i;
@@ -267,22 +270,22 @@ bool test_format( const int infd, std::string & file_type,
{
if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == bzip2_magic[1] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == bzip2_magic[2] )
- { file_type = "bzip2";
- *magic_datap = bzip2_magic; *magic_sizep = bzip2_magic_size; }
+ { *magic_datap = bzip2_magic; *magic_sizep = bzip2_magic_size;
+ return decompressor_names[fmt_bz2]; }
}
else if( buf[0] == gzip_magic[0] )
{
if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == gzip_magic[1] )
- { file_type = "gzip";
- *magic_datap = gzip_magic; *magic_sizep = gzip_magic_size; }
+ { *magic_datap = gzip_magic; *magic_sizep = gzip_magic_size;
+ return decompressor_names[fmt_gz]; }
}
else if( buf[0] == lzip_magic[0] )
{
if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[1] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[2] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[3] )
- { file_type = "lzip";
- *magic_datap = lzip_magic; *magic_sizep = lzip_magic_size; }
+ { *magic_datap = lzip_magic; *magic_sizep = lzip_magic_size;
+ return decompressor_names[fmt_lz]; }
}
else if( buf[0] == xz_magic[0] )
{
@@ -290,18 +293,12 @@ bool test_format( const int infd, std::string & file_type,
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[2] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[3] &&
readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[4] )
- { file_type = "xz";
- *magic_datap = xz_magic; *magic_sizep = xz_magic_size; }
+ { *magic_datap = xz_magic; *magic_sizep = xz_magic_size;
+ return decompressor_names[fmt_xz]; }
}
}
- if( file_type.size() ) return true;
- for( int j = 0; j < i; ++j )
- {
- file_type += xdigit( buf[j] >> 4 );
- file_type += xdigit( buf[j] & 0x0F );
- }
*magic_datap = buf; *magic_sizep = i;
- return false;
+ return 0;
}
@@ -314,10 +311,38 @@ int wait_for_child( const pid_t pid, const char * const name,
if( errno != EINTR )
{
if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Error waiting termination of `%s': %s.\n",
- util_name, name, std::strerror( errno ) );
+ std::fprintf( stderr, "%s: Error waiting termination of '%s': %s.\n",
+ util_name, name, std::strerror( errno ) );
+ _exit( eretval );
+ }
+ }
+
+ if( WIFEXITED( status ) )
+ {
+ const int tmp = WEXITSTATUS( status );
+ if( eretval == 1 && tmp == 1 ) return 2; // for ztest
+ return tmp;
+ }
+ return eretval;
+ }
+
+
+int child_status( const pid_t pid, const char * const name,
+ const int eretval )
+ {
+ int status;
+ while( true )
+ {
+ const int tmp = waitpid( pid, &status, WNOHANG );
+ if( tmp == -1 && errno != EINTR )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Error checking status of '%s': %s.\n",
+ util_name, name, std::strerror( errno ) );
_exit( eretval );
}
+ if( tmp == 0 ) return -1; // child not terminated
+ if( tmp == pid ) break; // child terminated
}
if( WIFEXITED( status ) ) return WEXITSTATUS( status );