- patch from Denis Vlasenko to add and use bb_xopen3()
[oweals/busybox.git] / archival / cpio.c
index 111807c433c143caa1b6798701520d1179bbf2eb..26f845bc95ced9ee24ff5c64235d690077db3eb3 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * Mini cpio implementation for busybox
  *
- * Copyright (C) 2001 by Glenn McGrath 
+ * Copyright (C) 2001 by Glenn McGrath
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  */
 #include <fcntl.h>
-#include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include "unarchive.h"
 #include "busybox.h"
 
-extern int cpio_main(int argc, char **argv)
+#define CPIO_OPT_EXTRACT                       0x01
+#define CPIO_OPT_TEST                          0x02
+#define CPIO_OPT_UNCONDITIONAL         0x04
+#define CPIO_OPT_VERBOSE                       0x08
+#define CPIO_OPT_FILE                          0x10
+#define CPIO_OPT_CREATE_LEADING_DIR    0x20
+#define CPIO_OPT_PRESERVE_MTIME                0x40
+
+int cpio_main(int argc, char **argv)
 {
        archive_handle_t *archive_handle;
-       int opt;
+       char *cpio_filename = NULL;
+       unsigned long opt;
 
        /* Initialise */
        archive_handle = init_handle();
-       archive_handle->src_fd = fileno(stdin);
+       archive_handle->src_fd = STDIN_FILENO;
        archive_handle->seek = seek_by_char;
-       archive_handle->action_header = header_list;
+       archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
+
+       opt = bb_getopt_ulflags(argc, argv, "ituvF:dm", &cpio_filename);
+
+       /* One of either extract or test options must be given */
+       if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
+               bb_show_usage();
+       }
 
-       while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) {
-               switch (opt) {
-               case 'i': /* extract */
-                       archive_handle->action_data = data_extract_all;
-                       break;
-               case 'd': /* create _leading_ directories */
-                       archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
-                       break;
-               case 'm': /* preserve modification time */
-                       archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
-                       break;
-               case 'v': /* verbosly list files */
+       if (opt & CPIO_OPT_TEST) {
+               /* if both extract and test options are given, ignore extract option */
+               if (opt & CPIO_OPT_EXTRACT) {
+                       opt &= ~CPIO_OPT_EXTRACT;
+               }
+               archive_handle->action_header = header_list;
+       }
+       if (opt & CPIO_OPT_EXTRACT) {
+               archive_handle->action_data = data_extract_all;
+       }
+       if (opt & CPIO_OPT_UNCONDITIONAL) {
+               archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
+               archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
+       }
+       if (opt & CPIO_OPT_VERBOSE) {
+               if (archive_handle->action_header == header_list) {
                        archive_handle->action_header = header_verbose_list;
-                       break;
-               case 'u': /* unconditional */
-                       archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
-                       break;
-               case 't': /* list files */
+               } else {
                        archive_handle->action_header = header_list;
-                       break;
-               case 'F':
-                       archive_handle->src_fd = bb_xopen(optarg, O_RDONLY);
-                       archive_handle->seek = seek_by_jump;
-                       break;
-               default:
-                       bb_show_usage();
                }
        }
+       if (cpio_filename) { /* CPIO_OPT_FILE */
+               archive_handle->src_fd = bb_xopen(cpio_filename, O_RDONLY);
+               archive_handle->seek = seek_by_jump;
+       }
+       if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
+               archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
+       }
 
        while (optind < argc) {
                archive_handle->filter = filter_accept_list;
@@ -81,4 +96,3 @@ extern int cpio_main(int argc, char **argv)
 
        return(EXIT_SUCCESS);
 }
-