A number of cleanups. Now compiles with libc5, glibc, and uClibc. Fix a few
[oweals/busybox.git] / dpkg.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <search.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <utime.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include "busybox.h"
13
14
15 #define DEPENDSMAX      64      /* maximum number of depends we can handle */
16
17 /* Should we do full dependency checking? */
18 #define DODEPENDS 1
19
20 /* Should we do debugging? */
21 #define DODEBUG 1
22
23 #ifdef DODEBUG
24 #define SYSTEM(x) do_system(x)
25 #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
26 #else
27 #define SYSTEM(x) system(x)
28 #define DPRINTF(fmt,args...) /* nothing */
29 #endif
30
31 /* from dpkg-deb.c */
32 extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
33 static const int dpkg_deb_contents = 1;
34 static const int dpkg_deb_control = 2;
35 //      const int dpkg_deb_info = 4;
36 static const int dpkg_deb_extract = 8;
37 static const int dpkg_deb_verbose_extract = 16;
38 static const int dpkg_deb_list = 32;
39
40 static const char statusfile[] = "/var/lib/dpkg/status.udeb";
41 static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
42 static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
43
44 static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
45
46 static const char infodir[] = "/var/lib/dpkg/info/";
47 static const char udpkg_quiet[] = "UDPKG_QUIET";
48
49 //static const int status_wantstart     = 0;
50 //static const int status_wantunknown   = (1 << 0);
51 static const int status_wantinstall     = (1 << 1);
52 //static const int status_wanthold      = (1 << 2);
53 //static const int status_wantdeinstall = (1 << 3);
54 //static const int status_wantpurge     = (1 << 4);
55 static const int status_wantmask        = 31;
56
57 //static const int status_flagstart     = 5;
58 static const int status_flagok  = (1 << 5);     /* 32 */
59 //static const int status_flagreinstreq = (1 << 6); 
60 //static const int status_flaghold      = (1 << 7);
61 //static const int status_flagholdreinstreq     = (1 << 8);
62 static const int status_flagmask        = 480;
63
64 //static const int status_statusstart   = 9;
65 //static const int status_statusnoninstalled    = (1 << 9); /* 512 */
66 static const int status_statusunpacked  = (1 << 10);
67 static const int status_statushalfconfigured    = (1 << 11); 
68 static const int status_statusinstalled = (1 << 12);
69 static const int status_statushalfinstalled     = (1 << 13);
70 //static const int status_statusconfigfiles     = (1 << 14);
71 //static const int status_statuspostinstfailed  = (1 << 15);
72 //static const int status_statusremovalfailed   = (1 << 16);
73 static const int status_statusmask =  130560; /* i assume status_statusinstalled is supposed to be included */
74
75 static const char *statuswords[][10] = {
76         { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
77         { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
78         { (char *) 9, "not-installed", "unpacked", "half-configured",
79                 "installed", "half-installed", "config-files",
80                 "post-inst-failed", "removal-failed", 0 }
81 };
82
83 static const int color_white    = 0;
84 static const int color_grey     = 1;
85 static const int color_black    = 2;
86
87 /* data structures */
88 typedef struct package_s {
89         char *file;
90         char *package;
91         char *version;
92         char *depends;
93         char *provides;
94         char *description;
95         int installer_menu_item;
96         unsigned long status;
97         char color; /* for topo-sort */
98         struct package_s *requiredfor[DEPENDSMAX]; 
99         unsigned short requiredcount;
100         struct package_s *next;
101 } package_t;
102
103 #ifdef DODEBUG
104 static int do_system(const char *cmd)
105 {
106         DPRINTF("cmd is %s\n", cmd);
107         return system(cmd);
108 }
109 #else
110 #define do_system(cmd) system(cmd)
111 #endif
112
113 static int package_compare(const void *p1, const void *p2)
114 {
115         return strcmp(((package_t *)p1)->package, 
116                 ((package_t *)p2)->package);
117 }
118
119 static int remove_dpkgcidir()
120 {
121         char *rm_dpkgcidir = NULL;
122
123         rm_dpkgcidir = (char *) xmalloc(strlen(dpkgcidir) + 8);
124         strcpy(rm_dpkgcidir, "rm -rf ");
125         strcat(rm_dpkgcidir, dpkgcidir);
126
127         if (SYSTEM(rm_dpkgcidir) != 0) {
128                 perror("mkdir ");
129                 return EXIT_FAILURE;
130         }
131         return EXIT_SUCCESS;
132 }
133
134 #ifdef DODEPENDS
135 #include <ctype.h>
136
137 static char **depends_split(const char *dependsstr)
138 {
139         static char *dependsvec[DEPENDSMAX];
140         char *p;
141         int i = 0;
142
143         dependsvec[0] = 0;
144         if (dependsstr == 0) {
145                 goto end;
146         }
147
148         p = strdup(dependsstr);
149         while (*p != 0 && *p != '\n') {
150                 if (*p != ' ') {
151                         if (*p == ',') {
152                                 *p = 0;
153                                 dependsvec[++i] = 0;
154                         } else {
155                                 if (dependsvec[i] == 0) {
156                                         dependsvec[i] = p;
157                                 }
158                         }
159                 } else {
160                         *p = 0; /* eat the space... */
161                 }
162                 p++;
163         }
164         *p = 0;
165
166 end:
167         dependsvec[i+1] = 0;
168         return dependsvec;
169 }
170
171 /* Topological sort algorithm:
172  * ordered is the output list, pkgs is the dependency graph, pkg is 
173  * the current node
174  *
175  * recursively add all the adjacent nodes to the ordered list, marking
176  * each one as visited along the way
177  *
178  * yes, this algorithm looks a bit odd when all the params have the
179  * same type :-)
180  */
181 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
182                 package_t *pkg)
183 {
184         unsigned short i;
185
186         /* mark node as processing */
187         pkg->color = color_grey;
188
189         /* visit each not-yet-visited node */
190         for (i = 0; i < pkg->requiredcount; i++)
191                 if (pkg->requiredfor[i]->color == color_white)
192                         depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
193
194 #if 0
195         /* add it to the list */
196         newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
197         /* make a shallow copy */
198         *newnode = *pkg;
199         newnode->next = *ordered;
200         *ordered = newnode;
201 #endif
202
203         pkg->next = *ordered;
204         *ordered = pkg;
205
206         /* mark node as done */
207         pkg->color = color_black;
208 }
209
210 static package_t *depends_sort(package_t *pkgs)
211 {
212         /* TODO: it needs to break cycles in the to-be-installed package 
213          * graph... */
214         package_t *ordered = NULL;
215         package_t *pkg;
216
217         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
218                 pkg->color = color_white;
219         }
220         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
221                 if (pkg->color == color_white) {
222                         depends_sort_visit(&ordered, pkgs, pkg);
223                 }
224         }
225
226         /* Leaks the old list... return the new one... */
227         return ordered;
228 }
229
230
231 /* resolve package dependencies -- 
232  * for each package in the list of packages to be installed, we parse its 
233  * dependency info to determine if the dependent packages are either 
234  * already installed, or are scheduled to be installed. If both tests fail
235  * than bail.
236  *
237  * The algorithm here is O(n^2*m) where n = number of packages to be 
238  * installed and m is the # of dependencies per package. Not a terribly
239  * efficient algorithm, but given that at any one time you are unlikely
240  * to install a very large number of packages it doesn't really matter
241  */
242 static package_t *depends_resolve(package_t *pkgs, void *status)
243 {
244         package_t *pkg, *chk;
245         package_t dependpkg;
246         char **dependsvec;
247         int i;
248         void *found;
249
250         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
251                 dependsvec = depends_split(pkg->depends);
252                 i = 0;
253                 while (dependsvec[i] != 0) {
254                         /* Check for dependencies; first look for installed packages */
255                         dependpkg.package = dependsvec[i];
256                         if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
257                             ((chk = *(package_t **)found) &&
258                              (chk->status & (status_flagok | status_statusinstalled)) != 
259                               (status_flagok | status_statusinstalled))) {
260
261                                 /* if it fails, we look through the list of packages we are going to 
262                                  * install */
263                                 for (chk = pkgs; chk != 0; chk = chk->next) {
264                                         if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && 
265                                              strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
266                                                 if (chk->requiredcount >= DEPENDSMAX) {
267                                                         fprintf(stderr, "Too many dependencies for %s\n", chk->package);
268                                                         return 0;
269                                                 }
270                                                 if (chk != pkg) {
271                                                         chk->requiredfor[chk->requiredcount++] = pkg;
272                                                 }
273                                                 break;
274                                         }
275                                 }
276                                 if (chk == 0) {
277                                         fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
278                                         return 0;
279                                 }
280                         }
281                         i++;
282                 }
283         }
284
285         return depends_sort(pkgs);
286 }
287 #endif
288
289 /* Status file handling routines
290  * 
291  * This is a fairly minimalistic implementation. there are two main functions 
292  * that are supported:
293  * 
294  * 1) reading the entire status file:
295  *    the status file is read into memory as a binary-tree, with just the 
296  *    package and status info preserved
297  *
298  * 2) merging the status file
299  *    control info from (new) packages is merged into the status file, 
300  *    replacing any pre-existing entries. when a merge happens, status info 
301  *    read using the status_read function is written back to the status file
302  */
303 static unsigned long status_parse(const char *line)
304 {
305         char *p;
306         int i, j;
307         unsigned long l = 0;
308
309         for (i = 0; i < 3; i++) {
310                 if ((p = strchr(line, ' ')) != NULL) {
311                         *p = 0;
312                 }
313                 j = 1;
314                 while (statuswords[i][j] != 0) {
315                         if (strcmp(line, statuswords[i][j]) == 0) {
316                                 l |= (1 << ((int)statuswords[i][0] + j - 1));
317                                 break;
318                         }
319                         j++;
320                 }
321                 /* parse error */
322                 if (statuswords[i][j] == 0) {
323                         return 0;
324                 }
325                 line = p+1;
326         }
327
328         return l;
329 }
330
331 static const char *status_print(unsigned long flags)
332 {
333         /* this function returns a static buffer... */
334         static char buf[256];
335         int i, j;
336
337         buf[0] = 0;
338         for (i = 0; i < 3; i++) {
339                 j = 1;
340                 while (statuswords[i][j] != 0) {
341                         if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0)     {
342                                 strcat(buf, statuswords[i][j]);
343                                 if (i < 2) strcat(buf, " ");
344                                 break;
345                         }
346                         j++;
347                 }
348                 if (statuswords[i][j] == 0) {
349                         fprintf(stderr, "corrupted status flag!!\n");
350                         return NULL;
351                 }
352         }
353
354         return buf;
355 }
356
357 /*
358  * Read a control file (or a stanza of a status file) and parse it,
359  * filling parsed fields into the package structure
360  */
361 static int control_read(FILE *file, package_t *p)
362 {
363         char *line;
364
365         while ((line = get_line_from_file(file)) != NULL) {
366                 line[strlen(line)] = 0;
367
368                 if (strlen(line) == 0) {
369                         break;
370                 } else
371                         if (strstr(line, "Package: ") == line) {
372                                 p->package = strdup(line + 9);
373                 } else
374                         if (strstr(line, "Status: ") == line) {
375                                 p->status = status_parse(line + 8);
376                 } else
377                         if (strstr(line, "Depends: ") == line) {
378                                 p->depends = strdup(line + 9);
379                 } else
380                         if (strstr(line, "Provides: ") == line) {
381                                 p->provides = strdup(line + 10);
382                 } else
383                         if (strstr(line, "Description: ") == line) {
384                                 p->description = strdup(line + 13);
385                 /* This is specific to the Debian Installer. Ifdef? */
386                 } else
387                         if (strstr(line, "installer-menu-item: ") == line) {
388                                 p->installer_menu_item = atoi(line + 21);
389                 }
390                 /* TODO: localized descriptions */
391         }
392         free(line);
393         return EXIT_SUCCESS;
394 }
395
396 static void *status_read(void)
397 {
398         FILE *f;
399         void *status = 0;
400         package_t *m = 0, *p = 0, *t = 0;
401
402         if ((f = fopen(statusfile, "r")) == NULL) {
403                 perror(statusfile);
404                 return 0;
405         }
406
407         if (getenv(udpkg_quiet) == NULL) {
408                 printf("(Reading database...)\n");
409         }
410
411         while (!feof(f)) {
412                 m = (package_t *)xmalloc(sizeof(package_t));
413                 memset(m, 0, sizeof(package_t));
414                 control_read(f, m);
415                 if (m->package) {
416                         /*
417                          * If there is an item in the tree by this name,
418                          * it must be a virtual package; insert real
419                          * package in preference.
420                          */
421                         tdelete(m, &status, package_compare);
422                         tsearch(m, &status, package_compare);
423                         if (m->provides) {
424                                 /* 
425                                  * A "Provides" triggers the insertion
426                                  * of a pseudo package into the status
427                                  * binary-tree.
428                                  */
429                                 p = (package_t *)xmalloc(sizeof(package_t));
430                                 memset(p, 0, sizeof(package_t));
431                                 p->package = strdup(m->provides);
432
433                                 t = *(package_t **)tsearch(p, &status, package_compare);
434                                 if (t != p) {
435                                         free(p->package);
436                                         free(p);
437                                 }
438                                 else {
439                                         /*
440                                          * Pseudo package status is the
441                                          * same as the status of the
442                                          * package providing it 
443                                          * FIXME: (not quite right, if 2
444                                          * packages of different statuses
445                                          * provide it).
446                                          */
447                                         t->status = m->status;
448                                 }
449                         }
450                 }
451                 else {
452                         free(m);
453                 }
454         }
455         fclose(f);
456         return status;
457 }
458
459 static int status_merge(void *status, package_t *pkgs)
460 {
461         FILE *fin, *fout;
462         char *line;
463         package_t *pkg = 0, *statpkg = 0;
464         package_t locpkg;
465         int r = 0;
466
467         if ((fin = fopen(statusfile, "r")) == NULL) {
468                 perror(statusfile);
469                 return 0;
470         }
471         if ((fout = fopen(new_statusfile, "w")) == NULL) {
472                 perror(new_statusfile);
473                 return 0;
474         }
475         if (getenv(udpkg_quiet) == NULL) {
476                 printf("(Updating database...)\n");
477         }
478
479         while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
480                 line[strlen(line)] = 0; /* trim newline */
481                 /* If we see a package header, find out if it's a package
482                  * that we have processed. if so, we skip that block for
483                  * now (write it at the end).
484                  *
485                  * we also look at packages in the status cache and update
486                  * their status fields
487                  */
488                 if (strstr(line, "Package: ") == line) {
489                         for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
490                                         pkg->package, strlen(line) - 9) != 0;
491                              pkg = pkg->next) ;
492
493                         locpkg.package = line + 9;
494                         statpkg = tfind(&locpkg, &status, package_compare);
495                         
496                         /* note: statpkg should be non-zero, unless the status
497                          * file was changed while we are processing (no locking
498                          * is currently done...
499                          */
500                         if (statpkg != 0) {
501                                 statpkg = *(package_t **)statpkg;
502                         }
503                 }
504                 if (pkg != 0) {
505                         continue;
506                 }
507                 if (strstr(line, "Status: ") == line && statpkg != 0) {
508                         snprintf(line, sizeof(line), "Status: %s",
509                                 status_print(statpkg->status));
510                 }
511                 fputs(line, fout);
512                 fputc('\n', fout);
513         }
514         free(line);
515
516         // Print out packages we processed.
517         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
518                 fprintf(fout, "Package: %s\nStatus: %s\n", 
519                         pkg->package, status_print(pkg->status));
520                 if (pkg->depends)
521                         fprintf(fout, "Depends: %s\n", pkg->depends);
522                 if (pkg->provides)
523                         fprintf(fout, "Provides: %s\n", pkg->provides);
524                 if (pkg->installer_menu_item)
525                         fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
526                 if (pkg->description)
527                         fprintf(fout, "Description: %s\n", pkg->description);
528                 fputc('\n', fout);
529         }
530         
531         fclose(fin);
532         fclose(fout);
533
534         r = rename(statusfile, bak_statusfile);
535         if (r == 0) {
536                 r = rename(new_statusfile, statusfile);
537         }
538
539         return 0;
540 }
541
542 static int is_file(const char *fn)
543 {
544         struct stat statbuf;
545
546         if (stat(fn, &statbuf) < 0) {
547                 return 0;
548         }
549         return S_ISREG(statbuf.st_mode);
550 }
551
552 static int dpkg_doconfigure(package_t *pkg)
553 {
554         int r;
555         char postinst[1024];
556         char buf[1024];
557
558         DPRINTF("Configuring %s\n", pkg->package);
559         pkg->status &= status_statusmask;
560         snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
561
562         if (is_file(postinst)) {
563                 snprintf(buf, sizeof(buf), "%s configure", postinst);
564                 if ((r = do_system(buf)) != 0) {
565                         fprintf(stderr, "postinst exited with status %d\n", r);
566                         pkg->status |= status_statushalfconfigured;
567                         return 1;
568                 }
569         }
570         pkg->status |= status_statusinstalled;
571         
572         return 0;
573 }
574
575 static int dpkg_dounpack(package_t *pkg)
576 {
577         int r = 0, i;
578         char *cwd;
579         FILE *outfp;
580         char *src_file = NULL;
581         char *dst_file = NULL;
582         char *lst_file = NULL;
583         char *adminscripts[] = { "/prerm", "/postrm", "/preinst", "/postinst",
584                         "/conffiles", "/md5sums", "/shlibs", "/templates" };
585         char buf[1024], buf2[1024];
586         FILE *myfile = stdout;
587
588         DPRINTF("Unpacking %s\n", pkg->package);
589
590         cwd = getcwd(0, 0);
591         chdir("/");
592         deb_extract(dpkg_deb_extract, "/", pkg->file);
593
594         /* Installs the package scripts into the info directory */
595         for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
596                 /* The full path of the current location of the admin file */
597                 src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
598                 strcpy(src_file, dpkgcidir);
599                 strcat(src_file, pkg->package);
600                 strcat(src_file, adminscripts[i]);
601
602                 /* the full path of where we want the file to be copied to */
603                 dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
604                 strcpy(dst_file, infodir);
605                 strcat(dst_file, pkg->package);
606                 strcat(dst_file, adminscripts[i]);
607
608                 /* copy admin file to permanent home */
609                 if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) {
610                         error_msg_and_die("Cannot copy %s to %s ", buf, buf2);
611                 }
612
613                 /* create the list file */
614                 lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
615                 strcpy(lst_file, infodir);
616                 strcat(lst_file, pkg->package);
617                 strcat(lst_file, ".list");
618                 outfp = freopen(lst_file, "w", myfile);
619                 deb_extract(dpkg_deb_list, NULL, pkg->file);
620                 myfile = freopen(NULL, "w", outfp);
621
622                 printf("done\n");
623                 getchar();
624
625                 fclose(outfp);
626         }
627
628         pkg->status &= status_wantmask;
629         pkg->status |= status_wantinstall;
630         pkg->status &= status_flagmask;
631         pkg->status |= status_flagok;
632         pkg->status &= status_statusmask;
633
634         if (r == 0) {
635                 pkg->status |= status_statusunpacked;
636         } else {
637                 pkg->status |= status_statushalfinstalled;
638         }
639         chdir(cwd);
640         return r;
641 }
642
643 /*
644  * Extract and parse the control.tar.gz from the specified package
645  */
646 static int dpkg_unpackcontrol(package_t *pkg)
647 {
648         char *tmp_name;
649         FILE *file;
650         int length;
651
652         /* clean the temp directory (dpkgcidir) be recreating it */
653         remove_dpkgcidir();
654         if (mkdir(dpkgcidir, S_IRWXU) != 0) {
655                 perror("mkdir");
656                 return EXIT_FAILURE;
657         }
658
659         /*
660          * Get the package name from the file name,
661          * first remove the directories
662          */
663         if ((tmp_name = strrchr(pkg->file, '/')) == NULL) {
664                 tmp_name = pkg->file;
665         } else {
666                 tmp_name++;
667         }
668         /* now remove trailing version numbers etc */
669         length = strcspn(tmp_name, "_.");
670         pkg->package = (char *) xmalloc(length + 1);
671         /* store the package name */
672         strncpy(pkg->package, tmp_name, length);
673
674         /* work out the full extraction path */
675         tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9);
676         memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
677         strcpy(tmp_name, dpkgcidir);
678         strcat(tmp_name, pkg->package);
679
680         /* extract control.tar.gz to the full extraction path */
681         deb_extract(dpkg_deb_control, tmp_name, pkg->file);
682
683         /* parse the extracted control file */
684         strcat(tmp_name, "/control");
685         if ((file = fopen(tmp_name, "r")) == NULL) {
686                 return EXIT_FAILURE;
687         }
688         if (control_read(file, pkg) == EXIT_FAILURE) {
689                 return EXIT_FAILURE;
690         }
691
692         return EXIT_SUCCESS;
693 }
694
695 static int dpkg_unpack(package_t *pkgs, void *status)
696 {
697         int r = 0;
698         package_t *pkg;
699
700         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
701                 if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) {
702                         return EXIT_FAILURE;
703                 }
704                 if ((r = dpkg_dounpack(pkg)) != 0 ) {
705                         break;
706                 }
707         }
708         status_merge(status, pkgs);
709         remove_dpkgcidir();
710
711         return r;
712 }
713
714 static int dpkg_configure(package_t *pkgs, void *status)
715 {
716         int r = 0;
717         void *found;
718         package_t *pkg;
719
720         for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
721                 found = tfind(pkg, &status, package_compare);
722
723                 if (found == 0) {
724                         fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
725                         r = 1;
726                 } 
727                 /* configure the package listed in the status file;
728                  * not pkg, as we have info only for the latter
729                  */
730                 else {
731                         r = dpkg_doconfigure(*(package_t **)found);
732                 }
733         }
734         status_merge(status, 0);
735
736         return r;
737 }
738
739 static int dpkg_install(package_t *pkgs, void *status)
740 {
741         package_t *p, *ordered = 0;
742
743         /* Stage 1: parse all the control information */
744         for (p = pkgs; p != 0; p = p->next) {
745                 if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
746                         perror(p->file);
747                         return EXIT_FAILURE;
748                 }
749         }
750
751         /* Stage 2: resolve dependencies */
752 #ifdef DODEPENDS
753         ordered = depends_resolve(pkgs, status);
754 #else
755         ordered = pkgs;
756 #endif
757         
758         /* Stage 3: install */
759         for (p = ordered; p != 0; p = p->next) {
760                 p->status &= status_wantmask;
761                 p->status |= status_wantinstall;
762
763                 /* for now the flag is always set to ok... this is probably
764                  * not what we want
765                  */
766                 p->status &= status_flagmask;
767                 p->status |= status_flagok;
768
769                 DPRINTF("Installing %s\n", p->package);
770                 if (dpkg_dounpack(p) != 0) {
771                         perror(p->file);
772                 }
773                 if (dpkg_doconfigure(p) != 0) {
774                         perror(p->file);
775                 }
776         }
777         
778         if (ordered != 0) {
779                 status_merge(status, pkgs);
780         }
781         remove_dpkgcidir();
782
783         return 0;
784 }
785
786 static int dpkg_remove(package_t *pkgs, void *status)
787 {
788         package_t *p;
789
790         for (p = pkgs; p != 0; p = p->next)
791         {
792         }
793         status_merge(status, 0);
794
795         return 0;
796 }
797
798 extern int dpkg_main(int argc, char **argv)
799 {
800         char opt = 0;
801         char *s;
802         package_t *p, *packages = NULL;
803         char *cwd = getcwd(0, 0);
804         void *status = NULL;
805
806         while (*++argv) {
807                 if (**argv == '-') {
808                         /* Nasty little hack to "parse" long options. */
809                         s = *argv;
810                         while (*s == '-') {
811                                 s++;
812                         }
813                         opt=s[0];
814                 } else {
815                         p = (package_t *)xmalloc(sizeof(package_t));
816                         memset(p, 0, sizeof(package_t));
817
818                         if (**argv == '/') {
819                                 p->file = *argv;
820                         } else
821                                 if (opt != 'c') {
822                                         p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
823                                         sprintf(p->file, "%s/%s", cwd, *argv);
824                         } else {
825                                 p->package = strdup(*argv);
826                         }
827
828                         p->next = packages;
829                         packages = p;
830                 }               
831         }
832
833         status = status_read();
834
835         switch (opt) {
836                 case 'i':
837                         return dpkg_install(packages, status);
838                 case 'r':
839                         return dpkg_remove(packages, status);
840                 case 'u':
841                         return dpkg_unpack(packages, status);
842                 case 'c':
843                         return dpkg_configure(packages, status);
844                 default :
845                         show_usage();
846                         return EXIT_FAILURE;
847         }
848 }