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