Patch from David Meggy to make the swap default to the new version if no
[oweals/busybox.git] / archival / cpio.c
index baaa72ea18a237b3f7edc2d38e6a5acf372ce24a..beee83d310b5b52b387c0befbe6f9501bf6fcd7b 100644 (file)
 
 extern int cpio_main(int argc, char **argv)
 {
-       FILE *src_stream = stdin;
-       char **extract_names = NULL;
-       int extract_function = 0;
-       int num_of_entries = 0;
-       int opt = 0;
-       mode_t oldmask = 0;
+       archive_handle_t *archive_handle;
+       int opt;
 
+       /* Initialise */
+       archive_handle = init_handle();
+       archive_handle->src_fd = fileno(stdin);
+       archive_handle->seek = seek_by_char;
+       archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
+       
        while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) {
                switch (opt) {
-               case 'i': // extract
-                       extract_function |= extract_all_to_fs;
+               case 'i': /* extract */
+                       archive_handle->action_data = data_extract_all;
                        break;
-               case 'd': // create _leading_ directories
-                       extract_function |= extract_create_leading_dirs;
-                       oldmask = umask(077); /* Make make_directory act like GNU cpio */
+               case 'd': /* create _leading_ directories */
+                       archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
                        break;
-               case 'm': // preserve modification time
-                       extract_function |= extract_preserve_date;
+#if 0
+               case 'm': /* preserve modification time */
+                       archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
                        break;
-               case 'v': // verbosly list files
-                       extract_function |= extract_verbose_list;
+#endif
+               case 'v': /* verbosly list files */
+                       if (archive_handle->action_header == header_list) {
+                               archive_handle->action_header = header_verbose_list;
+                       } else {
+                               archive_handle->action_header = header_list;
+                       }
                        break;
-               case 'u': // unconditional
-                       extract_function |= extract_unconditional;
+               case 'u': /* unconditional */
+                       archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
+                       archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
                        break;
-               case 't': // list files
-                       extract_function |= extract_list;
+               case 't': /* list files */
+                       if (archive_handle->action_header == header_list) {
+                               archive_handle->action_header = header_verbose_list;
+                       } else {
+                               archive_handle->action_header = header_list;
+                       }
                        break;
                case 'F':
-                       src_stream = xfopen(optarg, "r");
+                       archive_handle->src_fd = bb_xopen(optarg, O_RDONLY);
+                       archive_handle->seek = seek_by_jump;
                        break;
                default:
-                       show_usage();
+                       bb_show_usage();
                }
        }
 
-       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 */
-               extract_function ^= extract_verbose_list;
-               extract_function |= extract_list;
-       }
-
        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;
+               archive_handle->filter = filter_accept_list;
+               archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
                optind++;
        }
 
-       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;
-}
+       while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
 
+       return(EXIT_SUCCESS);
+}