Various fixes.
authorRichard W.M. Jones <rjones@redhat.com>
Mon, 21 Oct 2013 12:03:05 +0000 (13:03 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Mon, 21 Oct 2013 12:03:05 +0000 (13:03 +0100)
pxzcat.c

index 5348be1..0ac0eb9 100644 (file)
--- a/pxzcat.c
+++ b/pxzcat.c
 #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   }
 };
 
@@ -75,11 +78,11 @@ int
 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;
 
@@ -93,17 +96,18 @@ main (int argc, char *argv[])
       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);
 
@@ -111,6 +115,13 @@ main (int argc, char *argv[])
 }
 
 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;
@@ -118,7 +129,7 @@ xzfile_uncompress (const char *filename, const char *outputfile)
   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);
 
@@ -246,7 +257,7 @@ parse_indexes (const char *filename, int fd)
       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);
 
@@ -319,8 +330,6 @@ iter_indexes (lzma_index *idx)
 
 
   }
-
-  return 0;
 }
 
 #if 0