blob: 996809a6ffa9e44a6327fb5b80fd4fe56bbc9c07 [file] [log] [blame]
Glenn L McGrath0e757a22001-04-08 05:27:18 +00001#include <dirent.h>
2#include <getopt.h>
Glenn L McGrathc9005752001-02-10 02:05:24 +00003#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>
Glenn L McGrath649968c2001-02-10 14:26:48 +000013
Glenn L McGrathc9005752001-02-10 02:05:24 +000014#include "busybox.h"
15
Eric Andersen3c0b4252001-03-14 01:31:11 +000016
Glenn L McGrath3af1f882001-02-12 11:33:09 +000017#define DEPENDSMAX 64 /* maximum number of depends we can handle */
Glenn L McGrathc9005752001-02-10 02:05:24 +000018
Glenn L McGrath3af1f882001-02-12 11:33:09 +000019/* Should we do full dependency checking? */
Glenn L McGrathae1c7042001-04-16 10:26:46 +000020//#define DODEPENDS 0
Glenn L McGrathc9005752001-02-10 02:05:24 +000021
Glenn L McGrath3af1f882001-02-12 11:33:09 +000022/* Should we do debugging? */
Glenn L McGrathae1c7042001-04-16 10:26:46 +000023//#define DODEBUG 0
Glenn L McGrathc9005752001-02-10 02:05:24 +000024
25#ifdef DODEBUG
Glenn L McGrathc9005752001-02-10 02:05:24 +000026#define SYSTEM(x) do_system(x)
27#define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
28#else
Glenn L McGrathc9005752001-02-10 02:05:24 +000029#define SYSTEM(x) system(x)
30#define DPRINTF(fmt,args...) /* nothing */
31#endif
32
Glenn L McGrath3af1f882001-02-12 11:33:09 +000033/* from dpkg-deb.c */
Glenn L McGrathd22e5602001-04-11 02:12:08 +000034
Glenn L McGrath63106462001-02-11 01:40:23 +000035static const char statusfile[] = "/var/lib/dpkg/status.udeb";
36static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
37static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
38
Glenn L McGrath510f0dd2001-02-10 14:53:08 +000039static const char infodir[] = "/var/lib/dpkg/info/";
40static const char udpkg_quiet[] = "UDPKG_QUIET";
Glenn L McGrathc9005752001-02-10 02:05:24 +000041
Glenn L McGrath305fdfa2001-04-08 13:27:39 +000042//static const int status_want_unknown = 1;
Glenn L McGrath33431eb2001-04-16 04:52:19 +000043static 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;
Glenn L McGrathc9005752001-02-10 02:05:24 +000047
Glenn L McGrath33431eb2001-04-16 04:52:19 +000048static 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;
Glenn L McGrathc9005752001-02-10 02:05:24 +000052
Glenn L McGrath33431eb2001-04-16 04:52:19 +000053//static const int state_statusnoninstalled = 1;
54static const int state_status_unpacked = 2;
55static const int state_status_halfconfigured = 3;
56static const int state_status_installed = 4;
57static 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;
Glenn L McGrathc9005752001-02-10 02:05:24 +000061
Glenn L McGrath33431eb2001-04-16 04:52:19 +000062static const char *state_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 };
63static const char *state_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 };
64static const char *state_words_status[] = { "not-installed", "unpacked", "half-configured", "installed",
Glenn L McGrath305fdfa2001-04-08 13:27:39 +000065 "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 };
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +000066
Eric Andersen3e6ff902001-03-09 21:24:12 +000067static const int color_white = 0;
68static const int color_grey = 1;
69static const int color_black = 2;
Glenn L McGrathc9005752001-02-10 02:05:24 +000070
Glenn L McGrath33431eb2001-04-16 04:52:19 +000071/* data structures */
Glenn L McGrath649968c2001-02-10 14:26:48 +000072typedef struct package_s {
Glenn L McGrath33431eb2001-04-16 04:52:19 +000073 char *filename;
Glenn L McGrathc9005752001-02-10 02:05:24 +000074 char *package;
Glenn L McGrath33431eb2001-04-16 04:52:19 +000075 unsigned char state_want;
76 unsigned char state_flag;
77 unsigned char state_status;
Glenn L McGrathc9005752001-02-10 02:05:24 +000078 char *depends;
79 char *provides;
80 char *description;
Glenn L McGrath33431eb2001-04-16 04:52:19 +000081 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;
Glenn L McGrathc9005752001-02-10 02:05:24 +000096 int installer_menu_item;
Glenn L McGrathc9005752001-02-10 02:05:24 +000097 char color; /* for topo-sort */
Glenn L McGrath649968c2001-02-10 14:26:48 +000098 struct package_s *requiredfor[DEPENDSMAX];
Glenn L McGrathc9005752001-02-10 02:05:24 +000099 unsigned short requiredcount;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000100 struct package_s *next;
101} package_t;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000102
Glenn L McGrath63106462001-02-11 01:40:23 +0000103#ifdef DODEBUG
104static 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
Glenn L McGrath649968c2001-02-10 14:26:48 +0000113static int package_compare(const void *p1, const void *p2)
114{
115 return strcmp(((package_t *)p1)->package,
116 ((package_t *)p2)->package);
117}
Glenn L McGrathc9005752001-02-10 02:05:24 +0000118
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000119/*
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 */
124static int remove_dir(const char *dirname)
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000125{
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000126 struct dirent *fp;
127 DIR *dp = opendir(dirname);
128 while ((fp = readdir(dp)) != NULL) {
129 struct stat statbuf;
130 char *filename;
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000131
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000132 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);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000136
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000137 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 }
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000145 }
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000146 remove(dirname);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000147 return EXIT_SUCCESS;
148}
149
Glenn L McGrathc9005752001-02-10 02:05:24 +0000150#ifdef DODEPENDS
151#include <ctype.h>
152
153static char **depends_split(const char *dependsstr)
154{
155 static char *dependsvec[DEPENDSMAX];
156 char *p;
157 int i = 0;
158
159 dependsvec[0] = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000160 if (dependsstr == 0) {
161 goto end;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000162 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000163
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000164 p = xstrdup(dependsstr);
Glenn L McGrath63106462001-02-11 01:40:23 +0000165 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
182end:
Glenn L McGrathc9005752001-02-10 02:05:24 +0000183 dependsvec[i+1] = 0;
184 return dependsvec;
185}
186
Glenn L McGrath63106462001-02-11 01:40:23 +0000187/* 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 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000197static void depends_sort_visit(package_t **ordered, package_t *pkgs,
198 package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000199{
Glenn L McGrathc9005752001-02-10 02:05:24 +0000200 unsigned short i;
201
202 /* mark node as processing */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000203 pkg->color = color_grey;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000204
205 /* visit each not-yet-visited node */
206 for (i = 0; i < pkg->requiredcount; i++)
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000207 if (pkg->requiredfor[i]->color == color_white)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000208 depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
209
210#if 0
211 /* add it to the list */
Eric Andersenb50d7072001-02-15 19:50:11 +0000212 newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
Glenn L McGrathc9005752001-02-10 02:05:24 +0000213 /* make a shallow copy */
214 *newnode = *pkg;
215 newnode->next = *ordered;
216 *ordered = newnode;
217#endif
Glenn L McGrath63106462001-02-11 01:40:23 +0000218
Glenn L McGrathc9005752001-02-10 02:05:24 +0000219 pkg->next = *ordered;
220 *ordered = pkg;
221
222 /* mark node as done */
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000223 pkg->color = color_black;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000224}
225
Glenn L McGrath649968c2001-02-10 14:26:48 +0000226static package_t *depends_sort(package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000227{
228 /* TODO: it needs to break cycles in the to-be-installed package
229 * graph... */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000230 package_t *ordered = NULL;
231 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000232
Glenn L McGrath63106462001-02-11 01:40:23 +0000233 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathaf8c65d2001-02-10 03:19:51 +0000234 pkg->color = color_white;
Glenn L McGrath63106462001-02-11 01:40:23 +0000235 }
236 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
237 if (pkg->color == color_white) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000238 depends_sort_visit(&ordered, pkgs, pkg);
Glenn L McGrath63106462001-02-11 01:40:23 +0000239 }
240 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000241
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 */
Glenn L McGrath649968c2001-02-10 14:26:48 +0000258static package_t *depends_resolve(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000259{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000260 package_t *pkg, *chk;
261 package_t dependpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000262 char **dependsvec;
263 int i;
264 void *found;
265
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000266 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000267 dependsvec = depends_split(pkg->depends);
268 i = 0;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000269 while (dependsvec[i] != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000270 /* Check for dependencies; first look for installed packages */
271 dependpkg.package = dependsvec[i];
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000272 if (((found = tfind(&dependpkg, &status, package_compare)) == 0) ||
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000273 ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) &&
274 (chk->state_status & state_status_installed))) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000275
Glenn L McGrathc9005752001-02-10 02:05:24 +0000276 /* if it fails, we look through the list of packages we are going to
277 * install */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000278 for (chk = pkgs; chk != 0; chk = chk->next) {
Glenn L McGrath63106462001-02-11 01:40:23 +0000279 if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides &&
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000280 strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
281 if (chk->requiredcount >= DEPENDSMAX) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000282 error_msg("Too many dependencies for %s", chk->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000283 return 0;
284 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000285 if (chk != pkg) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000286 chk->requiredfor[chk->requiredcount++] = pkg;
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000287 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000288 break;
289 }
290 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000291 if (chk == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000292 error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000293 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 */
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000318static unsigned char status_parse(const char *line, const char **status_words)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000319{
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000320 unsigned char status_num;
321 int i = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000322
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000323 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);
Glenn L McGrath63106462001-02-11 01:40:23 +0000327 }
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000328 i++;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000329 }
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000330 /* parse error */
331 error_msg("Invalid status word");
332 return(0);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000333}
334
335/*
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000336 * Read the buffered control file and parse it,
Glenn L McGrathc9005752001-02-10 02:05:24 +0000337 * filling parsed fields into the package structure
338 */
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000339static int fill_package_struct(package_t *package, const char *package_buffer)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000340{
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000341 char *field = NULL;
342 int field_start = 0;
343 int field_length = 0;
Glenn L McGrathae1c7042001-04-16 10:26:46 +0000344
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000345 while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
346 field_length = strlen(field);
347 field_start += (field_length + 1);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000348
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000349 if (strlen(field) == 0) {
350 printf("empty line: *this shouldnt happen i dont think*\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000351 break;
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000352 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000353
354 /* these are common to both installed and uninstalled packages */
355 if (strstr(field, "Package: ") == field) {
356 package->package = strdup(field + 9);
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000357 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000358 else if (strstr(field, "Depends: ") == field) {
359 package->depends = strdup(field + 9);
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000360 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000361 else if (strstr(field, "Provides: ") == field) {
362 package->provides = strdup(field + 10);
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000363 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000364 /* This is specific to the Debian Installer. Ifdef? */
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000365 else if (strstr(field, "installer-menu-item: ") == field) {
366 package->installer_menu_item = atoi(field + 21);
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000367 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000368 else if (strstr(field, "Description: ") == field) {
369 package->description = strdup(field + 13);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000370 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000371 else if (strstr(field, "Priority: ") == field) {
372 package->priority = strdup(field + 10);
373 }
374 else if (strstr(field, "Section: ") == field) {
375 package->section = strdup(field + 9);
376 }
377 else if (strstr(field, "Installed-Size: ") == field) {
378 package->installed_size = strdup(field + 16);
379 }
380 else if (strstr(field, "Maintainer: ") == field) {
381 package->maintainer = strdup(field + 12);
382 }
383 else if (strstr(field, "Version: ") == field) {
384 package->version = strdup(field + 9);
385 }
386 else if (strstr(field, "Suggests: ") == field) {
387 package->suggests = strdup(field + 10);
388 }
389 else if (strstr(field, "Recommends: ") == field) {
390 package->recommends = strdup(field + 12);
391 }
392/* else if (strstr(field, "Conffiles: ") == field) {
393 package->conffiles = read_block(file);
394 package->conffiles = xcalloc(1, 1);
395 while ((field = strtok(NULL, "\n")) != NULL) {
396 package->long_description = xrealloc(package->conffiles,
397 strlen(package->conffiles) + strlen(field) + 1);
398 strcat(package->conffiles, field);
399 }
400 }
401*/
402 /* These are only in available file */
403 else if (strstr(field, "Architecture: ") == field) {
404 package->architecture = strdup(field + 14);
405 }
406 else if (strstr(field, "Filename: ") == field) {
407 package->filename = strdup(field + 10);
408 }
409 else if (strstr(field, "MD5sum ") == field) {
410 package->md5sum = strdup(field + 7);
411 }
412
413 /* This is only needed for status file */
414 if (strstr(field, "Status: ") == field) {
415 char *word_pointer;
416
417 word_pointer = strchr(field, ' ') + 1;
418 package->state_want = status_parse(word_pointer, state_words_want);
419 word_pointer = strchr(word_pointer, ' ') + 1;
420 package->state_flag = status_parse(word_pointer, state_words_flag);
421 word_pointer = strchr(word_pointer, ' ') + 1;
422 package->state_status = status_parse(word_pointer, state_words_status);
423 } else {
424 package->state_want = status_parse("purge", state_words_want);
425 package->state_flag = status_parse("ok", state_words_flag);
426 package->state_status = status_parse("not-installed", state_words_status);
427 }
428
429 free(field);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000430 }
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000431 return EXIT_SUCCESS;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000432}
433
Glenn L McGrathae1c7042001-04-16 10:26:46 +0000434extern void write_package(FILE *out_file, package_t *pkg)
435{
436 if (pkg->package) {
437 fprintf(out_file, "Package: %s\n", pkg->package);
438 }
439 if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) {
440 fprintf(out_file, "Status: %s %s %s\n",
441 state_words_want[pkg->state_want - 1],
442 state_words_flag[pkg->state_flag - 1],
443 state_words_status[pkg->state_status - 1]);
444 }
445 if (pkg->depends) {
446 fprintf(out_file, "Depends: %s\n", pkg->depends);
447 }
448 if (pkg->provides) {
449 fprintf(out_file, "Provides: %s\n", pkg->provides);
450 }
451 if (pkg->priority) {
452 fprintf(out_file, "Priority: %s\n", pkg->priority);
453 }
454 if (pkg->section) {
455 fprintf(out_file, "Section: %s\n", pkg->section);
456 }
457 if (pkg->section) {
458 fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size);
459 }
460 if (pkg->maintainer) {
461 fprintf(out_file, "Maintainer: %s\n", pkg->maintainer);
462 }
463 if (pkg->source) {
464 fprintf(out_file, "Source: %s\n", pkg->source);
465 }
466 if (pkg->version) {
467 fprintf(out_file, "Version: %s\n", pkg->version);
468 }
469 if (pkg->pre_depends) {
470 fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends);
471 }
472 if (pkg->replaces) {
473 fprintf(out_file, "Replaces: %s\n", pkg->replaces);
474 }
475 if (pkg->recommends) {
476 fprintf(out_file, "Recommends: %s\n", pkg->recommends);
477 }
478 if (pkg->suggests) {
479 fprintf(out_file, "Suggests: %s\n", pkg->suggests);
480 }
481 if (pkg->conflicts) {
482 fprintf(out_file, "Conflicts: %s\n", pkg->conflicts);
483 }
484 if (pkg->conffiles) {
485 fprintf(out_file, "Conf-files: %s\n", pkg->conffiles);
486 }
487 if (pkg->architecture) {
488 fprintf(out_file, "Architecture: %s\n", pkg->architecture);
489 }
490 if (pkg->filename) {
491 fprintf(out_file, "Filename: %s\n", pkg->filename);
492 }
493 if (pkg->md5sum) {
494 fprintf(out_file, "MD5sum: %s\n", pkg->md5sum);
495 }
496 if (pkg->installer_menu_item) {
497 fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item);
498 }
499 if (pkg->description) {
500 fprintf(out_file, "Description: %s\n", pkg->description);
501 }
502 fputc('\n', out_file);
503 pkg = pkg->next;
504}
505
Glenn L McGrath649968c2001-02-10 14:26:48 +0000506static void *status_read(void)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000507{
508 FILE *f;
509 void *status = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000510 package_t *m = 0, *p = 0, *t = 0;
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000511 char *package_control_buffer = NULL;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000512
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000513 if (getenv(udpkg_quiet) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000514 printf("(Reading database...)\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000515 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000516
Eric Andersene5dfced2001-04-09 22:48:12 +0000517 if ((f = wfopen(statusfile, "r")) == NULL) {
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000518 return(NULL);
519 }
520
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000521 while ( (package_control_buffer = read_text_file_to_buffer(f)) != NULL) {
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000522 m = (package_t *)xcalloc(1, sizeof(package_t));
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000523 fill_package_struct(m, package_control_buffer);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000524 if (m->package) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000525 /*
526 * If there is an item in the tree by this name,
527 * it must be a virtual package; insert real
528 * package in preference.
529 */
530 tdelete(m, &status, package_compare);
531 tsearch(m, &status, package_compare);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000532 if (m->provides) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000533 /*
534 * A "Provides" triggers the insertion
535 * of a pseudo package into the status
536 * binary-tree.
537 */
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000538 p = (package_t *)xcalloc(1, sizeof(package_t));
539 p->package = xstrdup(m->provides);
Glenn L McGrath649968c2001-02-10 14:26:48 +0000540 t = *(package_t **)tsearch(p, &status, package_compare);
Glenn L McGrath63106462001-02-11 01:40:23 +0000541 if (t != p) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000542 free(p->package);
543 free(p);
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000544 } else {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000545 /*
546 * Pseudo package status is the
547 * same as the status of the
548 * package providing it
549 * FIXME: (not quite right, if 2
550 * packages of different statuses
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000551 * provide it).
552 */
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000553 t->state_want = m->state_want;
554 t->state_flag = m->state_flag;
555 t->state_status = m->state_status;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000556 }
557 }
558 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000559 else {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000560 free(m);
561 }
562 }
563 fclose(f);
564 return status;
565}
566
Glenn L McGrath649968c2001-02-10 14:26:48 +0000567static int status_merge(void *status, package_t *pkgs)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000568{
569 FILE *fin, *fout;
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000570 char *line = NULL;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000571 package_t *pkg = 0, *statpkg = 0;
572 package_t locpkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000573
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000574 if ((fout = wfopen(new_statusfile, "w")) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000575 return 0;
576 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000577 if (getenv(udpkg_quiet) == NULL) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000578 printf("(Updating database...)\n");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000579 }
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000580
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000581 /*
582 * Dont use wfopen here, handle errors ourself
583 */
584 if ((fin = fopen(statusfile, "r")) != NULL) {
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000585 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
Eric Andersenc1bdffe2001-04-26 15:56:47 +0000586 chomp(line); /* trim newline */
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000587 /* If we see a package header, find out if it's a package
588 * that we have processed. if so, we skip that block for
589 * now (write it at the end).
590 *
591 * we also look at packages in the status cache and update
592 * their status fields
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000593 */
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000594 if (strstr(line, "Package: ") == line) {
595 for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
596 pkg->package) != 0; pkg = pkg->next) ;
Glenn L McGrath63106462001-02-11 01:40:23 +0000597
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000598 locpkg.package = line + 9;
599 statpkg = tfind(&locpkg, &status, package_compare);
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000600
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000601 /* note: statpkg should be non-zero, unless the status
602 * file was changed while we are processing (no locking
603 * is currently done...
604 */
605 if (statpkg != 0) {
606 statpkg = *(package_t **)statpkg;
607 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000608 }
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000609 if (pkg != 0) {
610 continue;
611 }
612 if (strstr(line, "Status: ") == line && statpkg != 0) {
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000613 snprintf(line, sizeof(line), "Status: %s %s %s",
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000614 state_words_want[statpkg->state_want - 1],
615 state_words_flag[statpkg->state_flag - 1],
616 state_words_status[statpkg->state_status - 1]);
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000617 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000618 fprintf(fout, "%s\n", line);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000619 }
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000620 fclose(fin);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000621 }
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000622 free(line);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000623
624 // Print out packages we processed.
625 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrathae1c7042001-04-16 10:26:46 +0000626 write_package(fout, pkg);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000627 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000628 fclose(fout);
629
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000630 /*
631 * Its ok if renaming statusfile fails becasue it doesnt exist
632 */
633 if (rename(statusfile, bak_statusfile) == -1) {
634 struct stat stat_buf;
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000635 if (stat(statusfile, &stat_buf) == 0) {
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000636 error_msg("Couldnt create backup status file");
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000637 return(EXIT_FAILURE);
638 }
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000639 error_msg("No status file found, creating new one");
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000640 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000641
Glenn L McGrath13e9c7a2001-04-08 07:18:08 +0000642 if (rename(new_statusfile, statusfile) == -1) {
643 error_msg("Couldnt create status file");
644 return(EXIT_FAILURE);
645 }
646 return(EXIT_SUCCESS);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000647}
648
Glenn L McGrathc9005752001-02-10 02:05:24 +0000649static int is_file(const char *fn)
650{
651 struct stat statbuf;
652
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000653 if (stat(fn, &statbuf) < 0) {
654 return 0;
655 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000656 return S_ISREG(statbuf.st_mode);
657}
658
Glenn L McGrath649968c2001-02-10 14:26:48 +0000659static int dpkg_doconfigure(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000660{
661 int r;
662 char postinst[1024];
663 char buf[1024];
Glenn L McGrath63106462001-02-11 01:40:23 +0000664
Glenn L McGrathc9005752001-02-10 02:05:24 +0000665 DPRINTF("Configuring %s\n", pkg->package);
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000666 pkg->state_status = 0;
Glenn L McGrath510f0dd2001-02-10 14:53:08 +0000667 snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
Glenn L McGrath63106462001-02-11 01:40:23 +0000668
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000669 if (is_file(postinst)) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000670 snprintf(buf, sizeof(buf), "%s configure", postinst);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000671 if ((r = do_system(buf)) != 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000672 error_msg("postinst exited with status %d\n", r);
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000673 pkg->state_status = state_status_halfconfigured;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000674 return 1;
675 }
676 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000677 pkg->state_status = state_status_installed;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000678
679 return 0;
680}
681
Glenn L McGrath649968c2001-02-10 14:26:48 +0000682static int dpkg_dounpack(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000683{
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000684 int r = 0;
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000685 int status = TRUE;
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000686 char *lst_path;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000687
688 DPRINTF("Unpacking %s\n", pkg->package);
689
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000690 /* extract the data file */
691 deb_extract(pkg->filename, extract_extract, "/", NULL);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000692
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000693 /* extract the control files */
694 deb_extract(pkg->filename, extract_control, infodir, pkg->package);
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000695
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000696 /* Create the list file */
697 lst_path = xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
698 strcpy(lst_path, infodir);
699 strcat(lst_path, pkg->package);
700 strcat(lst_path, ".list");
701 deb_extract(pkg->filename, extract_contents_to_file, lst_path, NULL);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000702
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000703 pkg->state_want = state_want_install;
704 pkg->state_flag = state_flag_ok;
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000705
706 if (status == TRUE) {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000707 pkg->state_status = state_status_unpacked;
Glenn L McGrath63106462001-02-11 01:40:23 +0000708 } else {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000709 pkg->state_status = state_status_halfinstalled;
Glenn L McGrath63106462001-02-11 01:40:23 +0000710 }
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000711
Glenn L McGrathc9005752001-02-10 02:05:24 +0000712 return r;
713}
714
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000715/*
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000716 * Extract and parse the control file from control.tar.gz
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000717 */
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000718static int dpkg_read_control(package_t *pkg)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000719{
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000720 FILE *pkg_file;
721 char *control_buffer = NULL;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000722
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000723 if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000724 return EXIT_FAILURE;
Glenn L McGrath63106462001-02-11 01:40:23 +0000725 }
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000726 control_buffer = deb_extract(pkg->filename, extract_field, NULL, NULL);
727 fill_package_struct(pkg, control_buffer);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000728 return EXIT_SUCCESS;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000729}
730
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000731static int dpkg_unpack(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000732{
733 int r = 0;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000734 package_t *pkg;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000735
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000736 for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000737 dpkg_read_control(pkg);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000738 if ((r = dpkg_dounpack(pkg)) != 0 ) {
739 break;
740 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000741 }
742 status_merge(status, pkgs);
Glenn L McGrath63106462001-02-11 01:40:23 +0000743
Glenn L McGrathc9005752001-02-10 02:05:24 +0000744 return r;
745}
746
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000747static int dpkg_configure(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000748{
749 int r = 0;
750 void *found;
Glenn L McGrath649968c2001-02-10 14:26:48 +0000751 package_t *pkg;
Glenn L McGrath63106462001-02-11 01:40:23 +0000752
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000753 for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000754 found = tfind(pkg, &status, package_compare);
Glenn L McGrath63106462001-02-11 01:40:23 +0000755
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000756 if (found == 0) {
Eric Andersene5dfced2001-04-09 22:48:12 +0000757 error_msg("Trying to configure %s, but it is not installed", pkg->package);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000758 r = 1;
Glenn L McGrath63106462001-02-11 01:40:23 +0000759 }
760 /* configure the package listed in the status file;
761 * not pkg, as we have info only for the latter
762 */
763 else {
Glenn L McGrath649968c2001-02-10 14:26:48 +0000764 r = dpkg_doconfigure(*(package_t **)found);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000765 }
766 }
767 status_merge(status, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000768
Glenn L McGrathc9005752001-02-10 02:05:24 +0000769 return r;
770}
771
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000772static int dpkg_install(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000773{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000774 package_t *p, *ordered = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000775
Glenn L McGrathc9005752001-02-10 02:05:24 +0000776 /* Stage 1: parse all the control information */
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000777 for (p = pkgs; p != 0; p = p->next) {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000778 dpkg_read_control(p);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000779 }
780
Glenn L McGrathc9005752001-02-10 02:05:24 +0000781 /* Stage 2: resolve dependencies */
782#ifdef DODEPENDS
783 ordered = depends_resolve(pkgs, status);
784#else
785 ordered = pkgs;
786#endif
787
788 /* Stage 3: install */
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000789 for (p = ordered; p != 0; p = p->next) {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000790 p->state_want = state_want_install;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000791
792 /* for now the flag is always set to ok... this is probably
793 * not what we want
794 */
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000795 p->state_flag = state_flag_ok;
Glenn L McGrathc9005752001-02-10 02:05:24 +0000796
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000797 DPRINTF("Installing %s\n", p->package);
798 if (dpkg_dounpack(p) != 0) {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000799 perror_msg(p->filename);
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000800 }
Glenn L McGrath305fdfa2001-04-08 13:27:39 +0000801
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000802 if (dpkg_doconfigure(p) != 0) {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000803 perror_msg(p->filename);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000804 }
805 }
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000806
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000807 if (ordered != 0) {
Glenn L McGrathc9005752001-02-10 02:05:24 +0000808 status_merge(status, pkgs);
Glenn L McGrath0c9d77c2001-02-11 00:17:22 +0000809 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000810
Glenn L McGrathc9005752001-02-10 02:05:24 +0000811 return 0;
812}
813
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000814/*
815 * Not implemented yet
816 *
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000817static int dpkg_remove(package_t *pkgs, void *status)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000818{
Glenn L McGrath649968c2001-02-10 14:26:48 +0000819 package_t *p;
Glenn L McGrath63106462001-02-11 01:40:23 +0000820
Glenn L McGrathc9005752001-02-10 02:05:24 +0000821 for (p = pkgs; p != 0; p = p->next)
822 {
823 }
824 status_merge(status, 0);
Glenn L McGrath63106462001-02-11 01:40:23 +0000825
Glenn L McGrathc9005752001-02-10 02:05:24 +0000826 return 0;
827}
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000828*/
Glenn L McGrathc9005752001-02-10 02:05:24 +0000829
Glenn L McGrath649968c2001-02-10 14:26:48 +0000830extern int dpkg_main(int argc, char **argv)
Glenn L McGrathc9005752001-02-10 02:05:24 +0000831{
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000832 const int arg_install = 1;
833 const int arg_unpack = 2;
834 const int arg_configure = 4;
835
Glenn L McGrath649968c2001-02-10 14:26:48 +0000836 package_t *p, *packages = NULL;
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000837 void *status = NULL;
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000838 char opt = 0;
839 int optflag = 0;
Glenn L McGrath63106462001-02-11 01:40:23 +0000840
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000841 while ((opt = getopt(argc, argv, "iruc")) != -1) {
842 switch (opt) {
843 case 'i':
844 optflag |= arg_install;
845 break;
846 case 'u':
847 optflag |= arg_unpack;
848 break;
849 case 'c':
850 optflag |= arg_configure;
851 break;
852 default:
853 show_usage();
854 }
Glenn L McGrathc9005752001-02-10 02:05:24 +0000855 }
Glenn L McGrath63106462001-02-11 01:40:23 +0000856
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000857 while (optind < argc) {
858 p = (package_t *) xcalloc(1, sizeof(package_t));
859 if (optflag & arg_configure) {
860 p->package = xstrdup(argv[optind]);
861 } else {
Glenn L McGrath33431eb2001-04-16 04:52:19 +0000862 p->filename = xstrdup(argv[optind]);
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000863 }
864 p->next = packages;
865 packages = p;
866
867 optind++;
868 }
869
Glenn L McGrath37849f32001-04-08 07:23:53 +0000870 create_path(infodir, S_IRWXU);
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000871
Glenn L McGrath3af1f882001-02-12 11:33:09 +0000872 status = status_read();
873
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000874 if (optflag & arg_install) {
875 return dpkg_install(packages, status);
Glenn L McGrathc9005752001-02-10 02:05:24 +0000876 }
Glenn L McGrath0e757a22001-04-08 05:27:18 +0000877 else if (optflag & arg_unpack) {
878 return dpkg_unpack(packages, status);
879 }
880 else if (optflag & arg_configure) {
881 return dpkg_configure(packages, status);
882 }
883 return(EXIT_FAILURE);
Eric Andersen67991cf2001-02-14 21:23:06 +0000884}