Reorganise archive extraction code
diff --git a/dpkg_deb.c b/dpkg_deb.c
index 17b5476..400f238 100644
--- a/dpkg_deb.c
+++ b/dpkg_deb.c
@@ -21,33 +21,54 @@
extern int dpkg_deb_main(int argc, char **argv)
{
- char *argument = 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 = 0;
+ 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 |= extract_contents;
+ deb_extract_funct |= extract_data_tar_gz;
+ deb_extract_funct |= extract_verbose_list;
break;
case 'e':
- optflag |= extract_control;
+ arg_type = arg_type_prefix;
+ deb_extract_funct |= extract_control_tar_gz;
+ deb_extract_funct |= extract_all_to_fs;
break;
case 'f':
- optflag |= extract_field;
+ arg_type = arg_type_field;
+ deb_extract_funct |= extract_control_tar_gz;
+ deb_extract_funct |= extract_one_to_buffer;
+ filename = xstrdup("./control");
break;
- case 't':
- optflag |= extract_fsys_tarfile;
+ 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 'X':
- optflag |= extract_verbose_extract;
- break;
+ 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':
- optflag |= extract_extract;
+ arg_type = arg_type_prefix;
+ deb_extract_funct |= extract_data_tar_gz;
+ deb_extract_funct |= extract_all_to_fs;
break;
case 'I':
- optflag |= extract_info;
+ arg_type = arg_type_filename;
+ deb_extract_funct |= extract_control_tar_gz;
+ deb_extract_funct |= extract_one_to_buffer;
break;
default:
show_usage();
@@ -58,48 +79,42 @@
show_usage();
}
- 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]);
+ /* 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, "/");
}
- 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:
+ }
}
- output_buffer = deb_extract(argv[optind], optflag, argument, NULL);
+ if (arg_type == arg_type_filename) {
+ if ((optind + 1) != argc) {
+ filename = xstrdup(argv[optind + 1]);
+ } else {
+ error_msg_and_die("-I currently requires a filename to be specified");
+ }
+ }
- if (optflag & extract_field) {
+ 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_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) {
- puts(field + strlen(argument) + 2);
+ field_start += (strlen(field) + 1);
+ if (strstr(field, argv[optind + 1]) == field) {
+ puts(field + strlen(argv[optind + 1]) + 2);
}
free(field);
}