Fixes with help from Stefan, Typo in prerm script, and when reinstall a package the...
[oweals/busybox.git] / archival / dpkg_deb.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU Library General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <getopt.h>
20 #include "busybox.h"
21
22 extern int dpkg_deb_main(int argc, char **argv)
23 {
24         char *prefix = NULL;
25         char *filename = NULL;
26         char *output_buffer = NULL;
27         int opt = 0;
28         int arg_type = 0;
29         int deb_extract_funct = extract_create_leading_dirs | extract_unconditional;    
30         
31         const int arg_type_prefix = 1;
32         const int arg_type_field = 2;
33         const int arg_type_filename = 4;
34 //      const int arg_type_un_ar_gz = 8;
35
36         while ((opt = getopt(argc, argv, "ceftXxI")) != -1) {
37                 switch (opt) {
38                         case 'c':
39                                 deb_extract_funct |= extract_data_tar_gz;
40                                 deb_extract_funct |= extract_verbose_list;
41                                 break;
42                         case 'e':
43                                 arg_type = arg_type_prefix;
44                                 deb_extract_funct |= extract_control_tar_gz;
45                                 deb_extract_funct |= extract_all_to_fs;
46                                 break;
47                         case 'f':
48                                 arg_type = arg_type_field;
49                                 deb_extract_funct |= extract_control_tar_gz;
50                                 deb_extract_funct |= extract_one_to_buffer;
51                                 filename = xstrdup("./control");
52                                 break;
53                         case 't': /* --fsys-tarfile, i just made up this short name */
54                                 /* Integrate the functionality needed with some code from ar.c */
55                                 error_msg_and_die("Option disabled");
56 //                              arg_type = arg_type_un_ar_gz;
57                                 break;
58                         case 'X':
59                                 arg_type = arg_type_prefix;
60                                 deb_extract_funct |= extract_data_tar_gz;
61                                 deb_extract_funct |= extract_all_to_fs;
62                                 deb_extract_funct |= extract_list;
63                         case 'x':
64                                 arg_type = arg_type_prefix;
65                                 deb_extract_funct |= extract_data_tar_gz;
66                                 deb_extract_funct |= extract_all_to_fs;
67                                 break;
68                         case 'I':
69                                 arg_type = arg_type_filename;
70                                 deb_extract_funct |= extract_control_tar_gz;
71                                 deb_extract_funct |= extract_one_to_buffer;
72                                 break;
73                         default:
74                                 show_usage();
75                 }
76         }
77
78         if (optind == argc)  {
79                 show_usage();
80         }
81
82         /* Workout where to extract the files */
83         if (arg_type == arg_type_prefix) {
84                 /* argument is a dir name */
85                 if ((optind + 1) == argc ) {
86                         prefix = xstrdup("./DEBIAN/");
87                 } else {
88                         prefix = (char *) xmalloc(strlen(argv[optind + 1]) + 2);
89                         strcpy(prefix, argv[optind + 1]);
90                         /* Make sure the directory has a trailing '/' */
91                         if (last_char_is(prefix, '/') == NULL) {
92                                 strcat(prefix, "/");
93                         }
94                 }
95                 mkdir(prefix, 0777);
96         }
97
98         if (arg_type == arg_type_filename) {
99                 if ((optind + 1) != argc) {
100                         filename = xstrdup(argv[optind + 1]);
101                 } else {
102                         error_msg_and_die("-I currently requires a filename to be specified");
103                 }
104         }
105
106         output_buffer = deb_extract(argv[optind], stdout, deb_extract_funct, prefix, filename);
107
108         if ((arg_type == arg_type_filename) && (output_buffer != NULL)) {
109                 puts(output_buffer);
110         }
111         else if (arg_type == arg_type_field) {
112                 char *field = NULL;
113                 char *name;
114                 char *value;
115                 int field_start = 0;
116
117                 while (1) {
118                         field_start += read_package_field(&output_buffer[field_start], &name, &value);
119                         if (name == NULL) {
120                                 break;
121                         }
122                         if (strcmp(name, argv[optind + 1]) == 0) {
123                                 puts(value);
124                         }
125                         free(field);
126                 }
127         }
128
129         return(EXIT_SUCCESS);
130 }