Dont unlink when testing !
[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  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Limitations:
22  *              Doesn't check CRC's
23  *              Only supports new ASCII and CRC formats
24  *
25  */
26 #include <fcntl.h>
27 #include <getopt.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include "unarchive.h"
32 #include "busybox.h"
33
34 extern int cpio_main(int argc, char **argv)
35 {
36         archive_handle_t *archive_handle;
37         int opt;
38
39         /* Initialise */
40         archive_handle = init_handle();
41         archive_handle->src_fd = fileno(stdin);
42         archive_handle->seek = seek_by_char;
43         archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
44         
45         while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) {
46                 switch (opt) {
47                 case 'i': /* extract */
48                         archive_handle->action_data = data_extract_all;
49                         break;
50                 case 'd': /* create _leading_ directories */
51                         archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
52                         break;
53 #if 0
54                 case 'm': /* preserve modification time */
55                         archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
56                         break;
57 #endif
58                 case 'v': /* verbosly list files */
59                         if (archive_handle->action_header == header_list) {
60                                 archive_handle->action_header = header_verbose_list;
61                         } else {
62                                 archive_handle->action_header = header_list;
63                         }
64                         break;
65                 case 'u': /* unconditional */
66                         archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
67                         archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
68                         break;
69                 case 't': /* list files */
70                         if (archive_handle->action_header == header_list) {
71                                 archive_handle->action_header = header_verbose_list;
72                         } else {
73                                 archive_handle->action_header = header_list;
74                         }
75                         break;
76                 case 'F':
77                         archive_handle->src_fd = bb_xopen(optarg, O_RDONLY);
78                         archive_handle->seek = seek_by_jump;
79                         break;
80                 default:
81                         bb_show_usage();
82                 }
83         }
84
85         while (optind < argc) {
86                 archive_handle->filter = filter_accept_list;
87                 archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
88                 optind++;
89         }
90
91         while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
92
93         return(EXIT_SUCCESS);
94 }