It seems all the stdout munging was useless anyways, since
[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
587         DPRINTF("Unpacking %s\n", pkg->package);
588
589         cwd = getcwd(0, 0);
590         chdir("/");
591         deb_extract(dpkg_deb_extract, "/", pkg->file);
592
593         /* Installs the package scripts into the info directory */
594         for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
595                 /* The full path of the current location of the admin file */
596                 src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
597                 strcpy(src_file, dpkgcidir);
598                 strcat(src_file, pkg->package);
599                 strcat(src_file, adminscripts[i]);
600
601                 /* the full path of where we want the file to be copied to */
602                 dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
603                 strcpy(dst_file, infodir);
604                 strcat(dst_file, pkg->package);
605                 strcat(dst_file, adminscripts[i]);
606
607                 /* copy admin file to permanent home */
608                 if (copy_file(src_file, dst_file, TRUE, FALSE, FALSE) < 0) {
609                         error_msg_and_die("Cannot copy %s to %s ", buf, buf2);
610                 }
611
612                 /* create the list file */
613                 lst_file = (char *) xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
614                 strcpy(lst_file, infodir);
615                 strcat(lst_file, pkg->package);
616                 strcat(lst_file, ".list");
617                 deb_extract(dpkg_deb_list, NULL, pkg->file);
618
619                 printf("done\n");
620                 getchar();
621
622                 fclose(outfp);
623         }
624
625         pkg->status &= status_wantmask;
626         pkg->status |= status_wantinstall;
627         pkg->status &= status_flagmask;
628         pkg->status |= status_flagok;
629         pkg->status &= status_statusmask;
630
631         if (r == 0) {
632                 pkg->status |= status_statusunpacked;
633         } else {
634                 pkg->status |= status_statushalfinstalled;
635         }
636         chdir(cwd);
637         return r;
638 }
639
640 /*
641  * Extract and parse the control.tar.gz from the specified package
642  */
643 static int dpkg_unpackcontrol(package_t *pkg)
644 {
645         char *tmp_name;
646         FILE *file;
647         int length;
648
649         /* clean the temp directory (dpkgcidir) be recreating it */
650         remove_dpkgcidir();
651         if (mkdir(dpkgcidir, S_IRWXU) != 0) {
652                 perror("mkdir");
653                 return EXIT_FAILURE;
654         }
655
656         /*
657          * Get the package name from the file name,
658          * first remove the directories
659          */
660         if ((tmp_name = strrchr(pkg->file, '/')) == NULL) {
661                 tmp_name = pkg->file;
662         } else {
663                 tmp_name++;
664         }
665         /* now remove trailing version numbers etc */
666         length = strcspn(tmp_name, "_.");
667         pkg->package = (char *) xmalloc(length + 1);
668         /* store the package name */
669         strncpy(pkg->package, tmp_name, length);
670
671         /* work out the full extraction path */
672         tmp_name = (char *) xmalloc(strlen(dpkgcidir) + strlen(pkg->package) + 9);
673         memset(tmp_name, 0, strlen(dpkgcidir) + strlen(pkg->package) + 9);
674         strcpy(tmp_name, dpkgcidir);
675         strcat(tmp_name, pkg->package);
676
677         /* extract control.tar.gz to the full extraction path */
678         deb_extract(dpkg_deb_control, tmp_name, pkg->file);
679
680         /* parse the extracted control file */
681         strcat(tmp_name, "/control");
682         if ((file = fopen(tmp_name, "r")) == NULL) {
683                 return EXIT_FAILURE;
684         }
685         if (control_read(file, pkg) == EXIT_FAILURE) {
686                 return EXIT_FAILURE;
687         }
688
689         return EXIT_SUCCESS;
690 }
691
692 static int dpkg_unpack(package_t *pkgs, void *status)
693 {
694         int r = 0;
695         package_t *pkg;
696
697         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
698                 if (dpkg_unpackcontrol(pkg) == EXIT_FAILURE) {
699                         return EXIT_FAILURE;
700                 }
701                 if ((r = dpkg_dounpack(pkg)) != 0 ) {
702                         break;
703                 }
704         }
705         status_merge(status, pkgs);
706         remove_dpkgcidir();
707
708         return r;
709 }
710
711 static int dpkg_configure(package_t *pkgs, void *status)
712 {
713         int r = 0;
714         void *found;
715         package_t *pkg;
716
717         for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
718                 found = tfind(pkg, &status, package_compare);
719
720                 if (found == 0) {
721                         fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
722                         r = 1;
723                 } 
724                 /* configure the package listed in the status file;
725                  * not pkg, as we have info only for the latter
726                  */
727                 else {
728                         r = dpkg_doconfigure(*(package_t **)found);
729                 }
730         }
731         status_merge(status, 0);
732
733         return r;
734 }
735
736 static int dpkg_install(package_t *pkgs, void *status)
737 {
738         package_t *p, *ordered = 0;
739
740         /* Stage 1: parse all the control information */
741         for (p = pkgs; p != 0; p = p->next) {
742                 if (dpkg_unpackcontrol(p) == EXIT_FAILURE) {
743                         perror(p->file);
744                         return EXIT_FAILURE;
745                 }
746         }
747
748         /* Stage 2: resolve dependencies */
749 #ifdef DODEPENDS
750         ordered = depends_resolve(pkgs, status);
751 #else
752         ordered = pkgs;
753 #endif
754         
755         /* Stage 3: install */
756         for (p = ordered; p != 0; p = p->next) {
757                 p->status &= status_wantmask;
758                 p->status |= status_wantinstall;
759
760                 /* for now the flag is always set to ok... this is probably
761                  * not what we want
762                  */
763                 p->status &= status_flagmask;
764                 p->status |= status_flagok;
765
766                 DPRINTF("Installing %s\n", p->package);
767                 if (dpkg_dounpack(p) != 0) {
768                         perror(p->file);
769                 }
770                 if (dpkg_doconfigure(p) != 0) {
771                         perror(p->file);
772                 }
773         }
774         
775         if (ordered != 0) {
776                 status_merge(status, pkgs);
777         }
778         remove_dpkgcidir();
779
780         return 0;
781 }
782
783 static int dpkg_remove(package_t *pkgs, void *status)
784 {
785         package_t *p;
786
787         for (p = pkgs; p != 0; p = p->next)
788         {
789         }
790         status_merge(status, 0);
791
792         return 0;
793 }
794
795 extern int dpkg_main(int argc, char **argv)
796 {
797         char opt = 0;
798         char *s;
799         package_t *p, *packages = NULL;
800         char *cwd = getcwd(0, 0);
801         void *status = NULL;
802
803         while (*++argv) {
804                 if (**argv == '-') {
805                         /* Nasty little hack to "parse" long options. */
806                         s = *argv;
807                         while (*s == '-') {
808                                 s++;
809                         }
810                         opt=s[0];
811                 } else {
812                         p = (package_t *)xmalloc(sizeof(package_t));
813                         memset(p, 0, sizeof(package_t));
814
815                         if (**argv == '/') {
816                                 p->file = *argv;
817                         } else
818                                 if (opt != 'c') {
819                                         p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
820                                         sprintf(p->file, "%s/%s", cwd, *argv);
821                         } else {
822                                 p->package = strdup(*argv);
823                         }
824
825                         p->next = packages;
826                         packages = p;
827                 }               
828         }
829
830         status = status_read();
831
832         switch (opt) {
833                 case 'i':
834                         return dpkg_install(packages, status);
835                 case 'r':
836                         return dpkg_remove(packages, status);
837                 case 'u':
838                         return dpkg_unpack(packages, status);
839                 case 'c':
840                         return dpkg_configure(packages, status);
841                 default :
842                         show_usage();
843                         return EXIT_FAILURE;
844         }
845 }