Another update from Larry:
[oweals/busybox.git] / dpkg_deb.c
index 450b04b8ed943365bf0673f607c3d0c1d541d9cc..d088828857b86730f1ebf9233374300f93a9a8fc 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 *argument = NULL;
+       char *output_buffer = NULL;
        int opt = 0;
        int optflag = 0;        
        
-       while ((opt = getopt(argc, argv, "cexXl")) != -1) {
+       while ((opt = getopt(argc, argv, "ceftXxI")) != -1) {
                switch (opt) {
                        case 'c':
-                               optflag |= dpkg_deb_contents;
+                               optflag |= extract_contents;
                                break;
                        case 'e':
-                               optflag |= dpkg_deb_control;
+                               optflag |= extract_control;
+                               break;
+                       case 'f':
+                               optflag |= extract_field;
+                               break;
+                       case 't':
+                               optflag |= extract_fsys_tarfile;
                                break;
                        case 'X':
-                               optflag |= dpkg_deb_verbose_extract;
+                               optflag |= extract_verbose_extract;
                                break;
                        case 'x':
-                               optflag |= dpkg_deb_extract;
-                               break;
-                       case 'l':
-                               optflag |= dpkg_deb_list;
+                               optflag |= extract_extract;
                                break;
-/*                     case 'I':
-                               optflag |= dpkg_deb_info;
+                       case 'I':
+                               optflag |= extract_info;
                                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");
-               } else {
-                       target_dir = (char *) xmalloc(strlen(argv[optind + 1]));
-                       strcpy(target_dir, argv[optind + 1]);
-               }
+
+       switch (optflag) {
+               case (extract_control):
+               case (extract_extract):
+               case (extract_verbose_extract):
+                       /* argument is a dir name */
+                       if ( (optind + 1) == argc ) {
+                               argument = xstrdup("DEBIAN");
+                       } else {
+                               argument = xstrdup(argv[optind + 1]);
+                       }
+                       break;
+               case (extract_field):
+                       /* argument is a control field name */
+                       if ((optind + 1) != argc) {
+                               argument = xstrdup(argv[optind + 1]);                           
+                       }
+                       break;
+               case (extract_info):
+                       /* argument is a control field name */
+                       if ((optind + 1) != argc) {
+                               argument = xstrdup(argv[optind + 1]);
+                               break;
+                       } else {
+                               error_msg("-I currently requires a filename to be specifies");
+                               return(EXIT_FAILURE);
+                       }
+                       /* argument is a filename */
+               default:
        }
-       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], optflag, argument, NULL);
+
+       if (optflag & extract_field) {
+               char *field = NULL;
+               int field_length = 0;
+               int field_start = 0;
+
+               while ((field = read_package_field(&output_buffer[field_start])) != NULL) {
+                       field_length = strlen(field);
+                       field_start += (field_length + 1);
+                       if (strstr(field, argument) == field) {
+                               printf("%s\n", field + strlen(argument) + 2);
+                       }
+                       free(field);
+               }
        }
-*/
+
        return(EXIT_SUCCESS);
 }