#include <sys/types.h>
#include <error.h>
#include <errno.h>
+#include <getopt.h>
#include <lzma.h>
#define DEBUG 1
#if DEBUG
-#define debug(fs...) fprintf (stderr, "pxzcat: debug: " fs ##__VA_ARGS__)
+#define debug(fs,...) fprintf (stderr, "pxzcat: debug: " fs, ## __VA_ARGS__)
#else
-#define debug(fs...) /* nothing */
+#define debug(fs,...) /* nothing */
#endif
#define XZ_HEADER_MAGIC "\xfd" "7zXZ\0"
#define XZ_FOOTER_MAGIC "YZ"
#define XZ_FOOTER_MAGIC_LEN 2
+static void usage (int exitcode);
static void xzfile_uncompress (const char *filename, const char *outputfile);
static int check_header_magic (int fd);
-static lzma_index *parse_indexes (const char *filename, int fd, size_t *);
+static lzma_index *parse_indexes (const char *filename, int fd);
static void iter_indexes (lzma_index *idx);
static struct option long_options[] = {
{ "output", required_argument, 0, 'o' },
+ { "help", 0, 0, '?' },
{ NULL, 0, 0, 0 }
};
main (int argc, char *argv[])
{
int c;
- int optind;
+ int longopt_index;
const char *outputfile = NULL;
for (;;) {
- c = getopt_long (argc, argv, options, long_options, &optind);
+ c = getopt_long (argc, argv, options, long_options, &longopt_index);
if (c == -1)
break;
break;
case '?':
+ usage (EXIT_SUCCESS);
+
default:
- error (EXIT_FAILURE, 0, "usage: %s -o output file\n", argv[0]);
+ usage (EXIT_FAILURE);
}
}
if (outputfile == NULL)
- error (EXIT_FAILURE, 0, "%s: you must give the -o (output file) option\n",
- argv[0]);
+ error (EXIT_FAILURE, 0, "you must give the -o (output file) option\n");
if (optind != argc - 1)
- error (EXIT_FAILURE, 0, "%s: input.xz\n", argv[0]);
+ usage (EXIT_FAILURE);
xzfile_uncompress (argv[optind], outputfile);
}
static void
+usage (int exitcode)
+{
+ printf ("usage: pxzcat -o output input.xz\n");
+ exit (exitcode);
+}
+
+static void
xzfile_uncompress (const char *filename, const char *outputfile)
{
int fd;
lzma_index *idx;
/* Open the file. */
- fd = open (filename, O_RDONLY|O_CLOEXEC);
+ fd = open (filename, O_RDONLY);
if (fd == -1)
error (EXIT_FAILURE, errno, "open: %s", filename);
r = lzma_code (&strm, LZMA_RUN);
} while (r == LZMA_OK);
- if (r != LZMA_STREAM_END) {
+ if (r != LZMA_STREAM_END)
error (EXIT_FAILURE, 0, "%s: could not parse index (error %d)",
filename, r);
}
-
- return 0;
}
#if 0