Implement suggestion from Adam Slattery, (don't default to killing closing bug #1190.
[oweals/busybox.git] / dpkg_deb.c
index 450b04b8ed943365bf0673f607c3d0c1d541d9cc..7f4dcbf01b1250183c52fc1c857b721e105258af 100644 (file)
  */
 
 #include <stdlib.h>
+#include <string.h>
 #include <getopt.h>
 #include "busybox.h"
 
 extern int dpkg_deb_main(int argc, char **argv)
 {
-       const int dpkg_deb_contents = 1;
-       const int dpkg_deb_control = 2;
-//     const int dpkg_deb_info = 4;
-       const int dpkg_deb_extract = 8;
-       const int dpkg_deb_verbose_extract = 16;
-       const int dpkg_deb_list = 32;
-       char *target_dir = NULL;
+       char *prefix = NULL;
+       char *filename = NULL;
+       char *output_buffer = NULL;
        int opt = 0;
-       int optflag = 0;        
+       int arg_type = 0;
+       int deb_extract_funct = extract_create_leading_dirs | extract_unconditional;    
        
-       while ((opt = getopt(argc, argv, "cexXl")) != -1) {
+       const int arg_type_prefix = 1;
+       const int arg_type_field = 2;
+       const int arg_type_filename = 4;
+//     const int arg_type_un_ar_gz = 8;
+
+       while ((opt = getopt(argc, argv, "ceftXxI")) != -1) {
                switch (opt) {
                        case 'c':
-                               optflag |= dpkg_deb_contents;
+                               deb_extract_funct |= extract_data_tar_gz;
+                               deb_extract_funct |= extract_verbose_list;
                                break;
                        case 'e':
-                               optflag |= dpkg_deb_control;
+                               arg_type = arg_type_prefix;
+                               deb_extract_funct |= extract_control_tar_gz;
+                               deb_extract_funct |= extract_all_to_fs;
                                break;
-                       case 'X':
-                               optflag |= dpkg_deb_verbose_extract;
+                       case 'f':
+                               arg_type = arg_type_field;
+                               deb_extract_funct |= extract_control_tar_gz;
+                               deb_extract_funct |= extract_one_to_buffer;
+                               filename = xstrdup("./control");
                                break;
-                       case 'x':
-                               optflag |= dpkg_deb_extract;
+                       case 't': /* --fsys-tarfile, i just made up this short name */
+                               /* Integrate the functionality needed with some code from ar.c */
+                               error_msg_and_die("Option disabled");
+//                             arg_type = arg_type_un_ar_gz;
                                break;
-                       case 'l':
-                               optflag |= dpkg_deb_list;
+                       case 'X':
+                               arg_type = arg_type_prefix;
+                               deb_extract_funct |= extract_data_tar_gz;
+                               deb_extract_funct |= extract_all_to_fs;
+                               deb_extract_funct |= extract_list;
+                       case 'x':
+                               arg_type = arg_type_prefix;
+                               deb_extract_funct |= extract_data_tar_gz;
+                               deb_extract_funct |= extract_all_to_fs;
                                break;
-/*                     case 'I':
-                               optflag |= dpkg_deb_info;
+                       case 'I':
+                               arg_type = arg_type_filename;
+                               deb_extract_funct |= extract_control_tar_gz;
+                               deb_extract_funct |= extract_one_to_buffer;
                                break;
-*/
                        default:
                                show_usage();
                }
        }
 
-       if (((optind + 1 ) > argc) || (optflag == 0))  {
+       if (optind == argc)  {
                show_usage();
        }
-       if ((optflag & dpkg_deb_control) || (optflag & dpkg_deb_extract) || (optflag & dpkg_deb_verbose_extract)) {
-               if ( (optind + 1) == argc ) {
-                       target_dir = (char *) xmalloc(7);
-                       strcpy(target_dir, "DEBIAN");
+
+       /* Workout where to extract the files */
+       if (arg_type == arg_type_prefix) {
+               /* argument is a dir name */
+               if ((optind + 1) == argc ) {
+                       prefix = xstrdup("./DEBIAN/");
+               } else {
+                       prefix = (char *) xmalloc(strlen(argv[optind + 1]) + 2);
+                       strcpy(prefix, argv[optind + 1]);
+                       /* Make sure the directory has a trailing '/' */
+                       if (last_char_is(prefix, '/') == NULL) {
+                               strcat(prefix, "/");
+                       }
+               }
+               mkdir(prefix, 0777);
+       }
+
+       if (arg_type == arg_type_filename) {
+               if ((optind + 1) != argc) {
+                       filename = xstrdup(argv[optind + 1]);
                } else {
-                       target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
-                       strcpy(target_dir, argv[optind + 1]);
+                       error_msg_and_die("-I currently requires a filename to be specified");
                }
        }
-       deb_extract(optflag, target_dir, argv[optind]);
-/*     else if (optflag & dpkg_deb_info) {
-               extract_flag = TRUE;
-               extract_to_stdout = TRUE;
-               strcpy(ar_filename, "control.tar.gz");
-               extract_list = argv+optind+1;
-               printf("list one is [%s]\n",extract_list[0]);
+
+       output_buffer = deb_extract(argv[optind], stdout, deb_extract_funct, prefix, filename);
+
+       if ((arg_type == arg_type_filename) && (output_buffer != NULL)) {
+               puts(output_buffer);
+       }
+       else if (arg_type == arg_type_field) {
+               char *field = NULL;
+               int field_start = 0;
+
+               while ((field = read_package_field(&output_buffer[field_start])) != NULL) {
+                       field_start += (strlen(field) + 1);
+                       if (strstr(field, argv[optind + 1]) == field) {
+                               puts(field + strlen(argv[optind + 1]) + 2);
+                       }
+                       free(field);
+               }
        }
-*/
+
        return(EXIT_SUCCESS);
 }