Don't delete source file when decompressing to stdout
[oweals/busybox.git] / archival / ar.c
index fd98d86f547906d382d69dd66f2bd9c098ce8bcc..ac8c41f966ca61ee2164ce3c7cf725c1d1a0b47c 100644 (file)
  *
  */
 #include <fcntl.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
-#include <unistd.h>
+#include "unarchive.h"
 #include "busybox.h"
 
 extern int ar_main(int argc, char **argv)
@@ -34,7 +35,7 @@ extern int ar_main(int argc, char **argv)
        FILE *src_stream = NULL;
        char **extract_names = NULL;
        char ar_magic[8];
-       int extract_function = 0;
+       int extract_function =  extract_unconditional;
        int opt;
        int num_of_entries = 0;
        extern off_t archive_offset;
@@ -70,20 +71,20 @@ extern int ar_main(int argc, char **argv)
 
        /* check ar magic */
        fread(ar_magic, 1, 8, src_stream);
+       archive_offset = 8;
        if (strncmp(ar_magic,"!<arch>",7) != 0) {
                error_msg_and_die("invalid magic");
        }
-       archive_offset = 8;
 
-       extract_names = malloc(sizeof(char *));
-       extract_names[0] = NULL;
+       /* Create a list of files to extract */
        while (optind < argc) {
+               extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2));
+               extract_names[num_of_entries] = xstrdup(argv[optind]);
                num_of_entries++;
-               *extract_names = realloc(*extract_names, num_of_entries);
-               extract_names[num_of_entries - 1] = xstrdup(argv[optind]);
+               extract_names[num_of_entries] = NULL;
                optind++;
        }
 
-       unarchive(src_stream, &get_header_ar, extract_function, "./", extract_names);
+       unarchive(src_stream, stdout, &get_header_ar, extract_function, "./", extract_names, NULL);
        return EXIT_SUCCESS;
 }