Don't delete source file when decompressing to stdout
[oweals/busybox.git] / archival / cpio.c
index ecd6f534a7769c73ac4f671a6d5cb48b2f6f3049..baaa72ea18a237b3f7edc2d38e6a5acf372ce24a 100644 (file)
@@ -21,7 +21,6 @@
  * Limitations:
  *             Doesn't check CRC's
  *             Only supports new ASCII and CRC formats
- *             Doesnt support hard links
  *
  */
 #include <fcntl.h>
@@ -29,6 +28,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "unarchive.h"
 #include "busybox.h"
 
 extern int cpio_main(int argc, char **argv)
@@ -45,9 +45,9 @@ extern int cpio_main(int argc, char **argv)
                case 'i': // extract
                        extract_function |= extract_all_to_fs;
                        break;
-               case 'd': // create directories
-                       extract_function |= extract_create_dirs;
-                       oldmask = umask(077); /* Make create_path act like GNU cpio */
+               case 'd': // create _leading_ directories
+                       extract_function |= extract_create_leading_dirs;
+                       oldmask = umask(077); /* Make make_directory act like GNU cpio */
                        break;
                case 'm': // preserve modification time
                        extract_function |= extract_preserve_date;
@@ -69,25 +69,28 @@ extern int cpio_main(int argc, char **argv)
                }
        }
 
-       if (extract_function & extract_all_to_fs && extract_function & extract_list) {
+       if ((extract_function & extract_all_to_fs) && (extract_function & extract_list)) {
                extract_function ^= extract_all_to_fs; /* If specify t, don't extract*/
        }
 
-       if (extract_function & extract_all_to_fs && extract_function & extract_verbose_list) { /* The meaning of v changes on extract */
+       if ((extract_function & extract_all_to_fs) && (extract_function & extract_verbose_list)) {
+               /* The meaning of v changes on extract */
                extract_function ^= extract_verbose_list;
                extract_function |= extract_list;
        }
 
-       extract_names = malloc(4);
        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_cpio, extract_function, "./", extract_names);
-       if (oldmask) umask(oldmask); /* Restore umask if we changed it */
+       unarchive(src_stream, stdout, &get_header_cpio, extract_function, "./", extract_names, NULL);
+       if (oldmask) {
+               umask(oldmask); /* Restore umask if we changed it */
+       }
        return EXIT_SUCCESS;
 }