enum entry for unarchive to be used by tar
[oweals/busybox.git] / ar.c
diff --git a/ar.c b/ar.c
index 7359f49105f35ddeee2056bde4e8f3269f81f2a2..7f3396c8cf9af0fc8f17096eb59191b1359622ed 100644 (file)
--- a/ar.c
+++ b/ar.c
@@ -24,6 +24,7 @@
  */
 #include <fcntl.h>
 #include <stdlib.h>
+#include <string.h>
 #include <getopt.h>
 #include <unistd.h>
 #include "busybox.h"
 extern int ar_main(int argc, char **argv)
 {
        FILE *src_stream = NULL;
-       int extract_function = 0, opt = 0;
-       file_headers_t *head;
-       file_headers_t *ar_extract_list = NULL;
+       char **extract_names = NULL;
+       char ar_magic[8];
+       int extract_function =  extract_unconditional;
+       int opt;
+       int num_of_entries = 0;
+       extern off_t archive_offset;
 
        while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
                switch (opt) {
@@ -63,27 +67,23 @@ extern int ar_main(int argc, char **argv)
        }
 
        src_stream = xfopen(argv[optind++], "r");
-       head = get_ar_headers(src_stream);
 
-       /* find files to extract or display */
-       /* search through argv and build extract list */
-       ar_extract_list = (file_headers_t *) xcalloc(1, sizeof(file_headers_t));
-       if (optind < argc) {
-               while (optind < argc) {
-                       ar_extract_list = add_from_archive_list(head, ar_extract_list, argv[optind]);
-                       optind++;
-               }
-       } else {
-               ar_extract_list = head;
+       /* 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");
        }
 
-       /* If there isnt even one possible entry then abort */
-       if (ar_extract_list->name == NULL) {
-               error_msg_and_die("No files to extract");
-       }       
-
-       fseek(src_stream, 0, SEEK_SET);
-       extract_archive(src_stream, stdout, ar_extract_list, extract_function, "./");
+       /* 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[num_of_entries] = NULL;
+               optind++;
+       }
 
+       unarchive(src_stream, stdout, &get_header_ar, extract_function, "./", extract_names);
        return EXIT_SUCCESS;
 }