Update the lash shell (hopefully the last time...) so things like
[oweals/busybox.git] / dpkg.c
diff --git a/dpkg.c b/dpkg.c
index 24b02041a950a66dd275a4cd7f068471f3691eef..4e53652e4fbdf446329e3dbdc795a4a3c775bb35 100644 (file)
--- a/dpkg.c
+++ b/dpkg.c
 
 #include "busybox.h"
 
-//#define PACKAGE "udpkg"
-//#define VERSION "0.1"
+#define DEPENDSMAX     64      /* maximum number of depends we can handle */
 
-/*
- * Should we do full dependency checking?
- */
+/* Should we do full dependency checking? */
 #define DODEPENDS 1
 
-/*
- * Should we do debugging?
- */
-#define DODEBUG 0
+/* Should we do debugging? */
+#define DODEBUG 1
 
 #ifdef DODEBUG
 #define SYSTEM(x) do_system(x)
 #define DPRINTF(fmt,args...) /* nothing */
 #endif
 
-#define BUFSIZE                4096
-#define DEPENDSMAX     64      /* maximum number of depends we can handle */
+/* from dpkg-deb.c */
+extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
+static const int dpkg_deb_contents = 1;
+static const int dpkg_deb_control = 2;
+//     const int dpkg_deb_info = 4;
+static const int dpkg_deb_extract = 8;
+static const int dpkg_deb_verbose_extract = 16;
+static const int dpkg_deb_list = 32;
+
+static const char statusfile[] = "/var/lib/dpkg/status.udeb";
+static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
+static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
+
+static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
 
-#define ADMINDIR "/var/lib/dpkg"
-#define STATUSFILE     ADMINDIR ## "/status.udeb"
-#define DPKGCIDIR      ADMINDIR ## "/tmp.ci/"
 static const char infodir[] = "/var/lib/dpkg/info/";
 static const char udpkg_quiet[] = "UDPKG_QUIET";
 
@@ -75,9 +79,9 @@ static const char *statuswords[][10] = {
                "post-inst-failed", "removal-failed", 0 }
 };
 
-const int color_white  = 0;
-const int color_grey   = 1;
-const int color_black  = 2;
+static const int color_white   = 0;
+static const int color_grey    = 1;
+static const int color_black   = 2;
 
 /* data structures */
 typedef struct package_s {
@@ -95,12 +99,37 @@ typedef struct package_s {
        struct package_s *next;
 } package_t;
 
+#ifdef DODEBUG
+static int do_system(const char *cmd)
+{
+       DPRINTF("cmd is %s\n", cmd);
+       return system(cmd);
+}
+#else
+#define do_system(cmd) system(cmd)
+#endif
+
 static int package_compare(const void *p1, const void *p2)
 {
        return strcmp(((package_t *)p1)->package, 
                ((package_t *)p2)->package);
 }
 
+static int remove_dpkgcidir()
+{
+       char *rm_dpkgcidir = NULL;
+
+       rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8);
+       strcpy(rm_dpkgcidir, "rm -rf ");
+       strcat(rm_dpkgcidir, dpkgcidir);
+
+       if (SYSTEM(rm_dpkgcidir) != 0) {
+               perror("mkdir ");
+               return EXIT_FAILURE;
+       }
+       return EXIT_SUCCESS;
+}
+
 #ifdef DODEPENDS
 #include <ctype.h>
 
@@ -111,45 +140,46 @@ static char **depends_split(const char *dependsstr)
        int i = 0;
 
        dependsvec[0] = 0;
+       if (dependsstr == 0) {
+               goto end;
+       }
 
-       if (dependsstr != 0)
-       {
-               p = strdup(dependsstr);
-               while (*p != 0 && *p != '\n')
-               {
-                       if (*p != ' ')
-                       {
-                               if (*p == ',')
-                               {
-                                       *p = 0;
-                                       dependsvec[++i] = 0;
-                               }
-                               else if (dependsvec[i] == 0)
+       p = strdup(dependsstr);
+       while (*p != 0 && *p != '\n') {
+               if (*p != ' ') {
+                       if (*p == ',') {
+                               *p = 0;
+                               dependsvec[++i] = 0;
+                       } else {
+                               if (dependsvec[i] == 0) {
                                        dependsvec[i] = p;
+                               }
                        }
-                       else
-                               *p = 0; /* eat the space... */
-                       p++;
+               } else {
+                       *p = 0; /* eat the space... */
                }
-               *p = 0;
+               p++;
        }
+       *p = 0;
+
+end:
        dependsvec[i+1] = 0;
        return dependsvec;
 }
 
+/* Topological sort algorithm:
+ * ordered is the output list, pkgs is the dependency graph, pkg is 
+ * the current node
+ *
+ * recursively add all the adjacent nodes to the ordered list, marking
+ * each one as visited along the way
+ *
+ * yes, this algorithm looks a bit odd when all the params have the
+ * same type :-)
+ */
 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
                package_t *pkg)
 {
-       /* Topological sort algorithm:
-        * ordered is the output list, pkgs is the dependency graph, pkg is 
-        * the current node
-        *
-        * recursively add all the adjacent nodes to the ordered list, marking
-        * each one as visited along the way
-        *
-        * yes, this algorithm looks a bit odd when all the params have the
-        * same type :-)
-        */
        unsigned short i;
 
        /* mark node as processing */
@@ -162,12 +192,13 @@ static void depends_sort_visit(package_t **ordered, package_t *pkgs,
 
 #if 0
        /* add it to the list */
-       newnode = (struct package_t *)malloc(sizeof(struct package_t));
+       newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
        /* make a shallow copy */
        *newnode = *pkg;
        newnode->next = *ordered;
        *ordered = newnode;
 #endif
+
        pkg->next = *ordered;
        *ordered = pkg;
 
@@ -182,12 +213,14 @@ static package_t *depends_sort(package_t *pkgs)
        package_t *ordered = NULL;
        package_t *pkg;
 
-       for (pkg = pkgs; pkg != 0; pkg = pkg->next)
+       for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
                pkg->color = color_white;
-
-       for (pkg = pkgs; pkg != 0; pkg = pkg->next)
-               if (pkg->color == color_white)
+       }
+       for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
+               if (pkg->color == color_white) {
                        depends_sort_visit(&ordered, pkgs, pkg);
+               }
+       }
 
        /* Leaks the old list... return the new one... */
        return ordered;
@@ -213,40 +246,33 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
        int i;
        void *found;
 
-       for (pkg = pkgs; pkg != 0; pkg = pkg->next)
-       {
+       for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
                dependsvec = depends_split(pkg->depends);
                i = 0;
-               while (dependsvec[i] != 0)
-               {
+               while (dependsvec[i] != 0) {
                        /* Check for dependencies; first look for installed packages */
                        dependpkg.package = dependsvec[i];
                        if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
                            ((chk = *(package_t **)found) &&
                             (chk->status & (status_flagok | status_statusinstalled)) != 
-                             (status_flagok | status_statusinstalled)))
-                       {
+                             (status_flagok | status_statusinstalled))) {
+
                                /* if it fails, we look through the list of packages we are going to 
                                 * install */
-                               for (chk = pkgs; chk != 0; chk = chk->next)
-                               {
-                                       if (strcmp(chk->package, dependsvec[i]) == 0 ||
-                                           (chk->provides && 
-                                            strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0))
-                                       {
-                                               if (chk->requiredcount >= DEPENDSMAX)
-                                               {
-                                                       fprintf(stderr, "Too many dependencies for %s\n", 
-                                                               chk->package);
+                               for (chk = pkgs; chk != 0; chk = chk->next) {
+                                       if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && 
+                                            strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
+                                               if (chk->requiredcount >= DEPENDSMAX) {
+                                                       fprintf(stderr, "Too many dependencies for %s\n", chk->package);
                                                        return 0;
                                                }
-                                               if (chk != pkg)
+                                               if (chk != pkg) {
                                                        chk->requiredfor[chk->requiredcount++] = pkg;
+                                               }
                                                break;
                                        }
                                }
-                               if (chk == 0)
-                               {
+                               if (chk == 0) {
                                        fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
                                        return 0;
                                }
@@ -273,29 +299,31 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
  *    replacing any pre-existing entries. when a merge happens, status info 
  *    read using the status_read function is written back to the status file
  */
-
 static unsigned long status_parse(const char *line)
 {
        char *p;
        int i, j;
        unsigned long l = 0;
-       for (i = 0; i < 3; i++)
-       {
-               p = strchr(line, ' ');
-               if (p) *p = 0; 
+
+       for (i = 0; i < 3; i++) {
+               if ((p = strchr(line, ' ')) != NULL) {
+                       *p = 0;
+               }
                j = 1;
-               while (statuswords[i][j] != 0) 
-               {
-                       if (strcmp(line, statuswords[i][j]) == 0) 
-                       {
+               while (statuswords[i][j] != 0) {
+                       if (strcmp(line, statuswords[i][j]) == 0) {
                                l |= (1 << ((int)statuswords[i][0] + j - 1));
                                break;
                        }
                        j++;
                }
-               if (statuswords[i][j] == 0) return 0; /* parse error */
+               /* parse error */
+               if (statuswords[i][j] == 0) {
+                       return 0;
+               }
                line = p+1;
        }
+
        return l;
 }
 
@@ -306,25 +334,22 @@ static const char *status_print(unsigned long flags)
        int i, j;
 
        buf[0] = 0;
-       for (i = 0; i < 3; i++)
-       {
+       for (i = 0; i < 3; i++) {
                j = 1;
-               while (statuswords[i][j] != 0)
-               {
-                       if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0)
-                       {
+               while (statuswords[i][j] != 0) {
+                       if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0)     {
                                strcat(buf, statuswords[i][j]);
                                if (i < 2) strcat(buf, " ");
                                break;
                        }
                        j++;
                }
-               if (statuswords[i][j] == 0)
-               {
+               if (statuswords[i][j] == 0) {
                        fprintf(stderr, "corrupted status flag!!\n");
                        return NULL;
                }
        }
+
        return buf;
 }
 
@@ -332,41 +357,39 @@ static const char *status_print(unsigned long flags)
  * Read a control file (or a stanza of a status file) and parse it,
  * filling parsed fields into the package structure
  */
-static void control_read(FILE *f, package_t *p)
+static int control_read(FILE *file, package_t *p)
 {
-       char buf[BUFSIZE];
-       while (fgets(buf, BUFSIZE, f) && !feof(f))
-       {
-               buf[strlen(buf)-1] = 0;
-               if (*buf == 0)
-                       return;
-               else if (strstr(buf, "Package: ") == buf)
-               {
-                       p->package = strdup(buf+9);
-               }
-               else if (strstr(buf, "Status: ") == buf)
-               {
-                       p->status = status_parse(buf+8);
-               }
-               else if (strstr(buf, "Depends: ") == buf)
-               {
-                       p->depends = strdup(buf+9);
-               }
-               else if (strstr(buf, "Provides: ") == buf)
-               {
-                       p->provides = strdup(buf+10);
-               }
+       char *line;
+
+       while ((line = get_line_from_file(file)) != NULL) {
+               line[strlen(line)] = 0;
+
+               if (strlen(line) == 0) {
+                       break;
+               } else
+                       if (strstr(line, "Package: ") == line) {
+                               p->package = strdup(line + 9);
+               } else
+                       if (strstr(line, "Status: ") == line) {
+                               p->status = status_parse(line + 8);
+               } else
+                       if (strstr(line, "Depends: ") == line) {
+                               p->depends = strdup(line + 9);
+               } else
+                       if (strstr(line, "Provides: ") == line) {
+                               p->provides = strdup(line + 10);
+               } else
+                       if (strstr(line, "Description: ") == line) {
+                               p->description = strdup(line + 13);
                /* This is specific to the Debian Installer. Ifdef? */
-               else if (strstr(buf, "installer-menu-item: ") == buf) 
-               {
-                       p->installer_menu_item = atoi(buf+21);
-               }
-               else if (strstr(buf, "Description: ") == buf)
-               {
-                       p->description = strdup(buf+13);
+               } else
+                       if (strstr(line, "installer-menu-item: ") == line) {
+                               p->installer_menu_item = atoi(line + 21);
                }
                /* TODO: localized descriptions */
        }
+       free(line);
+       return EXIT_SUCCESS;
 }
 
 static void *status_read(void)
@@ -375,20 +398,20 @@ static void *status_read(void)
        void *status = 0;
        package_t *m = 0, *p = 0, *t = 0;
 
-       if ((f = fopen(STATUSFILE, "r")) == NULL)
-       {
-               perror(STATUSFILE);
+       if ((f = fopen(statusfile, "r")) == NULL) {
+               perror(statusfile);
                return 0;
        }
-       if (getenv(udpkg_quiet) == NULL)
+
+       if (getenv(udpkg_quiet) == NULL) {
                printf("(Reading database...)\n");
-       while (!feof(f))
-       {
-               m = (package_t *)malloc(sizeof(package_t));
+       }
+
+       while (!feof(f)) {
+               m = (package_t *)xmalloc(sizeof(package_t));
                memset(m, 0, sizeof(package_t));
                control_read(f, m);
-               if (m->package)
-               {
+               if (m->package) {
                        /*
                         * If there is an item in the tree by this name,
                         * it must be a virtual package; insert real
@@ -396,20 +419,18 @@ static void *status_read(void)
                         */
                        tdelete(m, &status, package_compare);
                        tsearch(m, &status, package_compare);
-                       if (m->provides)
-                       {
+                       if (m->provides) {
                                /* 
                                 * A "Provides" triggers the insertion
                                 * of a pseudo package into the status
                                 * binary-tree.
                                 */
-                               p = (package_t *)malloc(sizeof(package_t));
+                               p = (package_t *)xmalloc(sizeof(package_t));
                                memset(p, 0, sizeof(package_t));
                                p->package = strdup(m->provides);
 
                                t = *(package_t **)tsearch(p, &status, package_compare);
-                               if (!(t == p))
-                               {
+                               if (t != p) {
                                        free(p->package);
                                        free(p);
                                }
@@ -426,8 +447,7 @@ static void *status_read(void)
                                }
                        }
                }
-               else
-               {
+               else {
                        free(m);
                }
        }
@@ -438,26 +458,25 @@ static void *status_read(void)
 static int status_merge(void *status, package_t *pkgs)
 {
        FILE *fin, *fout;
-       char buf[BUFSIZE];
+       char *line;
        package_t *pkg = 0, *statpkg = 0;
        package_t locpkg;
        int r = 0;
 
-       if ((fin = fopen(STATUSFILE, "r")) == NULL)
-       {
-               perror(STATUSFILE);
+       if ((fin = fopen(statusfile, "r")) == NULL) {
+               perror(statusfile);
                return 0;
        }
-       if ((fout = fopen(STATUSFILE ".new", "w")) == NULL)
-       {
-               perror(STATUSFILE ".new");
+       if ((fout = fopen(new_statusfile, "w")) == NULL) {
+               perror(new_statusfile);
                return 0;
        }
-       if (getenv(udpkg_quiet) == NULL)
+       if (getenv(udpkg_quiet) == NULL) {
                printf("(Updating database...)\n");
-       while (fgets(buf, BUFSIZE, fin) && !feof(fin))
-       {
-               buf[strlen(buf)-1] = 0; /* trim newline */
+       }
+
+       while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
+               line[strlen(line)] = 0; /* trim newline */
                /* If we see a package header, find out if it's a package
                 * that we have processed. if so, we skip that block for
                 * now (write it at the end).
@@ -465,31 +484,33 @@ static int status_merge(void *status, package_t *pkgs)
                 * we also look at packages in the status cache and update
                 * their status fields
                 */
-               if (strstr(buf, "Package: ") == buf)
-               {
-                       for (pkg = pkgs; pkg != 0 && strncmp(buf + 9,
-                                       pkg->package, strlen(buf) - 9)!=0;
+               if (strstr(line, "Package: ") == line) {
+                       for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
+                                       pkg->package, strlen(line) - 9) != 0;
                             pkg = pkg->next) ;
 
-                       locpkg.package = buf+9;
+                       locpkg.package = line + 9;
                        statpkg = tfind(&locpkg, &status, package_compare);
                        
                        /* note: statpkg should be non-zero, unless the status
                         * file was changed while we are processing (no locking
                         * is currently done...
                         */
-                       if (statpkg != 0) statpkg = *(package_t **)statpkg;
+                       if (statpkg != 0) {
+                               statpkg = *(package_t **)statpkg;
+                       }
                }
-               if (pkg != 0) continue;
-
-               if (strstr(buf, "Status: ") == buf && statpkg != 0)
-               {
-                         snprintf(buf, sizeof(buf), "Status: %s",
-                                status_print(statpkg->status));
+               if (pkg != 0) {
+                       continue;
+               }
+               if (strstr(line, "Status: ") == line && statpkg != 0) {
+                       snprintf(line, sizeof(line), "Status: %s",
+                               status_print(statpkg->status));
                }
-               fputs(buf, fout);
+               fputs(line, fout);
                fputc('\n', fout);
        }
+       free(line);
 
        // Print out packages we processed.
        for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
@@ -509,30 +530,21 @@ static int status_merge(void *status, package_t *pkgs)
        fclose(fin);
        fclose(fout);
 
-       r = rename(STATUSFILE, STATUSFILE ".bak");
-       if (r == 0) r = rename(STATUSFILE ".new", STATUSFILE);
-       return 0;
-}
-
-/* 
- * Main udpkg implementation routines
- */
+       r = rename(statusfile, bak_statusfile);
+       if (r == 0) {
+               r = rename(new_statusfile, statusfile);
+       }
 
-#ifdef DODEBUG
-static int do_system(const char *cmd)
-{
-       DPRINTF("cmd is %s\n", cmd);
-       return system(cmd);
+       return 0;
 }
-#else
-#define do_system(cmd) system(cmd)
-#endif
 
 static int is_file(const char *fn)
 {
        struct stat statbuf;
 
-       if (stat(fn, &statbuf) < 0) return 0;
+       if (stat(fn, &statbuf) < 0) {
+               return 0;
+       }
        return S_ISREG(statbuf.st_mode);
 }
 
@@ -541,20 +553,19 @@ static int dpkg_doconfigure(package_t *pkg)
        int r;
        char postinst[1024];
        char buf[1024];
+
        DPRINTF("Configuring %s\n", pkg->package);
        pkg->status &= status_statusmask;
        snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
-       if (is_file(postinst))
-       {
+
+       if (is_file(postinst)) {
                snprintf(buf, sizeof(buf), "%s configure", postinst);
-               if ((r = do_system(buf)) != 0)
-               {
+               if ((r = do_system(buf)) != 0) {
                        fprintf(stderr, "postinst exited with status %d\n", r);
                        pkg->status |= status_statushalfconfigured;
                        return 1;
                }
        }
-
        pkg->status |= status_statusinstalled;
        
        return 0;
@@ -562,204 +573,179 @@ static int dpkg_doconfigure(package_t *pkg)
 
 static int dpkg_dounpack(package_t *pkg)
 {
-       int r = 0;
-       char *cwd, *p;
-       FILE *infp, *outfp;
+       int r = 0, i;
+       char *cwd;
+       FILE *outfp;
+       char *src_file = NULL;
+       char *dst_file = NULL;
+       char *lst_file = NULL;
+       char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst",
+                       "/conffiles", "/md5sums", "/shlibs", "/templates" };
        char buf[1024], buf2[1024];
-       int i;
-       char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
-                                "conffiles", "md5sums", "shlibs", 
-                                "templates" };
 
        DPRINTF("Unpacking %s\n", pkg->package);
 
        cwd = getcwd(0, 0);
        chdir("/");
-       snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file);
-       if (SYSTEM(buf) == 0)
-       {
-               /* Installs the package scripts into the info directory */
-               for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]);
-                    i++)
-               {
-                       snprintf(buf, sizeof(buf), "%s%s/%s",
-                               DPKGCIDIR, pkg->package, adminscripts[i]);
-                       snprintf(buf2, sizeof(buf), "%s%s.%s", 
-                               infodir, pkg->package, adminscripts[i]);
-                       if (copy_file(buf, buf2, TRUE, FALSE, FALSE) < 0)
-                       {
-                               fprintf(stderr, "Cannot copy %s to %s: %s\n", 
-                                       buf, buf2, strerror(errno));
-                               r = 1;
-                               break;
-                       }
-                       else
-                       {
-                               /* ugly hack to create the list file; should
-                                * probably do something more elegant
-                                *
-                                * why oh why does dpkg create the list file
-                                * so oddly...
-                                */
-                               snprintf(buf, sizeof(buf), 
-                                       "ar -p %s data.tar.gz|zcat|tar -tf -", 
-                                       pkg->file);
-                               snprintf(buf2, sizeof(buf2),
-                                       "%s%s.list", infodir, pkg->package);
-                               if ((infp = popen(buf, "r")) == NULL ||
-                                   (outfp = fopen(buf2, "w")) == NULL)
-                               {
-                                       fprintf(stderr, "Cannot create %s\n",
-                                               buf2);
-                                       r = 1;
-                                       break;
-                               }
-                               while (fgets(buf, sizeof(buf), infp) &&
-                                      !feof(infp))
-                               {
-                                       p = buf;
-                                       if (*p == '.') p++;
-                                       if (*p == '/' && *(p+1) == '\n')
-                                       {
-                                               *(p+1) = '.';
-                                               *(p+2) = '\n';
-                                               *(p+3) = 0;
-                                       }
-                                       if (p[strlen(p)-2] == '/')
-                                       {
-                                               p[strlen(p)-2] = '\n';
-                                               p[strlen(p)-1] = 0;
-                                       }
-                                       fputs(p, outfp);
-                               }
-                               fclose(infp);
-                               fclose(outfp);
-                       }
+       deb_extract(dpkg_deb_extract, "/", pkg->file);
+
+       /* Installs the package scripts into the info directory */
+       for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
+               /* The full path of the current location of the admin file */
+               src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
+               strcpy(src_file, dpkgcidir);
+               strcat(src_file, pkg->package);
+               strcat(src_file, adminscripts[i]);
+
+               /* the full path of where we want the file to be copied to */
+               dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
+               strcpy(dst_file, infodir);
+               strcat(dst_file, pkg->package);
+               strcat(dst_file, adminscripts[i]);
+
+               /* copy admin file to permanent home */
+               if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) {
+                       error_msg_and_die("Cannot copy %s to %s ", buf, buf2);
                }
-               pkg->status &= status_wantmask;
-               pkg->status |= status_wantinstall;
-               pkg->status &= status_flagmask;
-               pkg->status |= status_flagok;
-               pkg->status &= status_statusmask;
-               if (r == 0)
-                       pkg->status |= status_statusunpacked;
-               else
-                       pkg->status |= status_statushalfinstalled;
+
+               /* create the list file */
+               lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
+               strcpy(lst_file, infodir);
+               strcat(lst_file, pkg->package);
+               strcat(lst_file, ".list");
+               outfp = freopen(lst_file, "w", stdout);
+               deb_extract(dpkg_deb_list, NULL, pkg->file);
+               stdout = freopen(NULL, "w", outfp);
+
+               printf("done\n");
+               getchar();
+
+               fclose(outfp);
+       }
+
+       pkg->status &= status_wantmask;
+       pkg->status |= status_wantinstall;
+       pkg->status &= status_flagmask;
+       pkg->status |= status_flagok;
+       pkg->status &= status_statusmask;
+
+       if (r == 0) {
+               pkg->status |= status_statusunpacked;
+       } else {
+               pkg->status |= status_statushalfinstalled;
        }
        chdir(cwd);
        return r;
 }
 
-static int dpkg_doinstall(package_t *pkg)
-{
-       DPRINTF("Installing %s\n", pkg->package);
-       return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg));
-}
-
+/*
+ * Extract and parse the control.tar.gz from the specified package
+ */
 static int dpkg_unpackcontrol(package_t *pkg)
 {
-       int r = 1;
-       char *cwd = 0;
-       char *p;
-       char buf[1024];
-       FILE *f;
+       char *tmp_name;
+       FILE *file;
+       int length;
 
-       p = strrchr(pkg->file, '/');
-       if (p) p++; else p = pkg->file;
-       p = pkg->package = strdup(p);
-       while (*p != 0 && *p != '_' && *p != '.') p++;
-       *p = 0;
+       /* clean the temp directory (dpkgcidir) be recreating it */
+       remove_dpkgcidir();
+       if (mkdir(dpkgcidir, S_IRWXU) != 0) {
+               perror("mkdir");
+               return EXIT_FAILURE;
+       }
 
-       cwd = getcwd(0, 0);
-       snprintf(buf, sizeof(buf), "%s%s", DPKGCIDIR, pkg->package);
-       DPRINTF("dir = %s\n", buf);
-       if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0)
-       {
-               snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -",
-                       pkg->file);
-               if (SYSTEM(buf) == 0)
-               {
-                       if ((f = fopen("control", "r")) != NULL) {
-                               control_read(f, pkg);
-                               r = 0;
-                       }
-               }
+       /*
+        * Get the package name from the file name,
+        * first remove the directories
+        */
+       if ((tmp_name = strrchr(pkg->file, '/')) == NULL) {
+               tmp_name = pkg->file;
+       } else {
+               tmp_name++;
+       }
+       /* now remove trailing version numbers etc */
+       length = strcspn(tmp_name, "_.");
+       pkg->package = (char *) xmalloc(length + 1);
+       /* store the package name */
+       strncpy(pkg->package, tmp_name, length);
+
+       /* work out the full extraction path */
+       tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9);
+       memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
+       strcpy(tmp_name, dpkgcidir);
+       strcat(tmp_name, pkg->package);
+
+       /* extract control.tar.gz to the full extraction path */
+       deb_extract(dpkg_deb_control, tmp_name, pkg->file);
+
+       /* parse the extracted control file */
+       strcat(tmp_name, "/control");
+       if ((file = fopen(tmp_name, "r")) == NULL) {
+               return EXIT_FAILURE;
+       }
+       if (control_read(file, pkg) == EXIT_FAILURE) {
+               return EXIT_FAILURE;
        }
 
-       chdir(cwd);
-       free(cwd);
-       return r;
+       return EXIT_SUCCESS;
 }
 
-static int dpkg_unpack(package_t *pkgs)
+static int dpkg_unpack(package_t *pkgs, void *status)
 {
        int r = 0;
        package_t *pkg;
-       void *status = status_read();
 
-       if (SYSTEM("rm -rf -- " DPKGCIDIR) != 0 ||
-           mkdir(DPKGCIDIR, S_IRWXU) != 0)
-       {
-               perror("mkdir");
-               return 1;
-       }
-       
-       for (pkg = pkgs; pkg != 0; pkg = pkg->next)
-       {
-               dpkg_unpackcontrol(pkg);
-               r = dpkg_dounpack(pkg);
-               if (r != 0) break;
+       for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
+               if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) {
+                       return EXIT_FAILURE;
+               }
+               if ((r = dpkg_dounpack(pkg)) != 0 ) {
+                       break;
+               }
        }
        status_merge(status, pkgs);
-       SYSTEM("rm -rf -- " DPKGCIDIR);
+       remove_dpkgcidir();
+
        return r;
 }
 
-static int dpkg_configure(package_t *pkgs)
+static int dpkg_configure(package_t *pkgs, void *status)
 {
        int r = 0;
        void *found;
        package_t *pkg;
-       void *status = status_read();
-       for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next)
-       {
+
+       for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
                found = tfind(pkg, &status, package_compare);
-               if (found == 0)
-               {
+
+               if (found == 0) {
                        fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
                        r = 1;
-               }
-               else
-               {
-                       /* configure the package listed in the status file;
-                        * not pkg, as we have info only for the latter */
+               } 
+               /* configure the package listed in the status file;
+                * not pkg, as we have info only for the latter
+                */
+               else {
                        r = dpkg_doconfigure(*(package_t **)found);
                }
        }
        status_merge(status, 0);
+
        return r;
 }
 
-static int dpkg_install(package_t *pkgs)
+static int dpkg_install(package_t *pkgs, void *status)
 {
        package_t *p, *ordered = 0;
-       void *status = status_read();
-       if (SYSTEM("rm -rf -- " DPKGCIDIR) != 0 ||
-           mkdir(DPKGCIDIR, S_IRWXU) != 0)
-       {
-               perror("mkdir");
-               return 1;
-       }
-       
+
        /* Stage 1: parse all the control information */
-       for (p = pkgs; p != 0; p = p->next)
-               if (dpkg_unpackcontrol(p) != 0)
-               {
+       for (p = pkgs; p != 0; p = p->next) {
+               if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
                        perror(p->file);
-                       /* force loop break, and prevents further ops */
-                       pkgs = 0;
+                       return EXIT_FAILURE;
                }
-       
+       }
+
        /* Stage 2: resolve dependencies */
 #ifdef DODEPENDS
        ordered = depends_resolve(pkgs, status);
@@ -768,8 +754,7 @@ static int dpkg_install(package_t *pkgs)
 #endif
        
        /* Stage 3: install */
-       for (p = ordered; p != 0; p = p->next)
-       {
+       for (p = ordered; p != 0; p = p->next) {
                p->status &= status_wantmask;
                p->status |= status_wantinstall;
 
@@ -779,26 +764,32 @@ static int dpkg_install(package_t *pkgs)
                p->status &= status_flagmask;
                p->status |= status_flagok;
 
-               if (dpkg_doinstall(p) != 0)
-               {
+               DPRINTF("Installing %s\n", p->package);
+               if (dpkg_dounpack(p) != 0) {
+                       perror(p->file);
+               }
+               if (dpkg_doconfigure(p) != 0) {
                        perror(p->file);
                }
        }
        
-       if (ordered != 0)
+       if (ordered != 0) {
                status_merge(status, pkgs);
-       SYSTEM("rm -rf -- " DPKGCIDIR);
+       }
+       remove_dpkgcidir();
+
        return 0;
 }
 
-static int dpkg_remove(package_t *pkgs)
+static int dpkg_remove(package_t *pkgs, void *status)
 {
        package_t *p;
-       void *status = status_read();
+
        for (p = pkgs; p != 0; p = p->next)
        {
        }
        status_merge(status, 0);
+
        return 0;
 }
 
@@ -808,44 +799,48 @@ extern int dpkg_main(int argc, char **argv)
        char *s;
        package_t *p, *packages = NULL;
        char *cwd = getcwd(0, 0);
-       while (*++argv)
-       {
+       void *status = NULL;
+
+       while (*++argv) {
                if (**argv == '-') {
                        /* Nasty little hack to "parse" long options. */
                        s = *argv;
-                       while (*s == '-')
+                       while (*s == '-') {
                                s++;
+                       }
                        opt=s[0];
-               }
-               else
-               {
-                       p = (package_t *)malloc(sizeof(package_t));
+               } else {
+                       p = (package_t *)xmalloc(sizeof(package_t));
                        memset(p, 0, sizeof(package_t));
-                       if (**argv == '/')
+
+                       if (**argv == '/') {
                                p->file = *argv;
-                       else if (opt != 'c')
-                       {
-                               p->file = malloc(strlen(cwd) + strlen(*argv) + 2);
-                               sprintf(p->file, "%s/%s", cwd, *argv);
-                       }
-                       else {
+                       } else
+                               if (opt != 'c') {
+                                       p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
+                                       sprintf(p->file, "%s/%s", cwd, *argv);
+                       } else {
                                p->package = strdup(*argv);
                        }
+
                        p->next = packages;
                        packages = p;
-               }
-                       
-       }
-       switch (opt)
-       {
-               case 'i': return dpkg_install(packages); break;
-               case 'r': return dpkg_remove(packages); break;
-               case 'u': return dpkg_unpack(packages); break;
-               case 'c': return dpkg_configure(packages); break;
+               }               
        }
 
-       /* if it falls through to here, some of the command line options were
-          wrong */
-       usage(dpkg_usage);
-       return 0;
-}
\ No newline at end of file
+       status = status_read();
+
+       switch (opt) {
+               case 'i':
+                       return dpkg_install(packages, status);
+               case 'r':
+                       return dpkg_remove(packages, status);
+               case 'u':
+                       return dpkg_unpack(packages, status);
+               case 'c':
+                       return dpkg_configure(packages, status);
+               default :
+                       show_usage();
+                       return EXIT_FAILURE;
+       }
+}