Fixed time command segfault with no arguments
[busybox4maemo] / archival / dpkg_deb.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * dpkg-deb packs, unpacks and provides information about Debian archives.
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  */
7 #include "libbb.h"
8 #include "unarchive.h"
9
10 #define DPKG_DEB_OPT_CONTENTS   1
11 #define DPKG_DEB_OPT_CONTROL    2
12 #define DPKG_DEB_OPT_FIELD      4
13 #define DPKG_DEB_OPT_EXTRACT    8
14 #define DPKG_DEB_OPT_EXTRACT_VERBOSE    16
15
16 int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
17 int dpkg_deb_main(int argc, char **argv)
18 {
19         archive_handle_t *ar_archive;
20         archive_handle_t *tar_archive;
21         llist_t *control_tar_llist = NULL;
22         unsigned opt;
23         const char *extract_dir = NULL;
24         short argcount = 1;
25
26         /* Setup the tar archive handle */
27         tar_archive = init_handle();
28
29         /* Setup an ar archive handle that refers to the gzip sub archive */
30         ar_archive = init_handle();
31         ar_archive->sub_archive = tar_archive;
32         ar_archive->filter = filter_accept_list_reassign;
33
34 #if ENABLE_FEATURE_DEB_TAR_GZ
35         llist_add_to(&(ar_archive->accept), (char*)"data.tar.gz");
36         llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
37 #endif
38
39 #if ENABLE_FEATURE_DEB_TAR_BZ2
40         llist_add_to(&(ar_archive->accept), (char*)"data.tar.bz2");
41         llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
42 #endif
43
44         opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
45         opt = getopt32(argv, "cefXx");
46
47         if (opt & DPKG_DEB_OPT_CONTENTS) {
48                 tar_archive->action_header = header_verbose_list;
49         }
50         if (opt & DPKG_DEB_OPT_CONTROL) {
51                 ar_archive->accept = control_tar_llist;
52                 tar_archive->action_data = data_extract_all;
53                 if (optind + 1 == argc) {
54                         extract_dir = "./DEBIAN";
55                 } else {
56                         argcount++;
57                 }
58         }
59         if (opt & DPKG_DEB_OPT_FIELD) {
60                 /* Print the entire control file
61                  * it should accept a second argument which specifies a
62                  * specific field to print */
63                 ar_archive->accept = control_tar_llist;
64                 llist_add_to(&(tar_archive->accept), (char*)"./control");
65                 tar_archive->filter = filter_accept_list;
66                 tar_archive->action_data = data_extract_to_stdout;
67         }
68         if (opt & DPKG_DEB_OPT_EXTRACT) {
69                 tar_archive->action_header = header_list;
70         }
71         if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
72                 tar_archive->action_data = data_extract_all;
73                 argcount = 2;
74         }
75
76         if ((optind + argcount) != argc) {
77                 bb_show_usage();
78         }
79
80         tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
81
82         /* Workout where to extract the files */
83         /* 2nd argument is a dir name */
84         if (argv[optind]) {
85                 extract_dir = argv[optind];
86         }
87         if (extract_dir) {
88                 mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
89                 xchdir(extract_dir);
90         }
91         unpack_ar_archive(ar_archive);
92
93         /* Cleanup */
94         close(ar_archive->src_fd);
95
96         return EXIT_SUCCESS;
97 }