summaryrefslogtreecommitdiffstats
path: root/rc.cc
diff options
context:
space:
mode:
Diffstat (limited to 'rc.cc')
-rw-r--r--rc.cc112
1 files changed, 110 insertions, 2 deletions
diff --git a/rc.cc b/rc.cc
index dac205c..3d991fc 100644
--- a/rc.cc
+++ b/rc.cc
@@ -17,19 +17,28 @@
#define _FILE_OFFSET_BITS 64
+#include <cerrno>
#include <cstdio>
#include <cstdlib>
+#include <cstring>
#include <string>
#include <vector>
-#include <stdint.h>
+#include <unistd.h>
+#include <sys/wait.h>
#include "arg_parser.h"
-#include "zutils.h"
#include "rc.h"
+const char * invocation_name = 0;
+const char * program_name = 0;
+int verbosity = 0;
+
namespace {
+const char * const config_file_name = "zutilsrc";
+const char * const program_year = "2013";
+
std::string compressor_names[num_formats] =
{ "bzip2", "gzip", "lzip", "xz" }; // default compressor names
@@ -227,3 +236,102 @@ const std::vector< std::string > & get_compressor_args( const int format_index )
{
return compressor_args[format_index];
}
+
+
+void show_help_addr()
+ {
+ 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 Program_name )
+ {
+ std::printf( "%s (zutils) %s\n", 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"
+ "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 )
+ {
+ if( verbosity >= 0 )
+ {
+ if( msg && msg[0] )
+ {
+ std::fprintf( stderr, "%s: %s", program_name, msg );
+ if( errcode > 0 )
+ std::fprintf( stderr, ": %s", std::strerror( errcode ) );
+ std::fprintf( stderr, "\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 )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: %s '%s': %s.\n",
+ program_name, msg, name, std::strerror( errno ) );
+ }
+
+
+void internal_error( const char * const msg )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: internal error: %s.\n", program_name, msg );
+ std::exit( 3 );
+ }
+
+
+void show_close_error( const char * const prog_name )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Can't close output of %s: %s.\n",
+ program_name, prog_name, std::strerror( errno ) );
+ }
+
+
+void show_exec_error( const char * const prog_name )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Can't exec '%s': %s.\n",
+ program_name, prog_name, std::strerror( errno ) );
+ }
+
+
+void show_fork_error( const char * const prog_name )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Can't fork '%s': %s.\n",
+ program_name, prog_name, std::strerror( errno ) );
+ }
+
+
+int wait_for_child( const pid_t pid, const char * const name,
+ const int eretval, const bool isgzxz )
+ {
+ int status;
+ while( waitpid( pid, &status, 0 ) == -1 )
+ {
+ if( errno != EINTR )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Error waiting termination of '%s': %s.\n",
+ program_name, name, std::strerror( errno ) );
+ _exit( eretval );
+ }
+ }
+ if( WIFEXITED( status ) )
+ {
+ const int tmp = WEXITSTATUS( status );
+ if( isgzxz && eretval == 1 && tmp == 1 ) return 2; // for ztest
+ return tmp;
+ }
+ return eretval;
+ }