aboutsummaryrefslogtreecommitdiff
path: root/src/xzdec/xzdec.c
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2009-09-19 09:47:30 +0300
committerLasse Collin <lasse.collin@tukaani.org>2009-09-19 09:47:30 +0300
commite599bba4216c0edb8cc8f40adad3a6dba88685f4 (patch)
treed52c699f599e276ff65302e5854a26998da1a928 /src/xzdec/xzdec.c
parentFix incorrect use of "restrict". (diff)
downloadxz-e599bba4216c0edb8cc8f40adad3a6dba88685f4.tar.xz
Various changes.
Separate a few reusable components from XZ Utils specific code. The reusable code is now in "tuklib" modules. A few more could be separated still, e.g. bswap.h. Fix some bugs in lzmainfo. Fix physmem and cpucores code on OS/2. Thanks to Elbert Pol for help. Add OpenVMS support into physmem. Add a few #ifdefs to ease building XZ Utils on OpenVMS. Thanks to Jouk Jansen for the original patch.
Diffstat (limited to 'src/xzdec/xzdec.c')
-rw-r--r--src/xzdec/xzdec.c55
1 files changed, 15 insertions, 40 deletions
diff --git a/src/xzdec/xzdec.c b/src/xzdec/xzdec.c
index bf26bd67..18bdb04e 100644
--- a/src/xzdec/xzdec.c
+++ b/src/xzdec/xzdec.c
@@ -18,14 +18,16 @@
#include <stdio.h>
#include <unistd.h>
-#ifdef DOSLIKE
+#include "getopt.h"
+#include "tuklib_progname.h"
+#include "tuklib_exit.h"
+#include "tuklib_physmem.h"
+
+#ifdef TUKLIB_DOSLIKE
# include <fcntl.h>
# include <io.h>
#endif
-#include "getopt.h"
-#include "physmem.h"
-
#ifdef LZMADEC
# define TOOL_FORMAT "lzma"
@@ -41,9 +43,6 @@ static uint64_t memlimit;
/// --quiet has been given at least twice.
static unsigned int display_errors = 2;
-/// Program name to be shown in error messages
-static const char *argv0;
-
static void lzma_attribute((format(printf, 1, 2)))
my_errorf(const char *fmt, ...)
@@ -52,7 +51,7 @@ my_errorf(const char *fmt, ...)
va_start(ap, fmt);
if (display_errors) {
- fprintf(stderr, "%s: ", argv0);
+ fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
@@ -63,29 +62,6 @@ my_errorf(const char *fmt, ...)
static void lzma_attribute((noreturn))
-my_exit(void)
-{
- int status = EXIT_SUCCESS;
-
- // Close stdout. We don't care about stderr, because we write to it
- // only when an error has already occurred.
- const int ferror_err = ferror(stdout);
- const int fclose_err = fclose(stdout);
-
- if (ferror_err || fclose_err) {
- // If it was fclose() that failed, we have the reason
- // in errno. If only ferror() indicated an error,
- // we have no idea what the reason was.
- my_errorf("Writing to standard output failed: %s", fclose_err
- ? strerror(errno) : "Unknown error");
- status = EXIT_FAILURE;
- }
-
- exit(status);
-}
-
-
-static void lzma_attribute((noreturn))
help(void)
{
printf(
@@ -108,8 +84,8 @@ help(void)
"\n"
"Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n"
PACKAGE_NAME " home page: <" PACKAGE_HOMEPAGE ">\n",
- argv0, memlimit / (1024 * 1024));
- my_exit();
+ progname, memlimit / (1024 * 1024));
+ tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
}
@@ -119,7 +95,7 @@ version(void)
printf(TOOL_FORMAT "dec (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n"
"liblzma %s\n", lzma_version_string());
- my_exit();
+ tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
}
@@ -128,7 +104,7 @@ version(void)
static void
memlimit_set_percentage(uint32_t percentage)
{
- uint64_t mem = physmem();
+ uint64_t mem = tuklib_physmem();
// If we cannot determine the amount of RAM, assume 32 MiB.
if (mem == 0)
@@ -441,9 +417,8 @@ uncompress(lzma_stream *strm, FILE *file, const char *filename)
int
main(int argc, char **argv)
{
- // Set the argv0 global so that we can print the command name in
- // error and help messages.
- argv0 = argv[0];
+ // Initialize progname which we will be used in error messages.
+ tuklib_progname_init(argv);
// Set the default memory usage limit. This is needed before parsing
// the command line arguments.
@@ -458,7 +433,7 @@ main(int argc, char **argv)
lzma_stream strm = LZMA_STREAM_INIT;
// Some systems require setting stdin and stdout to binary mode.
-#ifdef DOSLIKE
+#ifdef TUKLIB_DOSLIKE
setmode(fileno(stdin), O_BINARY);
setmode(fileno(stdout), O_BINARY);
#endif
@@ -492,5 +467,5 @@ main(int argc, char **argv)
lzma_end(&strm);
#endif
- my_exit();
+ tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
}