2025-02-24 05:01:08 +01:00
|
|
|
/* Zutils - Utilities dealing with compressed files
|
2025-02-24 05:40:39 +01:00
|
|
|
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
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 3 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
const char * const Program_name = "Zutils";
|
|
|
|
const char * const program_name = "zutils";
|
|
|
|
const char * const config_file_name = "zutilsrc";
|
|
|
|
const char * const program_year = "2013";
|
2025-02-24 05:01:08 +01:00
|
|
|
extern const char * invocation_name;
|
|
|
|
extern const char * util_name;
|
|
|
|
|
|
|
|
extern int verbosity;
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
enum { fmt_bz2, fmt_gz, fmt_lz, fmt_xz, num_formats };
|
|
|
|
const char * const format_names[num_formats] = { "bz2", "gz", "lz", "xz" };
|
|
|
|
const char * const simple_extensions[num_formats] =
|
|
|
|
{ ".bz2", ".gz", ".lz", ".xz" };
|
|
|
|
const int8_t format_order[num_formats] =
|
2025-02-24 05:42:28 +01:00
|
|
|
{ fmt_lz, fmt_bz2, fmt_gz, fmt_xz }; // search order
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
// first magic byte must be different among formats
|
|
|
|
enum { bzip2_magic_size = 3,
|
|
|
|
gzip_magic_size = 2,
|
|
|
|
lzip_magic_size = 4,
|
|
|
|
xz_magic_size = 5 };
|
|
|
|
const uint8_t bzip2_magic[bzip2_magic_size] =
|
|
|
|
{ 0x42, 0x5A, 0x68 }; // "BZh"
|
|
|
|
const uint8_t gzip_magic[gzip_magic_size] =
|
|
|
|
{ 0x1F, 0x8B };
|
|
|
|
const uint8_t lzip_magic[lzip_magic_size] =
|
|
|
|
{ 0x4C, 0x5A, 0x49, 0x50 }; // "LZIP"
|
|
|
|
const uint8_t xz_magic[xz_magic_size] =
|
|
|
|
{ 0xFD, 0x37, 0x7A, 0x58, 0x5A }; // 0xFD, "7zXZ"
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
int parse_format_type( const std::string & arg );
|
2025-02-24 05:40:39 +01:00
|
|
|
|
|
|
|
int readblock( const int fd, uint8_t * const buf, const int size );
|
|
|
|
int writeblock( const int fd, const uint8_t * const buf, const int size );
|
2025-02-24 05:01:08 +01:00
|
|
|
bool feed_data( const int infd, const int outfd,
|
|
|
|
const uint8_t * magic_data, const int magic_size );
|
2025-02-24 05:43:00 +01:00
|
|
|
|
|
|
|
struct Children
|
|
|
|
{
|
|
|
|
const char * compressor_name;
|
|
|
|
pid_t pid[2]; // data feeder, compressor
|
|
|
|
};
|
|
|
|
bool good_status( const Children & children, const bool finished );
|
|
|
|
bool set_data_feeder( int * const infdp, Children & children, int format_index );
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
void show_help_addr();
|
|
|
|
void show_version( const char * const Util_name = 0 );
|
2025-02-24 05:01:08 +01:00
|
|
|
void show_error( const char * const msg, const int errcode = 0,
|
2025-02-24 05:40:39 +01:00
|
|
|
const bool help = false );
|
|
|
|
void show_error2( const char * const msg, const char * const name );
|
|
|
|
void show_close_error( const char * const prog_name );
|
|
|
|
void show_exec_error( const char * const prog_name );
|
|
|
|
void show_fork_error( const char * const prog_name );
|
2025-02-24 05:01:08 +01:00
|
|
|
void internal_error( const char * const msg );
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
// Returns format index or -1 if uncompressed
|
|
|
|
//
|
|
|
|
int test_format( const int infd, const uint8_t ** const magic_datap,
|
|
|
|
int * const magic_sizep );
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
// Returns exit status of child process 'pid', or 'eretval' in case of error.
|
|
|
|
//
|
2025-02-24 05:04:54 +01:00
|
|
|
int wait_for_child( const pid_t pid, const char * const name,
|
2025-02-24 05:45:13 +01:00
|
|
|
const int eretval = 2, const bool isgzxz = false );
|
2025-02-24 05:40:39 +01:00
|
|
|
|
|
|
|
// Returns -1 if child not terminated, 'eretval' in case of error, or
|
|
|
|
// exit status of child process 'pid'.
|
|
|
|
//
|
|
|
|
int child_status( const pid_t pid, const char * const name,
|
|
|
|
const int eretval = 2 );
|