xgethostbyname: more readable
[oweals/busybox.git] / archival / cpio.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini cpio implementation for busybox
4  *
5  * Copyright (C) 2001 by Glenn McGrath
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  *
9  * Limitations:
10  *              Doesn't check CRC's
11  *              Only supports new ASCII and CRC formats
12  *
13  */
14 #include <fcntl.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include "unarchive.h"
19 #include "busybox.h"
20
21 #define CPIO_OPT_EXTRACT                        0x01
22 #define CPIO_OPT_TEST                           0x02
23 #define CPIO_OPT_UNCONDITIONAL          0x04
24 #define CPIO_OPT_VERBOSE                        0x08
25 #define CPIO_OPT_FILE                           0x10
26 #define CPIO_OPT_CREATE_LEADING_DIR     0x20
27 #define CPIO_OPT_PRESERVE_MTIME         0x40
28
29 int cpio_main(int argc, char **argv)
30 {
31         archive_handle_t *archive_handle;
32         char *cpio_filename = NULL;
33         unsigned opt;
34
35         /* Initialise */
36         archive_handle = init_handle();
37         archive_handle->src_fd = STDIN_FILENO;
38         archive_handle->seek = seek_by_read;
39         archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
40
41         opt = getopt32(argc, argv, "ituvF:dm", &cpio_filename);
42
43         /* One of either extract or test options must be given */
44         if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
45                 bb_show_usage();
46         }
47
48         if (opt & CPIO_OPT_TEST) {
49                 /* if both extract and test options are given, ignore extract option */
50                 if (opt & CPIO_OPT_EXTRACT) {
51                         opt &= ~CPIO_OPT_EXTRACT;
52                 }
53                 archive_handle->action_header = header_list;
54         }
55         if (opt & CPIO_OPT_EXTRACT) {
56                 archive_handle->action_data = data_extract_all;
57         }
58         if (opt & CPIO_OPT_UNCONDITIONAL) {
59                 archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
60                 archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
61         }
62         if (opt & CPIO_OPT_VERBOSE) {
63                 if (archive_handle->action_header == header_list) {
64                         archive_handle->action_header = header_verbose_list;
65                 } else {
66                         archive_handle->action_header = header_list;
67                 }
68         }
69         if (cpio_filename) { /* CPIO_OPT_FILE */
70                 archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
71                 archive_handle->seek = seek_by_jump;
72         }
73         if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
74                 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
75         }
76
77         while (optind < argc) {
78                 archive_handle->filter = filter_accept_list;
79                 llist_add_to(&(archive_handle->accept), argv[optind]);
80                 optind++;
81         }
82
83         while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
84
85         return(EXIT_SUCCESS);
86 }