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