Another update from Larry:
[oweals/busybox.git] / 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 *argument = NULL;
25         char *output_buffer = NULL;
26         int opt = 0;
27         int optflag = 0;        
28         
29         while ((opt = getopt(argc, argv, "ceftXxI")) != -1) {
30                 switch (opt) {
31                         case 'c':
32                                 optflag |= extract_contents;
33                                 break;
34                         case 'e':
35                                 optflag |= extract_control;
36                                 break;
37                         case 'f':
38                                 optflag |= extract_field;
39                                 break;
40                         case 't':
41                                 optflag |= extract_fsys_tarfile;
42                                 break;
43                         case 'X':
44                                 optflag |= extract_verbose_extract;
45                                 break;
46                         case 'x':
47                                 optflag |= extract_extract;
48                                 break;
49                         case 'I':
50                                 optflag |= extract_info;
51                                 break;
52                         default:
53                                 show_usage();
54                 }
55         }
56
57         if (optind == argc)  {
58                 show_usage();
59         }
60
61         switch (optflag) {
62                 case (extract_control):
63                 case (extract_extract):
64                 case (extract_verbose_extract):
65                         /* argument is a dir name */
66                         if ( (optind + 1) == argc ) {
67                                 argument = xstrdup("DEBIAN");
68                         } else {
69                                 argument = xstrdup(argv[optind + 1]);
70                         }
71                         break;
72                 case (extract_field):
73                         /* argument is a control field name */
74                         if ((optind + 1) != argc) {
75                                 argument = xstrdup(argv[optind + 1]);                           
76                         }
77                         break;
78                 case (extract_info):
79                         /* argument is a control field name */
80                         if ((optind + 1) != argc) {
81                                 argument = xstrdup(argv[optind + 1]);
82                                 break;
83                         } else {
84                                 error_msg("-I currently requires a filename to be specifies");
85                                 return(EXIT_FAILURE);
86                         }
87                         /* argument is a filename */
88                 default:
89         }
90
91         output_buffer = deb_extract(argv[optind], optflag, argument, NULL);
92
93         if (optflag & extract_field) {
94                 char *field = NULL;
95                 int field_length = 0;
96                 int field_start = 0;
97
98                 while ((field = read_package_field(&output_buffer[field_start])) != NULL) {
99                         field_length = strlen(field);
100                         field_start += (field_length + 1);
101                         if (strstr(field, argument) == field) {
102                                 printf("%s\n", field + strlen(argument) + 2);
103                         }
104                         free(field);
105                 }
106         }
107
108         return(EXIT_SUCCESS);
109 }