fix oversized packets in udhcpc (closes: #962), bump busybox release number in whiter...
[openwrt.git] / package / busybox / patches / 911-ipkg.patch
1 diff -ruN busybox-1.2.0-orig/archival/Config.in busybox-1.2.0+ipkg-0.99.162/archival/Config.in
2 --- busybox-1.2.0-orig/archival/Config.in       2006-07-01 00:42:04.000000000 +0200
3 +++ busybox-1.2.0+ipkg-0.99.162/archival/Config.in      2006-07-22 16:31:25.000000000 +0200
4 @@ -121,6 +121,14 @@
5           gzip is used to compress files.
6           It's probably the most widely used UNIX compression program.
7  
8 +config CONFIG_IPKG
9 +       bool "ipkg"
10 +       default n
11 +       select CONFIG_MD5SUM
12 +       select CONFIG_WGET
13 +       help
14 +         ipkg is the itsy package management system.
15 +       
16  config CONFIG_RPM2CPIO
17         bool "rpm2cpio"
18         default n
19 diff -ruN busybox-1.2.0-orig/archival/dpkg.c busybox-1.2.0+ipkg-0.99.162/archival/dpkg.c
20 --- busybox-1.2.0-orig/archival/dpkg.c  2006-07-01 00:42:04.000000000 +0200
21 +++ busybox-1.2.0+ipkg-0.99.162/archival/dpkg.c 2006-07-22 16:31:25.000000000 +0200
22 @@ -1430,6 +1430,10 @@
23         return(ar_handle->sub_archive->buffer);
24  }
25  
26 +/*
27 +
28 +// moved to data_extract_all.c
29 +
30  static void data_extract_all_prefix(archive_handle_t *archive_handle)
31  {
32         char *name_ptr = archive_handle->file_header->name;
33 @@ -1442,6 +1446,8 @@
34         return;
35  }
36  
37 +*/
38 +
39  static void unpack_package(deb_file_t *deb_file)
40  {
41         const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
42 diff -ruN busybox-1.2.0-orig/archival/ipkg.c busybox-1.2.0+ipkg-0.99.162/archival/ipkg.c
43 --- busybox-1.2.0-orig/archival/ipkg.c  1970-01-01 01:00:00.000000000 +0100
44 +++ busybox-1.2.0+ipkg-0.99.162/archival/ipkg.c 2006-07-22 16:31:25.000000000 +0200
45 @@ -0,0 +1,26 @@
46 +/* ipkg.c - the itsy package management system
47 +
48 +   Florina Boor
49 +
50 +   Copyright (C) 2003 kernel concepts
51 +
52 +   This program is free software; you can redistribute it and/or
53 +   modify it under the terms of the GNU General Public License as
54 +   published by the Free Software Foundation; either version 2, or (at
55 +   your option) any later version.
56 +
57 +   This program is distributed in the hope that it will be useful, but
58 +   WITHOUT ANY WARRANTY; without even the implied warranty of
59 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
60 +   General Public License for more details.
61 +   
62 +   ipkg command line frontend using libipkg
63 +   
64 +*/
65 +
66 +#include "libipkg/libipkg.h"
67 +
68 +int ipkg_main(int argc, char **argv)
69 +{
70 +       return ipkg_op(argc, argv);
71 +}
72 diff -ruN busybox-1.2.0-orig/archival/libipkg/args.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.c
73 --- busybox-1.2.0-orig/archival/libipkg/args.c  1970-01-01 01:00:00.000000000 +0100
74 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.c 2006-07-22 16:31:25.000000000 +0200
75 @@ -0,0 +1,242 @@
76 +/* args.c - parse command-line args
77
78 +  Carl D. Worth
79 +
80 +  Copyright 2001 University of Southern California
81
82 +  This program is free software; you can redistribute it and/or modify
83 +  it under the terms of the GNU General Public License as published by
84 +  the Free Software Foundation; either version 2, or (at your option)
85 +  any later version.
86
87 +  This program is distributed in the hope that it will be useful,
88 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
89 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
90 +  GNU General Public License for more details.
91 + */
92 +
93 +#include <getopt.h>
94 +#include <stdlib.h>
95 +#include <string.h>
96 +#include <unistd.h>
97 +
98 +#include "ipkg.h"
99 +#include "ipkg_message.h"
100 +
101 +#include "args.h"
102 +#include "sprintf_alloc.h"
103 +
104 +#include "libbb.h"
105 +
106 +
107 +static void print_version(void);
108 +
109 +enum long_args_opt
110 +{
111 +     ARGS_OPT_FORCE_DEFAULTS = 129,
112 +     ARGS_OPT_FORCE_DEPENDS,
113 +     ARGS_OPT_FORCE_OVERWRITE,
114 +     ARGS_OPT_FORCE_DOWNGRADE,
115 +     ARGS_OPT_FORCE_REINSTALL,
116 +     ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
117 +     ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
118 +     ARGS_OPT_FORCE_SPACE,
119 +     ARGS_OPT_NOACTION,
120 +     ARGS_OPT_NODEPS,
121 +     ARGS_OPT_VERBOSE_WGET,
122 +     ARGS_OPT_VERBOSITY,
123 +     ARGS_OPT_MULTIPLE_PROVIDERS
124 +};
125 +
126 +int args_init(args_t *args)
127 +{
128 +     char *conf_file_dir;
129 +
130 +     memset(args, 0, sizeof(args_t));
131 +
132 +     args->dest = ARGS_DEFAULT_DEST;
133 +
134 +     conf_file_dir = getenv("IPKG_CONF_DIR");
135 +     if (conf_file_dir == NULL || conf_file_dir[0] == '\0') {
136 +         conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
137 +     }
138 +     sprintf_alloc(&args->conf_file, "%s/%s", conf_file_dir,
139 +                  ARGS_DEFAULT_CONF_FILE_NAME);
140 +
141 +     args->force_defaults = ARGS_DEFAULT_FORCE_DEFAULTS;
142 +     args->force_depends = ARGS_DEFAULT_FORCE_DEPENDS;
143 +     args->force_overwrite = ARGS_DEFAULT_FORCE_OVERWRITE;
144 +     args->force_downgrade = ARGS_DEFAULT_FORCE_DOWNGRADE;
145 +     args->force_reinstall = ARGS_DEFAULT_FORCE_REINSTALL;
146 +     args->force_removal_of_dependent_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES;
147 +     args->force_removal_of_essential_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES;
148 +     args->noaction = ARGS_DEFAULT_NOACTION;
149 +     args->nodeps = ARGS_DEFAULT_NODEPS;
150 +     args->verbose_wget = ARGS_DEFAULT_VERBOSE_WGET;
151 +     args->verbosity = ARGS_DEFAULT_VERBOSITY;
152 +     args->offline_root = ARGS_DEFAULT_OFFLINE_ROOT;
153 +     args->offline_root_pre_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD;
154 +     args->offline_root_post_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD;
155 +     args->multiple_providers = 0;
156 +     args->nocheckfordirorfile = 0;
157 +     args->noreadfeedsfile = 0;
158 +
159 +     return 1;
160 +}
161 +
162 +void args_deinit(args_t *args)
163 +{
164 +     free(args->conf_file);
165 +     args->conf_file = NULL;
166 +}
167 +
168 +int args_parse(args_t *args, int argc, char *argv[])
169 +{
170 +     int c;
171 +     int option_index = 0;
172 +     int parse_err = 0;
173 +     static struct option long_options[] = {
174 +         {"query-all", 0, 0, 'A'},
175 +         {"conf-file", 1, 0, 'f'},
176 +         {"conf", 1, 0, 'f'},
177 +         {"dest", 1, 0, 'd'},
178 +         {"force-defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
179 +         {"force_defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
180 +         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
181 +         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
182 +         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
183 +         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
184 +         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
185 +         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
186 +         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
187 +         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
188 +         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
189 +         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
190 +         {"recursive", 0, 0,
191 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
192 +         {"force-removal-of-dependent-packages", 0, 0,
193 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
194 +         {"force_removal_of_dependent_packages", 0, 0,
195 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
196 +         {"force-removal-of-essential-packages", 0, 0,
197 +          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
198 +         {"force_removal_of_essential_packages", 0, 0,
199 +          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
200 +         {"multiple-providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
201 +         {"multiple_providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
202 +         {"noaction", 0, 0, ARGS_OPT_NOACTION},
203 +         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
204 +         {"offline", 1, 0, 'o'},
205 +         {"offline-root", 1, 0, 'o'},
206 +         {"test", 0, 0, ARGS_OPT_NOACTION},
207 +         {"tmp-dir", 1, 0, 't'},
208 +         {"verbose-wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
209 +         {"verbose_wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
210 +         {"verbosity", 2, 0, 'V'},
211 +         {"version", 0, 0, 'v'},
212 +         {0, 0, 0, 0}
213 +     };
214 +
215 +     while (1) {
216 +         c = getopt_long_only(argc, argv, "Ad:f:no:t:vV:", long_options, &option_index);
217 +         if (c == -1)
218 +              break;
219 +
220 +         switch (c) {
221 +         case 'A':
222 +              args->query_all = 1;
223 +              break;
224 +         case 'd':
225 +              args->dest = optarg;
226 +              break;
227 +         case 'f':
228 +              free(args->conf_file);
229 +              args->conf_file = strdup(optarg);
230 +              break;
231 +         case 'o':
232 +              args->offline_root = optarg;
233 +              break;
234 +         case 'n':
235 +              args->noaction = 1;
236 +              break;
237 +         case 't':
238 +              args->tmp_dir = strdup(optarg);
239 +              break;
240 +         case 'v':
241 +              print_version();
242 +              exit(0);
243 +         case 'V':
244 +         case ARGS_OPT_VERBOSITY:
245 +              if (optarg)
246 +                   args->verbosity = atoi(optarg);
247 +              else
248 +                   args->verbosity += 1;
249 +              break;
250 +         case ARGS_OPT_FORCE_DEFAULTS:
251 +              args->force_defaults = 1;
252 +              break;
253 +         case ARGS_OPT_FORCE_DEPENDS:
254 +              args->force_depends = 1;
255 +              break;
256 +         case ARGS_OPT_FORCE_OVERWRITE:
257 +              args->force_overwrite = 1;
258 +              break;
259 +         case ARGS_OPT_FORCE_DOWNGRADE:
260 +              args->force_downgrade = 1;
261 +              break;
262 +         case ARGS_OPT_FORCE_REINSTALL:
263 +              args->force_reinstall = 1;
264 +              break;
265 +         case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
266 +              args->force_removal_of_essential_packages = 1;
267 +              break;
268 +         case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
269 +              args->force_removal_of_dependent_packages = 1;
270 +              break;
271 +         case ARGS_OPT_FORCE_SPACE:
272 +              args->force_space = 1;
273 +              break;
274 +         case ARGS_OPT_VERBOSE_WGET:
275 +              args->verbose_wget = 1;
276 +              break;
277 +         case ARGS_OPT_MULTIPLE_PROVIDERS:
278 +              args->multiple_providers = 1;
279 +              break;
280 +         case ARGS_OPT_NODEPS:
281 +              args->nodeps = 1;
282 +              break;
283 +         case ARGS_OPT_NOACTION:
284 +              args->noaction = 1;
285 +              break;
286 +         case ':':
287 +              parse_err++;
288 +              break;
289 +         case '?':
290 +              parse_err++;
291 +              break;
292 +         default:
293 +              bb_error_msg("Confusion: getopt_long returned %d\n", c);
294 +         }
295 +     }
296 +    
297 +     if (parse_err) {
298 +         return -parse_err;
299 +     } else {
300 +         return optind;
301 +     }
302 +}
303 +
304 +void args_usage(char *complaint)
305 +{
306 +     if (complaint) {
307 +          bb_error_msg("%s\n", complaint);
308 +     }
309 +     print_version();
310 +     bb_show_usage();
311 +     exit(1);
312 +}
313 +
314 +static void print_version(void)
315 +{
316 +       bb_error_msg("version %s\n", IPKG_VERSION);
317 +}
318 diff -ruN busybox-1.2.0-orig/archival/libipkg/args.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.h
319 --- busybox-1.2.0-orig/archival/libipkg/args.h  1970-01-01 01:00:00.000000000 +0100
320 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.h 2006-07-22 16:31:25.000000000 +0200
321 @@ -0,0 +1,72 @@
322 +/* args.h - parse command-line args
323 +
324 +  Carl D. Worth
325 +
326 +  Copyright 2001 University of Southern California
327
328 +  This program is free software; you can redistribute it and/or modify
329 +  it under the terms of the GNU General Public License as published by
330 +  the Free Software Foundation; either version 2, or (at your option)
331 +  any later version.
332
333 +  This program is distributed in the hope that it will be useful,
334 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
335 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
336 +  GNU General Public License for more details.
337 +*/
338 +
339 +#ifndef ARGS_H
340 +#define ARGS_H
341 +
342 +struct args
343 +{
344 +    char *conf_file;
345 +    char *dest;
346 +    char *tmp_dir;
347 +    int force_defaults;
348 +    int force_depends;
349 +    int force_overwrite;
350 +    int force_downgrade;
351 +    int force_reinstall;
352 +    int force_removal_of_essential_packages;
353 +    int force_removal_of_dependent_packages;
354 +    int force_space;
355 +    int noaction;
356 +    int nodeps;
357 +    int multiple_providers;
358 +    int query_all;
359 +    int verbose_wget;
360 +    int verbosity;
361 +    int nocheckfordirorfile;
362 +    int noreadfeedsfile;
363 +    char *offline_root;
364 +    char *offline_root_pre_script_cmd;
365 +    char *offline_root_post_script_cmd;
366 +};
367 +typedef struct args args_t;
368 +
369 +#define ARGS_DEFAULT_CONF_FILE_DIR "/etc"
370 +#define ARGS_DEFAULT_CONF_FILE_NAME "ipkg.conf"
371 +#define ARGS_DEFAULT_DEST NULL
372 +#define ARGS_DEFAULT_FORCE_DEFAULTS 0
373 +#define ARGS_DEFAULT_FORCE_DEPENDS 0
374 +#define ARGS_DEFAULT_FORCE_OVERWRITE 0 
375 +#define ARGS_DEFAULT_FORCE_DOWNGRADE 0 
376 +#define ARGS_DEFAULT_FORCE_REINSTALL 0
377 +#define ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES 0
378 +#define ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES 0
379 +#define ARGS_DEFAULT_FORCE_SPACE 0
380 +#define ARGS_DEFAULT_OFFLINE_ROOT NULL
381 +#define ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD NULL
382 +#define ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD NULL
383 +#define ARGS_DEFAULT_NOACTION 0
384 +#define ARGS_DEFAULT_NODEPS 0
385 +#define ARGS_DEFAULT_VERBOSE_WGET 0
386 +#define ARGS_DEFAULT_VERBOSITY 1
387 +
388 +int args_init(args_t *args);
389 +void args_deinit(args_t *args);
390 +int args_parse(args_t *args, int argc, char *argv[]);
391 +void args_usage(char *complaint);
392 +
393 +#endif
394 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.c
395 --- busybox-1.2.0-orig/archival/libipkg/conffile.c      1970-01-01 01:00:00.000000000 +0100
396 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.c     2006-07-22 16:31:25.000000000 +0200
397 @@ -0,0 +1,64 @@
398 +/* conffile.c - the itsy package management system
399 +
400 +   Carl D. Worth
401 +
402 +   Copyright (C) 2001 University of Southern California
403 +
404 +   This program is free software; you can redistribute it and/or
405 +   modify it under the terms of the GNU General Public License as
406 +   published by the Free Software Foundation; either version 2, or (at
407 +   your option) any later version.
408 +
409 +   This program is distributed in the hope that it will be useful, but
410 +   WITHOUT ANY WARRANTY; without even the implied warranty of
411 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
412 +   General Public License for more details.
413 +*/
414 +
415 +#include <string.h>
416 +#include <stdlib.h>
417 +
418 +#include "ipkg.h"
419 +#include "ipkg_message.h"
420 +
421 +#include "conffile.h"
422 +#include "file_util.h"
423 +#include "sprintf_alloc.h"
424 +
425 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum)
426 +{
427 +    return nv_pair_init(conffile, file_name, md5sum);
428 +}
429 +
430 +void conffile_deinit(conffile_t *conffile)
431 +{
432 +    nv_pair_deinit(conffile);
433 +}
434 +
435 +int conffile_has_been_modified(ipkg_conf_t *conf, conffile_t *conffile)
436 +{
437 +    char *md5sum;
438 +    char *filename = conffile->name;
439 +    char *root_filename;
440 +    int ret;
441 +
442 +    if (conffile->value == NULL) {
443 +        ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s has no md5sum\n", __FUNCTION__, conffile->name);
444 +        return 1;
445 +    }
446 +
447 +    root_filename = root_filename_alloc(conf, filename);
448 +
449 +    md5sum = file_md5sum_alloc(root_filename);
450 +
451 +    ret = strcmp(md5sum, conffile->value);
452 +    if (ret) {
453 +      ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s: \t\nold md5=%s \t\nnew md5=%s\n", __FUNCTION__,
454 +              conffile->name, md5sum, conffile->value);
455 +    }
456 +
457 +    free(root_filename);
458 +    free(md5sum);
459 +
460 +    return ret;
461 +}
462 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.h
463 --- busybox-1.2.0-orig/archival/libipkg/conffile.h      1970-01-01 01:00:00.000000000 +0100
464 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.h     2006-07-22 16:31:25.000000000 +0200
465 @@ -0,0 +1,30 @@
466 +/* conffile.h - the itsy package management system
467 +
468 +   Carl D. Worth
469 +
470 +   Copyright (C) 2001 University of Southern California
471 +
472 +   This program is free software; you can redistribute it and/or
473 +   modify it under the terms of the GNU General Public License as
474 +   published by the Free Software Foundation; either version 2, or (at
475 +   your option) any later version.
476 +
477 +   This program is distributed in the hope that it will be useful, but
478 +   WITHOUT ANY WARRANTY; without even the implied warranty of
479 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
480 +   General Public License for more details.
481 +*/
482 +
483 +#ifndef CONFFILE_H
484 +#define CONFFILE_H
485 +
486 +#include "nv_pair.h"
487 +
488 +typedef struct nv_pair conffile_t;
489 +
490 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum);
491 +void conffile_deinit(conffile_t *conffile);
492 +int conffile_has_been_modified(struct ipkg_conf *conf, conffile_t *conffile);
493 +
494 +#endif
495 +
496 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.c
497 --- busybox-1.2.0-orig/archival/libipkg/conffile_list.c 1970-01-01 01:00:00.000000000 +0100
498 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.c        2006-07-22 16:31:25.000000000 +0200
499 @@ -0,0 +1,47 @@
500 +/* conffile_list.c - the itsy package management system
501 +
502 +   Carl D. Worth
503 +
504 +   Copyright (C) 2001 University of Southern California
505 +
506 +   This program is free software; you can redistribute it and/or
507 +   modify it under the terms of the GNU General Public License as
508 +   published by the Free Software Foundation; either version 2, or (at
509 +   your option) any later version.
510 +
511 +   This program is distributed in the hope that it will be useful, but
512 +   WITHOUT ANY WARRANTY; without even the implied warranty of
513 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
514 +   General Public License for more details.
515 +*/
516 +
517 +#include "ipkg.h"
518 +
519 +#include "conffile_list.h"
520 +
521 +int conffile_list_init(conffile_list_t *list)
522 +{
523 +    return nv_pair_list_init(list);
524 +}
525 +
526 +void conffile_list_deinit(conffile_list_t *list)
527 +{
528 +    nv_pair_list_deinit(list);
529 +}
530 +
531 +conffile_t *conffile_list_append(conffile_list_t *list, const char *file_name,
532 +                        const char *md5sum)
533 +{
534 +    return nv_pair_list_append(list, file_name, md5sum);
535 +}
536 +
537 +int conffile_list_push(conffile_list_t *list, conffile_t *data)
538 +{
539 +    return nv_pair_list_push(list, data);
540 +}
541 +
542 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list)
543 +{
544 +    return nv_pair_list_pop(list);
545 +}
546 +
547 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.h
548 --- busybox-1.2.0-orig/archival/libipkg/conffile_list.h 1970-01-01 01:00:00.000000000 +0100
549 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.h        2006-07-22 16:31:25.000000000 +0200
550 @@ -0,0 +1,36 @@
551 +/* conffile_list.h - the itsy package management system
552 +
553 +   Carl D. Worth
554 +
555 +   Copyright (C) 2001 University of Southern California
556 +
557 +   This program is free software; you can redistribute it and/or
558 +   modify it under the terms of the GNU General Public License as
559 +   published by the Free Software Foundation; either version 2, or (at
560 +   your option) any later version.
561 +
562 +   This program is distributed in the hope that it will be useful, but
563 +   WITHOUT ANY WARRANTY; without even the implied warranty of
564 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
565 +   General Public License for more details.
566 +*/
567 +
568 +#ifndef CONFFILE_LIST_H
569 +#define CONFFILE_LIST_H
570 +
571 +#include "conffile.h"
572 +#include "nv_pair_list.h"
573 +
574 +typedef struct nv_pair_list_elt conffile_list_elt_t;
575 +typedef struct nv_pair_list conffile_list_t;
576 +
577 +int conffile_list_init(conffile_list_t *list);
578 +void conffile_list_deinit(conffile_list_t *list);
579 +
580 +conffile_t *conffile_list_append(conffile_list_t *list, const char *name,
581 +                              const char *root_dir);
582 +int conffile_list_push(conffile_list_t *list, conffile_t *data);
583 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list);
584 +
585 +#endif
586 +
587 diff -ruN busybox-1.2.0-orig/archival/libipkg/file_util.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.c
588 --- busybox-1.2.0-orig/archival/libipkg/file_util.c     1970-01-01 01:00:00.000000000 +0100
589 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.c    2006-07-22 16:31:25.000000000 +0200
590 @@ -0,0 +1,177 @@
591 +/* file_util.c - convenience routines for common stat operations
592 +
593 +   Carl D. Worth
594 +
595 +   Copyright (C) 2001 University of Southern California
596 +
597 +   This program is free software; you can redistribute it and/or
598 +   modify it under the terms of the GNU General Public License as
599 +   published by the Free Software Foundation; either version 2, or (at
600 +   your option) any later version.
601 +
602 +   This program is distributed in the hope that it will be useful, but
603 +   WITHOUT ANY WARRANTY; without even the implied warranty of
604 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
605 +   General Public License for more details.
606 +*/
607 +
608 +#include "ipkg.h"
609 +#include <sys/types.h>
610 +#include <sys/stat.h>
611 +
612 +#include "sprintf_alloc.h"
613 +#include "file_util.h"
614 +#include "md5.h"
615 +#include "libbb.h"
616 +#undef strlen
617 +
618 +int file_exists(const char *file_name)
619 +{
620 +    int err;
621 +    struct stat stat_buf;
622 +
623 +    err = stat(file_name, &stat_buf);
624 +    if (err == 0) {
625 +       return 1;
626 +    } else {
627 +       return 0;
628 +    }
629 +}
630 +
631 +int file_is_dir(const char *file_name)
632 +{
633 +    int err;
634 +    struct stat stat_buf;
635 +
636 +    err = stat(file_name, &stat_buf);
637 +    if (err) {
638 +       return 0;
639 +    }
640 +
641 +    return S_ISDIR(stat_buf.st_mode);
642 +}
643 +
644 +/* read a single line from a file, stopping at a newline or EOF.
645 +   If a newline is read, it will appear in the resulting string.
646 +   Return value is a malloc'ed char * which should be freed at
647 +   some point by the caller.
648 +
649 +   Return value is NULL if the file is at EOF when called.
650 +*/
651 +#define FILE_READ_LINE_BUF_SIZE 1024
652 +char *file_read_line_alloc(FILE *file)
653 +{
654 +    char buf[FILE_READ_LINE_BUF_SIZE];
655 +    int buf_len;
656 +    char *line = NULL;
657 +    int line_size = 0;
658 +
659 +    memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
660 +    while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
661 +       buf_len = strlen(buf);
662 +       if (line) {
663 +           line_size += buf_len;
664 +           line = realloc(line, line_size);
665 +           if (line == NULL) {
666 +               fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
667 +               break;
668 +           }
669 +           strcat(line, buf);
670 +       } else {
671 +           line_size = buf_len + 1;
672 +           line = strdup(buf);
673 +       }
674 +       if (buf[buf_len - 1] == '\n') {
675 +           break;
676 +       }
677 +    }
678 +
679 +    return line;
680 +}
681 +
682 +int file_move(const char *src, const char *dest)
683 +{
684 +    int err;
685 +
686 +    err = rename(src, dest);
687 +
688 +    if (err && errno == EXDEV) {
689 +       err = file_copy(src, dest);
690 +       unlink(src);
691 +    } else if (err) {
692 +       fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
693 +               __FUNCTION__, src, dest, strerror(errno));
694 +    }
695 +
696 +    return err;
697 +}
698 +
699 +/* I put these here to keep libbb dependencies from creeping all over
700 +   the ipkg code */
701 +int file_copy(const char *src, const char *dest)
702 +{
703 +    int err;
704 +
705 +    err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
706 +    if (err) {
707 +       fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
708 +               __FUNCTION__, src, dest);
709 +    }
710 +
711 +    return err;
712 +}
713 +
714 +int file_mkdir_hier(const char *path, long mode)
715 +{
716 +    return bb_make_directory(path, mode, FILEUTILS_RECUR);
717 +}
718 +
719 +char *file_md5sum_alloc(const char *file_name)
720 +{
721 +    static const int md5sum_bin_len = 16;
722 +    static const int md5sum_hex_len = 32;
723 +
724 +    static const unsigned char bin2hex[16] = {
725 +       '0', '1', '2', '3',
726 +       '4', '5', '6', '7',
727 +       '8', '9', 'a', 'b',
728 +       'c', 'd', 'e', 'f'
729 +    };
730 +
731 +    int i, err;
732 +    FILE *file;
733 +    unsigned char *md5sum_hex;
734 +    unsigned char md5sum_bin[md5sum_bin_len];
735 +
736 +    md5sum_hex = malloc(md5sum_hex_len + 1);
737 +    if (md5sum_hex == NULL) {
738 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
739 +       return strdup("");
740 +    }
741 +
742 +    file = fopen(file_name, "r");
743 +    if (file == NULL) {
744 +       fprintf(stderr, "%s: Failed to open file %s: %s\n",
745 +               __FUNCTION__, file_name, strerror(errno));
746 +       return strdup("");
747 +    }
748 +
749 +    err = md5_stream(file, md5sum_bin);
750 +    if (err) {
751 +       fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
752 +               __FUNCTION__, file_name, strerror(err));
753 +       return strdup("");
754 +    }
755 +
756 +    fclose(file);
757 +
758 +    for (i=0; i < md5sum_bin_len; i++) {
759 +       md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
760 +       md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
761 +    }
762 +    
763 +    md5sum_hex[md5sum_hex_len] = '\0';
764 +    
765 +    return md5sum_hex;
766 +}
767 +
768 diff -ruN busybox-1.2.0-orig/archival/libipkg/file_util.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.h
769 --- busybox-1.2.0-orig/archival/libipkg/file_util.h     1970-01-01 01:00:00.000000000 +0100
770 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.h    2006-07-22 16:31:25.000000000 +0200
771 @@ -0,0 +1,29 @@
772 +/* file_util.h - convenience routines for common file operations
773 +
774 +   Carl D. Worth
775 +
776 +   Copyright (C) 2001 University of Southern California
777 +
778 +   This program is free software; you can redistribute it and/or
779 +   modify it under the terms of the GNU General Public License as
780 +   published by the Free Software Foundation; either version 2, or (at
781 +   your option) any later version.
782 +
783 +   This program is distributed in the hope that it will be useful, but
784 +   WITHOUT ANY WARRANTY; without even the implied warranty of
785 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
786 +   General Public License for more details.
787 +*/
788 +
789 +#ifndef FILE_UTIL_H
790 +#define FILE_UTIL_H
791 +
792 +int file_exists(const char *file_name);
793 +int file_is_dir(const char *file_name);
794 +char *file_read_line_alloc(FILE *file);
795 +int file_move(const char *src, const char *dest);
796 +int file_copy(const char *src, const char *dest);
797 +int file_mkdir_hier(const char *path, long mode);
798 +char *file_md5sum_alloc(const char *file_name);
799 +
800 +#endif
801 diff -ruN busybox-1.2.0-orig/archival/libipkg/hash_table.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.c
802 --- busybox-1.2.0-orig/archival/libipkg/hash_table.c    1970-01-01 01:00:00.000000000 +0100
803 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.c   2006-07-22 16:31:25.000000000 +0200
804 @@ -0,0 +1,155 @@
805 +/* hash.c - hash tables for ipkg
806 +
807 +   Steven M. Ayer, Jamey Hicks
808 +   
809 +   Copyright (C) 2002 Compaq Computer Corporation
810 +
811 +   This program is free software; you can redistribute it and/or
812 +   modify it under the terms of the GNU General Public License as
813 +   published by the Free Software Foundation; either version 2, or (at
814 +   your option) any later version.
815 +
816 +   This program is distributed in the hope that it will be useful, but
817 +   WITHOUT ANY WARRANTY; without even the implied warranty of
818 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
819 +   General Public License for more details.
820 +*/
821 +
822 +#include <errno.h>
823 +#include <stdio.h>
824 +#include <stdlib.h>
825 +#include <string.h>
826 +#include "hash_table.h"
827 +#include "ipkg_message.h"
828 +
829 +
830 +static int hash_index(hash_table_t *hash, const char *pkg_name);
831 +static int rotating(const char *key, int len, int prime);
832 +
833 +static int hash_index(hash_table_t *hash, const char *pkg_name)
834 +{
835 +    return rotating(pkg_name, strlen(pkg_name), hash->n_entries);
836 +}
837 +  
838 +static int rotating(const char *key, int len, int prime)
839 +{
840 +    unsigned int hash, i;
841 +    for (hash=len, i=0; i<len; ++i)
842 +       hash = (hash<<4)^(hash>>28)^key[i];
843 +    return (hash % prime);
844 +}
845 +
846 +
847 +/*
848 + * this is an open table keyed by strings
849 + */
850 +int hash_table_init(const char *name, hash_table_t *hash, int len)
851 +{
852 +    static int primes_table[] = {
853 +       379, 761, 983, 1423, 2711, 3361, 3931, 4679, 5519, 6701, 9587,
854 +       19471, 23143, 33961, 46499, 49727, 99529, 0
855 +    };
856 +    int *picker;
857 +
858 +    if (hash->entries != NULL) {
859 +       /* we have been here already */
860 +       return 0;
861 +    }
862 +
863 +    hash->name = name;
864 +    hash->entries = NULL;
865 +    hash->n_entries = 0;
866 +    hash->hash_entry_key = NULL;
867 +
868 +    picker = primes_table;
869 +    while(*picker && (*picker++ < len));
870 +    if(!*picker)
871 +       fprintf(stderr, "%s: primes table might not be big enough (! << %d)\n", __FUNCTION__, len);
872 +    --picker;
873 +
874 +    hash->n_entries = *picker;
875 +    hash->entries = (hash_entry_t *)calloc(hash->n_entries, sizeof(hash_entry_t));
876 +    if (hash->entries == NULL) {
877 +       fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__);
878 +       return ENOMEM;
879 +    }
880 +    return 0;
881 +}
882 +
883 +void hash_table_deinit(hash_table_t *hash)
884 +{
885 +    free(hash->entries);
886 +    hash->entries = NULL;
887 +    hash->n_entries = 0;
888 +}
889 +
890 +void *hash_table_get(hash_table_t *hash, const char *key)
891 +{
892 +  int ndx= hash_index(hash, key);
893 +  hash_entry_t *hash_entry = hash->entries + ndx;
894 +  while (hash_entry) 
895 +  {
896 +    if (hash_entry->key) 
897 +    {
898 +      if (strcmp(key, hash_entry->key) == 0) {
899 +         // ipkg_message(NULL, IPKG_DEBUG, "Function: %s. Key found for '%s' \n", __FUNCTION__, key);
900 +        return hash_entry->data;
901 +      }
902 +    }
903 +    hash_entry = hash_entry->next;
904 +  }
905 +  return NULL;
906 +}
907 +
908 +int hash_table_insert(hash_table_t *hash, const char *key, void *value)
909 +{
910 +     int ndx= hash_index(hash, key);
911 +     hash_entry_t *hash_entry = hash->entries + ndx;
912 +     if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Inserting in hash for '%s' \n", __FUNCTION__, key);
913 +     if (hash_entry->key) {
914 +         if (strcmp(hash_entry->key, key) == 0) {
915 +              /* alread in table, update the value */
916 +               if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash for '%s' \n", __FUNCTION__, key);
917 +              hash_entry->data = value;
918 +              return 0;
919 +         } else {
920 +              /* 
921 +               * if this is a collision, we have to go to the end of the ll,
922 +               * then add a new entry
923 +               * before we can hook up the value
924 +               */
925 +               if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash by collision for '%s' \n", __FUNCTION__, key);
926 +              while (hash_entry->next)
927 +                   hash_entry = hash_entry->next;
928 +              hash_entry->next = (hash_entry_t *)malloc(sizeof(hash_entry_t));
929 +              if (!hash_entry->next) {
930 +                   return -ENOMEM;
931 +              }
932 +              hash_entry = hash_entry->next;
933 +              hash_entry->next = NULL;
934 +         }
935 +     }
936 +     hash->n_elements++;
937 +     hash_entry->key = strdup(key);
938 +     hash_entry->data = value;
939 +
940 +     return 0;
941 +}
942 +
943 +
944 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data)
945 +{ 
946 +    int i;
947 +    if (!hash || !f)
948 +       return;
949 +
950 +    for (i = 0; i < hash->n_entries; i++) {
951 +       hash_entry_t *hash_entry = (hash->entries + i);
952 +       do {
953 +           if(hash_entry->key) {
954 +               f(hash_entry->key, hash_entry->data, data);
955 +           }
956 +       } while((hash_entry = hash_entry->next));
957 +    }
958 +}
959 +
960 diff -ruN busybox-1.2.0-orig/archival/libipkg/hash_table.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.h
961 --- busybox-1.2.0-orig/archival/libipkg/hash_table.h    1970-01-01 01:00:00.000000000 +0100
962 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.h   2006-07-22 16:31:25.000000000 +0200
963 @@ -0,0 +1,44 @@
964 +/* hash.h - hash tables for ipkg
965 +
966 +   Steven M. Ayer, Jamey Hicks
967 +   
968 +   Copyright (C) 2002 Compaq Computer Corporation
969 +
970 +   This program is free software; you can redistribute it and/or
971 +   modify it under the terms of the GNU General Public License as
972 +   published by the Free Software Foundation; either version 2, or (at
973 +   your option) any later version.
974 +
975 +   This program is distributed in the hope that it will be useful, but
976 +   WITHOUT ANY WARRANTY; without even the implied warranty of
977 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
978 +   General Public License for more details.
979 +*/
980 +
981 +#ifndef _HASH_TABLE_H_
982 +#define _HASH_TABLE_H_
983 +
984 +typedef struct hash_entry hash_entry_t;
985 +typedef struct hash_table hash_table_t;
986 +
987 +struct hash_entry {
988 +  const char * key;
989 +  void * data;
990 +  struct hash_entry * next;
991 +};
992 +
993 +struct hash_table {
994 +  const char *name; 
995 +  hash_entry_t * entries;  
996 +  int n_entries; /* number of buckets */
997 +  int n_elements;
998 +  const char * (*hash_entry_key)(void * data);
999 +};
1000 +
1001 +int hash_table_init(const char *name, hash_table_t *hash, int len);
1002 +void hash_table_deinit(hash_table_t *hash);
1003 +void *hash_table_get(hash_table_t *hash, const char *key);
1004 +int hash_table_insert(hash_table_t *hash, const char *key, void *value);
1005 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data);
1006 +
1007 +#endif /* _HASH_TABLE_H_ */
1008 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.c
1009 --- busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.c      1970-01-01 01:00:00.000000000 +0100
1010 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.c     2006-07-22 16:31:25.000000000 +0200
1011 @@ -0,0 +1,1431 @@
1012 +/* ipkg_cmd.c - the itsy package management system
1013 +
1014 +   Carl D. Worth
1015 +
1016 +   Copyright (C) 2001 University of Southern California
1017 +
1018 +   This program is free software; you can redistribute it and/or
1019 +   modify it under the terms of the GNU General Public License as
1020 +   published by the Free Software Foundation; either version 2, or (at
1021 +   your option) any later version.
1022 +
1023 +   This program is distributed in the hope that it will be useful, but
1024 +   WITHOUT ANY WARRANTY; without even the implied warranty of
1025 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1026 +   General Public License for more details.
1027 +*/
1028 +
1029 +#include <string.h>
1030 +
1031 +#include "ipkg.h"
1032 +#include <libgen.h>
1033 +#include <glob.h>
1034 +#include <errno.h>
1035 +#include <stdlib.h>
1036 +#include <unistd.h>
1037 +#include <signal.h>
1038 +#include <stdio.h>
1039 +#include <dirent.h>
1040 +
1041 +#include "ipkg_conf.h"
1042 +#include "ipkg_cmd.h"
1043 +#include "ipkg_message.h"
1044 +#include "pkg.h"
1045 +#include "pkg_dest.h"
1046 +#include "pkg_parse.h"
1047 +#include "sprintf_alloc.h"
1048 +#include "pkg.h"
1049 +#include "file_util.h"
1050 +#include "str_util.h"
1051 +#include "libbb.h"
1052 +#include "unarchive.h"
1053 +
1054 +#include <fnmatch.h>
1055 +
1056 +
1057 +#include "ipkg_download.h"
1058 +#include "ipkg_install.h"
1059 +#include "ipkg_upgrade.h"
1060 +#include "ipkg_remove.h"
1061 +#include "ipkg_configure.h"
1062 +#include "ipkg_message.h"
1063 +
1064 +#ifdef IPKG_LIB
1065 +#include "libipkg.h"
1066 +static void *p_userdata = NULL;
1067 +#endif
1068 +
1069 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv);
1070 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv);
1071 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv);
1072 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv);
1073 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv);
1074 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv);
1075 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv);
1076 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv);
1077 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv);
1078 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv);
1079 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv);
1080 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv);
1081 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv);
1082 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv);
1083 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1084 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1085 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv);
1086 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv);
1087 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1088 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv);
1089 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv);
1090 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv);
1091 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv);
1092 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv);
1093 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv);
1094 +
1095 +/* XXX: CLEANUP: The usage strings should be incorporated into this
1096 +   array for easier maintenance */
1097 +static ipkg_cmd_t cmds[] = {
1098 +     {"update", 0, (ipkg_cmd_fun_t)ipkg_update_cmd}, 
1099 +     {"upgrade", 0, (ipkg_cmd_fun_t)ipkg_upgrade_cmd},
1100 +     {"list", 0, (ipkg_cmd_fun_t)ipkg_list_cmd},
1101 +     {"list_installed", 0, (ipkg_cmd_fun_t)ipkg_list_installed_cmd},
1102 +     {"info", 0, (ipkg_cmd_fun_t)ipkg_info_cmd},
1103 +     {"flag", 1, (ipkg_cmd_fun_t)ipkg_flag_cmd},
1104 +     {"status", 0, (ipkg_cmd_fun_t)ipkg_status_cmd},
1105 +     {"install_pending", 0, (ipkg_cmd_fun_t)ipkg_install_pending_cmd},
1106 +     {"install", 1, (ipkg_cmd_fun_t)ipkg_install_cmd},
1107 +     {"remove", 1, (ipkg_cmd_fun_t)ipkg_remove_cmd},
1108 +     {"purge", 1, (ipkg_cmd_fun_t)ipkg_purge_cmd},
1109 +     {"configure", 0, (ipkg_cmd_fun_t)ipkg_configure_cmd},
1110 +     {"files", 1, (ipkg_cmd_fun_t)ipkg_files_cmd},
1111 +     {"search", 1, (ipkg_cmd_fun_t)ipkg_search_cmd},
1112 +     {"download", 1, (ipkg_cmd_fun_t)ipkg_download_cmd},
1113 +     {"compare_versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1114 +     {"compare-versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1115 +     {"print-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1116 +     {"print_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1117 +     {"print-installation-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1118 +     {"print_installation_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1119 +     {"depends", 1, (ipkg_cmd_fun_t)ipkg_depends_cmd},
1120 +     {"whatdepends", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_cmd},
1121 +     {"whatdependsrec", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_recursively_cmd},
1122 +     {"whatrecommends", 1, (ipkg_cmd_fun_t)ipkg_whatrecommends_cmd},
1123 +     {"whatsuggests", 1, (ipkg_cmd_fun_t)ipkg_whatsuggests_cmd},
1124 +     {"whatprovides", 1, (ipkg_cmd_fun_t)ipkg_whatprovides_cmd},
1125 +     {"whatreplaces", 1, (ipkg_cmd_fun_t)ipkg_whatreplaces_cmd},
1126 +     {"whatconflicts", 1, (ipkg_cmd_fun_t)ipkg_whatconflicts_cmd},
1127 +};
1128 +
1129 +int ipkg_state_changed;
1130 +static void write_status_files_if_changed(ipkg_conf_t *conf)
1131 +{
1132 +     if (ipkg_state_changed && !conf->noaction) {
1133 +         ipkg_message(conf, IPKG_INFO,
1134 +                      "  writing status file\n");
1135 +         ipkg_conf_write_status_files(conf);
1136 +         pkg_write_changed_filelists(conf);
1137 +     } else { 
1138 +         ipkg_message(conf, IPKG_NOTICE, "Nothing to be done\n");
1139 +     }
1140 +}
1141 +
1142 +
1143 +static int num_cmds = sizeof(cmds) / sizeof(ipkg_cmd_t);
1144 +
1145 +ipkg_cmd_t *ipkg_cmd_find(const char *name)
1146 +{
1147 +     int i;
1148 +     ipkg_cmd_t *cmd;
1149 +
1150 +     for (i=0; i < num_cmds; i++) {
1151 +         cmd = &cmds[i];
1152 +         if (strcmp(name, cmd->name) == 0) {
1153 +              return cmd;
1154 +         }
1155 +     }
1156 +
1157 +     return NULL;
1158 +}
1159 +
1160 +#ifdef IPKG_LIB
1161 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv, void *userdata)
1162 +{
1163 +       int result;
1164 +       p_userdata = userdata;
1165 +      
1166 +
1167 +       result = (cmd->fun)(conf, argc, argv);
1168 +        if ( result == 0 ) {
1169 +           ipkg_message(conf, IPKG_NOTICE, "Done.\n");
1170 +        } else {
1171 +           ipkg_message(conf, IPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
1172 +
1173 +        }
1174 +        if ( error_list ) {
1175 +           reverse_error_list(&error_list);
1176 +
1177 +           ipkg_message(conf, IPKG_NOTICE, "Collected errors:\n");
1178 +           /* Here we print the errors collected and free the list */
1179 +           while (error_list != NULL) {
1180 +                 ipkg_message(conf, IPKG_NOTICE, "%s",error_list->errmsg);
1181 +                 error_list = error_list->next;
1182 +
1183 +           }
1184 +           free_error_list(&error_list);
1185 +
1186 +        }
1187 +   
1188 +       p_userdata = NULL;
1189 +       return result;
1190 +}
1191 +#else
1192 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv)
1193 +{
1194 +     return (cmd->fun)(conf, argc, argv);
1195 +}
1196 +#endif
1197 +
1198 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv)
1199 +{
1200 +     int err;
1201 +     int failures;
1202 +     char *lists_dir;
1203 +     pkg_src_list_elt_t *iter;
1204 +     pkg_src_t *src;
1205 +
1206
1207 +    sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
1208
1209 +    if (! file_is_dir(lists_dir)) {
1210 +         if (file_exists(lists_dir)) {
1211 +              ipkg_message(conf, IPKG_ERROR,
1212 +                           "%s: ERROR: %s exists, but is not a directory\n",
1213 +                           __FUNCTION__, lists_dir);
1214 +              free(lists_dir);
1215 +              return EINVAL;
1216 +         }
1217 +         err = file_mkdir_hier(lists_dir, 0755);
1218 +         if (err) {
1219 +              ipkg_message(conf, IPKG_ERROR,
1220 +                           "%s: ERROR: failed to make directory %s: %s\n",
1221 +                           __FUNCTION__, lists_dir, strerror(errno));
1222 +              free(lists_dir);
1223 +              return EINVAL;
1224 +         }     
1225 +     } 
1226 +
1227 +     failures = 0;
1228 +     for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
1229 +         char *url, *list_file_name;
1230 +
1231 +         src = iter->data;
1232 +
1233 +         if (src->extra_data)  /* debian style? */
1234 +             sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, 
1235 +                           src->gzip ? "Packages.gz" : "Packages");
1236 +         else
1237 +             sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
1238 +
1239 +         sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
1240 +         if (src->gzip) {
1241 +             char *tmp;
1242 +             char *tmp_file_name;
1243 +             FILE *in, *out;
1244 +
1245 +             tmp = strdup ("/tmp/ipkg.XXXXXX");
1246 +
1247 +             if (mkdtemp (tmp) == NULL) {
1248 +                 perror ("mkdtemp");
1249 +                 failures++;
1250 +                 continue;
1251 +             }
1252 +             
1253 +             sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
1254 +             err = ipkg_download(conf, url, tmp_file_name);
1255 +             if (err == 0) {
1256 +                  ipkg_message (conf, IPKG_NOTICE, "Inflating %s\n", url);
1257 +                  in = fopen (tmp_file_name, "r");
1258 +                  out = fopen (list_file_name, "w");
1259 +                  if (in && out)
1260 +                       inflate_unzip (in, out);
1261 +                  else
1262 +                       err = 1;
1263 +                  if (in)
1264 +                       fclose (in);
1265 +                  if (out)
1266 +                       fclose (out);
1267 +                  unlink (tmp_file_name);
1268 +                  rmdir (tmp);
1269 +                  free (tmp);
1270 +             }
1271 +         } else
1272 +             err = ipkg_download(conf, url, list_file_name);
1273 +         if (err) {
1274 +              failures++;
1275 +         } else {
1276 +              ipkg_message(conf, IPKG_NOTICE,
1277 +                           "Updated list of available packages in %s\n",
1278 +                           list_file_name);
1279 +         }
1280 +         free(url);
1281 +         free(list_file_name);
1282 +     }
1283 +     free(lists_dir);
1284 +
1285 +#ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
1286 +#warning here
1287 +     /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
1288 +      * this is a hack to work around poor bookkeeping in old ipkg upgrade code 
1289 +      * -Jamey 3/1/03
1290 +      */
1291 +     {
1292 +         int i;
1293 +         int changed = 0;
1294 +         pkg_vec_t *available = pkg_vec_alloc();
1295 +         pkg_hash_fetch_available(&conf->pkg_hash, available);
1296 +         ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
1297 +         for (i = 0; i < available->len; i++) {
1298 +              pkg_t *pkg = available->pkgs[i];
1299 +              if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
1300 +                   ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
1301 +                   pkg->state_want = SW_UNKNOWN;
1302 +                   changed = 1;
1303 +              }
1304 +         }
1305 +         pkg_vec_free(available);
1306 +         if (changed) {
1307 +              write_status_files_if_changed(conf);
1308 +         }
1309 +     }
1310 +#endif
1311 +
1312 +     return failures;
1313 +}
1314 +
1315 +
1316 +/* scan the args passed and cache the local filenames of the packages */
1317 +int ipkg_multiple_files_scan(ipkg_conf_t *conf, int argc, char **argv)
1318 +{
1319 +     int i;
1320 +     int err;
1321 +    
1322 +     /* 
1323 +      * First scan through package names/urls
1324 +      * For any urls, download the packages and install in database.
1325 +      * For any files, install package info in database.
1326 +      */
1327 +     for (i = 0; i < argc; i ++) {
1328 +         char *filename = argv [i];
1329 +         //char *tmp = basename (tmp);
1330 +         //int tmplen = strlen (tmp);
1331 +
1332 +         //if (strcmp (tmp + (tmplen - strlen (IPKG_PKG_EXTENSION)), IPKG_PKG_EXTENSION) != 0)
1333 +         //     continue;
1334 +         //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
1335 +         //     continue;
1336 +       
1337 +          ipkg_message(conf, IPKG_DEBUG2, "Debug mfs: %s  \n",filename );
1338 +
1339 +         err = ipkg_prepare_url_for_install(conf, filename, &argv[i]);
1340 +         if (err)
1341 +           return err;
1342 +     }
1343 +     return 0;
1344 +}
1345 +
1346 +struct ipkg_intercept
1347 +{
1348 +    char *oldpath;
1349 +    char *statedir;
1350 +};
1351 +
1352 +typedef struct ipkg_intercept *ipkg_intercept_t;
1353 +
1354 +ipkg_intercept_t ipkg_prep_intercepts(ipkg_conf_t *conf)
1355 +{
1356 +    ipkg_intercept_t ctx;
1357 +    char *newpath;
1358 +    int gen;
1359 +
1360 +    ctx = malloc (sizeof (*ctx));
1361 +    ctx->oldpath = strdup (getenv ("PATH"));
1362 +
1363 +    sprintf_alloc (&newpath, "%s/ipkg/intercept:%s", IPKGLIBDIR, ctx->oldpath);
1364 +    setenv ("PATH", newpath, 1);
1365 +    free (newpath);
1366 +    
1367 +    gen = 0;
1368 + retry:
1369 +    sprintf_alloc (&ctx->statedir, "/tmp/ipkg-intercept-%d-%d", getpid (), gen);
1370 +    if (mkdir (ctx->statedir, 0770) < 0) {
1371 +       if (errno == EEXIST) {
1372 +           free (ctx->statedir);
1373 +           gen++;
1374 +           goto retry;
1375 +       }
1376 +       perror (ctx->statedir);
1377 +       return NULL;
1378 +    }
1379 +    setenv ("IPKG_INTERCEPT_DIR", ctx->statedir, 1);
1380 +    return ctx;
1381 +}
1382 +
1383 +int ipkg_finalize_intercepts(ipkg_intercept_t ctx)
1384 +{
1385 +    char *cmd;
1386 +    DIR *dir;
1387 +    int err = 0;
1388 +
1389 +    setenv ("PATH", ctx->oldpath, 1);
1390 +    free (ctx->oldpath);
1391 +
1392 +    dir = opendir (ctx->statedir);
1393 +    if (dir) {
1394 +       struct dirent *de;
1395 +       while (de = readdir (dir), de != NULL) {
1396 +           char *path;
1397 +           
1398 +           if (de->d_name[0] == '.')
1399 +               continue;
1400 +           
1401 +           sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
1402 +           if (access (path, X_OK) == 0) {
1403 +               if (system (path)) {
1404 +                   err = errno;
1405 +                   perror (de->d_name);
1406 +               }
1407 +           }
1408 +           free (path);
1409 +       }
1410 +    } else
1411 +       perror (ctx->statedir);
1412 +       
1413 +    sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
1414 +    system (cmd);
1415 +    free (cmd);
1416 +
1417 +    free (ctx->statedir);
1418 +    free (ctx);
1419 +
1420 +    return err;
1421 +}
1422 +
1423 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name)
1424 +{
1425 +     pkg_vec_t *all;
1426 +     int i;
1427 +     pkg_t *pkg;
1428 +     ipkg_intercept_t ic;
1429 +     int r, err = 0;
1430 +
1431 +     ipkg_message(conf, IPKG_INFO,
1432 +                 "Configuring unpacked packages\n");
1433 +     fflush( stdout );
1434 +
1435 +     all = pkg_vec_alloc();
1436 +     pkg_hash_fetch_available(&conf->pkg_hash, all);
1437 +
1438 +     ic = ipkg_prep_intercepts (conf);
1439 +    
1440 +     for(i = 0; i < all->len; i++) {
1441 +         pkg = all->pkgs[i];
1442 +
1443 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1444 +              continue;
1445 +
1446 +         if (pkg->state_status == SS_UNPACKED) {
1447 +              ipkg_message(conf, IPKG_NOTICE,
1448 +                           "Configuring %s\n", pkg->name);
1449 +              fflush( stdout );
1450 +              r = ipkg_configure(conf, pkg);
1451 +              if (r == 0) {
1452 +                   pkg->state_status = SS_INSTALLED;
1453 +                   pkg->parent->state_status = SS_INSTALLED;
1454 +                   pkg->state_flag &= ~SF_PREFER;
1455 +              } else {
1456 +                   if (!err)
1457 +                       err = r;
1458 +              }
1459 +         }
1460 +     }
1461 +
1462 +     r = ipkg_finalize_intercepts (ic);
1463 +     if (r && !err)
1464 +        err = r;
1465 +
1466 +     pkg_vec_free(all);
1467 +     return err;
1468 +}
1469 +
1470 +static void sigint_handler(int sig)
1471 +{
1472 +     signal(sig, SIG_DFL);
1473 +     ipkg_message(NULL, IPKG_NOTICE,
1474 +                 "ipkg: interrupted. writing out status database\n");
1475 +     write_status_files_if_changed(global_conf);
1476 +     exit(128 + sig);
1477 +}
1478 +
1479 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv)
1480 +{
1481 +     int i;
1482 +     char *arg;
1483 +     int err=0;
1484 +
1485 +     global_conf = conf;
1486 +     signal(SIGINT, sigint_handler);
1487 +
1488 +     /*
1489 +      * Now scan through package names and install
1490 +      */
1491 +     for (i=0; i < argc; i++) {
1492 +         arg = argv[i];
1493 +
1494 +          ipkg_message(conf, IPKG_DEBUG2, "Debug install_cmd: %s  \n",arg );
1495 +          err = ipkg_prepare_url_for_install(conf, arg, &argv[i]);
1496 +          if (err != EINVAL && err != 0)
1497 +              return err;
1498 +     }
1499 +     pkg_info_preinstall_check(conf);
1500 +
1501 +     for (i=0; i < argc; i++) {
1502 +         arg = argv[i];
1503 +         if (conf->multiple_providers)
1504 +              err = ipkg_install_multi_by_name(conf, arg);
1505 +         else{
1506 +              err = ipkg_install_by_name(conf, arg);
1507 +          }
1508 +         if (err == IPKG_PKG_HAS_NO_CANDIDATE) {
1509 +              ipkg_message(conf, IPKG_ERROR,
1510 +                           "Cannot find package %s.\n"
1511 +                           "Check the spelling or perhaps run 'ipkg update'\n",
1512 +                           arg);
1513 +         }
1514 +     }
1515 +
1516 +     /* recheck to verify that all dependences are satisfied */
1517 +     if (0) ipkg_satisfy_all_dependences(conf);
1518 +
1519 +     ipkg_configure_packages(conf, NULL);
1520 +
1521 +     write_status_files_if_changed(conf);
1522 +
1523 +     return err;
1524 +}
1525 +
1526 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv)
1527 +{
1528 +     int i;
1529 +     pkg_t *pkg;
1530 +     int err;
1531 +
1532 +     global_conf = conf;
1533 +     signal(SIGINT, sigint_handler);
1534 +
1535 +     if (argc) {
1536 +         for (i=0; i < argc; i++) {
1537 +              char *arg = argv[i];
1538 +
1539 +               err = ipkg_prepare_url_for_install(conf, arg, &arg);
1540 +               if (err != EINVAL && err != 0)
1541 +                   return err;
1542 +         }
1543 +         pkg_info_preinstall_check(conf);
1544 +
1545 +         for (i=0; i < argc; i++) {
1546 +              char *arg = argv[i];
1547 +              if (conf->restrict_to_default_dest) {
1548 +                   pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1549 +                                                               argv[i],
1550 +                                                               conf->default_dest);
1551 +                   if (pkg == NULL) {
1552 +                        ipkg_message(conf, IPKG_NOTICE,
1553 +                                     "Package %s not installed in %s\n",
1554 +                                     argv[i], conf->default_dest->name);
1555 +                        continue;
1556 +                   }
1557 +              } else {
1558 +                   pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1559 +                                                          argv[i]);
1560 +              }
1561 +              if (pkg)
1562 +                   ipkg_upgrade_pkg(conf, pkg);
1563 +              else {
1564 +                   ipkg_install_by_name(conf, arg);
1565 +               }
1566 +         }
1567 +     } else {
1568 +         pkg_vec_t *installed = pkg_vec_alloc();
1569 +
1570 +         pkg_info_preinstall_check(conf);
1571 +
1572 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1573 +         for (i = 0; i < installed->len; i++) {
1574 +              pkg = installed->pkgs[i];
1575 +              ipkg_upgrade_pkg(conf, pkg);
1576 +         }
1577 +         pkg_vec_free(installed);
1578 +     }
1579 +
1580 +     /* recheck to verify that all dependences are satisfied */
1581 +     if (0) ipkg_satisfy_all_dependences(conf);
1582 +
1583 +     ipkg_configure_packages(conf, NULL);
1584 +
1585 +     write_status_files_if_changed(conf);
1586 +
1587 +     return 0;
1588 +}
1589 +
1590 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv)
1591 +{
1592 +     int i, err;
1593 +     char *arg;
1594 +     pkg_t *pkg;
1595 +
1596 +     pkg_info_preinstall_check(conf);
1597 +     for (i = 0; i < argc; i++) {
1598 +         arg = argv[i];
1599 +
1600 +         pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
1601 +         if (pkg == NULL) {
1602 +              ipkg_message(conf, IPKG_ERROR,
1603 +                           "Cannot find package %s.\n"
1604 +                           "Check the spelling or perhaps run 'ipkg update'\n",
1605 +                           arg);
1606 +              continue;
1607 +         }
1608 +
1609 +         err = ipkg_download_pkg(conf, pkg, ".");
1610 +
1611 +         if (err) {
1612 +              ipkg_message(conf, IPKG_ERROR,
1613 +                           "Failed to download %s\n", pkg->name);
1614 +         } else {
1615 +              ipkg_message(conf, IPKG_NOTICE,
1616 +                           "Downloaded %s as %s\n",
1617 +                           pkg->name, pkg->local_filename);
1618 +         }
1619 +     }
1620 +
1621 +     return 0;
1622 +}
1623 +
1624 +
1625 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv)
1626 +{
1627 +     int i ;
1628 +     pkg_vec_t *available;
1629 +     pkg_t *pkg;
1630 +     char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1631 +     char *newline;
1632 +     char *pkg_name = NULL;
1633 +     char *version_str;
1634 +
1635 +     if (argc > 0) {
1636 +         pkg_name = argv[0];
1637 +     }
1638 +     available = pkg_vec_alloc();
1639 +     pkg_hash_fetch_available(&conf->pkg_hash, available);
1640 +     for (i=0; i < available->len; i++) {
1641 +         pkg = available->pkgs[i];
1642 +         /* if we have package name or pattern and pkg does not match, then skip it */
1643 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1644 +              continue;
1645 +         if (pkg->description) {
1646 +              strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1647 +         } else {
1648 +              desc_short[0] = '\0';
1649 +         }
1650 +         desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1651 +         newline = strchr(desc_short, '\n');
1652 +         if (newline) {
1653 +              *newline = '\0';
1654 +         }
1655 +#ifndef IPKG_LIB
1656 +         printf("%s - %s\n", pkg->name, desc_short);
1657 +#else
1658 +         if (ipkg_cb_list) {
1659 +               version_str = pkg_version_str_alloc(pkg);
1660 +               ipkg_cb_list(pkg->name,desc_short,
1661 +                                            version_str,
1662 +                                        pkg->state_status,
1663 +                                        p_userdata);
1664 +               free(version_str);
1665 +         }
1666 +#endif
1667 +     }
1668 +     pkg_vec_free(available);
1669 +
1670 +     return 0;
1671 +}
1672 +
1673 +
1674 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv)
1675 +{
1676 +     int i ;
1677 +     pkg_vec_t *available;
1678 +     pkg_t *pkg;
1679 +     char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1680 +     char *newline;
1681 +     char *pkg_name = NULL;
1682 +     char *version_str;
1683 +
1684 +     if (argc > 0) {
1685 +         pkg_name = argv[0];
1686 +     }
1687 +     available = pkg_vec_alloc();
1688 +     pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1689 +     for (i=0; i < available->len; i++) {
1690 +         pkg = available->pkgs[i];
1691 +         /* if we have package name or pattern and pkg does not match, then skip it */
1692 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1693 +              continue;
1694 +         if (pkg->description) {
1695 +              strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1696 +         } else {
1697 +              desc_short[0] = '\0';
1698 +         }
1699 +         desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1700 +         newline = strchr(desc_short, '\n');
1701 +         if (newline) {
1702 +              *newline = '\0';
1703 +         }
1704 +#ifndef IPKG_LIB
1705 +         printf("%s - %s\n", pkg->name, desc_short);
1706 +#else
1707 +         if (ipkg_cb_list) {
1708 +               version_str = pkg_version_str_alloc(pkg);
1709 +               ipkg_cb_list(pkg->name,desc_short,
1710 +                                            version_str,
1711 +                                        pkg->state_status,
1712 +                                        p_userdata);
1713 +               free(version_str);
1714 +         }
1715 +#endif
1716 +     }
1717 +
1718 +     return 0;
1719 +}
1720 +
1721 +static int ipkg_info_status_cmd(ipkg_conf_t *conf, int argc, char **argv, int installed_only)
1722 +{
1723 +     int i;
1724 +     pkg_vec_t *available;
1725 +     pkg_t *pkg;
1726 +     char *pkg_name = NULL;
1727 +     char **pkg_fields = NULL;
1728 +     int n_fields = 0;
1729 +     char *buff ; // = (char *)malloc(1);
1730 +
1731 +     if (argc > 0) {
1732 +         pkg_name = argv[0];
1733 +     }
1734 +     if (argc > 1) {
1735 +         pkg_fields = &argv[1];
1736 +         n_fields = argc - 1;
1737 +     }
1738 +
1739 +     available = pkg_vec_alloc();
1740 +     if (installed_only)
1741 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1742 +     else
1743 +         pkg_hash_fetch_available(&conf->pkg_hash, available);
1744 +     for (i=0; i < available->len; i++) {
1745 +         pkg = available->pkgs[i];
1746 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1747 +              continue;
1748 +         }
1749 +#ifndef IPKG_LIB
1750 +         if (n_fields) {
1751 +              for (j = 0; j < n_fields; j++)
1752 +                   pkg_print_field(pkg, stdout, pkg_fields[j]);
1753 +         } else {
1754 +              pkg_print_info(pkg, stdout);
1755 +         }
1756 +#else
1757 +
1758 +         buff = pkg_formatted_info(pkg);
1759 +          if ( buff ) {
1760 +              if (ipkg_cb_status) ipkg_cb_status(pkg->name,
1761 +                                                 pkg->state_status,
1762 +                                                 buff,
1763 +                                                 p_userdata);
1764 +/* 
1765 +   We should not forget that actually the pointer is allocated. 
1766 +   We need to free it :)  ( Thanks florian for seeing the error )
1767 +*/
1768 +               free(buff);
1769 +          }
1770 +#endif
1771 +         if (conf->verbosity > 1) {
1772 +              conffile_list_elt_t *iter;
1773 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
1774 +                   conffile_t *cf = iter->data;
1775 +                   int modified = conffile_has_been_modified(conf, cf);
1776 +                   ipkg_message(conf, IPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
1777 +                                cf->name, cf->value, modified);
1778 +              }
1779 +         }
1780 +     }
1781 +#ifndef IPKG_LIB
1782 +     if (buff)
1783 +         free(buff);
1784 +#endif
1785 +     pkg_vec_free(available);
1786 +
1787 +     return 0;
1788 +}
1789 +
1790 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv)
1791 +{
1792 +     return ipkg_info_status_cmd(conf, argc, argv, 0);
1793 +}
1794 +
1795 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv)
1796 +{
1797 +     return ipkg_info_status_cmd(conf, argc, argv, 1);
1798 +}
1799 +
1800 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv)
1801 +{
1802 +     
1803 +     int err;
1804 +     if (argc > 0) {
1805 +         char *pkg_name = NULL;
1806 +
1807 +         pkg_name = argv[0];
1808 +
1809 +         err = ipkg_configure_packages (conf, pkg_name);
1810 +     
1811 +     } else {
1812 +         err = ipkg_configure_packages (conf, NULL);
1813 +     }
1814 +
1815 +     write_status_files_if_changed(conf);
1816 +
1817 +     return err;
1818 +}
1819 +
1820 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv)
1821 +{
1822 +     int i, err;
1823 +     char *globpattern;
1824 +     glob_t globbuf;
1825 +    
1826 +     sprintf_alloc(&globpattern, "%s/*" IPKG_PKG_EXTENSION, conf->pending_dir);
1827 +     err = glob(globpattern, 0, NULL, &globbuf);
1828 +     free(globpattern);
1829 +     if (err) {
1830 +         return 0;
1831 +     }
1832 +
1833 +     ipkg_message(conf, IPKG_NOTICE,
1834 +                 "The following packages in %s will now be installed.\n",
1835 +                 conf->pending_dir);
1836 +     for (i = 0; i < globbuf.gl_pathc; i++) {
1837 +         ipkg_message(conf, IPKG_NOTICE,
1838 +                      "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
1839 +     }
1840 +     ipkg_message(conf, IPKG_NOTICE, "\n");
1841 +     for (i = 0; i < globbuf.gl_pathc; i++) {
1842 +         err = ipkg_install_from_file(conf, globbuf.gl_pathv[i]);
1843 +         if (err == 0) {
1844 +              err = unlink(globbuf.gl_pathv[i]);
1845 +              if (err) {
1846 +                   ipkg_message(conf, IPKG_ERROR,
1847 +                                "%s: ERROR: failed to unlink %s: %s\n",
1848 +                                __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
1849 +                   return err;
1850 +              }
1851 +         }
1852 +     }
1853 +     globfree(&globbuf);
1854 +
1855 +     return err;
1856 +}
1857 +
1858 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv)
1859 +{
1860 +     int i,a,done;
1861 +     pkg_t *pkg;
1862 +     pkg_t *pkg_to_remove;
1863 +     pkg_vec_t *available;
1864 +     char *pkg_name = NULL;
1865 +     global_conf = conf;
1866 +     signal(SIGINT, sigint_handler);
1867 +
1868 +// ENH: Add the "no pkg removed" just in case.
1869 +
1870 +    done = 0;
1871 +
1872 +     available = pkg_vec_alloc();
1873 +     pkg_info_preinstall_check(conf);
1874 +     if ( argc > 0 ) {
1875 +        pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1876 +        for (i=0; i < argc; i++) {
1877 +           pkg_name = malloc(strlen(argv[i])+2);
1878 +           strcpy(pkg_name,argv[i]);
1879 +           for (a=0; a < available->len; a++) {
1880 +               pkg = available->pkgs[a];
1881 +              if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1882 +                  continue;
1883 +               }
1884 +               if (conf->restrict_to_default_dest) {
1885 +                   pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1886 +                                                               pkg->name,
1887 +                                                               conf->default_dest);
1888 +               } else {
1889 +                   pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
1890 +               }
1891 +        
1892 +               if (pkg == NULL) {
1893 +                   ipkg_message(conf, IPKG_ERROR, "Package %s is not installed.\n", pkg->name);
1894 +                   continue;
1895 +               }
1896 +               if (pkg->state_status == SS_NOT_INSTALLED) {    // Added the control, so every already removed package could be skipped
1897 +                   ipkg_message(conf, IPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
1898 +                    continue;
1899 +               }
1900 +               ipkg_remove_pkg(conf, pkg_to_remove,0);
1901 +               done = 1;
1902 +           }
1903 +           free (pkg_name);
1904 +        }
1905 +        pkg_vec_free(available);
1906 +     } else {
1907 +         pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1908 +         int i;
1909 +         int flagged_pkg_count = 0;
1910 +         int removed;
1911 +
1912 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
1913 +
1914 +         for (i = 0; i < installed_pkgs->len; i++) {
1915 +              pkg_t *pkg = installed_pkgs->pkgs[i];
1916 +              if (pkg->state_flag & SF_USER) {
1917 +                   flagged_pkg_count++;
1918 +              } else {
1919 +                   if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
1920 +                        ipkg_message(conf, IPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
1921 +              }
1922 +         }
1923 +         if (!flagged_pkg_count) {
1924 +              ipkg_message(conf, IPKG_NOTICE, "No packages flagged as installed by user, \n"
1925 +                           "so refusing to uninstall unflagged non-leaf packages\n");
1926 +              return 0;
1927 +         }
1928 +
1929 +         /* find packages not flagged SF_USER (i.e., installed to
1930 +          * satisfy a dependence) and not having any dependents, and
1931 +          * remove them */
1932 +         do {
1933 +              removed = 0;
1934 +              for (i = 0; i < installed_pkgs->len; i++) {
1935 +                   pkg_t *pkg = installed_pkgs->pkgs[i];
1936 +                   if (!(pkg->state_flag & SF_USER)
1937 +                       && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
1938 +                        removed++;
1939 +                        ipkg_message(conf, IPKG_NOTICE, "Removing non-user leaf package %s\n");
1940 +                        ipkg_remove_pkg(conf, pkg,0);
1941 +                         done = 1;
1942 +                   }
1943 +              }
1944 +         } while (removed);
1945 +         pkg_vec_free(installed_pkgs);
1946 +     }
1947 +
1948 +     if ( done == 0 ) 
1949 +        ipkg_message(conf, IPKG_NOTICE, "No packages removed.\n");
1950 +
1951 +     write_status_files_if_changed(conf);
1952 +     return 0;
1953 +}
1954 +
1955 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv)
1956 +{
1957 +     int i;
1958 +     pkg_t *pkg;
1959 +
1960 +     global_conf = conf;
1961 +     signal(SIGINT, sigint_handler);
1962 +
1963 +     pkg_info_preinstall_check(conf);
1964 +
1965 +     for (i=0; i < argc; i++) {
1966 +         if (conf->restrict_to_default_dest) {
1967 +              pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1968 +                                                          argv[i],
1969 +                                                          conf->default_dest);
1970 +         } else {
1971 +              pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1972 +         }
1973 +
1974 +         if (pkg == NULL) {
1975 +              ipkg_message(conf, IPKG_ERROR,
1976 +                           "Package %s is not installed.\n", argv[i]);
1977 +              continue;
1978 +         }
1979 +         ipkg_purge_pkg(conf, pkg);
1980 +     }
1981 +
1982 +     write_status_files_if_changed(conf);
1983 +     return 0;
1984 +}
1985 +
1986 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv)
1987 +{
1988 +     int i;
1989 +     pkg_t *pkg;
1990 +     const char *flags = argv[0];
1991 +    
1992 +     global_conf = conf;
1993 +     signal(SIGINT, sigint_handler);
1994 +
1995 +     for (i=1; i < argc; i++) {
1996 +         if (conf->restrict_to_default_dest) {
1997 +              pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1998 +                                                          argv[i],
1999 +                                                          conf->default_dest);
2000 +         } else {
2001 +              pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
2002 +         }
2003 +
2004 +         if (pkg == NULL) {
2005 +              ipkg_message(conf, IPKG_ERROR,
2006 +                           "Package %s is not installed.\n", argv[i]);
2007 +              continue;
2008 +         }
2009 +          if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
2010 +              ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
2011 +             pkg->state_flag = pkg_state_flag_from_str(flags);
2012 +          }
2013 +/* pb_ asked this feature 03292004 */
2014 +/* Actually I will use only this two, but this is an open for various status */
2015 +          if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
2016 +             pkg->state_status = pkg_state_status_from_str(flags);
2017 +          }
2018 +         ipkg_state_changed++;
2019 +         ipkg_message(conf, IPKG_NOTICE,
2020 +                      "Setting flags for package %s to %s\n",
2021 +                      pkg->name, flags);
2022 +     }
2023 +
2024 +     write_status_files_if_changed(conf);
2025 +     return 0;
2026 +}
2027 +
2028 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv)
2029 +{
2030 +     pkg_t *pkg;
2031 +     str_list_t *installed_files;
2032 +     str_list_elt_t *iter;
2033 +     char *pkg_version;
2034 +     size_t buff_len = 8192;
2035 +     size_t used_len;
2036 +     char *buff ;
2037 +
2038 +     buff = (char *)malloc(buff_len);
2039 +     if ( buff == NULL ) {
2040 +        fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
2041 +        return ENOMEM;
2042 +     }
2043
2044 +     if (argc < 1) {
2045 +         return EINVAL;
2046 +     }
2047 +
2048 +     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
2049 +                                           argv[0]);
2050 +     if (pkg == NULL) {
2051 +         ipkg_message(conf, IPKG_ERROR,
2052 +                      "Package %s not installed.\n", argv[0]);
2053 +         return 0;
2054 +     }
2055 +
2056 +     installed_files = pkg_get_installed_files(pkg);
2057 +     pkg_version = pkg_version_str_alloc(pkg);
2058 +
2059 +#ifndef IPKG_LIB
2060 +     printf("Package %s (%s) is installed on %s and has the following files:\n",
2061 +           pkg->name, pkg_version, pkg->dest->name);
2062 +     for (iter = installed_files->head; iter; iter = iter->next) {
2063 +         puts(iter->data);
2064 +     }
2065 +#else
2066 +     if (buff) {
2067 +     try_again:
2068 +         used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
2069 +                             pkg->name, pkg_version, pkg->dest->name) + 1;
2070 +         if (used_len > buff_len) {
2071 +              buff_len *= 2;
2072 +              buff = realloc (buff, buff_len);
2073 +              goto try_again;
2074 +         }
2075 +         for (iter = installed_files->head; iter; iter = iter->next) {
2076 +              used_len += strlen (iter->data) + 1;
2077 +              while (buff_len <= used_len) {
2078 +                   buff_len *= 2;
2079 +                   buff = realloc (buff, buff_len);
2080 +              }
2081 +              strncat(buff, iter->data, buff_len);
2082 +              strncat(buff, "\n", buff_len);
2083 +         } 
2084 +         if (ipkg_cb_list) ipkg_cb_list(pkg->name,
2085 +                                        buff,
2086 +                                        pkg_version_str_alloc(pkg),
2087 +                                        pkg->state_status,
2088 +                                        p_userdata);
2089 +         free(buff);
2090 +     }
2091 +#endif
2092 +
2093 +     free(pkg_version);
2094 +     pkg_free_installed_files(pkg);
2095 +
2096 +     return 0;
2097 +}
2098 +
2099 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2100 +{
2101 +
2102 +     if (argc > 0) {
2103 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2104 +         const char *rel_str = "depends on";
2105 +         int i;
2106 +     
2107 +         pkg_info_preinstall_check(conf);
2108 +
2109 +         if (conf->query_all)
2110 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2111 +         else
2112 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2113 +         for (i = 0; i < argc; i++) {
2114 +              const char *target = argv[i];
2115 +              int j;
2116 +
2117 +              ipkg_message(conf, IPKG_ERROR, "target=%s\n", target);
2118 +
2119 +              for (j = 0; j < available_pkgs->len; j++) {
2120 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2121 +                   if (fnmatch(target, pkg->name, 0) == 0) {
2122 +                        int k;
2123 +                        int count = pkg->depends_count + pkg->pre_depends_count;
2124 +                        ipkg_message(conf, IPKG_ERROR, "What %s (arch=%s) %s\n",
2125 +                                     target, pkg->architecture, rel_str);
2126 +                        for (k = 0; k < count; k++) {
2127 +                             compound_depend_t *cdepend = &pkg->depends[k];
2128 +                             int l;
2129 +                             for (l = 0; l < cdepend->possibility_count; l++) {
2130 +                                  depend_t *possibility = cdepend->possibilities[l];
2131 +                                  ipkg_message(conf, IPKG_ERROR, "    %s", possibility->pkg->name);
2132 +                                  if (conf->verbosity > 0) {
2133 +                                       // char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 
2134 +                                       ipkg_message(conf, IPKG_NOTICE, " %s", possibility->version);
2135 +                                       if (possibility->version) {
2136 +                                            char *typestr = NULL;
2137 +                                            switch (possibility->constraint) {
2138 +                                            case NONE: typestr = "none"; break;
2139 +                                            case EARLIER: typestr = "<"; break;
2140 +                                            case EARLIER_EQUAL: typestr = "<="; break;
2141 +                                            case EQUAL: typestr = "="; break;
2142 +                                            case LATER_EQUAL: typestr = ">="; break;
2143 +                                            case LATER: typestr = ">"; break;
2144 +                                            }
2145 +                                            ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2146 +                                       }
2147 +                                       // free(ver);
2148 +                                  }
2149 +                                  ipkg_message(conf, IPKG_ERROR, "\n");
2150 +                             }
2151 +                        }
2152 +                   }
2153 +              }
2154 +         }
2155 +         pkg_vec_free(available_pkgs);
2156 +     }
2157 +     return 0;
2158 +}
2159 +
2160 +enum what_field_type {
2161 +  WHATDEPENDS,
2162 +  WHATCONFLICTS,
2163 +  WHATPROVIDES,
2164 +  WHATREPLACES,
2165 +  WHATRECOMMENDS,
2166 +  WHATSUGGESTS
2167 +};
2168 +
2169 +static int ipkg_what_depends_conflicts_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
2170 +{
2171 +
2172 +     if (argc > 0) {
2173 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2174 +         const char *rel_str = NULL;
2175 +         int i;
2176 +         int changed;
2177 +
2178 +         switch (what_field_type) {
2179 +         case WHATDEPENDS: rel_str = "depends on"; break;
2180 +         case WHATCONFLICTS: rel_str = "conflicts with"; break;
2181 +         case WHATSUGGESTS: rel_str = "suggests"; break;
2182 +         case WHATRECOMMENDS: rel_str = "recommends"; break;
2183 +         case WHATPROVIDES: rel_str = "provides"; break;
2184 +         case WHATREPLACES: rel_str = "replaces"; break;
2185 +         }
2186 +     
2187 +         if (conf->query_all)
2188 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2189 +         else
2190 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2191 +
2192 +         /* mark the root set */
2193 +         pkg_vec_clear_marks(available_pkgs);
2194 +         ipkg_message(conf, IPKG_NOTICE, "Root set:\n");
2195 +         for (i = 0; i < argc; i++) {
2196 +              const char *dependee_pattern = argv[i];
2197 +              pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
2198 +         }
2199 +         for (i = 0; i < available_pkgs->len; i++) {
2200 +              pkg_t *pkg = available_pkgs->pkgs[i];
2201 +              if (pkg->state_flag & SF_MARKED) {
2202 +                   /* mark the parent (abstract) package */
2203 +                   pkg_mark_provides(pkg);
2204 +                   ipkg_message(conf, IPKG_NOTICE, "  %s\n", pkg->name);
2205 +              }
2206 +         }
2207 +
2208 +         ipkg_message(conf, IPKG_NOTICE, "What %s root set\n", rel_str);
2209 +         do {
2210 +              int j;
2211 +              changed = 0;
2212 +
2213 +              for (j = 0; j < available_pkgs->len; j++) {
2214 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2215 +                   int k;
2216 +                   int count = ((what_field_type == WHATCONFLICTS)
2217 +                                ? pkg->conflicts_count
2218 +                                : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
2219 +                   /* skip this package if it is already marked */
2220 +                   if (pkg->parent->state_flag & SF_MARKED) {
2221 +                        continue;
2222 +                   }
2223 +                   for (k = 0; k < count; k++) {
2224 +                        compound_depend_t *cdepend = 
2225 +                             (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
2226 +                        int l;
2227 +                        for (l = 0; l < cdepend->possibility_count; l++) {
2228 +                             depend_t *possibility = cdepend->possibilities[l];
2229 +                             if (possibility->pkg->state_flag & SF_MARKED) {
2230 +                                  /* mark the depending package so we won't visit it again */
2231 +                                  pkg->state_flag |= SF_MARKED;
2232 +                                  pkg_mark_provides(pkg);
2233 +                                  changed++;
2234 +
2235 +                                  ipkg_message(conf, IPKG_NOTICE, "    %s", pkg->name);
2236 +                                  if (conf->verbosity > 0) {
2237 +                                       char *ver = pkg_version_str_alloc(pkg); 
2238 +                                       ipkg_message(conf, IPKG_NOTICE, " %s", ver);
2239 +                                       ipkg_message(conf, IPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
2240 +                                       if (possibility->version) {
2241 +                                            char *typestr = NULL;
2242 +                                            switch (possibility->constraint) {
2243 +                                            case NONE: typestr = "none"; break;
2244 +                                            case EARLIER: typestr = "<"; break;
2245 +                                            case EARLIER_EQUAL: typestr = "<="; break;
2246 +                                            case EQUAL: typestr = "="; break;
2247 +                                            case LATER_EQUAL: typestr = ">="; break;
2248 +                                            case LATER: typestr = ">"; break;
2249 +                                            }
2250 +                                            ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2251 +                                       }
2252 +                                       free(ver);
2253 +                                       if (!pkg_dependence_satisfiable(conf, possibility))
2254 +                                            ipkg_message(conf, IPKG_NOTICE, " unsatisfiable");
2255 +                                  }
2256 +                                  ipkg_message(conf, IPKG_NOTICE, "\n");
2257 +                                  goto next_package;
2258 +                             }
2259 +                        }
2260 +                   }
2261 +              next_package:
2262 +                   ;
2263 +              }
2264 +         } while (changed && recursive);
2265 +         pkg_vec_free(available_pkgs);
2266 +     }
2267 +
2268 +     return 0;
2269 +}
2270 +
2271 +int pkg_mark_provides(pkg_t *pkg)
2272 +{
2273 +     int provides_count = pkg->provides_count;
2274 +     abstract_pkg_t **provides = pkg->provides;
2275 +     int i;
2276 +     pkg->parent->state_flag |= SF_MARKED;
2277 +     for (i = 0; i < provides_count; i++) {
2278 +         provides[i]->state_flag |= SF_MARKED;
2279 +     }
2280 +     return 0;
2281 +}
2282 +
2283 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv)
2284 +{
2285 +     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
2286 +}
2287 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2288 +{
2289 +     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
2290 +}
2291 +
2292 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv)
2293 +{
2294 +     return ipkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
2295 +}
2296 +
2297 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2298 +{
2299 +     return ipkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
2300 +}
2301 +
2302 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv)
2303 +{
2304 +     return ipkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
2305 +}
2306 +
2307 +static int ipkg_what_provides_replaces_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
2308 +{
2309 +
2310 +     if (argc > 0) {
2311 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2312 +         const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
2313 +         int i;
2314 +     
2315 +         pkg_info_preinstall_check(conf);
2316 +
2317 +         if (conf->query_all)
2318 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2319 +         else
2320 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2321 +         for (i = 0; i < argc; i++) {
2322 +              const char *target = argv[i];
2323 +              int j;
2324 +
2325 +              ipkg_message(conf, IPKG_ERROR, "What %s %s\n",
2326 +                           rel_str, target);
2327 +              for (j = 0; j < available_pkgs->len; j++) {
2328 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2329 +                   int k;
2330 +                   int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
2331 +                   for (k = 0; k < count; k++) {
2332 +                        abstract_pkg_t *apkg = 
2333 +                             ((what_field_type == WHATPROVIDES) 
2334 +                              ? pkg->provides[k]
2335 +                              : pkg->replaces[k]);
2336 +                        if (fnmatch(target, apkg->name, 0) == 0) {
2337 +                             ipkg_message(conf, IPKG_ERROR, "    %s", pkg->name);
2338 +                             if (strcmp(target, apkg->name) != 0)
2339 +                                  ipkg_message(conf, IPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
2340 +                             ipkg_message(conf, IPKG_ERROR, "\n");
2341 +                        }
2342 +                   }
2343 +              }
2344 +         }
2345 +         pkg_vec_free(available_pkgs);
2346 +     }
2347 +     return 0;
2348 +}
2349 +
2350 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv)
2351 +{
2352 +     return ipkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
2353 +}
2354 +
2355 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv)
2356 +{
2357 +     return ipkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
2358 +}
2359 +
2360 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv)
2361 +{
2362 +     int i;
2363 +
2364 +     pkg_vec_t *installed;
2365 +     pkg_t *pkg;
2366 +     str_list_t *installed_files;
2367 +     str_list_elt_t *iter;
2368 +     char *installed_file;
2369 +
2370 +     if (argc < 1) {
2371 +         return EINVAL;
2372 +     }
2373
2374 +     installed = pkg_vec_alloc();
2375 +     pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
2376 +
2377 +     for (i=0; i < installed->len; i++) {
2378 +         pkg = installed->pkgs[i];
2379 +
2380 +         installed_files = pkg_get_installed_files(pkg);
2381 +
2382 +         for (iter = installed_files->head; iter; iter = iter->next) {
2383 +              installed_file = iter->data;
2384 +              if (fnmatch(argv[0], installed_file, 0)==0)  {
2385 +#ifndef IPKG_LIB
2386 +                   printf("%s: %s\n", pkg->name, installed_file);
2387 +#else
2388 +                       if (ipkg_cb_list) ipkg_cb_list(pkg->name, 
2389 +                                                      installed_file, 
2390 +                                                      pkg_version_str_alloc(pkg), 
2391 +                                                      pkg->state_status, p_userdata);
2392 +#endif                    
2393 +              }                
2394 +         }
2395 +
2396 +         pkg_free_installed_files(pkg);
2397 +     }
2398 +
2399 +     /* XXX: CLEANUP: It's not obvious from the name of
2400 +       pkg_hash_fetch_all_installed that we need to call
2401 +       pkg_vec_free to avoid a memory leak. */
2402 +     pkg_vec_free(installed);
2403 +
2404 +     return 0;
2405 +}
2406 +
2407 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv)
2408 +{
2409 +     if (argc == 3) {
2410 +         /* this is a bit gross */
2411 +         struct pkg p1, p2;
2412 +         parseVersion(&p1, argv[0]); 
2413 +         parseVersion(&p2, argv[2]); 
2414 +         return pkg_version_satisfied(&p1, &p2, argv[1]);
2415 +     } else {
2416 +         ipkg_message(conf, IPKG_ERROR,
2417 +                      "ipkg compare_versions <v1> <op> <v2>\n"
2418 +                      "<op> is one of <= >= << >> =\n");
2419 +         return -1;
2420 +     }
2421 +}
2422 +
2423 +#ifndef HOST_CPU_STR
2424 +#define HOST_CPU_STR__(X) #X
2425 +#define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
2426 +#define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
2427 +#endif
2428 +
2429 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv)
2430 +{
2431 +     nv_pair_list_elt_t *l;
2432 +
2433 +     l = conf->arch_list.head;
2434 +     while (l) {
2435 +         nv_pair_t *nv = l->data;
2436 +         printf("arch %s %s\n", nv->name, nv->value);
2437 +         l = l->next;
2438 +     }
2439 +     return 0;
2440 +}
2441 +
2442 +
2443 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.h
2444 --- busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.h      1970-01-01 01:00:00.000000000 +0100
2445 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.h     2006-07-22 16:31:25.000000000 +0200
2446 @@ -0,0 +1,46 @@
2447 +/* ipkg_cmd.h - the itsy package management system
2448 +
2449 +   Carl D. Worth
2450 +
2451 +   Copyright (C) 2001 University of Southern California
2452 +
2453 +   This program is free software; you can redistribute it and/or
2454 +   modify it under the terms of the GNU General Public License as
2455 +   published by the Free Software Foundation; either version 2, or (at
2456 +   your option) any later version.
2457 +
2458 +   This program is distributed in the hope that it will be useful, but
2459 +   WITHOUT ANY WARRANTY; without even the implied warranty of
2460 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2461 +   General Public License for more details.
2462 +*/
2463 +
2464 +#ifndef IPKG_CMD_H
2465 +#define IPKG_CMD_H
2466 +
2467 +typedef int (*ipkg_cmd_fun_t)(ipkg_conf_t *conf, int argc, const char **argv);
2468 +
2469 +struct ipkg_cmd
2470 +{
2471 +    char *name;
2472 +    int requires_args;
2473 +    ipkg_cmd_fun_t fun;
2474 +};
2475 +typedef struct ipkg_cmd ipkg_cmd_t;
2476 +
2477 +ipkg_cmd_t *ipkg_cmd_find(const char *name);
2478 +#ifdef IPKG_LIB
2479 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, 
2480 +                  const char **argv, void *userdata);
2481 +#else
2482 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv);
2483 +#endif
2484 +int ipkg_multiple_files_scan (ipkg_conf_t *conf, int argc, char *argv[]);
2485 +/* install any packges with state_want == SW_INSTALL */
2486 +int ipkg_install_wanted_packages(ipkg_conf_t *conf);
2487 +/* ensure that all dependences are satisfied */
2488 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name);
2489 +
2490 +int pkg_mark_provides(pkg_t *pkg);
2491 +
2492 +#endif
2493 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_conf.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.c
2494 --- busybox-1.2.0-orig/archival/libipkg/ipkg_conf.c     1970-01-01 01:00:00.000000000 +0100
2495 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.c    2006-07-22 16:31:25.000000000 +0200
2496 @@ -0,0 +1,711 @@
2497 +/* ipkg_conf.c - the itsy package management system
2498 +
2499 +   Carl D. Worth
2500 +
2501 +   Copyright (C) 2001 University of Southern California
2502 +
2503 +   This program is free software; you can redistribute it and/or
2504 +   modify it under the terms of the GNU General Public License as
2505 +   published by the Free Software Foundation; either version 2, or (at
2506 +   your option) any later version.
2507 +
2508 +   This program is distributed in the hope that it will be useful, but
2509 +   WITHOUT ANY WARRANTY; without even the implied warranty of
2510 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2511 +   General Public License for more details.
2512 +*/
2513 +
2514 +#include <glob.h>
2515 +
2516 +#include "ipkg.h"
2517 +#include "ipkg_conf.h"
2518 +
2519 +#include "xregex.h"
2520 +#include "sprintf_alloc.h"
2521 +#include "ipkg_conf.h"
2522 +#include "ipkg_message.h"
2523 +#include "file_util.h"
2524 +#include "str_util.h"
2525 +#include "xsystem.h"
2526 +
2527 +
2528 +ipkg_conf_t *global_conf;
2529 +
2530 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2531 +                               pkg_src_list_t *pkg_src_list,
2532 +                               nv_pair_list_t *tmp_dest_nv_pair_list,
2533 +                               char **tmp_lists_dir);
2534 +static int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options);
2535 +static int ipkg_conf_set_option(const ipkg_option_t *options,
2536 +                               const char *name, const char *value);
2537 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2538 +                                     const char *default_dest_name);
2539 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf,
2540 +                                    pkg_src_list_t *nv_pair_list);
2541 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf,
2542 +                                     nv_pair_list_t *nv_pair_list, char * lists_dir);
2543 +
2544 +int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options)
2545 +{
2546 +     ipkg_option_t tmp[] = {
2547 +         { "force_defaults", IPKG_OPT_TYPE_BOOL, &conf->force_defaults },
2548 +         { "force_depends", IPKG_OPT_TYPE_BOOL, &conf->force_depends },
2549 +         { "force_overwrite", IPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
2550 +         { "force_downgrade", IPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
2551 +         { "force_reinstall", IPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
2552 +         { "force_space", IPKG_OPT_TYPE_BOOL, &conf->force_space },
2553 +         { "ftp_proxy", IPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
2554 +         { "http_proxy", IPKG_OPT_TYPE_STRING, &conf->http_proxy },
2555 +         { "multiple_providers", IPKG_OPT_TYPE_BOOL, &conf->multiple_providers },
2556 +         { "no_proxy", IPKG_OPT_TYPE_STRING, &conf->no_proxy },
2557 +         { "test", IPKG_OPT_TYPE_INT, &conf->noaction },
2558 +         { "noaction", IPKG_OPT_TYPE_INT, &conf->noaction },
2559 +         { "nodeps", IPKG_OPT_TYPE_BOOL, &conf->nodeps },
2560 +         { "offline_root", IPKG_OPT_TYPE_STRING, &conf->offline_root },
2561 +         { "offline_root_post_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
2562 +         { "offline_root_pre_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
2563 +         { "proxy_passwd", IPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
2564 +         { "proxy_user", IPKG_OPT_TYPE_STRING, &conf->proxy_user },
2565 +         { "query-all", IPKG_OPT_TYPE_BOOL, &conf->query_all },
2566 +         { "verbose-wget", IPKG_OPT_TYPE_BOOL, &conf->verbose_wget },
2567 +         { "verbosity", IPKG_OPT_TYPE_BOOL, &conf->verbosity },
2568 +         { NULL }
2569 +     };
2570 +
2571 +     *options = (ipkg_option_t *)malloc(sizeof(tmp));
2572 +     if ( options == NULL ){
2573 +        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
2574 +        return -1;
2575 +     }
2576 +
2577 +     memcpy(*options, tmp, sizeof(tmp));
2578 +     return 0;
2579 +};
2580 +
2581 +static void ipkg_conf_override_string(char **conf_str, char *arg_str) 
2582 +{
2583 +     if (arg_str) {
2584 +         if (*conf_str) {
2585 +              free(*conf_str);
2586 +         }
2587 +         *conf_str = strdup(arg_str);
2588 +     }
2589 +}
2590 +
2591 +static void ipkg_conf_free_string(char **conf_str)
2592 +{
2593 +     if (*conf_str) {
2594 +         free(*conf_str);
2595 +         *conf_str = NULL;
2596 +     }
2597 +}
2598 +
2599 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args)
2600 +{
2601 +     int err;
2602 +     char *tmp_dir_base;
2603 +     nv_pair_list_t tmp_dest_nv_pair_list;
2604 +     char * lists_dir =NULL;
2605 +     glob_t globbuf;
2606 +     char *etc_ipkg_conf_pattern = "/etc/ipkg/*.conf";
2607 +     char *pending_dir  =NULL;
2608 +
2609 +     memset(conf, 0, sizeof(ipkg_conf_t));
2610 +
2611 +     pkg_src_list_init(&conf->pkg_src_list);
2612 +
2613 +     nv_pair_list_init(&tmp_dest_nv_pair_list);
2614 +     pkg_dest_list_init(&conf->pkg_dest_list);
2615 +
2616 +     nv_pair_list_init(&conf->arch_list);
2617 +
2618 +     conf->restrict_to_default_dest = 0;
2619 +     conf->default_dest = NULL;
2620 +
2621 +
2622 +     if (args->tmp_dir)
2623 +         tmp_dir_base = args->tmp_dir;
2624 +     else 
2625 +         tmp_dir_base = getenv("TMPDIR");
2626 +     sprintf_alloc(&conf->tmp_dir, "%s/%s",
2627 +                  tmp_dir_base ? tmp_dir_base : IPKG_CONF_DEFAULT_TMP_DIR_BASE,
2628 +                  IPKG_CONF_TMP_DIR_SUFFIX);
2629 +     conf->tmp_dir = mkdtemp(conf->tmp_dir);
2630 +     if (conf->tmp_dir == NULL) {
2631 +         fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
2632 +                 __FUNCTION__, conf->tmp_dir, strerror(errno));
2633 +         return errno;
2634 +     }
2635 +
2636 +     conf->force_depends = 0;
2637 +     conf->force_defaults = 0;
2638 +     conf->force_overwrite = 0;
2639 +     conf->force_downgrade = 0;
2640 +     conf->force_reinstall = 0;
2641 +     conf->force_space = 0;
2642 +     conf->force_removal_of_essential_packages = 0;
2643 +     conf->force_removal_of_dependent_packages = 0;
2644 +     conf->nodeps = 0;
2645 +     conf->verbose_wget = 0;
2646 +     conf->offline_root = NULL;
2647 +     conf->offline_root_pre_script_cmd = NULL;
2648 +     conf->offline_root_post_script_cmd = NULL;
2649 +     conf->multiple_providers = 0;
2650 +     conf->verbosity = 1;
2651 +     conf->noaction = 0;
2652 +
2653 +     conf->http_proxy = NULL;
2654 +     conf->ftp_proxy = NULL;
2655 +     conf->no_proxy = NULL;
2656 +     conf->proxy_user = NULL;
2657 +     conf->proxy_passwd = NULL;
2658 +
2659 +     pkg_hash_init("pkg-hash", &conf->pkg_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2660 +     hash_table_init("file-hash", &conf->file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2661 +     hash_table_init("obs-file-hash", &conf->obs_file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2662 +     lists_dir=(char *)malloc(1);
2663 +     lists_dir[0]='\0';
2664 +     if (args->conf_file) {
2665 +         struct stat stat_buf;
2666 +         err = stat(args->conf_file, &stat_buf);
2667 +         if (err == 0)
2668 +              if (ipkg_conf_parse_file(conf, args->conf_file,
2669 +                                   &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2670 +                   /* Memory leakage from ipkg_conf_parse-file */
2671 +                   return -1;
2672 +               }
2673 +                   
2674 +     }
2675 +
2676 +     /* if (!lists_dir ){*/
2677 +     if (strlen(lists_dir)<=1 ){
2678 +        lists_dir = realloc(lists_dir,strlen(IPKG_CONF_LISTS_DIR)+2);
2679 +        sprintf (lists_dir,"%s",IPKG_CONF_LISTS_DIR);
2680 +     }
2681 +
2682 +     if (args->offline_root) {
2683 +            char *tmp = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
2684 +            sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
2685 +            free(lists_dir);
2686 +            lists_dir = tmp;
2687 +     }
2688 +
2689 +     pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
2690 +     snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
2691 +
2692 +     conf->lists_dir = strdup(lists_dir);
2693 +     conf->pending_dir = strdup(pending_dir);
2694 +
2695 +     if (args->offline_root) 
2696 +         sprintf_alloc(&etc_ipkg_conf_pattern, "%s/etc/ipkg/*.conf", args->offline_root);
2697 +     memset(&globbuf, 0, sizeof(globbuf));
2698 +     err = glob(etc_ipkg_conf_pattern, 0, NULL, &globbuf);
2699 +     if (!err) {
2700 +         int i;
2701 +         for (i = 0; i < globbuf.gl_pathc; i++) {
2702 +              if (globbuf.gl_pathv[i]) 
2703 +                   if ( ipkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
2704 +                                        &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2705 +                        /* Memory leakage from ipkg_conf_parse-file */
2706 +                        return -1;
2707 +                   }
2708 +         }
2709 +     }
2710 +     globfree(&globbuf);
2711 +
2712 +     /* if no architectures were defined, then default all, noarch, and host architecture */
2713 +     if (nv_pair_list_empty(&conf->arch_list)) {
2714 +         nv_pair_list_append(&conf->arch_list, "all", "1");
2715 +         nv_pair_list_append(&conf->arch_list, "noarch", "1");
2716 +         nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
2717 +     }
2718 +
2719 +     /* Even if there is no conf file, we'll need at least one dest. */
2720 +     if (tmp_dest_nv_pair_list.head == NULL) {
2721 +         nv_pair_list_append(&tmp_dest_nv_pair_list,
2722 +                             IPKG_CONF_DEFAULT_DEST_NAME,
2723 +                             IPKG_CONF_DEFAULT_DEST_ROOT_DIR);
2724 +     }
2725 +
2726 +     /* After parsing the file, set options from command-line, (so that
2727 +       command-line arguments take precedence) */
2728 +     /* XXX: CLEANUP: The interaction between args.c and ipkg_conf.c
2729 +       really needs to be cleaned up. There is so much duplication
2730 +       right now it is ridiculous. Maybe ipkg_conf_t should just save
2731 +       a pointer to args_t (which could then not be freed), rather
2732 +       than duplicating every field here? */
2733 +     if (args->force_depends) {
2734 +         conf->force_depends = 1;
2735 +     }
2736 +     if (args->force_defaults) {
2737 +         conf->force_defaults = 1;
2738 +     }
2739 +     if (args->force_overwrite) {
2740 +         conf->force_overwrite = 1;
2741 +     }
2742 +     if (args->force_downgrade) {
2743 +         conf->force_downgrade = 1;
2744 +     }
2745 +     if (args->force_reinstall) {
2746 +         conf->force_reinstall = 1;
2747 +     }
2748 +     if (args->force_removal_of_dependent_packages) {
2749 +         conf->force_removal_of_dependent_packages = 1;
2750 +     }
2751 +     if (args->force_removal_of_essential_packages) {
2752 +         conf->force_removal_of_essential_packages = 1;
2753 +     }
2754 +     if (args->nodeps) {
2755 +         conf->nodeps = 1;
2756 +     }
2757 +     if (args->noaction) {
2758 +         conf->noaction = 1;
2759 +     }
2760 +     if (args->query_all) {
2761 +         conf->query_all = 1;
2762 +     }
2763 +     if (args->verbose_wget) {
2764 +         conf->verbose_wget = 1;
2765 +     }
2766 +     if (args->multiple_providers) {
2767 +         conf->multiple_providers = 1;
2768 +     }
2769 +     if (args->verbosity != conf->verbosity) {
2770 +         conf->verbosity = args->verbosity;
2771 +     } 
2772 +
2773 +     ipkg_conf_override_string(&conf->offline_root, 
2774 +                              args->offline_root);
2775 +     ipkg_conf_override_string(&conf->offline_root_pre_script_cmd, 
2776 +                              args->offline_root_pre_script_cmd);
2777 +     ipkg_conf_override_string(&conf->offline_root_post_script_cmd, 
2778 +                              args->offline_root_post_script_cmd);
2779 +
2780 +/* Pigi: added a flag to disable the checking of structures if the command does not need to 
2781 +         read anything from there.
2782 +*/
2783 +     if ( !(args->nocheckfordirorfile)){
2784 +        /* need to run load the source list before dest list -Jamey */
2785 +        if ( !(args->noreadfeedsfile))
2786 +           set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
2787 +   
2788 +        /* Now that we have resolved conf->offline_root, we can commit to
2789 +          the directory names for the dests and load in all the package
2790 +          lists. */
2791 +        set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
2792 +   
2793 +        if (args->dest) {
2794 +            err = ipkg_conf_set_default_dest(conf, args->dest);
2795 +            if (err) {
2796 +                 return err;
2797 +            }
2798 +        }
2799 +     }
2800 +     nv_pair_list_deinit(&tmp_dest_nv_pair_list);
2801 +     free(lists_dir);
2802 +     free(pending_dir);
2803 +
2804 +     return 0;
2805 +}
2806 +
2807 +void ipkg_conf_deinit(ipkg_conf_t *conf)
2808 +{
2809 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
2810 +#error
2811 +     fprintf(stderr, "%s: Not cleaning up %s since ipkg compiled "
2812 +            "with IPKG_DEBUG_NO_TMP_CLEANUP\n",
2813 +            __FUNCTION__, conf->tmp_dir);
2814 +#else
2815 +     int err;
2816 +
2817 +     err = rmdir(conf->tmp_dir);
2818 +     if (err) {
2819 +         if (errno == ENOTEMPTY) {
2820 +              char *cmd;
2821 +              sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
2822 +              err = xsystem(cmd);
2823 +              free(cmd);
2824 +         }
2825 +         if (err)
2826 +              fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
2827 +     }
2828 +#endif /* IPKG_DEBUG_NO_TMP_CLEANUP */
2829 +
2830 +     free(conf->tmp_dir); /*XXX*/
2831 +
2832 +     pkg_src_list_deinit(&conf->pkg_src_list);
2833 +     pkg_dest_list_deinit(&conf->pkg_dest_list);
2834 +     nv_pair_list_deinit(&conf->arch_list);
2835 +     if (&conf->pkg_hash)
2836 +                   pkg_hash_deinit(&conf->pkg_hash);
2837 +     if (&conf->file_hash)
2838 +                   hash_table_deinit(&conf->file_hash);
2839 +     if (&conf->obs_file_hash)
2840 +                   hash_table_deinit(&conf->obs_file_hash);
2841 +
2842 +     ipkg_conf_free_string(&conf->offline_root);
2843 +     ipkg_conf_free_string(&conf->offline_root_pre_script_cmd);
2844 +     ipkg_conf_free_string(&conf->offline_root_post_script_cmd);
2845 +
2846 +     if (conf->verbosity > 1) { 
2847 +         int i;
2848 +         hash_table_t *hashes[] = {
2849 +              &conf->pkg_hash,
2850 +              &conf->file_hash,
2851 +              &conf->obs_file_hash };
2852 +         for (i = 0; i < 3; i++) {
2853 +              hash_table_t *hash = hashes[i];
2854 +              int c = 0;
2855 +              int n_conflicts = 0;
2856 +              int j;
2857 +              for (j = 0; j < hash->n_entries; j++) {
2858 +                   int len = 0;
2859 +                   hash_entry_t *e = &hash->entries[j];
2860 +                   if (e->next)
2861 +                        n_conflicts++;
2862 +                   while (e && e->key) {
2863 +                        len++;
2864 +                        e = e->next;
2865 +                   }
2866 +                   if (len > c) 
2867 +                        c = len;
2868 +              }
2869 +              ipkg_message(conf, IPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 
2870 +                           hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
2871 +              hash_table_deinit(hash);
2872 +         }
2873 +     }
2874 +}
2875 +
2876 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2877 +                                     const char *default_dest_name)
2878 +{
2879 +     pkg_dest_list_elt_t *iter;
2880 +     pkg_dest_t *dest;
2881 +
2882 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
2883 +         dest = iter->data;
2884 +         if (strcmp(dest->name, default_dest_name) == 0) {
2885 +              conf->default_dest = dest;
2886 +              conf->restrict_to_default_dest = 1;
2887 +              return 0;
2888 +         }
2889 +     }
2890 +
2891 +     fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
2892 +
2893 +     return 1;
2894 +}
2895 +
2896 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
2897 +{
2898 +     pkg_src_list_elt_t *iter;
2899 +     pkg_src_t *src;
2900 +     char *list_file;
2901 +
2902 +     for (iter = pkg_src_list->head; iter; iter = iter->next) {
2903 +          src = iter->data;
2904 +         if (src == NULL) {
2905 +              continue;
2906 +         }
2907 +
2908 +         sprintf_alloc(&list_file, "%s/%s", 
2909 +                         conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
2910 +                         src->name);
2911 +
2912 +         if (file_exists(list_file)) {
2913 +              pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
2914 +         }
2915 +         free(list_file);
2916 +     }
2917 +
2918 +     return 0;
2919 +}
2920 +
2921 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
2922 +{
2923 +     nv_pair_list_elt_t *iter;
2924 +     nv_pair_t *nv_pair;
2925 +     pkg_dest_t *dest;
2926 +     char *root_dir;
2927 +
2928 +     for (iter = nv_pair_list->head; iter; iter = iter->next) {
2929 +         nv_pair = iter->data;
2930 +
2931 +         if (conf->offline_root) {
2932 +              sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
2933 +         } else {
2934 +              root_dir = strdup(nv_pair->value);
2935 +         }
2936 +         dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
2937 +         free(root_dir);
2938 +         if (dest == NULL) {
2939 +              continue;
2940 +         }
2941 +         if (conf->default_dest == NULL) {
2942 +              conf->default_dest = dest;
2943 +         }
2944 +         if (file_exists(dest->status_file_name)) {
2945 +              pkg_hash_add_from_file(conf, dest->status_file_name,
2946 +                                     NULL, dest, 1);
2947 +         }
2948 +     }
2949 +
2950 +     return 0;
2951 +}
2952 +
2953 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2954 +                               pkg_src_list_t *pkg_src_list,
2955 +                               nv_pair_list_t *tmp_dest_nv_pair_list,
2956 +                               char **lists_dir)
2957 +{
2958 +     ipkg_option_t * options;
2959 +     FILE *file = fopen(filename, "r");
2960 +     regex_t valid_line_re, comment_re;
2961 +#define regmatch_size 12
2962 +     regmatch_t regmatch[regmatch_size];
2963 +
2964 +     if (ipkg_init_options_array(conf, &options)<0)
2965 +        return ENOMEM;
2966 +
2967 +     if (file == NULL) {
2968 +         fprintf(stderr, "%s: failed to open %s: %s\n",
2969 +                 __FUNCTION__, filename, strerror(errno));
2970 +         free(options);
2971 +         return errno;
2972 +     }
2973 +     ipkg_message(conf, IPKG_NOTICE, "loading conf file %s\n", filename);
2974 +
2975 +     xregcomp(&comment_re, 
2976 +                   "^[[:space:]]*(#.*|[[:space:]]*)$",
2977 +                   REG_EXTENDED);
2978 +     xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
2979 +
2980 +     while(1) {
2981 +         int line_num = 0;
2982 +         char *line;
2983 +         char *type, *name, *value, *extra;
2984 +
2985 +         line = file_read_line_alloc(file);
2986 +         line_num++;
2987 +         if (line == NULL) {
2988 +              break;
2989 +         }
2990 +
2991 +         str_chomp(line);
2992 +
2993 +         if (regexec(&comment_re, line, 0, 0, 0) == 0) {
2994 +              goto NEXT_LINE;
2995 +         }
2996 +
2997 +         if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
2998 +              str_chomp(line);
2999 +              fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
3000 +                      filename, line_num, line);
3001 +              goto NEXT_LINE;
3002 +         }
3003 +
3004 +         /* This has to be so ugly to deal with optional quotation marks */
3005 +         if (regmatch[2].rm_so > 0) {
3006 +              type = strndup(line + regmatch[2].rm_so,
3007 +                             regmatch[2].rm_eo - regmatch[2].rm_so);
3008 +         } else {
3009 +              type = strndup(line + regmatch[3].rm_so,
3010 +                             regmatch[3].rm_eo - regmatch[3].rm_so);
3011 +         }
3012 +         if (regmatch[5].rm_so > 0) {
3013 +              name = strndup(line + regmatch[5].rm_so,
3014 +                             regmatch[5].rm_eo - regmatch[5].rm_so);
3015 +         } else {
3016 +              name = strndup(line + regmatch[6].rm_so,
3017 +                             regmatch[6].rm_eo - regmatch[6].rm_so);
3018 +         }
3019 +         if (regmatch[8].rm_so > 0) {
3020 +              value = strndup(line + regmatch[8].rm_so,
3021 +                              regmatch[8].rm_eo - regmatch[8].rm_so);
3022 +         } else {
3023 +              value = strndup(line + regmatch[9].rm_so,
3024 +                              regmatch[9].rm_eo - regmatch[9].rm_so);
3025 +         }
3026 +         extra = NULL;
3027 +         if (regmatch[11].rm_so > 0) {
3028 +              extra = strndup (line + regmatch[11].rm_so,
3029 +                               regmatch[11].rm_eo - regmatch[11].rm_so);
3030 +         }
3031 +
3032 +         /* We use the tmp_dest_nv_pair_list below instead of
3033 +            conf->pkg_dest_list because we might encounter an
3034 +            offline_root option later and that would invalidate the
3035 +            directories we would have computed in
3036 +            pkg_dest_list_init. (We do a similar thing with
3037 +            tmp_src_nv_pair_list for sake of symmetry.) */
3038 +         if (strcmp(type, "option") == 0) {
3039 +              ipkg_conf_set_option(options, name, value);
3040 +         } else if (strcmp(type, "src") == 0) {
3041 +              if (!nv_pair_list_find(pkg_src_list, name)) {
3042 +                   pkg_src_list_append (pkg_src_list, name, value, extra, 0);
3043 +              } else {
3044 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
3045 +                                name, value);
3046 +              }
3047 +         } else if (strcmp(type, "src/gz") == 0) {
3048 +              if (!nv_pair_list_find(pkg_src_list, name)) {
3049 +                   pkg_src_list_append (pkg_src_list, name, value, extra, 1);
3050 +              } else {
3051 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
3052 +                                name, value);
3053 +              }
3054 +         } else if (strcmp(type, "dest") == 0) {
3055 +              nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
3056 +         } else if (strcmp(type, "lists_dir") == 0) {
3057 +              *lists_dir = realloc(*lists_dir,strlen(value)+1);
3058 +               if (*lists_dir == NULL) {
3059 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: Not enough memory\n");
3060 +                   free(options);
3061 +                   return EINVAL;
3062 +               }
3063 +               sprintf (*lists_dir,"%s",value);
3064 +         } else if (strcmp(type, "arch") == 0) {
3065 +              ipkg_message(conf, IPKG_INFO, "supported arch %s priority (%s)\n", name, value);
3066 +              if (!value) {
3067 +                   ipkg_message(conf, IPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
3068 +                   value = strdup("10");
3069 +              }
3070 +              nv_pair_list_append(&conf->arch_list, strdup(name), strdup(value));
3071 +         } else {
3072 +              fprintf(stderr, "WARNING: Ignoring unknown configuration "
3073 +                      "parameter: %s %s %s\n", type, name, value);
3074 +              free(options);
3075 +              return EINVAL;
3076 +         }
3077 +
3078 +         free(type);
3079 +         free(name);
3080 +         free(value);
3081 +         if (extra)
3082 +              free (extra);
3083 +
3084 +     NEXT_LINE:
3085 +         free(line);
3086 +     }
3087 +
3088 +     free(options);
3089 +     regfree(&comment_re);
3090 +     regfree(&valid_line_re);
3091 +     fclose(file);
3092 +
3093 +     return 0;
3094 +}
3095 +
3096 +static int ipkg_conf_set_option(const ipkg_option_t *options,
3097 +                               const char *name, const char *value)
3098 +{
3099 +     int i = 0;
3100 +     while (options[i].name) {
3101 +         if (strcmp(options[i].name, name) == 0) {
3102 +              switch (options[i].type) {
3103 +              case IPKG_OPT_TYPE_BOOL:
3104 +                   *((int *)options[i].value) = 1;
3105 +                   return 0;
3106 +              case IPKG_OPT_TYPE_INT:
3107 +                   if (value) {
3108 +                        *((int *)options[i].value) = atoi(value);
3109 +                        return 0;
3110 +                   } else {
3111 +                        printf("%s: Option %s need an argument\n",
3112 +                               __FUNCTION__, name);
3113 +                        return EINVAL;
3114 +                   }               
3115 +              case IPKG_OPT_TYPE_STRING:
3116 +                   if (value) {
3117 +                        *((char **)options[i].value) = strdup(value);
3118 +                        return 0;
3119 +                   } else {
3120 +                        printf("%s: Option %s need an argument\n",
3121 +                               __FUNCTION__, name);
3122 +                        return EINVAL;
3123 +                   }
3124 +              }
3125 +         }
3126 +         i++;
3127 +     }
3128 +    
3129 +     fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
3130 +            __FUNCTION__, name, value);
3131 +     return EINVAL;
3132 +}
3133 +
3134 +int ipkg_conf_write_status_files(ipkg_conf_t *conf)
3135 +{
3136 +     pkg_dest_list_elt_t *iter;
3137 +     pkg_dest_t *dest;
3138 +     pkg_vec_t *all;
3139 +     pkg_t *pkg;
3140 +     register int i;
3141 +     int err;
3142 +
3143 +     if (conf->noaction)
3144 +         return 0;
3145 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3146 +         dest = iter->data;
3147 +         dest->status_file = fopen(dest->status_file_tmp_name, "w");
3148 +         if (dest->status_file == NULL) {
3149 +              fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
3150 +                      __FUNCTION__, dest->status_file_name, strerror(errno));
3151 +         }
3152 +     }
3153 +
3154 +     all = pkg_vec_alloc();
3155 +     pkg_hash_fetch_available(&conf->pkg_hash, all);
3156 +
3157 +     for(i = 0; i < all->len; i++) {
3158 +         pkg = all->pkgs[i];
3159 +         /* We don't need most uninstalled packages in the status file */
3160 +         if (pkg->state_status == SS_NOT_INSTALLED
3161 +             && (pkg->state_want == SW_UNKNOWN
3162 +                 || pkg->state_want == SW_DEINSTALL
3163 +                 || pkg->state_want == SW_PURGE)) {
3164 +              continue;
3165 +         }
3166 +         if (!pkg) {
3167 +           fprintf(stderr, "Null package\n");
3168 +         }
3169 +         if (pkg->dest == NULL) {
3170 +              fprintf(stderr, "%s: ERROR: Can't write status for "
3171 +                      "package %s since it has a NULL dest\n",
3172 +                      __FUNCTION__, pkg->name);
3173 +              continue;
3174 +         }
3175 +         if (pkg->dest->status_file) {
3176 +              pkg_print_status(pkg, pkg->dest->status_file);
3177 +         }
3178 +     }
3179 +
3180 +     pkg_vec_free(all);
3181 +
3182 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3183 +         dest = iter->data;
3184 +         if (dest->status_file) {
3185 +              err = ferror(dest->status_file);
3186 +              fclose(dest->status_file);
3187 +              dest->status_file = NULL;
3188 +              if (!err) {
3189 +                   file_move(dest->status_file_tmp_name, dest->status_file_name);
3190 +              } else {
3191 +                   fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
3192 +                           "retaining old %s\n", __FUNCTION__, 
3193 +                           dest->status_file_tmp_name, dest->status_file_name);
3194 +              }
3195 +         }
3196 +     }
3197 +
3198 +     return 0;
3199 +}
3200 +
3201 +
3202 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename)
3203 +{
3204 +     char *root_filename;
3205 +     sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
3206 +     return root_filename;
3207 +}
3208 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_conf.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.h
3209 --- busybox-1.2.0-orig/archival/libipkg/ipkg_conf.h     1970-01-01 01:00:00.000000000 +0100
3210 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.h    2006-07-22 16:31:25.000000000 +0200
3211 @@ -0,0 +1,107 @@
3212 +/* ipkg_conf.h - the itsy package management system
3213 +
3214 +   Carl D. Worth
3215 +
3216 +   Copyright (C) 2001 University of Southern California
3217 +
3218 +   This program is free software; you can redistribute it and/or
3219 +   modify it under the terms of the GNU General Public License as
3220 +   published by the Free Software Foundation; either version 2, or (at
3221 +   your option) any later version.
3222 +
3223 +   This program is distributed in the hope that it will be useful, but
3224 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3225 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3226 +   General Public License for more details.
3227 +*/
3228 +
3229 +#ifndef IPKG_CONF_H
3230 +#define IPKG_CONF_H
3231 +
3232 +typedef struct ipkg_conf ipkg_conf_t;
3233 +
3234 +#include "hash_table.h"
3235 +#include "ipkg.h"
3236 +#include "args.h"
3237 +#include "pkg.h"
3238 +#include "pkg_hash.h"
3239 +#include "pkg_src_list.h"
3240 +#include "pkg_dest_list.h"
3241 +#include "nv_pair_list.h"
3242 +
3243 +#define IPKG_CONF_DEFAULT_TMP_DIR_BASE "/tmp"
3244 +#define IPKG_CONF_TMP_DIR_SUFFIX "ipkg-XXXXXX"
3245 +#define IPKG_CONF_LISTS_DIR  IPKG_STATE_DIR_PREFIX "/lists"
3246 +#define IPKG_CONF_PENDING_DIR IPKG_STATE_DIR_PREFIX "/pending"
3247 +
3248 +/* In case the config file defines no dest */
3249 +#define IPKG_CONF_DEFAULT_DEST_NAME "root"
3250 +#define IPKG_CONF_DEFAULT_DEST_ROOT_DIR "/"
3251 +
3252 +#define IPKG_CONF_DEFAULT_HASH_LEN 1024
3253 +
3254 +struct ipkg_conf
3255 +{
3256 +     pkg_src_list_t pkg_src_list;
3257 +     pkg_dest_list_t pkg_dest_list;
3258 +     nv_pair_list_t arch_list;
3259 +
3260 +     int restrict_to_default_dest;
3261 +     pkg_dest_t *default_dest;
3262 +
3263 +     char *tmp_dir;
3264 +     const char *lists_dir;
3265 +     const char *pending_dir;
3266 +
3267 +     /* options */
3268 +     int force_depends;
3269 +     int force_defaults;
3270 +     int force_overwrite;
3271 +     int force_downgrade;
3272 +     int force_reinstall;
3273 +     int force_space;
3274 +     int force_removal_of_dependent_packages;
3275 +     int force_removal_of_essential_packages;
3276 +     int nodeps; /* do not follow dependences */
3277 +     int verbose_wget;
3278 +     int multiple_providers;
3279 +     char *offline_root;
3280 +     char *offline_root_pre_script_cmd;
3281 +     char *offline_root_post_script_cmd;
3282 +     int query_all;
3283 +     int verbosity;
3284 +     int noaction;
3285 +
3286 +     /* proxy options */
3287 +     char *http_proxy;
3288 +     char *ftp_proxy;
3289 +     char *no_proxy;
3290 +     char *proxy_user;
3291 +     char *proxy_passwd;
3292 +
3293 +     hash_table_t pkg_hash;
3294 +     hash_table_t file_hash;
3295 +     hash_table_t obs_file_hash;
3296 +};
3297 +
3298 +enum ipkg_option_type {
3299 +     IPKG_OPT_TYPE_BOOL,
3300 +     IPKG_OPT_TYPE_INT,
3301 +     IPKG_OPT_TYPE_STRING
3302 +};
3303 +typedef enum ipkg_option_type ipkg_option_type_t;
3304 +
3305 +typedef struct ipkg_option ipkg_option_t;
3306 +struct ipkg_option {
3307 +     const char *name;
3308 +     const ipkg_option_type_t type;
3309 +     const void *value;
3310 +};
3311 +
3312 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args);
3313 +void ipkg_conf_deinit(ipkg_conf_t *conf);
3314 +
3315 +int ipkg_conf_write_status_files(ipkg_conf_t *conf);
3316 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename);
3317 +
3318 +#endif
3319 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_configure.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.c
3320 --- busybox-1.2.0-orig/archival/libipkg/ipkg_configure.c        1970-01-01 01:00:00.000000000 +0100
3321 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.c       2006-07-22 16:31:25.000000000 +0200
3322 @@ -0,0 +1,40 @@
3323 +/* ipkg_configure.c - the itsy package management system
3324 +
3325 +   Carl D. Worth
3326 +
3327 +   Copyright (C) 2001 University of Southern California
3328 +
3329 +   This program is free software; you can redistribute it and/or
3330 +   modify it under the terms of the GNU General Public License as
3331 +   published by the Free Software Foundation; either version 2, or (at
3332 +   your option) any later version.
3333 +
3334 +   This program is distributed in the hope that it will be useful, but
3335 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3336 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3337 +   General Public License for more details.
3338 +*/
3339 +
3340 +#include "ipkg.h"
3341 +
3342 +#include "ipkg_configure.h"
3343 +
3344 +int ipkg_configure(ipkg_conf_t *conf, pkg_t *pkg)
3345 +{
3346 +    int err;
3347 +
3348 +    /* DPKG_INCOMPATIBILITY:
3349 +       dpkg actually does some conffile handling here, rather than at the
3350 +       end of ipkg_install(). Do we care? */
3351 +    /* DPKG_INCOMPATIBILITY:
3352 +       dpkg actually includes a version number to this script call */
3353 +    err = pkg_run_script(conf, pkg, "postinst", "configure");
3354 +    if (err) {
3355 +       printf("ERROR: %s.postinst returned %d\n", pkg->name, err);
3356 +       return err;
3357 +    }
3358 +
3359 +    ipkg_state_changed++;
3360 +    return 0;
3361 +}
3362 +
3363 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_configure.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.h
3364 --- busybox-1.2.0-orig/archival/libipkg/ipkg_configure.h        1970-01-01 01:00:00.000000000 +0100
3365 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.h       2006-07-22 16:31:25.000000000 +0200
3366 @@ -0,0 +1,25 @@
3367 +/* ipkg_configure.h - the itsy package management system
3368 +
3369 +   Carl D. Worth
3370 +
3371 +   Copyright (C) 2001 University of Southern California
3372 +
3373 +   This program is free software; you can redistribute it and/or
3374 +   modify it under the terms of the GNU General Public License as
3375 +   published by the Free Software Foundation; either version 2, or (at
3376 +   your option) any later version.
3377 +
3378 +   This program is distributed in the hope that it will be useful, but
3379 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3380 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3381 +   General Public License for more details.
3382 +*/
3383 +
3384 +#ifndef IPKG_CONFIGURE_H
3385 +#define IPKG_CONFIGURE_H
3386 +
3387 +#include "ipkg_conf.h"
3388 +
3389 +int ipkg_configure(ipkg_conf_t *ipkg_conf, pkg_t *pkg);
3390 +
3391 +#endif
3392 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_download.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.c
3393 --- busybox-1.2.0-orig/archival/libipkg/ipkg_download.c 1970-01-01 01:00:00.000000000 +0100
3394 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.c        2006-07-22 16:31:25.000000000 +0200
3395 @@ -0,0 +1,195 @@
3396 +/* ipkg_download.c - the itsy package management system
3397 +
3398 +   Carl D. Worth
3399 +
3400 +   Copyright (C) 2001 University of Southern California
3401 +
3402 +   This program is free software; you can redistribute it and/or
3403 +   modify it under the terms of the GNU General Public License as
3404 +   published by the Free Software Foundation; either version 2, or (at
3405 +   your option) any later version.
3406 +
3407 +   This program is distributed in the hope that it will be useful, but
3408 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3409 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3410 +   General Public License for more details.
3411 +*/
3412 +
3413 +#include "ipkg.h"
3414 +#include "ipkg_download.h"
3415 +#include "ipkg_message.h"
3416 +
3417 +#include "sprintf_alloc.h"
3418 +#include "xsystem.h"
3419 +#include "file_util.h"
3420 +#include "str_util.h"
3421 +
3422 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
3423 +{
3424 +    int err = 0;
3425 +
3426 +    char *src_basec = strdup(src);
3427 +    char *src_base = basename(src_basec);
3428 +    char *tmp_file_location;
3429 +    char *cmd;
3430 +
3431 +    ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
3432 +       
3433 +    fflush(stdout);
3434 +    
3435 +    if (str_starts_with(src, "file:")) {
3436 +       int ret;
3437 +       const char *file_src = src + 5;
3438 +       ipkg_message(conf,IPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
3439 +       ret = file_copy(src + 5, dest_file_name);
3440 +       ipkg_message(conf,IPKG_INFO,"Done.\n");
3441 +       return ret;
3442 +    }
3443 +
3444 +    sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
3445 +    err = unlink(tmp_file_location);
3446 +    if (err && errno != ENOENT) {
3447 +       ipkg_message(conf,IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
3448 +               __FUNCTION__, tmp_file_location, strerror(errno));
3449 +       free(tmp_file_location);
3450 +       return errno;
3451 +    }
3452 +
3453 +    if (conf->http_proxy) {
3454 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
3455 +       setenv("http_proxy", conf->http_proxy, 1);
3456 +    }
3457 +    if (conf->ftp_proxy) {
3458 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
3459 +       setenv("ftp_proxy", conf->ftp_proxy, 1);
3460 +    }
3461 +    if (conf->no_proxy) {
3462 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
3463 +       setenv("no_proxy", conf->no_proxy, 1);
3464 +    }
3465 +
3466 +    /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */ 
3467 +    sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
3468 +                 (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
3469 +                 conf->proxy_user ? "--proxy-user=" : "",
3470 +                 conf->proxy_user ? conf->proxy_user : "",
3471 +                 conf->proxy_passwd ? "--proxy-passwd=" : "",
3472 +                 conf->proxy_passwd ? conf->proxy_passwd : "",
3473 +                 conf->verbose_wget ? "" : "-q",
3474 +                 conf->tmp_dir,
3475 +                 src);
3476 +    err = xsystem(cmd);
3477 +    if (err) {
3478 +       if (err != -1) {
3479 +           ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
3480 +                   __FUNCTION__, err, cmd);
3481 +       } 
3482 +       unlink(tmp_file_location);
3483 +       free(tmp_file_location);
3484 +       free(src_basec);
3485 +       free(cmd);
3486 +       return EINVAL;
3487 +    }
3488 +    free(cmd);
3489 +
3490 +    err = file_move(tmp_file_location, dest_file_name);
3491 +
3492 +    free(tmp_file_location);
3493 +    free(src_basec);
3494 +
3495 +    if (err) {
3496 +       return err;
3497 +    }
3498 +
3499 +    return 0;
3500 +}
3501 +
3502 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
3503 +{
3504 +    int err;
3505 +    char *url;
3506 +
3507 +    if (pkg->src == NULL) {
3508 +       ipkg_message(conf,IPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
3509 +               pkg->name, pkg->parent->name);
3510 +       return -1;
3511 +    }
3512 +
3513 +    sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
3514 +
3515 +    /* XXX: BUG: The pkg->filename might be something like
3516 +       "../../foo.ipk". While this is correct, and exactly what we
3517 +       want to use to construct url above, here we actually need to
3518 +       use just the filename part, without any directory. */
3519 +    sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
3520 +
3521 +    err = ipkg_download(conf, url, pkg->local_filename);
3522 +    free(url);
3523 +
3524 +    return err;
3525 +}
3526 +
3527 +/*
3528 + * Downloads file from url, installs in package database, return package name. 
3529 + */
3530 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
3531 +{
3532 +     int err = 0;
3533 +     pkg_t *pkg;
3534 +     pkg = pkg_new();
3535 +     if (pkg == NULL)
3536 +         return ENOMEM;
3537 +
3538 +     if (str_starts_with(url, "http://")
3539 +        || str_starts_with(url, "ftp://")) {
3540 +         char *tmp_file;
3541 +         char *file_basec = strdup(url);
3542 +         char *file_base = basename(file_basec);
3543 +
3544 +         sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
3545 +         err = ipkg_download(conf, url, tmp_file);
3546 +         if (err)
3547 +              return err;
3548 +
3549 +         err = pkg_init_from_file(pkg, tmp_file);
3550 +         if (err)
3551 +              return err;
3552 +         pkg->local_filename = strdup(tmp_file);
3553 +
3554 +         free(tmp_file);
3555 +         free(file_basec);
3556 +
3557 +     } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
3558 +               || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
3559 +
3560 +         err = pkg_init_from_file(pkg, url);
3561 +         if (err)
3562 +              return err;
3563 +         pkg->local_filename = strdup(url);
3564 +         ipkg_message(conf, IPKG_DEBUG2, "Package %s provided by hand \(%s\).\n", pkg->name,pkg->local_filename);
3565 +          pkg->provided_by_hand = 1;
3566 +
3567 +     } else {
3568 +       pkg_deinit(pkg);
3569 +       free(pkg);
3570 +       return 0;
3571 +     }
3572 +
3573 +     if (!pkg->architecture) {
3574 +         ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3575 +         return -EINVAL;
3576 +     }
3577 +
3578 +     pkg->dest = conf->default_dest;
3579 +     pkg->state_want = SW_INSTALL;
3580 +     pkg->state_flag |= SF_PREFER;
3581 +     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
3582 +     if ( pkg == NULL ){
3583 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
3584 +        return 0;
3585 +     }
3586 +     if (namep) {
3587 +         *namep = strdup(pkg->name);
3588 +     }
3589 +     return 0;
3590 +}
3591 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_download.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.h
3592 --- busybox-1.2.0-orig/archival/libipkg/ipkg_download.h 1970-01-01 01:00:00.000000000 +0100
3593 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.h        2006-07-22 16:31:25.000000000 +0200
3594 @@ -0,0 +1,30 @@
3595 +/* ipkg_download.h - the itsy package management system
3596 +
3597 +   Carl D. Worth
3598 +
3599 +   Copyright (C) 2001 University of Southern California
3600 +
3601 +   This program is free software; you can redistribute it and/or
3602 +   modify it under the terms of the GNU General Public License as
3603 +   published by the Free Software Foundation; either version 2, or (at
3604 +   your option) any later version.
3605 +
3606 +   This program is distributed in the hope that it will be useful, but
3607 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3608 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3609 +   General Public License for more details.
3610 +*/
3611 +
3612 +#ifndef IPKG_DOWNLOAD_H
3613 +#define IPKG_DOWNLOAD_H
3614 +
3615 +#include "ipkg_conf.h"
3616 +
3617 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name);
3618 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir);
3619 +/*
3620 + * Downloads file from url, installs in package database, return package name. 
3621 + */
3622 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep);
3623 +
3624 +#endif
3625 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg.h
3626 --- busybox-1.2.0-orig/archival/libipkg/ipkg.h  1970-01-01 01:00:00.000000000 +0100
3627 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg.h 2006-07-22 16:31:25.000000000 +0200
3628 @@ -0,0 +1,74 @@
3629 +/* ipkg.h - the itsy package management system
3630 +
3631 +   Carl D. Worth
3632 +
3633 +   Copyright (C) 2001 University of Southern California
3634 +
3635 +   This program is free software; you can redistribute it and/or
3636 +   modify it under the terms of the GNU General Public License as
3637 +   published by the Free Software Foundation; either version 2, or (at
3638 +   your option) any later version.
3639 +
3640 +   This program is distributed in the hope that it will be useful, but
3641 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3642 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3643 +   General Public License for more details.
3644 +*/
3645 +
3646 +#ifndef IPKG_H
3647 +#define IPKG_H
3648 +
3649 +/*
3650 +#ifdef HAVE_CONFIG_H
3651 +#include "config.h"
3652 +#endif
3653 +*/
3654 +
3655 +#if 0
3656 +#define IPKG_DEBUG_NO_TMP_CLEANUP
3657 +#endif
3658 +
3659 +#include "ipkg_includes.h"
3660 +#include "ipkg_conf.h"
3661 +#include "ipkg_message.h"
3662 +
3663 +#define IPKG_PKG_EXTENSION ".ipk"
3664 +#define DPKG_PKG_EXTENSION ".deb"
3665 +
3666 +#define IPKG_LEGAL_PKG_NAME_CHARS "abcdefghijklmnopqrstuvwxyz0123456789.+-"
3667 +#define IPKG_PKG_VERSION_SEP_CHAR '_'
3668 +
3669 +#define IPKG_STATE_DIR_PREFIX IPKGLIBDIR"/ipkg"
3670 +#define IPKG_LISTS_DIR_SUFFIX "lists"
3671 +#define IPKG_INFO_DIR_SUFFIX "info"
3672 +#define IPKG_STATUS_FILE_SUFFIX "status"
3673 +
3674 +#define IPKG_BACKUP_SUFFIX "-ipkg.backup"
3675 +
3676 +#define IPKG_LIST_DESCRIPTION_LENGTH 128
3677 +
3678 +#define IPKG_VERSION "0.99.162"
3679 +
3680 +
3681 +enum ipkg_error {
3682 +    IPKG_SUCCESS = 0,
3683 +    IPKG_PKG_DEPS_UNSATISFIED,
3684 +    IPKG_PKG_IS_ESSENTIAL,
3685 +    IPKG_PKG_HAS_DEPENDENTS,
3686 +    IPKG_PKG_HAS_NO_CANDIDATE
3687 +};
3688 +typedef enum ipkg_error ipkg_error_t;
3689 +
3690 +extern int ipkg_state_changed;
3691 +
3692 +
3693 +struct errlist {
3694 +    char * errmsg;
3695 +    struct errlist * next;
3696 +} ;
3697 +
3698 +extern struct errlist* error_list;
3699 +
3700 +extern ipkg_conf_t *global_conf;
3701 +
3702 +#endif
3703 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_includes.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_includes.h
3704 --- busybox-1.2.0-orig/archival/libipkg/ipkg_includes.h 1970-01-01 01:00:00.000000000 +0100
3705 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_includes.h        2006-07-22 16:31:25.000000000 +0200
3706 @@ -0,0 +1,79 @@
3707 +#ifndef IPKG_INCLUDES_H
3708 +#define IPKG_INCLUDES_H
3709 +
3710 +/* Define to 1 if you have the <memory.h> header file. */
3711 +#define HAVE_MEMORY_H 1
3712 +
3713 +/* Define to 1 if you have the <regex.h> header file. */
3714 +#define HAVE_REGEX_H 1
3715 +
3716 +/* Define to 1 if you have the <stdlib.h> header file. */
3717 +#define HAVE_STDLIB_H 1
3718 +
3719 +/* Define to 1 if you have the <strings.h> header file. */
3720 +#define HAVE_STRINGS_H 1
3721 +
3722 +/* Define to 1 if you have the <string.h> header file. */
3723 +#define HAVE_STRING_H 1
3724 +
3725 +/* Define to 1 if you have the <sys/stat.h> header file. */
3726 +#define HAVE_SYS_STAT_H 1
3727 +
3728 +/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
3729 +#define HAVE_SYS_WAIT_H 1
3730 +
3731 +/* Define to 1 if you have the <unistd.h> header file. */
3732 +#define HAVE_UNISTD_H 1
3733 +
3734 +/* Define to 1 if you have the ANSI C header files. */
3735 +#define STDC_HEADERS 1
3736 +
3737 +
3738 +#include <stdio.h>
3739 +
3740 +#if STDC_HEADERS
3741 +# include <stdlib.h>
3742 +# include <stdarg.h>
3743 +# include <stddef.h>
3744 +# include <ctype.h>
3745 +# include <errno.h>
3746 +#else
3747 +# if HAVE_STDLIB_H
3748 +#  include <stdlib.h>
3749 +# endif
3750 +#endif
3751 +
3752 +#if HAVE_REGEX_H
3753 +# include <regex.h>
3754 +#endif
3755 +
3756 +#if HAVE_STRING_H
3757 +# if !STDC_HEADERS && HAVE_MEMORY_H
3758 +#  include <memory.h>
3759 +# endif
3760 +/* XXX: What's the right way to pick up GNU's strndup declaration? */
3761 +# if __GNUC__
3762 +#   define __USE_GNU 1
3763 +# endif
3764 +# include <string.h>
3765 +# undef __USE_GNU
3766 +#endif
3767 +
3768 +#if HAVE_STRINGS_H
3769 +# include <strings.h>
3770 +#endif
3771 +
3772 +#if HAVE_SYS_STAT_H
3773 +# include <sys/stat.h>
3774 +#endif
3775 +
3776 +#if HAVE_SYS_WAIT_H
3777 +# include <sys/wait.h>
3778 +#endif
3779 +
3780 +#if HAVE_UNISTD_H
3781 +# include <sys/types.h>
3782 +# include <unistd.h>
3783 +#endif
3784 +
3785 +#endif /* IPKG_INCLUDES_H */
3786 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_install.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.c
3787 --- busybox-1.2.0-orig/archival/libipkg/ipkg_install.c  1970-01-01 01:00:00.000000000 +0100
3788 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.c 2006-07-22 16:31:25.000000000 +0200
3789 @@ -0,0 +1,1942 @@
3790 +/* ipkg_install.c - the itsy package management system
3791 +
3792 +   Carl D. Worth
3793 +
3794 +   Copyright (C) 2001 University of Southern California
3795 +
3796 +   This program is free software; you can redistribute it and/or
3797 +   modify it under the terms of the GNU General Public License as
3798 +   published by the Free Software Foundation; either version 2, or (at
3799 +   your option) any later version.
3800 +
3801 +   This program is distributed in the hope that it will be useful, but
3802 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3803 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3804 +   General Public License for more details.
3805 +*/
3806 +
3807 +#include "ipkg.h"
3808 +#include <errno.h>
3809 +#include <dirent.h>
3810 +#include <glob.h>
3811 +#include <time.h>
3812 +#include <signal.h>
3813 +typedef void (*sighandler_t)(int);
3814 +
3815 +#include "pkg.h"
3816 +#include "pkg_hash.h"
3817 +#include "pkg_extract.h"
3818 +
3819 +#include "ipkg_install.h"
3820 +#include "ipkg_configure.h"
3821 +#include "ipkg_download.h"
3822 +#include "ipkg_remove.h"
3823 +
3824 +#include "ipkg_utils.h"
3825 +#include "ipkg_message.h"
3826 +
3827 +#include "sprintf_alloc.h"
3828 +#include "file_util.h"
3829 +#include "str_util.h"
3830 +#include "xsystem.h"
3831 +#include "user.h"
3832 +
3833 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
3834 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg);
3835 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg);
3836 +
3837 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3838 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3839 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3840 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3841 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3842 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3843 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3844 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3845 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3846 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3847 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3848 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3849 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3850 +
3851 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3852 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3853 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg);
3854 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg);
3855 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg);
3856 +
3857 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg);
3858 +
3859 +static int user_prefers_old_conffile(const char *file, const char *backup);
3860 +
3861 +static char *backup_filename_alloc(const char *file_name);
3862 +static int backup_make_backup(ipkg_conf_t *conf, const char *file_name);
3863 +static int backup_exists_for(const char *file_name);
3864 +static int backup_remove(const char *file_name);
3865 +
3866 +
3867 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename)
3868 +{
3869 +     int err, cmp;
3870 +     pkg_t *pkg, *old;
3871 +     char *old_version, *new_version;
3872 +
3873 +     pkg = pkg_new();
3874 +     if (pkg == NULL) {
3875 +         return ENOMEM;
3876 +     }
3877 +
3878 +     err = pkg_init_from_file(pkg, filename);
3879 +     if (err) {
3880 +         return err;
3881 +     }
3882 +
3883 +     if (!pkg->architecture) {
3884 +         ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3885 +         return -EINVAL;
3886 +     }
3887 +
3888 +     /* XXX: CLEANUP: hash_insert_pkg has a nasty side effect of possibly
3889 +       freeing the pkg that we pass in. It might be nice to clean this up
3890 +       if possible.  */
3891 +     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
3892 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
3893 +
3894 +     pkg->local_filename = strdup(filename);
3895 +
3896 +     if (old) {
3897 +         old_version = pkg_version_str_alloc(old);
3898 +         new_version = pkg_version_str_alloc(pkg);
3899 +
3900 +         cmp = pkg_compare_versions(old, pkg);
3901 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
3902 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
3903 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
3904 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
3905 +          } 
3906 +         if (cmp > 0) {
3907 +                ipkg_message(conf, IPKG_NOTICE,
3908 +                             "Not downgrading package %s on %s from %s to %s.\n",
3909 +                             old->name, old->dest->name, old_version, new_version);
3910 +                pkg->state_want = SW_DEINSTALL;
3911 +                pkg->state_flag |= SF_OBSOLETE;
3912 +                free(old_version);
3913 +                free(new_version);
3914 +                return 0;
3915 +         } else {
3916 +              free(old_version);
3917 +              free(new_version);
3918 +         }
3919 +     }
3920 +
3921 +     ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
3922 +     return ipkg_install_pkg(conf, pkg,0);
3923 +}
3924 +
3925 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name)
3926 +{
3927 +     int cmp;
3928 +     pkg_t *old, *new;
3929 +     char *old_version, *new_version;
3930 +
3931 +     ipkg_message(conf, IPKG_DEBUG2, " Getting old  from pkg_hash_fetch \n" );
3932 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
3933 +     if ( old ) 
3934 +        ipkg_message(conf, IPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n",  old->version );
3935 +    
3936 +     ipkg_message(conf, IPKG_DEBUG2, " Getting new  from pkg_hash_fetch \n" );
3937 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
3938 +     if ( new ) 
3939 +        ipkg_message(conf, IPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n",  new->version );
3940 +
3941 +/* Pigi Basically here is broken the version stuff.
3942 +   What's happening is that nothing provide the version to differents 
3943 +   functions, so the returned struct is always the latest.
3944 +   That's why the install by name don't work.
3945 +*/
3946 +     ipkg_message(conf, IPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
3947 +
3948 +     if ( old ) 
3949 +        ipkg_message(conf, IPKG_DEBUG2, " old %s ", old->version );
3950 +     if ( new ) 
3951 +        ipkg_message(conf, IPKG_DEBUG2, " new %s ", new->version );
3952 +     ipkg_message(conf, IPKG_DEBUG2, " \n");
3953 +
3954 +     if (new == NULL) {
3955 +         return IPKG_PKG_HAS_NO_CANDIDATE;
3956 +     }
3957 +
3958 +     new->state_flag |= SF_USER;
3959 +     if (old) {
3960 +         old_version = pkg_version_str_alloc(old);
3961 +         new_version = pkg_version_str_alloc(new);
3962 +
3963 +         cmp = pkg_compare_versions(old, new);
3964 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
3965 +            ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade \n");
3966 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
3967 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
3968 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
3969 +          } 
3970 +         ipkg_message(conf, IPKG_DEBUG, 
3971 +                      "Comparing visible versions of pkg %s:"
3972 +                      "\n\t%s is installed "
3973 +                      "\n\t%s is available "
3974 +                      "\n\t%d was comparison result\n",
3975 +                      pkg_name, old_version, new_version, cmp);
3976 +         if (cmp == 0 && !conf->force_reinstall) {
3977 +              ipkg_message(conf, IPKG_NOTICE,
3978 +                           "Package %s (%s) installed in %s is up to date.\n",
3979 +                           old->name, old_version, old->dest->name);
3980 +              free(old_version);
3981 +              free(new_version);
3982 +              return 0;
3983 +         } else if (cmp > 0) {
3984 +              ipkg_message(conf, IPKG_NOTICE,
3985 +                           "Not downgrading package %s on %s from %s to %s.\n",
3986 +                           old->name, old->dest->name, old_version, new_version);
3987 +              free(old_version);
3988 +              free(new_version);
3989 +              return 0;
3990 +         } else if (cmp < 0) {
3991 +              new->dest = old->dest;
3992 +              old->state_want = SW_DEINSTALL;    /* Here probably the problem for bug 1277 */
3993 +         }
3994 +     }
3995 +
3996 +     /* XXX: CLEANUP: The error code of ipkg_install_by_name is really
3997 +       supposed to be an ipkg_error_t, but ipkg_install_pkg could
3998 +       return any kind of integer, (might be errno from a syscall,
3999 +       etc.). This is a real mess and will need to be cleaned up if
4000 +       anyone ever wants to make a nice libipkg. */
4001 +
4002 +     ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4003 +     return ipkg_install_pkg(conf, new,0);
4004 +}
4005 +
4006 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name)
4007 +{
4008 +     abstract_pkg_vec_t *providers = pkg_hash_fetch_all_installation_candidates (&conf->pkg_hash, pkg_name);
4009 +     int i;
4010 +     ipkg_error_t err;
4011 +     abstract_pkg_t *ppkg ;
4012 +
4013 +     if (providers == NULL)
4014 +         return IPKG_PKG_HAS_NO_CANDIDATE;
4015 +
4016 +     for (i = 0; i < providers->len; i++) {
4017 +         ppkg = abstract_pkg_vec_get(providers, i);
4018 +          ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_by_name %d \n",__FUNCTION__, i);
4019 +         err = ipkg_install_by_name(conf, ppkg->name);
4020 +         if (err)
4021 +              return err;
4022 +/* XXX Maybe ppkg should be freed ? */
4023 +     }
4024 +     return 0;
4025 +}
4026 +
4027 +/*
4028 + * Walk dependence graph starting with pkg, collect packages to be
4029 + * installed into pkgs_needed, in dependence order.
4030 + */
4031 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *pkgs_needed)
4032 +{
4033 +     int i, err;
4034 +     pkg_vec_t *depends = pkg_vec_alloc();
4035 +     char **unresolved = NULL;
4036 +     int ndepends;
4037 +
4038 +     ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
4039 +                                                       pkg, depends, 
4040 +                                                       &unresolved);
4041 +
4042 +     if (unresolved) {
4043 +         ipkg_message(conf, IPKG_ERROR,
4044 +                      "%s: Cannot satisfy the following dependencies for %s:\n\t",
4045 +                      conf->force_depends ? "Warning" : "ERROR", pkg->name);
4046 +         while (*unresolved) {
4047 +              ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4048 +              unresolved++;
4049 +         }
4050 +         ipkg_message(conf, IPKG_ERROR, "\n");
4051 +         if (! conf->force_depends) {
4052 +              ipkg_message(conf, IPKG_INFO,
4053 +                           "This could mean that your package list is out of date or that the packages\n"
4054 +                           "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4055 +                           "of this problem try again with the '-force-depends' option.\n");
4056 +              pkg_vec_free(depends);
4057 +              return IPKG_PKG_DEPS_UNSATISFIED;
4058 +         }
4059 +     }
4060 +
4061 +     if (ndepends <= 0) {
4062 +         pkg_vec_free(depends);
4063 +         return 0;
4064 +     }
4065 +
4066 +     for (i = 0; i < depends->len; i++) {
4067 +         pkg_t *dep = depends->pkgs[i];
4068 +         /* The package was uninstalled when we started, but another
4069 +            dep earlier in this loop may have depended on it and pulled
4070 +            it in, so check first. */
4071 +         if ((dep->state_status != SS_INSTALLED)
4072 +             && (dep->state_status != SS_UNPACKED)
4073 +             && (dep->state_want != SW_INSTALL)) {
4074 +
4075 +              /* Mark packages as to-be-installed */
4076 +              dep->state_want = SW_INSTALL;
4077 +
4078 +              /* Dependencies should be installed the same place as pkg */
4079 +              if (dep->dest == NULL) {
4080 +                   dep->dest = pkg->dest;
4081 +              }
4082 +
4083 +              err = pkg_mark_dependencies_for_installation(conf, dep, pkgs_needed);
4084 +              if (err) {
4085 +                   pkg_vec_free(depends);
4086 +                   return err;
4087 +              }
4088 +         }
4089 +     }
4090 +     if (pkgs_needed)
4091 +         pkg_vec_insert(pkgs_needed, pkg);
4092 +
4093 +     pkg_vec_free(depends);
4094 +
4095 +     return 0;
4096 +}
4097 +
4098 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed)
4099 +{
4100 +     int cmp;
4101 +     pkg_t *old, *new;
4102 +     char *old_version, *new_version;
4103 +
4104 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
4105 +    
4106 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
4107 +     if (new == NULL) {
4108 +         return IPKG_PKG_HAS_NO_CANDIDATE;
4109 +     }
4110 +     if (old) {
4111 +         old_version = pkg_version_str_alloc(old);
4112 +         new_version = pkg_version_str_alloc(new);
4113 +
4114 +         cmp = pkg_compare_versions(old, new);
4115 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
4116 +           ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade ");
4117 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
4118 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
4119 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
4120 +          } 
4121 +         ipkg_message(conf, IPKG_DEBUG, 
4122 +                      "comparing visible versions of pkg %s:"
4123 +                      "\n\t%s is installed "
4124 +                      "\n\t%s is available "
4125 +                      "\n\t%d was comparison result\n",
4126 +                      pkg_name, old_version, new_version, cmp);
4127 +         if (cmp == 0 && !conf->force_reinstall) {
4128 +              ipkg_message(conf, IPKG_NOTICE,
4129 +                           "Package %s (%s) installed in %s is up to date.\n",
4130 +                           old->name, old_version, old->dest->name);
4131 +              free(old_version);
4132 +              free(new_version);
4133 +              return 0;
4134 +         } else if (cmp > 0) {
4135 +              ipkg_message(conf, IPKG_NOTICE,
4136 +                           "Not downgrading package %s on %s from %s to %s.\n",
4137 +                           old->name, old->dest->name, old_version, new_version);
4138 +              free(old_version);
4139 +              free(new_version);
4140 +              return 0;
4141 +         } else if (cmp < 0) {
4142 +              new->dest = old->dest;
4143 +              old->state_want = SW_DEINSTALL;
4144 +              old->state_flag |= SF_OBSOLETE;
4145 +         }
4146 +     }
4147 +     return pkg_mark_dependencies_for_installation(conf, new, pkgs_needed);
4148 +}
4149 +
4150 +\f
4151 +
4152 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg)
4153 +{
4154 +     int i, err;
4155 +     pkg_vec_t *depends = pkg_vec_alloc();
4156 +     pkg_t *dep;
4157 +     char **unresolved = NULL;
4158 +     int ndepends;
4159 +
4160 +     ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
4161 +                                                       pkg, depends, 
4162 +                                                       &unresolved);
4163 +
4164 +     if (unresolved) {
4165 +         ipkg_message(conf, IPKG_ERROR,
4166 +                      "%s: Cannot satisfy the following dependencies for %s:\n\t",
4167 +                      conf->force_depends ? "Warning" : "ERROR", pkg->name);
4168 +         while (*unresolved) {
4169 +              ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4170 +              unresolved++;
4171 +         }
4172 +         ipkg_message(conf, IPKG_ERROR, "\n");
4173 +         if (! conf->force_depends) {
4174 +              ipkg_message(conf, IPKG_INFO,
4175 +                           "This could mean that your package list is out of date or that the packages\n"
4176 +                           "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4177 +                           "of this problem try again with the '-force-depends' option.\n");
4178 +              pkg_vec_free(depends);
4179 +              return IPKG_PKG_DEPS_UNSATISFIED;
4180 +         }
4181 +     }
4182 +
4183 +     if (ndepends <= 0) {
4184 +         return 0;
4185 +     }
4186 +
4187 +     /* Mark packages as to-be-installed */
4188 +     for (i=0; i < depends->len; i++) {
4189 +         /* Dependencies should be installed the same place as pkg */
4190 +         if (depends->pkgs[i]->dest == NULL) {
4191 +              depends->pkgs[i]->dest = pkg->dest;
4192 +         }
4193 +         depends->pkgs[i]->state_want = SW_INSTALL;
4194 +     }
4195 +
4196 +     for (i = 0; i < depends->len; i++) {
4197 +         dep = depends->pkgs[i];
4198 +         /* The package was uninstalled when we started, but another
4199 +            dep earlier in this loop may have depended on it and pulled
4200 +            it in, so check first. */
4201 +         if ((dep->state_status != SS_INSTALLED)
4202 +             && (dep->state_status != SS_UNPACKED)) {
4203 +               ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4204 +              err = ipkg_install_pkg(conf, dep,0);
4205 +              if (err) {
4206 +                   pkg_vec_free(depends);
4207 +                   return err;
4208 +              }
4209 +         }
4210 +     }
4211 +
4212 +     pkg_vec_free(depends);
4213 +
4214 +     return 0;
4215 +}
4216 +
4217 +
4218 +/* check all packages have their dependences satisfied, e.g., in case an upgraded package split */ 
4219 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf)
4220 +{
4221 +     if (conf->nodeps == 0) {
4222 +         int i;
4223 +         pkg_vec_t *installed = pkg_vec_alloc();
4224 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
4225 +         for (i = 0; i < installed->len; i++) {
4226 +              pkg_t *pkg = installed->pkgs[i];
4227 +              satisfy_dependencies_for(conf, pkg);
4228 +         }
4229 +         pkg_vec_free(installed);
4230 +     }
4231 +     return 0;
4232 +}
4233 +
4234 +\f
4235 +
4236 +static int check_conflicts_for(ipkg_conf_t *conf, pkg_t *pkg)
4237 +{
4238 +     int i;
4239 +     pkg_vec_t *conflicts = NULL;
4240 +     int level;
4241 +     const char *prefix;
4242 +     if (conf->force_depends) {
4243 +         level = IPKG_NOTICE;
4244 +         prefix = "Warning";
4245 +     } else {
4246 +         level = IPKG_ERROR;
4247 +         prefix = "ERROR";
4248 +     }
4249 +
4250 +     if (!conf->force_depends)
4251 +         conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
4252 +
4253 +     if (conflicts) {
4254 +         ipkg_message(conf, level,
4255 +                      "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
4256 +         i = 0;
4257 +         while (i < conflicts->len)
4258 +              ipkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
4259 +         ipkg_message(conf, level, "\n");
4260 +         pkg_vec_free(conflicts);
4261 +         return IPKG_PKG_DEPS_UNSATISFIED;
4262 +     }
4263 +     return 0;
4264 +}
4265 +
4266 +static int update_file_ownership(ipkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
4267 +{
4268 +     str_list_t *new_list = pkg_get_installed_files(new_pkg);
4269 +     str_list_elt_t *iter;
4270 +
4271 +     for (iter = new_list->head; iter; iter = iter->next) {
4272 +         char *new_file = iter->data;
4273 +         pkg_t *owner = file_hash_get_file_owner(conf, new_file);
4274 +         if (!new_file)
4275 +              ipkg_message(conf, IPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
4276 +         if (!owner || (owner == old_pkg))
4277 +              file_hash_set_file_owner(conf, new_file, new_pkg);
4278 +     }
4279 +     if (old_pkg) {
4280 +         str_list_t *old_list = pkg_get_installed_files(old_pkg);
4281 +         for (iter = old_list->head; iter; iter = iter->next) {
4282 +              char *old_file = iter->data;
4283 +              pkg_t *owner = file_hash_get_file_owner(conf, old_file);
4284 +              if (owner == old_pkg) {
4285 +                   /* obsolete */
4286 +                   hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
4287 +              }
4288 +         }
4289 +     }
4290 +     return 0;
4291 +}
4292 +
4293 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg)
4294 +{
4295 +    /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
4296 +
4297 +    /* sma 6.20.02:  yup; here's the first bit */
4298 +    /* 
4299 +     * XXX: BUG easy for cworth
4300 +     * 1) please point the call below to the correct current root destination
4301 +     * 2) we need to resolve how to check the required space for a pending pkg, 
4302 +     *    my diddling with the .ipk file size below isn't going to cut it.
4303 +     * 3) return a proper error code instead of 1
4304 +     */
4305 +     int comp_size, blocks_available;
4306 +    
4307 +     if (!conf->force_space && pkg->installed_size != NULL) {
4308 +         blocks_available = get_available_blocks(conf->default_dest->root_dir);
4309 +
4310 +         comp_size = strtoul(pkg->installed_size, NULL, 0);
4311 +         /* round up a blocks count without doing fancy-but-slow casting jazz */ 
4312 +         comp_size = (int)((comp_size + 1023) / 1024);
4313 +
4314 +         if (comp_size >= blocks_available) {
4315 +              ipkg_message(conf, IPKG_ERROR,
4316 +                           "Only have %d available blocks on filesystem %s, pkg %s needs %d\n", 
4317 +                           blocks_available, conf->default_dest->root_dir, pkg->name, comp_size);
4318 +              return ENOSPC;
4319 +         }
4320 +     }
4321 +     return 0;
4322 +}
4323 +
4324 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg)
4325 +{
4326 +     int err;
4327 +     char *conffiles_file_name;
4328 +     char *root_dir;
4329 +     FILE *conffiles_file;
4330 +
4331 +     sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
4332 +
4333 +     pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
4334 +     if (pkg->tmp_unpack_dir == NULL) {
4335 +         ipkg_message(conf, IPKG_ERROR,
4336 +                      "%s: Failed to create temporary directory '%s': %s\n",
4337 +                      __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
4338 +         return errno;
4339 +     }
4340 +
4341 +     err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
4342 +     if (err) {
4343 +         return err;
4344 +     }
4345 +
4346 +     /* XXX: CLEANUP: There might be a cleaner place to read in the
4347 +       conffiles. Seems like I should be able to get everything to go
4348 +       through pkg_init_from_file. If so, maybe it would make sense to
4349 +       move all of unpack_pkg_control_files to that function. */
4350 +
4351 +     /* Don't need to re-read conffiles if we already have it */
4352 +     if (pkg->conffiles.head) {
4353 +         return 0;
4354 +     }
4355 +
4356 +     sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
4357 +     if (! file_exists(conffiles_file_name)) {
4358 +         free(conffiles_file_name);
4359 +         return 0;
4360 +     }
4361 +    
4362 +     conffiles_file = fopen(conffiles_file_name, "r");
4363 +     if (conffiles_file == NULL) {
4364 +         fprintf(stderr, "%s: failed to open %s: %s\n",
4365 +                 __FUNCTION__, conffiles_file_name, strerror(errno));
4366 +         free(conffiles_file_name);
4367 +         return errno;
4368 +     }
4369 +     free(conffiles_file_name);
4370 +
4371 +     while (1) {
4372 +         char *cf_name;
4373 +         char *cf_name_in_dest;
4374 +
4375 +         cf_name = file_read_line_alloc(conffiles_file);
4376 +         if (cf_name == NULL) {
4377 +              break;
4378 +         }
4379 +         str_chomp(cf_name);
4380 +         if (cf_name[0] == '\0') {
4381 +              continue;
4382 +         }
4383 +
4384 +         /* Prepend dest->root_dir to conffile name.
4385 +            Take pains to avoid multiple slashes. */
4386 +         root_dir = pkg->dest->root_dir;
4387 +         if (conf->offline_root)
4388 +              /* skip the offline_root prefix */
4389 +              root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
4390 +         sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
4391 +                       cf_name[0] == '/' ? (cf_name + 1) : cf_name);
4392 +
4393 +         /* Can't get an md5sum now, (file isn't extracted yet).
4394 +            We'll wait until resolve_conffiles */
4395 +         conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
4396 +
4397 +         free(cf_name);
4398 +         free(cf_name_in_dest);
4399 +     }
4400 +
4401 +     fclose(conffiles_file);
4402 +
4403 +     return 0;
4404 +}
4405 +
4406 +/* returns number of installed replacees */
4407 +int pkg_get_installed_replacees(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
4408 +{
4409 +     abstract_pkg_t **replaces = pkg->replaces;
4410 +     int replaces_count = pkg->replaces_count;
4411 +     int i, j;
4412 +     for (i = 0; i < replaces_count; i++) {
4413 +         abstract_pkg_t *ab_pkg = replaces[i];
4414 +         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
4415 +         if (pkg_vec) {
4416 +              for (j = 0; j < pkg_vec->len; j++) {
4417 +                   pkg_t *replacee = pkg_vec->pkgs[j];
4418 +                   if (!pkg_conflicts(pkg, replacee))
4419 +                        continue;
4420 +                   if (replacee->state_status == SS_INSTALLED) {
4421 +                        pkg_vec_insert(installed_replacees, replacee);
4422 +                   }
4423 +              }
4424 +         }
4425 +     }
4426 +     return installed_replacees->len;
4427 +}
4428 +
4429 +int pkg_remove_installed_replacees(ipkg_conf_t *conf, pkg_vec_t *replacees)
4430 +{
4431 +     int i;
4432 +     int replaces_count = replacees->len;
4433 +     for (i = 0; i < replaces_count; i++) {
4434 +         pkg_t *replacee = replacees->pkgs[i];
4435 +         int err;
4436 +         replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
4437 +         err = ipkg_remove_pkg(conf, replacee,0);
4438 +         if (err)
4439 +              return err;
4440 +     }
4441 +     return 0;
4442 +}
4443 +
4444 +/* to unwind the removal: make sure they are installed */
4445 +int pkg_remove_installed_replacees_unwind(ipkg_conf_t *conf, pkg_vec_t *replacees)
4446 +{
4447 +     int i, err;
4448 +     int replaces_count = replacees->len;
4449 +     for (i = 0; i < replaces_count; i++) {
4450 +         pkg_t *replacee = replacees->pkgs[i];
4451 +         if (replacee->state_status != SS_INSTALLED) {
4452 +               ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4453 +              err = ipkg_install_pkg(conf, replacee,0);
4454 +              if (err)
4455 +                   return err;
4456 +         }
4457 +     }
4458 +     return 0;
4459 +}
4460 +
4461 +int caught_sigint = 0;
4462 +static void ipkg_install_pkg_sigint_handler(int sig)
4463 +{
4464 +     caught_sigint = sig;
4465 +}
4466 +
4467 +/* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
4468 +static int ipkg_install_check_downgrade(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
4469 +{        
4470 +     if (old_pkg) {
4471 +          char message_out[15];
4472 +         char *old_version = pkg_version_str_alloc(old_pkg);
4473 +         char *new_version = pkg_version_str_alloc(pkg);
4474 +         int cmp = pkg_compare_versions(old_pkg, pkg);
4475 +         int rc = 0;
4476 +
4477 +          memset(message_out,'\x0',15);
4478 +          strncpy (message_out,"Upgrading ",strlen("Upgrading ")); 
4479 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
4480 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
4481 +             strncpy (message_out,"Downgrading ",strlen("Downgrading "));         /* We need to use a value < 0 because in the 0 case we are asking to */
4482 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
4483 +          } 
4484 +
4485 +         if (cmp > 0) {
4486 +              ipkg_message(conf, IPKG_NOTICE,
4487 +                           "Not downgrading package %s on %s from %s to %s.\n",
4488 +                           old_pkg->name, old_pkg->dest->name, old_version, new_version);
4489 +              rc = 1;
4490 +         } else if (cmp < 0) {
4491 +              ipkg_message(conf, IPKG_NOTICE,
4492 +                           "%s%s on %s from %s to %s...\n",
4493 +                           message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
4494 +              pkg->dest = old_pkg->dest;
4495 +              rc = 0;
4496 +         } else /* cmp == 0 */ {
4497 +              if (conf->force_reinstall) {
4498 +                   ipkg_message(conf, IPKG_NOTICE,
4499 +                                "Reinstalling %s (%s) on %s...\n",
4500 +                                pkg->name, new_version, old_pkg->dest->name);
4501 +                   pkg->dest = old_pkg->dest;
4502 +                   rc = 0;
4503 +              } else {
4504 +                   ipkg_message(conf, IPKG_NOTICE,
4505 +                                "Not installing %s (%s) on %s -- already installed.\n",
4506 +                                pkg->name, new_version, old_pkg->dest->name);
4507 +                   rc = 1;
4508 +              }
4509 +         } 
4510 +         free(old_version);
4511 +         free(new_version);
4512 +         return rc;
4513 +     } else {
4514 +      char message_out[15] ;
4515 +      memset(message_out,'\x0',15);
4516 +      if ( message ) 
4517 +          strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
4518 +      else
4519 +          strncpy( message_out,"Installing ",strlen("Installing ") );
4520 +         char *version = pkg_version_str_alloc(pkg);
4521 +      
4522 +         ipkg_message(conf, IPKG_NOTICE,
4523 +                      "%s%s (%s) to %s...\n", message_out,
4524 +                      pkg->name, version, pkg->dest->name);
4525 +         free(version);
4526 +         return 0;
4527 +     }
4528 +}
4529 +
4530 +/* and now the meat... */
4531 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
4532 +{
4533 +     int err = 0;
4534 +     int message = 0;
4535 +     pkg_t *old_pkg = NULL;
4536 +     pkg_vec_t *replacees;
4537 +     abstract_pkg_t *ab_pkg = NULL;
4538 +     int old_state_flag;
4539 +     char* file_md5;
4540 +
4541 +    
4542 +     if ( from_upgrade ) 
4543 +        message = 1;            /* Coming from an upgrade, and should change the output message */
4544 +
4545 +     if (!pkg) {
4546 +         ipkg_message(conf, IPKG_ERROR,
4547 +                      "INTERNAL ERROR: null pkg passed to ipkg_install_pkg\n");
4548 +         return -EINVAL;
4549 +     }
4550 +
4551 +     ipkg_message(conf, IPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
4552 +
4553 +     if (!pkg_arch_supported(conf, pkg)) {
4554 +         ipkg_message(conf, IPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
4555 +                      pkg->architecture, pkg->name);
4556 +         return -EINVAL;
4557 +     }
4558 +     if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
4559 +         err = satisfy_dependencies_for(conf, pkg);
4560 +         if (err) { return err; }
4561 +
4562 +         ipkg_message(conf, IPKG_NOTICE,
4563 +                      "Package %s is already installed in %s.\n", 
4564 +                      pkg->name, pkg->dest->name);
4565 +         return 0;
4566 +     }
4567 +
4568 +     if (pkg->dest == NULL) {
4569 +         pkg->dest = conf->default_dest;
4570 +     }
4571 +
4572 +     old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
4573 +
4574 +     err = ipkg_install_check_downgrade(conf, pkg, old_pkg, message);
4575 +     if (err) { return err; }
4576 +
4577 +     pkg->state_want = SW_INSTALL;
4578 +     if (old_pkg){                          
4579 +         old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
4580 +     }
4581 +
4582 +
4583 +     /* Abhaya: conflicts check */
4584 +     err = check_conflicts_for(conf, pkg);
4585 +     if (err) { return err; }
4586 +    
4587 +     /* this setup is to remove the upgrade scenario in the end when
4588 +       installing pkg A, A deps B & B deps on A. So both B and A are
4589 +       installed. Then A's installation is started resulting in an
4590 +       uncecessary upgrade */ 
4591 +     if (pkg->state_status == SS_INSTALLED
4592 +        && conf->force_reinstall == 0) return 0;
4593 +    
4594 +     err = verify_pkg_installable(conf, pkg);
4595 +     if (err) { return err; }
4596 +
4597 +     if (pkg->local_filename == NULL) {
4598 +         err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
4599 +         if (err) {
4600 +              ipkg_message(conf, IPKG_ERROR,
4601 +                           "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
4602 +                           pkg->name);
4603 +              return err;
4604 +         }
4605 +     }
4606 +
4607 +/* Check for md5 values */
4608 +     if (pkg->md5sum)
4609 +     {
4610 +         file_md5 = file_md5sum_alloc(pkg->local_filename);
4611 +         if (strcmp(file_md5, pkg->md5sum))
4612 +         {
4613 +              ipkg_message(conf, IPKG_ERROR,
4614 +                           "Package %s md5sum mismatch. Either the ipkg or the package index are corrupt. Try 'ipkg update'.\n",
4615 +                           pkg->name);
4616 +              free(file_md5);
4617 +              return err;
4618 +         }
4619 +         free(file_md5);
4620 +     }
4621 +
4622 +     if (pkg->tmp_unpack_dir == NULL) {
4623 +         unpack_pkg_control_files(conf, pkg);
4624 +     }
4625 +
4626 +     /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
4627 +/* Pigi: check if it will pass from here when replacing. It seems to fail */
4628 +/* That's rather strange that files don't change owner. Investigate !!!!!!*/
4629 +     err = update_file_ownership(conf, pkg, old_pkg);
4630 +     if (err) { return err; }
4631 +
4632 +     if (conf->nodeps == 0) {
4633 +         err = satisfy_dependencies_for(conf, pkg);
4634 +         if (err) { return err; }
4635 +     }
4636 +
4637 +     replacees = pkg_vec_alloc();
4638 +     pkg_get_installed_replacees(conf, pkg, replacees);
4639 +
4640 +     /* this next section we do with SIGINT blocked to prevent inconsistency between ipkg database and filesystem */
4641 +     {
4642 +         sigset_t newset, oldset;
4643 +         sighandler_t old_handler = NULL;
4644 +         int use_signal = 0;
4645 +         caught_sigint = 0;
4646 +         if (use_signal) {
4647 +              old_handler = signal(SIGINT, ipkg_install_pkg_sigint_handler);
4648 +         } else {
4649 +              sigemptyset(&newset);
4650 +              sigaddset(&newset, SIGINT);
4651 +              sigprocmask(SIG_BLOCK, &newset, &oldset);
4652 +         }
4653 +
4654 +         ipkg_state_changed++;
4655 +         pkg->state_flag |= SF_FILELIST_CHANGED;
4656 +
4657 +         /* XXX: BUG: we really should treat replacement more like an upgrade
4658 +          *      Instead, we're going to remove the replacees 
4659 +          */
4660 +         err = pkg_remove_installed_replacees(conf, replacees);
4661 +         if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
4662 +
4663 +         err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
4664 +         if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
4665 +
4666 +         err = prerm_deconfigure_conflictors(conf, pkg, replacees);
4667 +         if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
4668 +
4669 +         err = preinst_configure(conf, pkg, old_pkg);
4670 +         if (err) goto UNWIND_PREINST_CONFIGURE;
4671 +
4672 +         err = backup_modified_conffiles(conf, pkg, old_pkg);
4673 +         if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
4674 +
4675 +         err = check_data_file_clashes(conf, pkg, old_pkg);
4676 +         if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
4677 +
4678 +         err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
4679 +         if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
4680 +
4681 +         if (conf->noaction) return 0;
4682 +
4683 +         /* point of no return: no unwinding after this */
4684 +         if (old_pkg && !conf->force_reinstall) {
4685 +              old_pkg->state_want = SW_DEINSTALL;
4686 +
4687 +              if (old_pkg->state_flag & SF_NOPRUNE) {
4688 +                   ipkg_message(conf, IPKG_INFO,
4689 +                                "  not removing obsolesced files because package marked noprune\n");
4690 +              } else {
4691 +                   ipkg_message(conf, IPKG_INFO,
4692 +                                "  removing obsolesced files\n");
4693 +                   remove_obsolesced_files(conf, pkg, old_pkg);
4694 +              }
4695 +               /* removing files from old package, to avoid ghost files */ 
4696 +               remove_data_files_and_list(conf, old_pkg);
4697 +/* Pigi : It should be better to remove also maintainer and postrem scripts here, just in case*/
4698 +               remove_maintainer_scripts_except_postrm(conf, old_pkg);
4699 +               remove_postrm(conf, old_pkg);
4700 +/* Pigi */
4701 +
4702 +         }
4703 +
4704 +
4705 +         ipkg_message(conf, IPKG_INFO,
4706 +                      "  installing maintainer scripts\n");
4707 +         install_maintainer_scripts(conf, pkg, old_pkg);
4708 +
4709 +         /* the following just returns 0 */
4710 +         remove_disappeared(conf, pkg);
4711 +
4712 +         ipkg_message(conf, IPKG_INFO,
4713 +                      "  installing data files\n");
4714 +         install_data_files(conf, pkg);
4715 +
4716 +/* read comments from function for detail but I will execute this here as all other tests are ok.*/
4717 +         err = check_data_file_clashes_change(conf, pkg, old_pkg);
4718 +
4719 +         ipkg_message(conf, IPKG_INFO,
4720 +                      "  resolving conf files\n");
4721 +         resolve_conffiles(conf, pkg);
4722 +
4723 +         pkg->state_status = SS_UNPACKED;
4724 +         old_state_flag = pkg->state_flag;
4725 +         pkg->state_flag &= ~SF_PREFER;
4726 +         ipkg_message(conf, IPKG_DEBUG, "   pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
4727 +
4728 +         if (old_pkg && !conf->force_reinstall) {
4729 +              old_pkg->state_status = SS_NOT_INSTALLED;
4730 +         }
4731 +
4732 +         time(&pkg->installed_time);
4733 +
4734 +         ipkg_message(conf, IPKG_INFO,
4735 +                      "  cleanup temp files\n");
4736 +         cleanup_temporary_files(conf, pkg);
4737 +
4738 +         ab_pkg = pkg->parent;
4739 +         if (ab_pkg)
4740 +              ab_pkg->state_status = pkg->state_status;
4741 +
4742 +         ipkg_message(conf, IPKG_INFO, "Done.\n");
4743 +
4744 +         if (use_signal)
4745 +              signal(SIGINT, old_handler);
4746 +         else
4747 +              sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4748 +
4749 +         return 0;
4750 +     
4751 +
4752 +     UNWIND_POSTRM_UPGRADE_OLD_PKG:
4753 +         postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4754 +     UNWIND_CHECK_DATA_FILE_CLASHES:
4755 +         check_data_file_clashes_unwind(conf, pkg, old_pkg);
4756 +     UNWIND_BACKUP_MODIFIED_CONFFILES:
4757 +         backup_modified_conffiles_unwind(conf, pkg, old_pkg);
4758 +     UNWIND_PREINST_CONFIGURE:
4759 +         preinst_configure_unwind(conf, pkg, old_pkg);
4760 +     UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
4761 +         prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
4762 +     UNWIND_PRERM_UPGRADE_OLD_PKG:
4763 +         prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4764 +     UNWIND_REMOVE_INSTALLED_REPLACEES:
4765 +         pkg_remove_installed_replacees_unwind(conf, replacees);
4766 +
4767 +         ipkg_message(conf, IPKG_INFO,
4768 +                      "  cleanup temp files\n");
4769 +         cleanup_temporary_files(conf, pkg);
4770 +
4771 +         ipkg_message(conf, IPKG_INFO,
4772 +                      "Failed.\n");
4773 +         if (use_signal)
4774 +              signal(SIGINT, old_handler);
4775 +         else
4776 +              sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4777 +
4778 +         return err;
4779 +     }
4780 +}
4781 +
4782 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4783 +{
4784 +     /* DPKG_INCOMPATIBILITY:
4785 +       dpkg does some things here that we don't do yet. Do we care?
4786 +       
4787 +       1. If a version of the package is already installed, call
4788 +          old-prerm upgrade new-version
4789 +       2. If the script runs but exits with a non-zero exit status
4790 +          new-prerm failed-upgrade old-version
4791 +          Error unwind, for both the above cases:
4792 +          old-postinst abort-upgrade new-version
4793 +     */
4794 +     return 0;
4795 +}
4796 +
4797 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4798 +{
4799 +     /* DPKG_INCOMPATIBILITY:
4800 +       dpkg does some things here that we don't do yet. Do we care?
4801 +       (See prerm_upgrade_old_package for details)
4802 +     */
4803 +     return 0;
4804 +}
4805 +
4806 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4807 +{
4808 +     /* DPKG_INCOMPATIBILITY:
4809 +       dpkg does some things here that we don't do yet. Do we care?
4810 +       2. If a 'conflicting' package is being removed at the same time:
4811 +               1. If any packages depended on that conflicting package and
4812 +                  --auto-deconfigure is specified, call, for each such package:
4813 +                  deconfigured's-prerm deconfigure \
4814 +                  in-favour package-being-installed version \
4815 +                  removing conflicting-package version
4816 +               Error unwind:
4817 +                  deconfigured's-postinst abort-deconfigure \
4818 +                  in-favour package-being-installed-but-failed version \
4819 +                  removing conflicting-package version
4820 +
4821 +                  The deconfigured packages are marked as requiring
4822 +                  configuration, so that if --install is used they will be
4823 +                  configured again if possible.
4824 +               2. To prepare for removal of the conflicting package, call:
4825 +                  conflictor's-prerm remove in-favour package new-version
4826 +               Error unwind:
4827 +                  conflictor's-postinst abort-remove in-favour package new-version
4828 +     */
4829 +     return 0;
4830 +}
4831 +
4832 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4833 +{
4834 +     /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
4835 +       do yet. Do we care?  (See prerm_deconfigure_conflictors for
4836 +       details) */
4837 +     return 0;
4838 +}
4839 +
4840 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4841 +{
4842 +     int err;
4843 +     char *preinst_args;
4844 +
4845 +     if (old_pkg) {
4846 +         char *old_version = pkg_version_str_alloc(old_pkg);
4847 +         sprintf_alloc(&preinst_args, "upgrade %s", old_version);
4848 +         free(old_version);
4849 +     } else if (pkg->state_status == SS_CONFIG_FILES) {
4850 +         char *pkg_version = pkg_version_str_alloc(pkg);
4851 +         sprintf_alloc(&preinst_args, "install %s", pkg_version);
4852 +         free(pkg_version);
4853 +     } else {
4854 +         preinst_args = strdup("install");
4855 +     }
4856 +
4857 +     err = pkg_run_script(conf, pkg, "preinst", preinst_args);
4858 +     if (err) {
4859 +         ipkg_message(conf, IPKG_ERROR,
4860 +                      "Aborting installation of %s\n", pkg->name);
4861 +         return 1;
4862 +     }
4863 +
4864 +     free(preinst_args);
4865 +
4866 +     return 0;
4867 +}
4868 +
4869 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4870 +{
4871 +     /* DPKG_INCOMPATIBILITY:
4872 +       dpkg does the following error unwind, should we?
4873 +       pkg->postrm abort-upgrade old-version
4874 +       OR pkg->postrm abort-install old-version
4875 +       OR pkg->postrm abort-install
4876 +     */
4877 +     return 0;
4878 +}
4879 +
4880 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4881 +{
4882 +     int err;
4883 +     conffile_list_elt_t *iter;
4884 +     conffile_t *cf;
4885 +
4886 +     if (conf->noaction) return 0;
4887 +
4888 +     /* Backup all modified conffiles */
4889 +     if (old_pkg) {
4890 +         for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4891 +              char *cf_name;
4892 +              
4893 +              cf = iter->data;
4894 +              cf_name = root_filename_alloc(conf, cf->name);
4895 +
4896 +              /* Don't worry if the conffile is just plain gone */
4897 +              if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
4898 +                   err = backup_make_backup(conf, cf_name);
4899 +                   if (err) {
4900 +                        return err;
4901 +                   }
4902 +              }
4903 +              free(cf_name);
4904 +         }
4905 +     }
4906 +
4907 +     /* Backup all conffiles that were not conffiles in old_pkg */
4908 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4909 +         char *cf_name;
4910 +         cf = iter->data;
4911 +         cf_name = root_filename_alloc(conf, cf->name);
4912 +         /* Ignore if this was a conffile in old_pkg as well */
4913 +         if (pkg_get_conffile(old_pkg, cf->name)) {
4914 +              continue;
4915 +         }
4916 +
4917 +         if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
4918 +              err = backup_make_backup(conf, cf_name);
4919 +              if (err) {
4920 +                   return err;
4921 +              }
4922 +         }
4923 +         free(cf_name);
4924 +     }
4925 +
4926 +     return 0;
4927 +}
4928 +
4929 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4930 +{
4931 +     conffile_list_elt_t *iter;
4932 +
4933 +     if (old_pkg) {
4934 +         for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4935 +              backup_remove(iter->data->name);
4936 +         }
4937 +     }
4938 +
4939 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4940 +         backup_remove(iter->data->name);
4941 +     }
4942 +
4943 +     return 0;
4944 +}
4945 +
4946 +
4947 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4948 +{
4949 +     /* DPKG_INCOMPATIBILITY:
4950 +       ipkg takes a slightly different approach than dpkg at this
4951 +       point.  dpkg installs each file in the new package while
4952 +       creating a backup for any file that is replaced, (so that it
4953 +       can unwind if necessary).  To avoid complexity and redundant
4954 +       storage, ipkg doesn't do any installation until later, (at the
4955 +       point at which dpkg removes the backups.
4956 +       
4957 +       But, we do have to check for data file clashes, since after
4958 +       installing a package with a file clash, removing either of the
4959 +       packages involved in the clash has the potential to break the
4960 +       other package.
4961 +     */
4962 +     str_list_t *files_list;
4963 +     str_list_elt_t *iter;
4964 +
4965 +     int clashes = 0;
4966 +
4967 +     files_list = pkg_get_installed_files(pkg);
4968 +     for (iter = files_list->head; iter; iter = iter->next) {
4969 +         char *root_filename;
4970 +         char *filename = iter->data;
4971 +         root_filename = root_filename_alloc(conf, filename);
4972 +         if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
4973 +              pkg_t *owner;
4974 +              pkg_t *obs;
4975 +              /* Pre-existing conffiles are OK */
4976 +              /* @@@@ should have way to check that it is a conffile -Jamey */
4977 +              if (backup_exists_for(root_filename)) {
4978 +                   continue;
4979 +              }
4980 +
4981 +              /* Pre-existing files are OK if force-overwrite was asserted. */ 
4982 +              if (conf->force_overwrite) {
4983 +                   /* but we need to change who owns this file */
4984 +                   file_hash_set_file_owner(conf, filename, pkg);
4985 +                   continue;
4986 +              }
4987 +
4988 +              owner = file_hash_get_file_owner(conf, filename);
4989 +
4990 +              /* Pre-existing files are OK if owned by the pkg being upgraded. */
4991 +              if (owner && old_pkg) {
4992 +                   if (strcmp(owner->name, old_pkg->name) == 0) {
4993 +                        continue;
4994 +                   }
4995 +              }
4996 +
4997 +              /* Pre-existing files are OK if owned by a package replaced by new pkg. */
4998 +              if (owner) {
4999 +                    ipkg_message(conf, IPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
5000 +                   if (pkg_replaces(pkg, owner)) {
5001 +                        continue;
5002 +                   }
5003 +/* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
5004 +   then it's ok to overwrite. */
5005 +                    if (strcmp(owner->name,pkg->name)==0){
5006 +                        ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
5007 +                        continue;
5008 +                    }
5009 +              }
5010 +
5011 +              /* Pre-existing files are OK if they are obsolete */
5012 +              obs = hash_table_get(&conf->obs_file_hash, filename);
5013 +              if (obs) {
5014 +                   ipkg_message(conf, IPKG_INFO, "Pre-exiting file %s is obsolete.  obs_pkg=%s\n", filename, obs->name);
5015 +                   continue;
5016 +              }
5017 +
5018 +              /* We have found a clash. */
5019 +              ipkg_message(conf, IPKG_ERROR,
5020 +                           "Package %s wants to install file %s\n"
5021 +                           "\tBut that file is already provided by package ",
5022 +                           pkg->name, filename);
5023 +              if (owner) {
5024 +                   ipkg_message(conf, IPKG_ERROR,
5025 +                                "%s\n", owner->name);
5026 +              } else {
5027 +                   ipkg_message(conf, IPKG_ERROR,
5028 +                                "<no package>\nPlease move this file out of the way and try again.\n");
5029 +              }
5030 +              clashes++;
5031 +         }
5032 +         free(root_filename);
5033 +     }
5034 +     pkg_free_installed_files(pkg);
5035 +
5036 +     return clashes;
5037 +}
5038 +
5039 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5040 +{
5041 +    /* Basically that's the worst hack I could do to be able to change ownership of
5042 +       file list, but, being that we have no way to unwind the mods, due to structure
5043 +       of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
5044 +       What we do here is change the ownership of file in hash if a replace ( or similar events
5045 +       happens )
5046 +       Only the action that are needed to change name should be considered.
5047 +       @@@ To change after 1.0 release.
5048 +     */
5049 +     str_list_t *files_list;
5050 +     str_list_elt_t *iter;
5051 +
5052 +     int clashes = 0;
5053 +
5054 +     files_list = pkg_get_installed_files(pkg);
5055 +     for (iter = files_list->head; iter; iter = iter->next) {
5056 +         char *root_filename;
5057 +         char *filename = iter->data;
5058 +         root_filename = root_filename_alloc(conf, filename);
5059 +         if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
5060 +              pkg_t *owner;
5061 +
5062 +              if (conf->force_overwrite) {
5063 +                   /* but we need to change who owns this file */
5064 +                   file_hash_set_file_owner(conf, filename, pkg);
5065 +                   continue;
5066 +              }
5067 +
5068 +              owner = file_hash_get_file_owner(conf, filename);
5069 +
5070 +              /* Pre-existing files are OK if owned by a package replaced by new pkg. */
5071 +              if (owner) {
5072 +                   if (pkg_replaces(pkg, owner)) {
5073 +/* It's now time to change the owner of that file. 
5074 +   It has been "replaced" from the new "Replaces", then I need to inform lists file about that.  */
5075 +                        ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
5076 +                        file_hash_set_file_owner(conf, filename, pkg);
5077 +                        continue;
5078 +                   }
5079 +              }
5080 +
5081 +         }
5082 +         free(root_filename);
5083 +     }
5084 +     pkg_free_installed_files(pkg);
5085 +
5086 +     return clashes;
5087 +}
5088 +
5089 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5090 +{
5091 +     /* Nothing to do since check_data_file_clashes doesn't change state */
5092 +     return 0;
5093 +}
5094 +
5095 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5096 +{
5097 +     /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
5098 +       1. If the package is being upgraded, call
5099 +          old-postrm upgrade new-version
5100 +       2. If this fails, attempt:
5101 +          new-postrm failed-upgrade old-version
5102 +       Error unwind, for both cases:
5103 +          old-preinst abort-upgrade new-version    */
5104 +     return 0;
5105 +}
5106 +
5107 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5108 +{
5109 +     /* DPKG_INCOMPATIBILITY:
5110 +       dpkg does some things here that we don't do yet. Do we care?
5111 +       (See postrm_upgrade_old_pkg for details)
5112 +     */
5113 +    return 0;
5114 +}
5115 +
5116 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5117 +{
5118 +     int err;
5119 +     str_list_t *old_files;
5120 +     str_list_elt_t *of;
5121 +     str_list_t *new_files;
5122 +     str_list_elt_t *nf;
5123 +
5124 +     if (old_pkg == NULL) {
5125 +         return 0;
5126 +     }
5127 +
5128 +     old_files = pkg_get_installed_files(old_pkg);
5129 +     new_files = pkg_get_installed_files(pkg);
5130 +
5131 +     for (of = old_files->head; of; of = of->next) {
5132 +         pkg_t *owner;
5133 +         char *old, *new;
5134 +         old = of->data;
5135 +         for (nf = new_files->head; nf; nf = nf->next) {
5136 +              new = nf->data;
5137 +              if (strcmp(old, new) == 0) {
5138 +                   goto NOT_OBSOLETE;
5139 +              }
5140 +         }
5141 +         if (file_is_dir(old)) {
5142 +              continue;
5143 +         }
5144 +         owner = file_hash_get_file_owner(conf, old);
5145 +         if (owner != old_pkg) {
5146 +              /* in case obsolete file no longer belongs to old_pkg */
5147 +              continue;
5148 +         }
5149
5150 +         /* old file is obsolete */
5151 +         ipkg_message(conf, IPKG_INFO,
5152 +                      "    removing obsolete file %s\n", old);
5153 +         if (!conf->noaction) {
5154 +              err = unlink(old);
5155 +              if (err) {
5156 +                   ipkg_message(conf, IPKG_ERROR, "    Warning: remove %s failed: %s\n", old,
5157 +                                strerror(errno));
5158 +              }
5159 +         }
5160 +
5161 +     NOT_OBSOLETE:
5162 +         ;
5163 +     }
5164 +
5165 +     pkg_free_installed_files(old_pkg);
5166 +     pkg_free_installed_files(pkg);
5167 +
5168 +     return 0;
5169 +}
5170 +
5171 +static int remove_obsolete_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5172 +{
5173 +     int i;
5174 +     int err = 0;
5175 +     char *globpattern;
5176 +     glob_t globbuf;
5177 +     if (0) {
5178 +         if (!pkg->dest) {
5179 +              ipkg_message(conf, IPKG_ERROR, "%s: no dest for package %s\n", __FUNCTION__, pkg->name);
5180 +              return -1;
5181 +         }
5182 +         sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name);
5183 +         err = glob(globpattern, 0, NULL, &globbuf);
5184 +         free(globpattern);
5185 +         if (err) {
5186 +              return err;
5187 +         }
5188 +         /* XXXX this should perhaps only remove the ones that are not overwritten in new package.  Jamey 11/11/2003 */
5189 +         for (i = 0; i < globbuf.gl_pathc; i++) {
5190 +              ipkg_message(conf, IPKG_DEBUG, "Removing control file %s from old_pkg %s\n",
5191 +                           globbuf.gl_pathv[i], old_pkg->name);
5192 +              if (!conf->noaction)
5193 +                   unlink(globbuf.gl_pathv[i]);
5194 +         }
5195 +         globfree(&globbuf);
5196 +     }
5197 +     return err;
5198 +}
5199 +
5200 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5201 +{
5202 +     int ret;
5203 +     char *prefix;
5204 +
5205 +     if (old_pkg)
5206 +         remove_obsolete_maintainer_scripts(conf, pkg, old_pkg);
5207 +     sprintf_alloc(&prefix, "%s.", pkg->name);
5208 +     ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
5209 +                                                       pkg->dest->info_dir,
5210 +                                                       prefix);
5211 +     free(prefix);
5212 +     return ret;
5213 +}
5214 +
5215 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg)
5216 +{
5217 +     /* DPKG_INCOMPATIBILITY:
5218 +       This is a fairly sophisticated dpkg operation. Shall we
5219 +       skip it? */
5220 +     
5221 +     /* Any packages all of whose files have been overwritten during the
5222 +       installation, and which aren't required for dependencies, are
5223 +       considered to have been removed. For each such package
5224 +       1. disappearer's-postrm disappear overwriter overwriter-version
5225 +       2. The package's maintainer scripts are removed
5226 +       3. It is noted in the status database as being in a sane state,
5227 +           namely not installed (any conffiles it may have are ignored,
5228 +          rather than being removed by dpkg). Note that disappearing
5229 +          packages do not have their prerm called, because dpkg doesn't
5230 +          know in advance that the package is going to vanish.
5231 +     */
5232 +     return 0;
5233 +}
5234 +
5235 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg)
5236 +{
5237 +     int err;
5238 +
5239 +     /* ipkg takes a slightly different approach to data file backups
5240 +       than dpkg. Rather than removing backups at this point, we
5241 +       actually do the data file installation now. See comments in
5242 +       check_data_file_clashes() for more details. */
5243 +    
5244 +     ipkg_message(conf, IPKG_INFO,
5245 +                 "    extracting data files to %s\n", pkg->dest->root_dir);
5246 +     err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
5247 +     if (err) {
5248 +         return err;
5249 +     }
5250 +
5251 +     /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
5252 +        so we can't save ourself from removing important packages
5253 +        At this point we (should) have extracted the .control file, so it
5254 +        would be a good idea to reload the data in it, and set the Essential 
5255 +        state in *pkg. From now on the Essential is back in status file and
5256 +        we can protect again.
5257 +        We should operate this way:
5258 +        fopen the file ( pkg->dest->root_dir/pkg->name.control )
5259 +        check for "Essential" in it 
5260 +        set the value in pkg->essential.
5261 +        This new routine could be useful also for every other flag
5262 +        Pigi: 16/03/2004 */
5263 +     set_flags_from_control(conf, pkg) ;
5264 +     
5265 +     ipkg_message(conf, IPKG_DEBUG, "    Calling pkg_write_filelist from %s\n", __FUNCTION__);
5266 +     err = pkg_write_filelist(conf, pkg);
5267 +     if (err)
5268 +         return err;
5269 +
5270 +     /* XXX: FEATURE: ipkg should identify any files which existed
5271 +       before installation and which were overwritten, (see
5272 +       check_data_file_clashes()). What it must do is remove any such
5273 +       files from the filelist of the old package which provided the
5274 +       file. Otherwise, if the old package were removed at some point
5275 +       it would break the new package. Removing the new package will
5276 +       also break the old one, but this cannot be helped since the old
5277 +       package's file has already been deleted. This is the importance
5278 +       of check_data_file_clashes(), and only allowing ipkg to install
5279 +       a clashing package with a user force. */
5280 +
5281 +     return 0;
5282 +}
5283 +
5284 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg)
5285 +{
5286 +     conffile_list_elt_t *iter;
5287 +     conffile_t *cf;
5288 +     char *cf_backup;
5289 +
5290 +    char *md5sum;
5291 +
5292 +    
5293 +     if (conf->noaction) return 0;
5294 +
5295 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
5296 +         char *root_filename;
5297 +         cf = iter->data;
5298 +         root_filename = root_filename_alloc(conf, cf->name);
5299 +
5300 +         /* Might need to initialize the md5sum for each conffile */
5301 +         if (cf->value == NULL) {
5302 +              cf->value = file_md5sum_alloc(root_filename);
5303 +         }
5304 +
5305 +         if (!file_exists(root_filename)) {
5306 +              free(root_filename);
5307 +              continue;
5308 +         }
5309 +
5310 +         cf_backup = backup_filename_alloc(root_filename);
5311 +
5312 +
5313 +         if (file_exists(cf_backup)) {
5314 + /* Let's compute md5 to test if files are changed */
5315 +             md5sum = file_md5sum_alloc(cf_backup);
5316 +               if (strcmp( cf->value,md5sum) != 0 ) {
5317 +                 if (conf->force_defaults
5318 +                     || user_prefers_old_conffile(cf->name, cf_backup) ) {
5319 +                      rename(cf_backup, root_filename);
5320 +                 }
5321 +              }
5322 +              unlink(cf_backup);
5323 +              free(md5sum);
5324 +         }
5325 +
5326 +         free(cf_backup);
5327 +         free(root_filename);
5328 +     }
5329 +
5330 +     return 0;
5331 +}
5332 +
5333 +static int user_prefers_old_conffile(const char *file_name, const char *backup)
5334 +{
5335 +     char *response;
5336 +     const char *short_file_name;
5337 +
5338 +     short_file_name = strrchr(file_name, '/');
5339 +     if (short_file_name) {
5340 +         short_file_name++;
5341 +     } else {
5342 +         short_file_name = file_name;
5343 +     }
5344 +
5345 +     while (1) {
5346 +         response = get_user_response("    Configuration file '%s'\n"
5347 +                                      "    ==> File on system created by you or by a script.\n"
5348 +                                      "    ==> File also in package provided by package maintainer.\n"
5349 +                                      "       What would you like to do about it ?  Your options are:\n"
5350 +                                      "        Y or I  : install the package maintainer's version\n"
5351 +                                      "        N or O  : keep your currently-installed version\n"
5352 +                                      "          D     : show the differences between the versions (if diff is installed)\n"
5353 +                                      "     The default action is to keep your current version.\n"
5354 +                                      "    *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
5355 +         if (strcmp(response, "y") == 0
5356 +             || strcmp(response, "i") == 0
5357 +             || strcmp(response, "yes") == 0) {
5358 +              free(response);
5359 +              return 0;
5360 +         }
5361 +
5362 +         if (strcmp(response, "d") == 0) {
5363 +              char *cmd;
5364 +
5365 +              free(response);
5366 +              /* XXX: BUG rewrite to use exec or busybox's internal diff */
5367 +              sprintf_alloc(&cmd, "diff -u %s %s", backup, file_name);
5368 +              xsystem(cmd);
5369 +              free(cmd);
5370 +              printf("    [Press ENTER to continue]\n");
5371 +              response = file_read_line_alloc(stdin);
5372 +              free(response);
5373 +              continue;
5374 +         }
5375 +
5376 +         free(response);
5377 +         return 1;
5378 +     }
5379 +}
5380 +
5381 +/* XXX: CLEANUP: I'd like to move all of the code for
5382 +   creating/cleaning pkg->tmp_unpack_dir directly into pkg.c. (Then,
5383 +   it would make sense to cleanup pkg->tmp_unpack_dir directly from
5384 +   pkg_deinit for example). */
5385 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg)
5386 +{
5387 +     DIR *tmp_dir;
5388 +     struct dirent *dirent;
5389 +     char *tmp_file;
5390 +
5391 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
5392 +#error
5393 +     ipkg_message(conf, IPKG_DEBUG,
5394 +                 "%s: Not cleaning up %s since ipkg compiled with IPKG_DEBUG_NO_TMP_CLEANUP\n",
5395 +                 __FUNCTION__, pkg->tmp_unpack_dir);
5396 +     return 0;
5397 +#endif
5398 +
5399 +     if (pkg->tmp_unpack_dir && file_is_dir(pkg->tmp_unpack_dir)) {
5400 +         tmp_dir = opendir(pkg->tmp_unpack_dir);
5401 +         if (tmp_dir) {
5402 +              while (1) {
5403 +                   dirent = readdir(tmp_dir);
5404 +                   if (dirent == NULL) {
5405 +                        break;
5406 +                   }
5407 +                   sprintf_alloc(&tmp_file, "%s/%s",
5408 +                                 pkg->tmp_unpack_dir, dirent->d_name);
5409 +                   if (! file_is_dir(tmp_file)) {
5410 +                        unlink(tmp_file);
5411 +                   }
5412 +                   free(tmp_file);
5413 +              }
5414 +              closedir(tmp_dir);
5415 +              rmdir(pkg->tmp_unpack_dir);
5416 +              free(pkg->tmp_unpack_dir);
5417 +              pkg->tmp_unpack_dir = NULL;
5418 +         }
5419 +     }
5420 +
5421 +     ipkg_message(conf, IPKG_INFO, "cleanup_temporary_files: pkg=%s local_filename=%s tmp_dir=%s\n",
5422 +                 pkg->name, pkg->local_filename, conf->tmp_dir);
5423 +     if (pkg->local_filename && strncmp(pkg->local_filename, conf->tmp_dir, strlen(conf->tmp_dir)) == 0) {
5424 +         unlink(pkg->local_filename);
5425 +         free(pkg->local_filename);
5426 +         pkg->local_filename = NULL;
5427 +     }
5428 +
5429 +     return 0;
5430 +}
5431 +
5432 +static char *backup_filename_alloc(const char *file_name)
5433 +{
5434 +     char *backup;
5435 +
5436 +     sprintf_alloc(&backup, "%s%s", file_name, IPKG_BACKUP_SUFFIX);
5437 +
5438 +     return backup;
5439 +}
5440 +
5441 +int backup_make_backup(ipkg_conf_t *conf, const char *file_name)
5442 +{
5443 +     int err;
5444 +     char *backup;
5445 +    
5446 +     backup = backup_filename_alloc(file_name);
5447 +     err = file_copy(file_name, backup);
5448 +     if (err) {
5449 +         ipkg_message(conf, IPKG_ERROR,
5450 +                      "%s: Failed to copy %s to %s\n",
5451 +                      __FUNCTION__, file_name, backup);
5452 +     }
5453 +
5454 +     free(backup);
5455 +
5456 +     return err;
5457 +}
5458 +
5459 +static int backup_exists_for(const char *file_name)
5460 +{
5461 +     int ret;
5462 +     char *backup;
5463 +
5464 +     backup = backup_filename_alloc(file_name);
5465 +
5466 +     ret = file_exists(backup);
5467 +
5468 +     free(backup);
5469 +
5470 +     return ret;
5471 +}
5472 +
5473 +static int backup_remove(const char *file_name)
5474 +{
5475 +     char *backup;
5476 +
5477 +     backup = backup_filename_alloc(file_name);
5478 +     unlink(backup);
5479 +     free(backup);
5480 +
5481 +     return 0;
5482 +}
5483 +
5484 +\f
5485 +
5486 +#ifdef CONFIG_IPKG_PROCESS_ACTIONS
5487 +
5488 +int ipkg_remove_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove) 
5489 +{
5490 +     /* first, remove the packages that need removing */
5491 +     for (i = 0 ; i < pkgs_to_remove->len; i++ ) {
5492 +         pkg_t *pkg = pkgs_to_remove->pkgs[i];
5493 +         err = ipkg_remove_pkg(conf, pkg,0);
5494 +         if (err) return err;
5495 +     }
5496 +     return 0;
5497 +}
5498 +
5499 +int ipkg_process_actions_sanity_check(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_superseded, pkg_vec_t *pkgs_to_install)
5500 +{
5501 +     int i;
5502 +     /* now one more pass checking on the ones that need to be installed */
5503 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5504 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5505 +         if (pkg->dest == NULL)
5506 +              pkg->dest = conf->default_dest;
5507 +
5508 +         pkg->state_want = SW_INSTALL;
5509 +
5510 +         /* Abhaya: conflicts check */
5511 +         err = check_conflicts_for(conf, pkg);
5512 +         if (err) { return err; }
5513 +     }
5514 +     return 0;
5515 +}
5516 +
5517 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5518 +{
5519 +     int i;
5520 +     /* now one more pass checking on the ones that need to be installed */
5521 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5522 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5523 +
5524 +         /* XXX: FEATURE: Need to really support Provides/Replaces: here at some point */
5525 +         pkg_vec_t *replacees = pkg_vec_alloc();
5526 +         pkg_get_installed_replacees(conf, pkg, replacees);
5527 +
5528 +         /* XXX: BUG: we really should treat replacement more like an upgrade
5529 +          *      Instead, we're going to remove the replacees 
5530 +          */
5531 +         err = pkg_remove_installed_replacees(conf, replacees);
5532 +         if (err) return err;
5533 +         pkg->state_flag |= SF_REMOVED_REPLACEES;
5534 +     }
5535 +     return 0;
5536 +}
5537 +
5538 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5539 +{
5540 +     int i;
5541 +     /* now one more pass checking on the ones that need to be installed */
5542 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5543 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5544 +         if (pkg->local_filename == NULL) {
5545 +              err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
5546 +              if (err) {
5547 +                   ipkg_message(conf, IPKG_ERROR,
5548 +                                "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
5549 +                                pkg->name);
5550 +                   return err;
5551 +              }
5552 +         }
5553 +         if (pkg->tmp_unpack_dir == NULL) {
5554 +              err = unpack_pkg_control_files(conf, pkg);
5555 +              if (err) return err;
5556 +         }
5557 +     }
5558 +     return 0;
5559 +}
5560 +
5561 +int ipkg_process_actions_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5562 +{
5563 +     int i;
5564 +     /* now one more pass checking on the ones that need to be installed */
5565 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5566 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5567 +         pkg_t *old_pkg = pkg->old_pkg;
5568 +
5569 +         err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
5570 +         if (err) return err;
5571 +
5572 +         err = prerm_deconfigure_conflictors(conf, pkg, replacees);
5573 +         if (err) return err;
5574 +
5575 +         err = preinst_configure(conf, pkg, old_pkg);
5576 +         if (err) return err;
5577 +
5578 +         err = backup_modified_conffiles(conf, pkg, old_pkg);
5579 +         if (err) return err;
5580 +
5581 +         err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
5582 +         if (err) return err;
5583 +     }
5584 +     return 0;
5585 +}
5586 +
5587 +int ipkg_process_actions_install(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5588 +{
5589 +     int i;
5590 +     /* now one more pass checking on the ones that need to be installed */
5591 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5592 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5593 +         pkg_t *old_pkg = pkg->old_pkg;
5594 +
5595 +         if (old_pkg) {
5596 +              old_pkg->state_want = SW_DEINSTALL;
5597 +
5598 +              if (old_pkg->state_flag & SF_NOPRUNE) {
5599 +                   ipkg_message(conf, IPKG_INFO,
5600 +                                "  not removing obsolesced files because package marked noprune\n");
5601 +              } else {
5602 +                   ipkg_message(conf, IPKG_INFO,
5603 +                                "  removing obsolesced files\n");
5604 +                   remove_obsolesced_files(conf, pkg, old_pkg);
5605 +              }
5606 +         }
5607 +
5608 +         ipkg_message(conf, IPKG_INFO,
5609 +                      "  installing maintainer scripts\n");
5610 +         install_maintainer_scripts(conf, pkg, old_pkg);
5611 +
5612 +         /* the following just returns 0 */
5613 +         remove_disappeared(conf, pkg);
5614 +
5615 +         ipkg_message(conf, IPKG_INFO,
5616 +                      "  installing data files\n");
5617 +         install_data_files(conf, pkg);
5618 +
5619 +         ipkg_message(conf, IPKG_INFO,
5620 +                      "  resolving conf files\n");
5621 +         resolve_conffiles(conf, pkg);
5622 +
5623 +         pkg->state_status = SS_UNPACKED;
5624 +
5625 +         if (old_pkg) {
5626 +              old_pkg->state_status = SS_NOT_INSTALLED;
5627 +         }
5628 +
5629 +         time(&pkg->installed_time);
5630 +
5631 +         ipkg_message(conf, IPKG_INFO,
5632 +                      "  cleanup temp files\n");
5633 +         cleanup_temporary_files(conf, pkg);
5634 +
5635 +         if (pkg->parent)
5636 +              pkg->parent->state_status = pkg->state_status;
5637 +     }
5638 +     return 0;
5639 +}
5640 +
5641 +int ipkg_process_actions_unwind_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5642 +{
5643 +     int i;
5644 +     /* now one more pass checking on the ones that need to be installed */
5645 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5646 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5647 +         pkg_t *old_pkg = pkg->old_pkg;
5648 +
5649 +         if (old_pkg) {
5650 +              if (old_pkg->state_flags & SF_POSTRM_UPGRADE)
5651 +                   postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5652 +              if (old_pkg->state_flags & SF_CHECK_DATA_FILE_CLASHES)
5653 +                   check_data_file_clashes_unwind(conf, pkg, old_pkg);
5654 +              if (old_pkg->state_flags & SF_BACKUP_MODIFIED_CONFFILES)
5655 +                   backup_modified_conffiles_unwind(conf, pkg, old_pkg);
5656 +              if (old_pkg->state_flags & SF_PREINST_CONFIGURE)
5657 +                   preinst_configure_unwind(conf, pkg, old_pkg);
5658 +              if (old_pkg->state_flags & SF_DECONFIGURE_CONFLICTORS)
5659 +                   prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
5660 +              if (old_pkg->state_flags & SF_PRERM_UPGRADE)
5661 +                   prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5662 +
5663 +              if (old_pkg->state_flags & SF_REMOVED_REPLACEES)
5664 +                   remove_installed_replacees_unwind(conf, pkg, old_pkg);
5665 +
5666 +         }
5667 +     }
5668 +     return 0;
5669 +}
5670 +
5671 +/* 
5672 + * Perform all the actions.
5673 + *
5674 + * pkgs_to_remove are packages marked for removal.
5675 + * pkgs_superseded are the old packages being replaced by upgrades.
5676 + *
5677 + * Assumes pkgs_to_install includes all dependences, recursively, sorted in installable order.
5678 + */
5679 +int ipkg_process_actions(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_superseded, pkg_vec_t *pkgs_to_install)
5680 +{
5681 +     int err;
5682 +     int i;
5683 +
5684 +     err = ipkg_remove_packages(conf, pkgs_to_remove);
5685 +     if (err) return err;
5686 +
5687 +     err = ipkg_process_actions_sanity_check(conf, pkgs_superseded, pkgs_to_install);
5688 +     if (err) return err;
5689 +
5690 +     err = ipkg_process_actions_remove_replacees(conf, pkgs_to_install);
5691 +     if (err) goto UNWIND;
5692 +
5693 +     /* @@@@ look at ipkg_install_pkg for handling replacements */
5694 +     err = ipkg_process_actions_unpack_packages(conf, pkgs_to_install);
5695 +     if (err) goto UNWIND;
5696 +
5697 +     /* 
5698 +      * Now that we have the packages unpacked, we can look for data
5699 +      * file clashes.  First, we mark the files from the superseded
5700 +      * packages as obsolete.  Then we scan the files in
5701 +      * pkgs_to_install, and only complain about clashes with
5702 +      * non-obsolete files.
5703 +      */
5704 +
5705 +     err = ipkg_process_actions_check_data_file_clashes(conf, pkgs_superseded, pkgs_to_install);
5706 +     if (err) goto UNWIND;
5707 +
5708 +     /* this was before checking data file clashes */
5709 +     err = ipkg_process_actions_prerm(conf, pkgs_superseded, pkgs_to_install);
5710 +     if (err) goto UNWIND;
5711 +
5712 +     /* point of no return: no unwinding after this */
5713 +     err = ipkg_process_actions_install(conf, pkgs_to_install);
5714 +     if (err) return err;
5715 +
5716 +     ipkg_message(conf, IPKG_INFO, "Done.\n");
5717 +     return 0;
5718 +
5719 + UNWIND:
5720 +     ipkg_process_actions_unwind(conf, pkgs_to_install);
5721 +
5722 +     ipkg_message(conf, IPKG_INFO,
5723 +                 "  cleanup temp files\n");
5724 +     cleanup_temporary_files(conf, pkg);
5725 +
5726 +     ipkg_message(conf, IPKG_INFO,
5727 +                 "Failed.\n");
5728 +     return err;
5729 +}
5730 +
5731 +#endif
5732 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_install.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.h
5733 --- busybox-1.2.0-orig/archival/libipkg/ipkg_install.h  1970-01-01 01:00:00.000000000 +0100
5734 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.h 2006-07-22 16:31:25.000000000 +0200
5735 @@ -0,0 +1,35 @@
5736 +/* ipkg_install.h - the itsy package management system
5737 +
5738 +   Carl D. Worth
5739 +
5740 +   Copyright (C) 2001 University of Southern California
5741 +
5742 +   This program is free software; you can redistribute it and/or
5743 +   modify it under the terms of the GNU General Public License as
5744 +   published by the Free Software Foundation; either version 2, or (at
5745 +   your option) any later version.
5746 +
5747 +   This program is distributed in the hope that it will be useful, but
5748 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5749 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5750 +   General Public License for more details.
5751 +*/
5752 +
5753 +#ifndef IPKG_INSTALL_H
5754 +#define IPKG_INSTALL_H
5755 +
5756 +#include "pkg.h"
5757 +#include "ipkg_conf.h"
5758 +
5759 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name);
5760 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name);
5761 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename);
5762 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg,int from_upgrading);
5763 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
5764 +
5765 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf);
5766 +
5767 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg_name, pkg_vec_t *pkgs_needed);
5768 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed);
5769 +
5770 +#endif
5771 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_message.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.c
5772 --- busybox-1.2.0-orig/archival/libipkg/ipkg_message.c  1970-01-01 01:00:00.000000000 +0100
5773 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.c 2006-07-22 16:31:25.000000000 +0200
5774 @@ -0,0 +1,61 @@
5775 +/* ipkg_message.c - the itsy package management system
5776 +
5777 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5778 +
5779 +   This program is free software; you can redistribute it and/or
5780 +   modify it under the terms of the GNU General Public License as
5781 +   published by the Free Software Foundation; either version 2, or (at
5782 +   your option) any later version.
5783 +
5784 +   This program is distributed in the hope that it will be useful, but
5785 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5786 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5787 +   General Public License for more details.
5788 +*/
5789 +
5790 +
5791 +#include "ipkg.h"
5792 +#include "ipkg_conf.h"
5793 +#include "ipkg_message.h"
5794 +
5795 +#ifndef IPKG_LIB
5796 +
5797 +void
5798 +ipkg_message (ipkg_conf_t * conf, message_level_t level, char *fmt, ...)
5799 +{
5800 +       va_list ap;
5801 +
5802 +       if (conf && (conf->verbosity < level))
5803 +       {
5804 +               return;
5805 +       }
5806 +       else
5807 +       {
5808 +
5809 +               va_start (ap, fmt);
5810 +               vprintf (fmt, ap);
5811 +               va_end (ap);
5812 +       }
5813 +}
5814 +
5815 +#else
5816 +
5817 +#include "libipkg.h"
5818 +
5819 +//#define ipkg_message(conf, level, fmt, arg...) ipkg_cb_message(conf, level, fmt, ## arg)
5820 +
5821 +void
5822 +ipkg_message (ipkg_conf_t * conf, message_level_t level, char *fmt, ...)
5823 +{
5824 +       va_list ap;
5825 +       char ts[256];
5826 +
5827 +       if (ipkg_cb_message)
5828 +       {
5829 +               va_start (ap, fmt);
5830 +               vsnprintf (ts,256,fmt, ap);
5831 +               va_end (ap);
5832 +               ipkg_cb_message(conf,level,ts);
5833 +       }
5834 +}
5835 +#endif
5836 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_message.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.h
5837 --- busybox-1.2.0-orig/archival/libipkg/ipkg_message.h  1970-01-01 01:00:00.000000000 +0100
5838 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.h 2006-07-22 16:31:25.000000000 +0200
5839 @@ -0,0 +1,32 @@
5840 +/* ipkg_message.h - the itsy package management system
5841 +
5842 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5843 +
5844 +   This program is free software; you can redistribute it and/or
5845 +   modify it under the terms of the GNU General Public License as
5846 +   published by the Free Software Foundation; either version 2, or (at
5847 +   your option) any later version.
5848 +
5849 +   This program is distributed in the hope that it will be useful, but
5850 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5851 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5852 +   General Public License for more details.
5853 +*/
5854 +
5855 +#ifndef _IPKG_MESSAGE_H_
5856 +#define _IPKG_MESSAGE_H_
5857 +
5858 +#include "ipkg.h"
5859 +#include "ipkg_conf.h"
5860 +
5861 +typedef enum {
5862 +     IPKG_ERROR,       /* error conditions */
5863 +     IPKG_NOTICE,      /* normal but significant condition */
5864 +     IPKG_INFO,                /* informational message */
5865 +     IPKG_DEBUG,       /* debug level message */
5866 +     IPKG_DEBUG2,      /* more debug level message */
5867 +} message_level_t;
5868 +
5869 +extern void ipkg_message(ipkg_conf_t *conf, message_level_t level, char *fmt, ...);
5870 +
5871 +#endif /* _IPKG_MESSAGE_H_ */
5872 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_remove.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.c
5873 --- busybox-1.2.0-orig/archival/libipkg/ipkg_remove.c   1970-01-01 01:00:00.000000000 +0100
5874 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.c  2006-07-22 16:31:25.000000000 +0200
5875 @@ -0,0 +1,383 @@
5876 +/* ipkg_remove.c - the itsy package management system
5877 +
5878 +   Carl D. Worth
5879 +
5880 +   Copyright (C) 2001 University of Southern California
5881 +
5882 +   This program is free software; you can redistribute it and/or
5883 +   modify it under the terms of the GNU General Public License as
5884 +   published by the Free Software Foundation; either version 2, or (at
5885 +   your option) any later version.
5886 +
5887 +   This program is distributed in the hope that it will be useful, but
5888 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5889 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5890 +   General Public License for more details.
5891 +*/
5892 +
5893 +#include "ipkg.h"
5894 +#include "ipkg_message.h"
5895 +
5896 +#include <glob.h>
5897 +
5898 +#include "ipkg_remove.h"
5899 +
5900 +#include "file_util.h"
5901 +#include "sprintf_alloc.h"
5902 +#include "str_util.h"
5903 +
5904 +#include "ipkg_cmd.h"
5905 +
5906 +/*
5907 + * Returns number of the number of packages depending on the packages provided by this package.
5908 + * Every package implicitly provides itself.
5909 + */
5910 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
5911 +{
5912 +     int nprovides = pkg->provides_count;
5913 +     abstract_pkg_t **provides = pkg->provides;
5914 +     int n_installed_dependents = 0;
5915 +     int i;
5916 +     for (i = 0; i <= nprovides; i++) {
5917 +         abstract_pkg_t *providee = provides[i];
5918 +         abstract_pkg_t **dependers = providee->depended_upon_by;
5919 +         abstract_pkg_t *dep_ab_pkg;
5920 +         if (dependers == NULL)
5921 +              continue;
5922 +         while ((dep_ab_pkg = *dependers++) != NULL) {
5923 +              if (dep_ab_pkg->state_status == SS_INSTALLED){
5924 +                   n_installed_dependents++;
5925 +               }
5926 +         }
5927 +
5928 +     }
5929 +     /* if caller requested the set of installed dependents */
5930 +     if (pdependents) {
5931 +         int p = 0;
5932 +         abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
5933 +
5934 +          if ( dependents == NULL ){
5935 +              fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
5936 +              return -1;  
5937 +          }
5938 +
5939 +         *pdependents = dependents;
5940 +         for (i = 0; i <= nprovides; i++) {
5941 +              abstract_pkg_t *providee = provides[i];
5942 +              abstract_pkg_t **dependers = providee->depended_upon_by;
5943 +              abstract_pkg_t *dep_ab_pkg;
5944 +              if (dependers == NULL)
5945 +                   continue;
5946 +              while ((dep_ab_pkg = *dependers++) != NULL) {
5947 +                   if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
5948 +                        dependents[p++] = dep_ab_pkg;
5949 +                        dep_ab_pkg->state_flag |= SF_MARKED;
5950 +                   }
5951 +              }
5952 +         }
5953 +         dependents[p] = NULL;
5954 +         /* now clear the marks */
5955 +         for (i = 0; i < p; i++) {
5956 +              abstract_pkg_t *dep_ab_pkg = dependents[i];
5957 +              dep_ab_pkg->state_flag &= ~SF_MARKED;
5958 +         }
5959 +     }
5960 +     return n_installed_dependents;
5961 +}
5962 +
5963 +int ipkg_remove_dependent_pkgs (ipkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
5964 +{
5965 +    int i;
5966 +    int a;
5967 +    int count;
5968 +    pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
5969 +    abstract_pkg_t * ab_pkg;
5970 +
5971 +    if((ab_pkg = pkg->parent) == NULL){
5972 +       fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
5973 +               __FUNCTION__, pkg->name);
5974 +       return 0;
5975 +    }
5976 +    
5977 +    if (dependents == NULL)
5978 +           return 0;
5979 +
5980 +    // here i am using the dependencies_checked
5981 +    if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
5982 +       return 0;                          // has already been encountered in the process
5983 +                                          // of marking packages for removal - Karthik
5984 +    ab_pkg->dependencies_checked = 2;
5985 +
5986 +    i = 0;
5987 +    count = 1;
5988 +    while (dependents [i] != NULL) {
5989 +        abstract_pkg_t *dep_ab_pkg = dependents[i];
5990 +       
5991 +       if (dep_ab_pkg->dependencies_checked == 2){
5992 +           i++;
5993 +           continue;   
5994 +        }
5995 +        if (dep_ab_pkg->state_status == SS_INSTALLED) {
5996 +            for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
5997 +                pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
5998 +                if (dep_pkg->state_status == SS_INSTALLED) {
5999 +                    pkg_vec_insert(dependent_pkgs, dep_pkg);
6000 +                    count++;
6001 +                }
6002 +            }
6003 +        }
6004 +       i++;
6005 +       /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
6006 +        * 2 - to keep track of pkgs whose deps have been checked alrdy  - Karthik */   
6007 +    }
6008 +    
6009 +    if (count == 1)
6010 +           return 0;
6011 +    
6012 +    
6013 +    for (i = 0; i < dependent_pkgs->len; i++) {
6014 +        int err = ipkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
6015 +        if (err)
6016 +            return err;
6017 +    }
6018 +    return 0;
6019 +}
6020 +
6021 +static int user_prefers_removing_dependents(ipkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
6022 +{
6023 +    abstract_pkg_t *dep_ab_pkg;
6024 +    ipkg_message(conf, IPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
6025 +    while ((dep_ab_pkg = *dependents++) != NULL) {
6026 +        if (dep_ab_pkg->state_status == SS_INSTALLED)
6027 +             ipkg_message(conf, IPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
6028 +    }
6029 +    ipkg_message(conf, IPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
6030 +    ipkg_message(conf, IPKG_ERROR, "");
6031 +    ipkg_message(conf, IPKG_ERROR, "You can force removal of this package with -force-depends.\n");
6032 +    ipkg_message(conf, IPKG_ERROR, "You can force removal of this package and its dependents\n");
6033 +    ipkg_message(conf, IPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
6034 +    ipkg_message(conf, IPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
6035 +    ipkg_message(conf, IPKG_ERROR, "in ipkg.conf.\n");
6036 +    return 0;
6037 +}
6038 +
6039 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message)
6040 +{
6041 +/* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
6042 +   thus I wan't check for essential, as I'm upgrading.
6043 +   I hope it won't break anything :) 
6044 +*/
6045 +     int err;
6046 +     abstract_pkg_t *parent_pkg = NULL;
6047 +       
6048 +     if (pkg->essential && !message) {
6049 +         if (conf->force_removal_of_essential_packages) {
6050 +              fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
6051 +                      "\tIf your system breaks, you get to keep both pieces\n",
6052 +                      pkg->name);
6053 +         } else {
6054 +              fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
6055 +                      "\tRemoving an essential package may lead to an unusable system, but if\n"
6056 +                      "\tyou enjoy that kind of pain, you can force ipkg to proceed against\n"
6057 +                      "\tits will with the option: -force-removal-of-essential-packages\n",
6058 +                      pkg->name);
6059 +              return IPKG_PKG_IS_ESSENTIAL;
6060 +         }
6061 +     }
6062 +
6063 +     if ((parent_pkg = pkg->parent) == NULL)
6064 +         return 0;
6065 +
6066 +     /* only attempt to remove dependent installed packages if
6067 +      * force_depends is not specified or the package is being
6068 +      * replaced.
6069 +      */
6070 +     if (!conf->force_depends
6071 +        && !(pkg->state_flag & SF_REPLACE)) {
6072 +         abstract_pkg_t **dependents;
6073 +         int has_installed_dependents = 
6074 +              pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
6075 +
6076 +         if (has_installed_dependents) {
6077 +              /*
6078 +               * if this package is depended up by others, then either we should
6079 +               * not remove it or we should remove it and all of its dependents 
6080 +               */
6081 +
6082 +              if (!conf->force_removal_of_dependent_packages
6083 +                  && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
6084 +                   return IPKG_PKG_HAS_DEPENDENTS;
6085 +              }
6086 +
6087 +              /* remove packages depending on this package - Karthik */
6088 +              err = ipkg_remove_dependent_pkgs (conf, pkg, dependents);
6089 +              free(dependents);
6090 +              if (err) return err;
6091 +         }
6092 +     }
6093 +
6094 +     if ( message==0 ){
6095 +         printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
6096 +         fflush(stdout);
6097 +     }
6098 +     pkg->state_flag |= SF_FILELIST_CHANGED;
6099 +
6100 +     pkg->state_want = SW_DEINSTALL;
6101 +     ipkg_state_changed++;
6102 +
6103 +     pkg_run_script(conf, pkg, "prerm", "remove");
6104 +
6105 +     /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
6106 +       maintains an empty filelist rather than deleting it. That seems
6107 +       like a big pain, and I don't see that that should make a big
6108 +       difference, but for anyone who wants tighter compatibility,
6109 +       feel free to fix this. */
6110 +     remove_data_files_and_list(conf, pkg);
6111 +
6112 +     pkg_run_script(conf, pkg, "postrm", "remove");
6113 +
6114 +     remove_maintainer_scripts_except_postrm(conf, pkg);
6115 +
6116 +     /* Aman Gupta - Since ipkg is made for handheld devices with limited
6117 +      * space, it doesn't make sense to leave extra configurations, files, 
6118 +      * and maintainer scripts left around. So, we make remove like purge, 
6119 +      * and take out all the crap :) */
6120 +
6121 +     remove_postrm(conf, pkg);
6122 +     pkg->state_status = SS_NOT_INSTALLED;
6123 +
6124 +     if (parent_pkg) 
6125 +         parent_pkg->state_status = SS_NOT_INSTALLED;
6126 +
6127 +     return 0;
6128 +}
6129 +
6130 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg)
6131 +{
6132 +    ipkg_remove_pkg(conf, pkg,0);
6133 +    return 0;
6134 +}
6135 +
6136 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg)
6137 +{
6138 +     str_list_t installed_dirs;
6139 +     str_list_t *installed_files;
6140 +     str_list_elt_t *iter;
6141 +     char *file_name;
6142 +     conffile_t *conffile;
6143 +     int removed_a_dir;
6144 +     pkg_t *owner;
6145 +
6146 +     str_list_init(&installed_dirs);
6147 +     installed_files = pkg_get_installed_files(pkg);
6148 +
6149 +     for (iter = installed_files->head; iter; iter = iter->next) {
6150 +         file_name = iter->data;
6151 +
6152 +         if (file_is_dir(file_name)) {
6153 +              str_list_append(&installed_dirs, strdup(file_name));
6154 +              continue;
6155 +         }
6156 +
6157 +         conffile = pkg_get_conffile(pkg, file_name);
6158 +         if (conffile) {
6159 +              /* XXX: QUESTION: Is this right? I figure we only need to
6160 +                 save the conffile if it has been modified. Is that what
6161 +                 dpkg does? Or does dpkg preserve all conffiles? If so,
6162 +                 this seems like a better thing to do to conserve
6163 +                 space. */
6164 +              if (conffile_has_been_modified(conf, conffile)) {
6165 +                   printf("  not deleting modified conffile %s\n", file_name);
6166 +                   fflush(stdout);
6167 +                   continue;
6168 +              }
6169 +         }
6170 +
6171 +         ipkg_message(conf, IPKG_INFO, "  deleting %s (noaction=%d)\n", file_name, conf->noaction);
6172 +         if (!conf->noaction)
6173 +              unlink(file_name);
6174 +     }
6175 +
6176 +     if (!conf->noaction) {
6177 +         do {
6178 +              removed_a_dir = 0;
6179 +              for (iter = installed_dirs.head; iter; iter = iter->next) {
6180 +                   file_name = iter->data;
6181 +           
6182 +                   if (rmdir(file_name) == 0) {
6183 +                        ipkg_message(conf, IPKG_INFO, "  deleting %s\n", file_name);
6184 +                        removed_a_dir = 1;
6185 +                        str_list_remove(&installed_dirs, &iter);
6186 +                   }
6187 +              }
6188 +         } while (removed_a_dir);
6189 +     }
6190 +
6191 +     pkg_free_installed_files(pkg);
6192 +     /* We have to remove the file list now, so that
6193 +       find_pkg_owning_file does not always just report this package */
6194 +     pkg_remove_installed_files_list(conf, pkg);
6195 +
6196 +     /* Don't print warning for dirs that are provided by other packages */
6197 +     for (iter = installed_dirs.head; iter; iter = iter->next) {
6198 +         file_name = iter->data;
6199 +
6200 +         owner = file_hash_get_file_owner(conf, file_name);
6201 +         if (owner) {
6202 +              free(iter->data);
6203 +              iter->data = NULL;
6204 +              str_list_remove(&installed_dirs, &iter);
6205 +         }
6206 +     }
6207 +
6208 +     /* cleanup */
6209 +     for (iter = installed_dirs.head; iter; iter = iter->next) {
6210 +         free(iter->data);
6211 +         iter->data = NULL;
6212 +     }
6213 +     str_list_deinit(&installed_dirs);
6214 +
6215 +     return 0;
6216 +}
6217 +
6218 +int remove_maintainer_scripts_except_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6219 +{
6220 +    int i, err;
6221 +    char *globpattern;
6222 +    glob_t globbuf;
6223 +    
6224 +    if (conf->noaction) return 0;
6225 +
6226 +    sprintf_alloc(&globpattern, "%s/%s.*",
6227 +                 pkg->dest->info_dir, pkg->name);
6228 +    err = glob(globpattern, 0, NULL, &globbuf);
6229 +    free(globpattern);
6230 +    if (err) {
6231 +       return 0;
6232 +    }
6233 +
6234 +    for (i = 0; i < globbuf.gl_pathc; i++) {
6235 +       if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
6236 +           continue;
6237 +       }
6238 +        ipkg_message(conf, IPKG_INFO, "  deleting %s\n", globbuf.gl_pathv[i]);
6239 +       unlink(globbuf.gl_pathv[i]);
6240 +    }
6241 +    globfree(&globbuf);
6242 +
6243 +    return 0;
6244 +}
6245 +
6246 +int remove_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6247 +{
6248 +    char *postrm_file_name;
6249 +
6250 +    if (conf->noaction) return 0;
6251 +
6252 +    sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
6253 +                 pkg->dest->info_dir, pkg->name);
6254 +    unlink(postrm_file_name);
6255 +    free(postrm_file_name);
6256 +
6257 +    return 0;
6258 +}
6259 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_remove.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.h
6260 --- busybox-1.2.0-orig/archival/libipkg/ipkg_remove.h   1970-01-01 01:00:00.000000000 +0100
6261 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.h  2006-07-22 16:31:25.000000000 +0200
6262 @@ -0,0 +1,33 @@
6263 +/* ipkg_remove.h - the itsy package management system
6264 +
6265 +   Carl D. Worth
6266 +
6267 +   Copyright (C) 2001 University of Southern California
6268 +
6269 +   This program is free software; you can redistribute it and/or
6270 +   modify it under the terms of the GNU General Public License as
6271 +   published by the Free Software Foundation; either version 2, or (at
6272 +   your option) any later version.
6273 +
6274 +   This program is distributed in the hope that it will be useful, but
6275 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6276 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6277 +   General Public License for more details.
6278 +*/
6279 +
6280 +#ifndef IPKG_REMOVE_H
6281 +#define IPKG_REMOVE_H
6282 +
6283 +#include "pkg.h"
6284 +#include "ipkg_conf.h"
6285 +
6286 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message);
6287 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg);
6288 +int possible_broken_removal_of_packages (ipkg_conf_t *conf, pkg_t *pkg);
6289 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents);
6290 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg);
6291 +int remove_maintainer_scripts_except_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6292 +int remove_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6293 +
6294 +
6295 +#endif
6296 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.c
6297 --- busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.c  1970-01-01 01:00:00.000000000 +0100
6298 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.c 2006-07-22 16:31:25.000000000 +0200
6299 @@ -0,0 +1,77 @@
6300 +/* ipkg_upgrade.c - the itsy package management system
6301 +
6302 +   Carl D. Worth
6303 +   Copyright (C) 2001 University of Southern California
6304 +
6305 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6306 +
6307 +   This program is free software; you can redistribute it and/or
6308 +   modify it under the terms of the GNU General Public License as
6309 +   published by the Free Software Foundation; either version 2, or (at
6310 +   your option) any later version.
6311 +
6312 +   This program is distributed in the hope that it will be useful, but
6313 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6314 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6315 +   General Public License for more details.
6316 +*/
6317 +
6318 +#include "ipkg.h"
6319 +#include "ipkg_install.h"
6320 +#include "ipkg_message.h"
6321 +
6322 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old)
6323 +{
6324 +     pkg_t *new;
6325 +     int cmp;
6326 +     char *old_version, *new_version;
6327 +
6328 +     if (old->state_flag & SF_HOLD) {
6329 +          ipkg_message(conf, IPKG_NOTICE,
6330 +                       "Not upgrading package %s which is marked "
6331 +                       "hold (flags=%#x)\n", old->name, old->state_flag);
6332 +          return 0;
6333 +     }
6334 +
6335 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name);
6336 +     if (new == NULL) {
6337 +          old_version = pkg_version_str_alloc(old);
6338 +          ipkg_message(conf, IPKG_NOTICE,
6339 +                       "Assuming locally installed package %s (%s) "
6340 +                       "is up to date.\n", old->name, old_version);
6341 +          free(old_version);
6342 +          return 0;
6343 +     }
6344 +          
6345 +     old_version = pkg_version_str_alloc(old);
6346 +     new_version = pkg_version_str_alloc(new);
6347 +               
6348 +     cmp = pkg_compare_versions(old, new);
6349 +     ipkg_message(conf, IPKG_DEBUG,
6350 +                  "comparing visible versions of pkg %s:"
6351 +                  "\n\t%s is installed "
6352 +                  "\n\t%s is available "
6353 +                  "\n\t%d was comparison result\n",
6354 +                  old->name, old_version, new_version, cmp);
6355 +     if (cmp == 0) {
6356 +          ipkg_message(conf, IPKG_INFO,
6357 +                       "Package %s (%s) installed in %s is up to date.\n",
6358 +                       old->name, old_version, old->dest->name);
6359 +          free(old_version);
6360 +          free(new_version);
6361 +          return 0;
6362 +     } else if (cmp > 0) {
6363 +          ipkg_message(conf, IPKG_NOTICE,
6364 +                       "Not downgrading package %s on %s from %s to %s.\n",
6365 +                       old->name, old->dest->name, old_version, new_version);
6366 +          free(old_version);
6367 +          free(new_version);
6368 +          return 0;
6369 +     } else if (cmp < 0) {
6370 +          new->dest = old->dest;
6371 +          old->state_want = SW_DEINSTALL;
6372 +     }
6373 +
6374 +     new->state_flag |= SF_USER;
6375 +     return ipkg_install_pkg(conf, new,1);
6376 +}
6377 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.h
6378 --- busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.h  1970-01-01 01:00:00.000000000 +0100
6379 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.h 2006-07-22 16:31:25.000000000 +0200
6380 @@ -0,0 +1,18 @@
6381 +/* ipkg_upgrade.c - the itsy package management system
6382 +
6383 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6384 +
6385 +   This program is free software; you can redistribute it and/or
6386 +   modify it under the terms of the GNU General Public License as
6387 +   published by the Free Software Foundation; either version 2, or (at
6388 +   your option) any later version.
6389 +
6390 +   This program is distributed in the hope that it will be useful, but
6391 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6392 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6393 +   General Public License for more details.
6394 +*/
6395 +
6396 +#include "ipkg.h"
6397 +
6398 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old);
6399 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_utils.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.c
6400 --- busybox-1.2.0-orig/archival/libipkg/ipkg_utils.c    1970-01-01 01:00:00.000000000 +0100
6401 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.c   2006-07-22 16:31:25.000000000 +0200
6402 @@ -0,0 +1,181 @@
6403 +/* ipkg_utils.c - the itsy package management system
6404 +
6405 +   Steven M. Ayer
6406 +   
6407 +   Copyright (C) 2002 Compaq Computer Corporation
6408 +
6409 +   This program is free software; you can redistribute it and/or
6410 +   modify it under the terms of the GNU General Public License as
6411 +   published by the Free Software Foundation; either version 2, or (at
6412 +   your option) any later version.
6413 +
6414 +   This program is distributed in the hope that it will be useful, but
6415 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6416 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6417 +   General Public License for more details.
6418 +*/
6419 +
6420 +#include "ipkg.h"
6421 +#include <errno.h>
6422 +#include <ctype.h>
6423 +#include <sys/vfs.h>
6424 +
6425 +#include "ipkg_utils.h"
6426 +#include "pkg.h"
6427 +#include "pkg_hash.h"
6428 +
6429 +struct errlist* error_list;
6430 +
6431 +int get_available_blocks(char * filesystem)
6432 +{
6433 +     struct statfs sfs;
6434 +
6435 +     if(statfs(filesystem, &sfs)){
6436 +         fprintf(stderr, "bad statfs\n");
6437 +         return 0;
6438 +     }
6439 +     /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
6440 +     return ((sfs.f_bavail * sfs.f_bsize) / 1024);
6441 +}
6442 +
6443 +char **read_raw_pkgs_from_file(const char *file_name)
6444 +{
6445 +     FILE *fp; 
6446 +     char **ret;
6447 +    
6448 +     if(!(fp = fopen(file_name, "r"))){
6449 +         fprintf(stderr, "can't get %s open for read\n", file_name);
6450 +         return NULL;
6451 +     }
6452 +
6453 +     ret = read_raw_pkgs_from_stream(fp);
6454 +
6455 +     fclose(fp);
6456 +
6457 +     return ret;
6458 +}
6459 +
6460 +char **read_raw_pkgs_from_stream(FILE *fp)
6461 +{    
6462 +     char **raw = NULL, *buf, *scout;
6463 +     int count = 0;
6464 +     size_t size = 512;
6465 +     
6466 +     buf = malloc (size);
6467 +
6468 +     while (fgets(buf, size, fp)) {
6469 +         while (strlen (buf) == (size - 1)
6470 +                && buf[size-2] != '\n') {
6471 +              size_t o = size - 1;
6472 +              size *= 2;
6473 +              buf = realloc (buf, size);
6474 +              if (fgets (buf + o, size - o, fp) == NULL)
6475 +                   break;
6476 +         }
6477 +         
6478 +         if(!(count % 50))
6479 +              raw = realloc(raw, (count + 50) * sizeof(char *));
6480 +       
6481 +         if((scout = strchr(buf, '\n')))
6482 +              *scout = '\0';
6483 +
6484 +         raw[count++] = strdup(buf);
6485 +     }
6486 +    
6487 +     raw = realloc(raw, (count + 1) * sizeof(char *));
6488 +     raw[count] = NULL;
6489 +
6490 +     free (buf);
6491 +    
6492 +     return raw;
6493 +}
6494 +
6495 +/* something to remove whitespace, a hash pooper */
6496 +char *trim_alloc(char *line)
6497 +{
6498 +     char *new; 
6499 +     char *dest, *src, *end;
6500 +    
6501 +     new = malloc(strlen(line) + 1);
6502 +     if ( new == NULL ){
6503 +        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
6504 +        return NULL;
6505 +     }
6506 +     dest = new, src = line, end = line + (strlen(line) - 1);
6507 +
6508 +     /* remove it from the front */    
6509 +     while(src && 
6510 +          isspace(*src) &&
6511 +          *src)
6512 +         src++;
6513 +     /* and now from the back */
6514 +     while((end > src) &&
6515 +          isspace(*end))
6516 +         end--;
6517 +     end++;
6518 +     *end = '\0';
6519 +     strcpy(new, src);
6520 +     /* this does from the first space
6521 +      *  blasting away any versions stuff in depends
6522 +      while(src && 
6523 +      !isspace(*src) &&
6524 +      *src)
6525 +      *dest++ = *src++;
6526 +      *dest = '\0';
6527 +      */
6528 +    
6529 +     return new;
6530 +}
6531 +
6532 +int line_is_blank(const char *line)
6533 +{
6534 +     const char *s;
6535 +
6536 +     for (s = line; *s; s++) {
6537 +         if (!isspace(*s))
6538 +              return 0;
6539 +     }
6540 +     return 1;
6541 +}
6542 +
6543 +void push_error_list(struct errlist ** errors, char * msg){
6544 +  struct errlist *err_lst_tmp;
6545 +
6546 +
6547 +  err_lst_tmp = malloc ( sizeof (err_lst_tmp) );
6548 +  err_lst_tmp->errmsg=strdup(msg) ;
6549 +  err_lst_tmp->next = *errors;
6550 +  *errors = err_lst_tmp;
6551 +}
6552 +
6553 +
6554 +void reverse_error_list(struct errlist **errors){
6555 +   struct errlist *result=NULL;
6556 +   struct errlist *current= *errors;
6557 +   struct errlist *next;
6558 +
6559 +   while ( current != NULL ) {
6560 +      next = current->next;
6561 +      current->next=result;
6562 +      result=current;
6563 +      current=next;
6564 +   }
6565 +   *errors=result;
6566 +
6567 +}
6568 +
6569 +       
6570 +void free_error_list(struct errlist **errors){
6571 +  struct errlist *current = *errors;
6572 +
6573 +    while (current != NULL) {
6574 +      free(current->errmsg);
6575 +      current = (*errors)->next;
6576 +      free(*errors);
6577 +      *errors = current;
6578 +    }
6579 +
6580 +
6581 +}
6582 +
6583 +       
6584 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_utils.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.h
6585 --- busybox-1.2.0-orig/archival/libipkg/ipkg_utils.h    1970-01-01 01:00:00.000000000 +0100
6586 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.h   2006-07-22 16:31:25.000000000 +0200
6587 @@ -0,0 +1,29 @@
6588 +/* ipkg_utils.h - the itsy package management system
6589 +
6590 +   Steven M. Ayer
6591 +   
6592 +   Copyright (C) 2002 Compaq Computer Corporation
6593 +
6594 +   This program is free software; you can redistribute it and/or
6595 +   modify it under the terms of the GNU General Public License as
6596 +   published by the Free Software Foundation; either version 2, or (at
6597 +   your option) any later version.
6598 +
6599 +   This program is distributed in the hope that it will be useful, but
6600 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6601 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6602 +   General Public License for more details.
6603 +*/
6604 +
6605 +#ifndef IPKG_UTILS_H
6606 +#define IPKG_UTILS_H
6607 +
6608 +#include "pkg.h"
6609 +
6610 +int get_available_blocks(char * filesystem);
6611 +char **read_raw_pkgs_from_file(const char *file_name);
6612 +char **read_raw_pkgs_from_stream(FILE *fp);
6613 +char *trim_alloc(char * line);
6614 +int line_is_blank(const char *line);
6615 +
6616 +#endif
6617 diff -ruN busybox-1.2.0-orig/archival/libipkg/libipkg.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.c
6618 --- busybox-1.2.0-orig/archival/libipkg/libipkg.c       1970-01-01 01:00:00.000000000 +0100
6619 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.c      2006-07-22 16:31:25.000000000 +0200
6620 @@ -0,0 +1,527 @@
6621 +/* ipkglib.c - the itsy package management system
6622 +
6623 +   Florina Boor
6624 +
6625 +   Copyright (C) 2003 kernel concepts
6626 +
6627 +   This program is free software; you can redistribute it and/or
6628 +   modify it under the terms of the GNU General Public License as
6629 +   published by the Free Software Foundation; either version 2, or (at
6630 +   your option) any later version.
6631 +
6632 +   This program is distributed in the hope that it will be useful, but
6633 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6634 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6635 +   General Public License for more details.
6636 +*/
6637 +
6638 +#ifdef IPKG_LIB
6639 +
6640 +#include "ipkg.h"
6641 +#include "ipkg_includes.h"
6642 +#include "libipkg.h"
6643 +
6644 +#include "args.h"
6645 +#include "ipkg_conf.h"
6646 +#include "ipkg_cmd.h"
6647 +#include "file_util.h"
6648 +
6649 +
6650 +
6651 +ipkg_message_callback ipkg_cb_message = NULL;
6652 +ipkg_response_callback ipkg_cb_response = NULL;
6653 +ipkg_status_callback ipkg_cb_status = NULL;
6654 +ipkg_list_callback ipkg_cb_list = NULL;
6655 +
6656 +
6657 +int
6658 +ipkg_init (ipkg_message_callback mcall, 
6659 +           ipkg_response_callback rcall,
6660 +           args_t * args)
6661 +{
6662 +       ipkg_cb_message = mcall;
6663 +       ipkg_cb_response = rcall;
6664 +
6665 +       args_init (args);
6666 +
6667 +       return 0;
6668 +}
6669 +
6670 +
6671 +int
6672 +ipkg_deinit (args_t * args)
6673 +{
6674 +       args_deinit (args);
6675 +       ipkg_cb_message = NULL;
6676 +       ipkg_cb_response = NULL;
6677 +
6678 +       /* place other cleanup stuff here */
6679 +
6680 +       return 0;
6681 +}
6682 +
6683 +
6684 +int
6685 +ipkg_packages_list(args_t *args, 
6686 +                   const char *packages, 
6687 +                   ipkg_list_callback cblist,
6688 +                   void *userdata)
6689 +{
6690 +       ipkg_cmd_t *cmd;
6691 +       ipkg_conf_t ipkg_conf;
6692 +       int err;
6693 +
6694 +       err = ipkg_conf_init (&ipkg_conf, args);
6695 +       if (err)
6696 +       {
6697 +               return err;
6698 +       }
6699 +
6700 +       ipkg_cb_list = cblist;
6701 +       /* we need to do this because of static declarations, 
6702 +        * maybe a good idea to change */
6703 +       cmd = ipkg_cmd_find ("list");
6704 +       if (packages)
6705 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6706 +       else
6707 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6708 +       ipkg_cb_list = NULL;
6709 +       ipkg_conf_deinit (&ipkg_conf);
6710 +       return (err);
6711 +}
6712 +
6713 +
6714 +int
6715 +ipkg_packages_status(args_t *args,
6716 +                     const char *packages,
6717 +                     ipkg_status_callback cbstatus,
6718 +                     void *userdata)
6719 +{
6720 +       ipkg_cmd_t *cmd;
6721 +       ipkg_conf_t ipkg_conf;
6722 +       int err;
6723 +
6724 +       err = ipkg_conf_init (&ipkg_conf, args);
6725 +       if (err)
6726 +       {
6727 +               return err;
6728 +       }
6729 +
6730 +       ipkg_cb_status = cbstatus;
6731 +
6732 +       /* we need to do this because of static declarations,
6733 +        * maybe a good idea to change */
6734 +       cmd = ipkg_cmd_find ("status");
6735 +       if (packages)
6736 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6737 +       else
6738 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6739 +
6740 +       ipkg_cb_status = NULL;
6741 +       ipkg_conf_deinit (&ipkg_conf);
6742 +       return (err);
6743 +}
6744 +
6745 +
6746 +int
6747 +ipkg_packages_info(args_t *args,
6748 +                   const char *packages,
6749 +                   ipkg_status_callback cbstatus,
6750 +                   void *userdata)
6751 +{
6752 +       ipkg_cmd_t *cmd;
6753 +       ipkg_conf_t ipkg_conf;
6754 +       int err;
6755 +
6756 +       err = ipkg_conf_init (&ipkg_conf, args);
6757 +       if (err)
6758 +       {
6759 +               return err;
6760 +       }
6761 +
6762 +       ipkg_cb_status = cbstatus;
6763 +
6764 +       /* we need to do this because of static declarations,
6765 +        * maybe a good idea to change */
6766 +       cmd = ipkg_cmd_find ("info");
6767 +       if (packages)
6768 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6769 +       else
6770 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6771 +
6772 +       ipkg_cb_status = NULL;
6773 +       ipkg_conf_deinit (&ipkg_conf);
6774 +       return (err);
6775 +}
6776 +
6777 +
6778 +int
6779 +ipkg_packages_install (args_t * args, const char *name)
6780 +{
6781 +       ipkg_cmd_t *cmd;
6782 +       ipkg_conf_t ipkg_conf;
6783 +       int err;
6784 +
6785 +       /* this error should be handled in application */
6786 +       if (!name || !strlen (name))
6787 +               return (-1);
6788 +
6789 +       err = ipkg_conf_init (&ipkg_conf, args);
6790 +       if (err)
6791 +       {
6792 +               return err;
6793 +       }
6794 +
6795 +       /* we need to do this because of static declarations,
6796 +        * maybe a good idea to change */
6797 +       cmd = ipkg_cmd_find ("install");
6798 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6799 +
6800 +       ipkg_conf_deinit(&ipkg_conf);
6801 +       return (err);
6802 +}
6803 +
6804 +
6805 +int
6806 +ipkg_packages_remove(args_t *args, const char *name, int purge)
6807 +{
6808 +       ipkg_cmd_t *cmd;
6809 +       ipkg_conf_t ipkg_conf;
6810 +       int err;
6811 +
6812 +       /* this error should be handled in application */
6813 +       if (!name || !strlen (name))
6814 +               return (-1);
6815 +
6816 +       err = ipkg_conf_init (&ipkg_conf, args);
6817 +       if (err)
6818 +       {
6819 +               return err;
6820 +       }
6821 +
6822 +       /* we need to do this because of static declarations, 
6823 +        * maybe a good idea to change */
6824 +       if (purge)
6825 +               cmd = ipkg_cmd_find ("purge");
6826 +       else
6827 +               cmd = ipkg_cmd_find ("remove");
6828 +
6829 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6830 +       
6831 +       ipkg_conf_deinit(&ipkg_conf);
6832 +       return (err);
6833 +}
6834 +
6835 +
6836 +int 
6837 +ipkg_lists_update(args_t *args)
6838 +{
6839 +       ipkg_cmd_t *cmd;
6840 +       ipkg_conf_t ipkg_conf;
6841 +       int err;
6842 +
6843 +       err = ipkg_conf_init (&ipkg_conf, args);
6844 +       if (err)
6845 +       {
6846 +               return err;
6847 +       }
6848 +
6849 +       /* we need to do this because of static declarations, 
6850 +        * maybe a good idea to change */
6851 +       cmd = ipkg_cmd_find ("update");
6852 +
6853 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6854 +       
6855 +       ipkg_conf_deinit(&ipkg_conf);
6856 +       return (err);
6857 +}
6858 +
6859 +
6860 +int 
6861 +ipkg_packages_upgrade(args_t *args)
6862 +{
6863 +       ipkg_cmd_t *cmd;
6864 +       ipkg_conf_t ipkg_conf;
6865 +       int err;
6866 +
6867 +       err = ipkg_conf_init (&ipkg_conf, args);
6868 +       if (err)
6869 +       {
6870 +               return err;
6871 +       }
6872 +
6873 +       /* we need to do this because of static declarations, 
6874 +        * maybe a good idea to change */
6875 +       cmd = ipkg_cmd_find ("upgrade");
6876 +
6877 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6878 +       
6879 +       ipkg_conf_deinit(&ipkg_conf);
6880 +       return (err);
6881 +}
6882 +
6883 +
6884 +int
6885 +ipkg_packages_download (args_t * args, const char *name)
6886 +{
6887 +       ipkg_cmd_t *cmd;
6888 +       ipkg_conf_t ipkg_conf;
6889 +       int err;
6890 +
6891 +       /* this error should be handled in application */
6892 +       if (!name || !strlen (name))
6893 +               return (-1);
6894 +
6895 +       err = ipkg_conf_init (&ipkg_conf, args);
6896 +       if (err)
6897 +       {
6898 +               return err;
6899 +       }
6900 +
6901 +       /* we need to do this because of static declarations,
6902 +        * maybe a good idea to change */
6903 +       cmd = ipkg_cmd_find ("download");
6904 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6905 +
6906 +       ipkg_conf_deinit(&ipkg_conf);
6907 +       return (err);
6908 +}
6909 +
6910 +
6911 +int
6912 +ipkg_package_files(args_t *args, 
6913 +                   const char *name, 
6914 +                   ipkg_list_callback cblist,
6915 +                   void *userdata)
6916 +{
6917 +       ipkg_cmd_t *cmd;
6918 +       ipkg_conf_t ipkg_conf;
6919 +       int err;
6920 +
6921 +       /* this error should be handled in application */
6922 +       if (!name || !strlen (name))
6923 +               return (-1);
6924 +
6925 +       err = ipkg_conf_init (&ipkg_conf, args);
6926 +       if (err)
6927 +       {
6928 +               return err;
6929 +       }
6930 +
6931 +       ipkg_cb_list = cblist;
6932 +       
6933 +       /* we need to do this because of static declarations, 
6934 +        * maybe a good idea to change */
6935 +       cmd = ipkg_cmd_find ("files");
6936 +
6937 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, userdata);
6938 +       
6939 +       ipkg_cb_list = NULL;
6940 +       ipkg_conf_deinit(&ipkg_conf);
6941 +       return (err);
6942 +}
6943 +
6944 +
6945 +int 
6946 +ipkg_file_search(args_t *args, 
6947 +                const char *file,
6948 +                               ipkg_list_callback cblist,
6949 +                void *userdata)
6950 +{
6951 +       ipkg_cmd_t *cmd;
6952 +       ipkg_conf_t ipkg_conf;
6953 +       int err;
6954 +       
6955 +       /* this error should be handled in application */
6956 +       if (!file || !strlen (file))
6957 +               return (-1);
6958 +
6959 +       err = ipkg_conf_init (&ipkg_conf, args);
6960 +       if (err)
6961 +       {
6962 +               return err;
6963 +       }
6964 +
6965 +       ipkg_cb_list = cblist;
6966 +
6967 +       /* we need to do this because of static declarations, 
6968 +        * maybe a good idea to change */
6969 +       cmd = ipkg_cmd_find ("search");
6970 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, userdata);
6971 +       
6972 +       ipkg_cb_list = NULL;
6973 +       ipkg_conf_deinit(&ipkg_conf);
6974 +       return(err);
6975 +}
6976 +
6977 +
6978 +int 
6979 +ipkg_file_what(args_t *args, const char *file, const char* command)
6980 +{
6981 +       ipkg_cmd_t *cmd;
6982 +       ipkg_conf_t ipkg_conf;
6983 +       int err;
6984 +       
6985 +       /* this error should be handled in application */
6986 +       if (!file || !strlen (file))
6987 +               return (-1);
6988 +
6989 +       err = ipkg_conf_init (&ipkg_conf, args);
6990 +       if (err)
6991 +       {
6992 +               return err;
6993 +       }
6994 +
6995 +       /* we need to do this because of static declarations, 
6996 +        * maybe a good idea to change */
6997 +       cmd = ipkg_cmd_find (command);
6998 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, NULL);
6999 +       
7000 +       ipkg_conf_deinit(&ipkg_conf);
7001 +       return(err);
7002 +}
7003 +
7004 +#define ipkg_package_whatdepends(args,file) ipkg_file_what(args,file,"whatdepends")
7005 +#define ipkg_package_whatrecommends(args, file) ipkg_file_what(args,file,"whatrecommends")
7006 +#define ipkg_package_whatprovides(args, file) ipkg_file_what(args,file,"whatprovides")
7007 +#define ipkg_package_whatconflicts(args, file) ipkg_file_what(args,file,"whatconflicts")
7008 +#define ipkg_package_whatreplaces(args, file) ipkg_file_what(args,file,"whatreplaces")
7009 +
7010 +
7011 +int default_ipkg_message_callback(ipkg_conf_t *conf, message_level_t level, 
7012 +                                 char *msg)
7013 +{
7014 +     if (conf && (conf->verbosity < level)) {
7015 +         return 0;
7016 +     } else {
7017 +#ifdef IPKG_LIB
7018 +          if ( level == IPKG_ERROR ){
7019 +             push_error_list(&error_list, msg); 
7020 +//          printf(msg);
7021 +          } else
7022 +#endif
7023 +            printf(msg);
7024 +     }
7025 +     return 0;
7026 +}
7027 +
7028 +int default_ipkg_list_callback(char *name, char *desc, char *version, 
7029 +                              pkg_state_status_t status, void *userdata)
7030 +{
7031 +     if (desc)
7032 +         printf("%s - %s - %s\n", name, version, desc);
7033 +     else
7034 +         printf("%s - %s\n", name, version);
7035 +     return 0;
7036 +}
7037 +
7038 +int default_ipkg_files_callback(char *name, char *desc, char *version,
7039 +                   pkg_state_status_t status, void *userdata)
7040 +{
7041 +     if (desc)
7042 +         printf("%s\n", desc);
7043 +     return 0;
7044 +}
7045 +
7046 +int default_ipkg_status_callback(char *name, int istatus, char *desc,
7047 +                                void *userdata)
7048 +{
7049 +     printf("%s\n", desc);
7050 +     return 0;
7051 +}
7052 +
7053 +char* default_ipkg_response_callback(char *question)
7054 +{
7055 +     char *response = NULL;
7056 +     printf(question);
7057 +     fflush(stdout);
7058 +     do {
7059 +         response = (char *)file_read_line_alloc(stdin);
7060 +     } while (response == NULL);
7061 +     return response;
7062 +}
7063 +
7064 +/* This is used for backward compatibility */
7065 +int
7066 +ipkg_op (int argc, char *argv[])
7067 +{
7068 +       int err, optind;
7069 +       args_t args;
7070 +       char *cmd_name;
7071 +       ipkg_cmd_t *cmd;
7072 +       ipkg_conf_t ipkg_conf;
7073 +
7074 +       args_init (&args);
7075 +
7076 +       optind = args_parse (&args, argc, argv);
7077 +       if (optind == argc || optind < 0)
7078 +       {
7079 +               args_usage ("ipkg must have one sub-command argument");
7080 +       }
7081 +
7082 +       cmd_name = argv[optind++];
7083 +/* Pigi: added a flag to disable the checking of structures if the command does not need to 
7084 +         read anything from there.
7085 +*/
7086 +        if ( !strcmp(cmd_name,"print-architecture") ||
7087 +             !strcmp(cmd_name,"print_architecture") ||
7088 +             !strcmp(cmd_name,"print-installation-architecture") ||
7089 +             !strcmp(cmd_name,"print_installation_architecture") )
7090 +           args.nocheckfordirorfile = 1;
7091 +
7092 +/* Pigi: added a flag to disable the reading of feed files  if the command does not need to 
7093 +         read anything from there.
7094 +*/
7095 +        if ( !strcmp(cmd_name,"flag") ||
7096 +             !strcmp(cmd_name,"configure") ||
7097 +             !strcmp(cmd_name,"remove") ||
7098 +             !strcmp(cmd_name,"files") ||
7099 +             !strcmp(cmd_name,"search") ||
7100 +             !strcmp(cmd_name,"compare_versions") ||
7101 +             !strcmp(cmd_name,"compare-versions") ||
7102 +             !strcmp(cmd_name,"list_installed") ||
7103 +             !strcmp(cmd_name,"list-installed") ||
7104 +             !strcmp(cmd_name,"status") )
7105 +           args.noreadfeedsfile = 1;
7106 +
7107 +
7108 +       err = ipkg_conf_init (&ipkg_conf, &args);
7109 +       if (err)
7110 +       {
7111 +               return err;
7112 +       }
7113 +
7114 +       args_deinit (&args);
7115 +
7116 +       ipkg_cb_message = default_ipkg_message_callback;
7117 +       ipkg_cb_response = default_ipkg_response_callback;
7118 +       ipkg_cb_status = default_ipkg_status_callback;
7119 +       if ( strcmp(cmd_name, "files")==0)
7120 +            ipkg_cb_list = default_ipkg_files_callback;
7121 +       else
7122 +            ipkg_cb_list = default_ipkg_list_callback;
7123 +
7124 +       cmd = ipkg_cmd_find (cmd_name);
7125 +       if (cmd == NULL)
7126 +       {
7127 +               fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
7128 +                        cmd_name);
7129 +               args_usage (NULL);
7130 +       }
7131 +
7132 +       if (cmd->requires_args && optind == argc)
7133 +       {
7134 +               fprintf (stderr,
7135 +                        "%s: the ``%s'' command requires at least one argument\n",
7136 +                        __FUNCTION__, cmd_name);
7137 +               args_usage (NULL);
7138 +       }
7139 +
7140 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, argc - optind, (const char **) (argv + optind), NULL);
7141 +
7142 +       ipkg_conf_deinit (&ipkg_conf);
7143 +
7144 +       return err;
7145 +}
7146 +
7147 +#endif /* IPKG_LIB */
7148 diff -ruN busybox-1.2.0-orig/archival/libipkg/libipkg.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.h
7149 --- busybox-1.2.0-orig/archival/libipkg/libipkg.h       1970-01-01 01:00:00.000000000 +0100
7150 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.h      2006-07-22 16:31:25.000000000 +0200
7151 @@ -0,0 +1,87 @@
7152 +/* ipkglib.h - the itsy package management system
7153 +
7154 +   Florian Boor <florian.boor@kernelconcepts.de>
7155 +
7156 +   This program is free software; you can redistribute it and/or
7157 +   modify it under the terms of the GNU General Public License as
7158 +   published by the Free Software Foundation; either version 2, or (at
7159 +   your option) any later version.
7160 +
7161 +   This program is distributed in the hope that it will be useful, but
7162 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7163 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7164 +   General Public License for more details.
7165 +*/
7166 +
7167 +#ifndef IPKGLIB_H
7168 +#define IPKGLIB_H
7169 +
7170 +#ifdef IPKG_LIB
7171 +
7172 +#include "ipkg_conf.h"
7173 +#include "ipkg_message.h"
7174 +
7175 +#include "args.h"
7176 +#include "pkg.h"
7177 +
7178 +typedef int (*ipkg_message_callback)(ipkg_conf_t *conf, message_level_t level, 
7179 +       char *msg);
7180 +typedef int (*ipkg_list_callback)(char *name, char *desc, char *version, 
7181 +       pkg_state_status_t status, void *userdata);
7182 +typedef int (*ipkg_status_callback)(char *name, int istatus, char *desc,
7183 +       void *userdata);
7184 +typedef char* (*ipkg_response_callback)(char *question);
7185 +
7186 +extern int ipkg_op(int argc, char *argv[]); /* ipkglib.c */
7187 +extern int ipkg_init (ipkg_message_callback mcall, 
7188 +                      ipkg_response_callback rcall,
7189 +                                         args_t * args);
7190 +
7191 +extern int ipkg_deinit (args_t *args);
7192 +extern int ipkg_packages_list(args_t *args, 
7193 +                              const char *packages, 
7194 +                              ipkg_list_callback cblist,
7195 +                              void *userdata);
7196 +extern int ipkg_packages_status(args_t *args, 
7197 +                                const char *packages, 
7198 +                                ipkg_status_callback cbstatus,
7199 +                                                               void *userdata);
7200 +extern int ipkg_packages_info(args_t *args,
7201 +                              const char *packages,
7202 +                              ipkg_status_callback cbstatus,
7203 +                              void *userdata);
7204 +extern int ipkg_packages_install(args_t *args, const char *name);
7205 +extern int ipkg_packages_remove(args_t *args, const char *name, int purge);
7206 +extern int ipkg_lists_update(args_t *args);
7207 +extern int ipkg_packages_upgrade(args_t *args);
7208 +extern int ipkg_packages_download(args_t *args, const char *name);
7209 +extern int ipkg_package_files(args_t *args,
7210 +                              const char *name,
7211 +                                                         ipkg_list_callback cblist,
7212 +                                                         void *userdata);
7213 +extern int ipkg_file_search(args_t *args,
7214 +                            const char *file,
7215 +                                                       ipkg_list_callback cblist,
7216 +                                                       void *userdata);
7217 +extern int ipkg_package_whatdepends(args_t *args, const char *file);
7218 +extern int ipkg_package_whatrecommends(args_t *args, const char *file);
7219 +extern int ipkg_package_whatprovides(args_t *args, const char *file);
7220 +extern int ipkg_package_whatconflicts(args_t *args, const char *file);
7221 +extern int ipkg_package_whatreplaces(args_t *args, const char *file);
7222 +
7223 +extern ipkg_message_callback ipkg_cb_message; /* ipkglib.c */
7224 +extern ipkg_response_callback ipkg_cb_response;
7225 +extern ipkg_status_callback ipkg_cb_status;
7226 +extern ipkg_list_callback ipkg_cb_list;
7227 +extern void push_error_list(struct errlist **errors,char * msg);
7228 +extern void reverse_error_list(struct errlist **errors);
7229 +extern void free_error_list(struct errlist **errors);
7230 +
7231 +#else
7232 +
7233 +extern int ipkg_op(int argc, char *argv[]);
7234 +
7235 +#endif
7236 +
7237 +
7238 +#endif
7239 diff -ruN busybox-1.2.0-orig/archival/libipkg/Makefile busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile
7240 --- busybox-1.2.0-orig/archival/libipkg/Makefile        1970-01-01 01:00:00.000000000 +0100
7241 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile       2006-07-22 16:31:25.000000000 +0200
7242 @@ -0,0 +1,32 @@
7243 +# Makefile for busybox
7244 +#
7245 +# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7246 +#
7247 +# This program is free software; you can redistribute it and/or modify
7248 +# it under the terms of the GNU General Public License as published by
7249 +# the Free Software Foundation; either version 2 of the License, or
7250 +# (at your option) any later version.
7251 +#
7252 +# This program is distributed in the hope that it will be useful,
7253 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7254 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7255 +# General Public License for more details.
7256 +#
7257 +# You should have received a copy of the GNU General Public License
7258 +# along with this program; if not, write to the Free Software
7259 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7260 +#
7261 +
7262 +top_srcdir=../..
7263 +top_builddir=../..
7264 +srcdir=$(top_srcdir)/archival/libipkg
7265 +LIBIPKG_DIR:=./
7266 +include $(top_builddir)/Rules.mak
7267 +include $(top_builddir)/.config
7268 +include $(srcdir)/Makefile.in
7269 +all: $(libraries-y)
7270 +-include $(top_builddir)/.depend
7271 +
7272 +clean:
7273 +       rm -f *.o *.a $(AR_TARGET)
7274 +
7275 diff -ruN busybox-1.2.0-orig/archival/libipkg/Makefile.in busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile.in
7276 --- busybox-1.2.0-orig/archival/libipkg/Makefile.in     1970-01-01 01:00:00.000000000 +0100
7277 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile.in    2006-10-08 12:29:39.000000000 +0200
7278 @@ -0,0 +1,88 @@
7279 +# Makefile for busybox
7280 +#
7281 +# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7282 +#
7283 +# This program is free software; you can redistribute it and/or modify
7284 +# it under the terms of the GNU General Public License as published by
7285 +# the Free Software Foundation; either version 2 of the License, or
7286 +# (at your option) any later version.
7287 +#
7288 +# This program is distributed in the hope that it will be useful,
7289 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7290 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7291 +# General Public License for more details.
7292 +#
7293 +# You should have received a copy of the GNU General Public License
7294 +# along with this program; if not, write to the Free Software
7295 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7296 +#
7297 +
7298 +LIBIPKG_AR:=libipkg.a
7299 +ifndef $(LIBIPKG_DIR)
7300 +LIBIPKG_DIR:=$(top_builddir)/archival/libipkg/
7301 +endif
7302 +srcdir=$(top_srcdir)/archival/libipkg
7303 +
7304 +LIBIPKG_CORE_SOURCES:= \
7305 +       args.c \
7306 +       libipkg.c \
7307 +       user.c \
7308 +
7309 +LIBIPKG_CMD_SOURCES:= \
7310 +       ipkg_cmd.c \
7311 +       ipkg_configure.c \
7312 +       ipkg_download.c \
7313 +       ipkg_install.c \
7314 +       ipkg_remove.c \
7315 +       ipkg_upgrade.c \
7316 +
7317 +LIBIPKG_DB_SOURCES:= \
7318 +       hash_table.c \
7319 +       ipkg_conf.c \
7320 +       ipkg_utils.c \
7321 +       pkg.c \
7322 +       pkg_depends.c \
7323 +       pkg_extract.c \
7324 +       pkg_hash.c \
7325 +       pkg_parse.c \
7326 +       pkg_vec.c \
7327 +
7328 +LIBIPKG_LIST_SOURCES:= \
7329 +       conffile.c \
7330 +       conffile_list.c \
7331 +       nv_pair.c \
7332 +       nv_pair_list.c \
7333 +       pkg_dest.c \
7334 +       pkg_dest_list.c \
7335 +       pkg_src.c \
7336 +       pkg_src_list.c \
7337 +       str_list.c \
7338 +       void_list.c \
7339 +
7340 +LIBIPKG_UTIL_SOURCES:= \
7341 +       file_util.c \
7342 +       ipkg_message.c \
7343 +       md5.c \
7344 +       str_util.c \
7345 +       xsystem.c \
7346 +
7347 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_CORE_SOURCES)
7348 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_CMD_SOURCES)
7349 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_DB_SOURCES)
7350 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_LIST_SOURCES)
7351 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_UTIL_SOURCES)
7352 +LIBIPKG_OBJS=$(patsubst %.c,$(LIBIPKG_DIR)%.o, $(LIBIPKG-y))
7353 +
7354 +ifeq ($(strip $(IPKG_ARCH)),)
7355 +IPKG_ARCH:=$(TARGET_ARCH)
7356 +endif
7357 +CFLAGS += -DIPKG_LIB -DIPKGLIBDIR="\"/usr/lib\"" -DHOST_CPU_STR="\"$(IPKG_ARCH)\""
7358 +
7359 +libraries-$(CONFIG_IPKG) += $(LIBIPKG_DIR)$(LIBIPKG_AR)
7360 +
7361 +$(LIBIPKG_DIR)$(LIBIPKG_AR): $(LIBIPKG_OBJS)
7362 +       $(do_ar)
7363 +
7364 +$(LIBIPKG_OBJS): $(LIBIPKG_DIR)%.o : $(srcdir)/%.c
7365 +       $(compile.c)
7366 +
7367 diff -ruN busybox-1.2.0-orig/archival/libipkg/md5.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.c
7368 --- busybox-1.2.0-orig/archival/libipkg/md5.c   1970-01-01 01:00:00.000000000 +0100
7369 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.c  2006-07-22 16:31:25.000000000 +0200
7370 @@ -0,0 +1,48 @@
7371 +/* md5.c - wrappers to busybox md5 functions
7372 + *
7373 + * Copyright (C) 1995-1999 Free Software Foundation, Inc.
7374 + *
7375 + * This program is free software; you can redistribute it and/or modify
7376 + * it under the terms of the GNU General Public License as published by
7377 + * the Free Software Foundation; either version 2, or (at your option)
7378 + * any later version.
7379 + *
7380 + * This program is distributed in the hope that it will be useful,
7381 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7382 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7383 + * GNU General Public License for more details.
7384 + *
7385 + * You should have received a copy of the GNU General Public License
7386 + * along with this program; if not, write to the Free Software Foundation,
7387 + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7388 + */
7389 +
7390 +#include <stdio.h>
7391 +#include "libbb.h"
7392 +
7393 +#include "md5.h"
7394 +
7395 +int md5_stream(FILE *stream, void *resblock)
7396 +{
7397 +       int fd;
7398 +       int sum;
7399 +       
7400 +       if( (fd = fileno(stream)) == -1 ) {
7401 +               bb_error_msg("bad file descriptor");
7402 +               return 1;
7403 +       }
7404 +
7405 +       hash_fd(fd, HASH_MD5, (uint8_t *)resblock);
7406 +
7407 +       return 0;
7408 +}
7409 +
7410 +void *md5_buffer(const char *buffer, size_t len, void *resblock)
7411 +{
7412 +       md5_ctx_t md5_cx;
7413 +
7414 +       md5_begin(&md5_cx);
7415 +       md5_hash(buffer, len, &md5_cx);
7416 +       return md5_end(resblock, &md5_cx);
7417 +}
7418 +
7419 diff -ruN busybox-1.2.0-orig/archival/libipkg/md5.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.h
7420 --- busybox-1.2.0-orig/archival/libipkg/md5.h   1970-01-01 01:00:00.000000000 +0100
7421 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.h  2006-07-22 16:31:25.000000000 +0200
7422 @@ -0,0 +1,35 @@
7423 +/* md5.h - Compute MD5 checksum of files or strings according to the
7424 + *         definition of MD5 in RFC 1321 from April 1992.
7425 + * Copyright (C) 1995-1999 Free Software Foundation, Inc.
7426 + *
7427 + * This program is free software; you can redistribute it and/or modify
7428 + * it under the terms of the GNU General Public License as published by
7429 + * the Free Software Foundation; either version 2, or (at your option)
7430 + * any later version.
7431 + *
7432 + * This program is distributed in the hope that it will be useful,
7433 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7434 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7435 + * GNU General Public License for more details.
7436 + *
7437 + * You should have received a copy of the GNU General Public License
7438 + * along with this program; if not, write to the Free Software Foundation,
7439 + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7440 + */
7441 +
7442 +#ifndef MD5_H
7443 +#define MD5_H
7444 +
7445 +/* Compute MD5 message digest for bytes read from STREAM.  The
7446 +   resulting message digest number will be written into the 16 bytes
7447 +   beginning at RESBLOCK.  */
7448 +int md5_stream(FILE *stream, void *resblock);
7449 +
7450 +/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
7451 +   result is always in little endian byte order, so that a byte-wise
7452 +   output yields to the wanted ASCII representation of the message
7453 +   digest.  */
7454 +void *md5_buffer(const char *buffer, size_t len, void *resblock);
7455 +
7456 +#endif
7457 +
7458 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.c
7459 --- busybox-1.2.0-orig/archival/libipkg/nv_pair.c       1970-01-01 01:00:00.000000000 +0100
7460 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.c      2006-07-22 16:31:25.000000000 +0200
7461 @@ -0,0 +1,40 @@
7462 +/* nv_pair.c - the itsy package management system
7463 +
7464 +   Carl D. Worth
7465 +
7466 +   Copyright (C) 2001 University of Southern California
7467 +
7468 +   This program is free software; you can redistribute it and/or
7469 +   modify it under the terms of the GNU General Public License as
7470 +   published by the Free Software Foundation; either version 2, or (at
7471 +   your option) any later version.
7472 +
7473 +   This program is distributed in the hope that it will be useful, but
7474 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7475 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7476 +   General Public License for more details.
7477 +*/
7478 +
7479 +#include "ipkg.h"
7480 +
7481 +#include "nv_pair.h"
7482 +#include "str_util.h"
7483 +
7484 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value)
7485 +{
7486 +    nv_pair->name = str_dup_safe(name);
7487 +    nv_pair->value = str_dup_safe(value);
7488 +
7489 +    return 0;
7490 +}
7491 +
7492 +void nv_pair_deinit(nv_pair_t *nv_pair)
7493 +{
7494 +    free(nv_pair->name);
7495 +    nv_pair->name = NULL;
7496 +
7497 +    free(nv_pair->value);
7498 +    nv_pair->value = NULL;
7499 +}
7500 +
7501 +
7502 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.h
7503 --- busybox-1.2.0-orig/archival/libipkg/nv_pair.h       1970-01-01 01:00:00.000000000 +0100
7504 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.h      2006-07-22 16:31:25.000000000 +0200
7505 @@ -0,0 +1,32 @@
7506 +/* nv_pair.h - the itsy package management system
7507 +
7508 +   Carl D. Worth
7509 +
7510 +   Copyright (C) 2001 University of Southern California
7511 +
7512 +   This program is free software; you can redistribute it and/or
7513 +   modify it under the terms of the GNU General Public License as
7514 +   published by the Free Software Foundation; either version 2, or (at
7515 +   your option) any later version.
7516 +
7517 +   This program is distributed in the hope that it will be useful, but
7518 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7519 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7520 +   General Public License for more details.
7521 +*/
7522 +
7523 +#ifndef NV_PAIR_H
7524 +#define NV_PAIR_H
7525 +
7526 +typedef struct nv_pair nv_pair_t;
7527 +struct nv_pair
7528 +{
7529 +    char *name;
7530 +    char *value;
7531 +};
7532 +
7533 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value);
7534 +void nv_pair_deinit(nv_pair_t *nv_pair);
7535 +
7536 +#endif
7537 +
7538 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.c
7539 --- busybox-1.2.0-orig/archival/libipkg/nv_pair_list.c  1970-01-01 01:00:00.000000000 +0100
7540 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.c 2006-07-22 16:31:25.000000000 +0200
7541 @@ -0,0 +1,98 @@
7542 +/* nv_pair_list.c - the itsy package management system
7543 +
7544 +   Carl D. Worth
7545 +
7546 +   Copyright (C) 2001 University of Southern California
7547 +
7548 +   This program is free software; you can redistribute it and/or
7549 +   modify it under the terms of the GNU General Public License as
7550 +   published by the Free Software Foundation; either version 2, or (at
7551 +   your option) any later version.
7552 +
7553 +   This program is distributed in the hope that it will be useful, but
7554 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7555 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7556 +   General Public License for more details.
7557 +*/
7558 +
7559 +#include "ipkg.h"
7560 +
7561 +#include "nv_pair.h"
7562 +#include "void_list.h"
7563 +#include "nv_pair_list.h"
7564 +
7565 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data)
7566 +{
7567 +    return void_list_elt_init((void_list_elt_t *) elt, data);
7568 +}
7569 +
7570 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt)
7571 +{
7572 +    void_list_elt_deinit((void_list_elt_t *) elt);
7573 +}
7574 +
7575 +int nv_pair_list_init(nv_pair_list_t *list)
7576 +{
7577 +    return void_list_init((void_list_t *) list);
7578 +}
7579 +
7580 +void nv_pair_list_deinit(nv_pair_list_t *list)
7581 +{
7582 +    nv_pair_list_elt_t *iter;
7583 +    nv_pair_t *nv_pair;
7584 +
7585 +    for (iter = list->head; iter; iter = iter->next) {
7586 +       nv_pair = iter->data;
7587 +       nv_pair_deinit(nv_pair);
7588 +
7589 +       /* malloced in nv_pair_list_append */
7590 +       free(nv_pair);
7591 +       iter->data = NULL;
7592 +    }
7593 +    void_list_deinit((void_list_t *) list);
7594 +}
7595 +
7596 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value)
7597 +{
7598 +    int err;
7599 +
7600 +    /* freed in nv_pair_list_deinit */
7601 +    nv_pair_t *nv_pair = malloc(sizeof(nv_pair_t));
7602 +
7603 +    if (nv_pair == NULL) {
7604 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7605 +       return NULL;
7606 +    }
7607 +    nv_pair_init(nv_pair, name, value);
7608 +
7609 +    err = void_list_append((void_list_t *) list, nv_pair);
7610 +    if (err) {
7611 +       return NULL;
7612 +    }
7613 +
7614 +    return nv_pair;
7615 +}
7616 +
7617 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data)
7618 +{
7619 +    return void_list_push((void_list_t *) list, data);
7620 +}
7621 +
7622 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list)
7623 +{
7624 +    return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list);
7625 +}
7626 +
7627 +char *nv_pair_list_find(nv_pair_list_t *list, char *name)
7628 +{
7629 +     nv_pair_list_elt_t *iter;
7630 +     nv_pair_t *nv_pair;
7631 +
7632 +     for (iter = list->head; iter; iter = iter->next) {
7633 +         nv_pair = iter->data;
7634 +         if (strcmp(nv_pair->name, name) == 0) {
7635 +              return nv_pair->value;
7636 +         }
7637 +     }    
7638 +     return NULL;
7639 +}
7640 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.h
7641 --- busybox-1.2.0-orig/archival/libipkg/nv_pair_list.h  1970-01-01 01:00:00.000000000 +0100
7642 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.h 2006-07-22 16:31:25.000000000 +0200
7643 @@ -0,0 +1,60 @@
7644 +/* nv_pair_list.h - the itsy package management system
7645 +
7646 +   Carl D. Worth
7647 +
7648 +   Copyright (C) 2001 University of Southern California
7649 +
7650 +   This program is free software; you can redistribute it and/or
7651 +   modify it under the terms of the GNU General Public License as
7652 +   published by the Free Software Foundation; either version 2, or (at
7653 +   your option) any later version.
7654 +
7655 +   This program is distributed in the hope that it will be useful, but
7656 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7657 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7658 +   General Public License for more details.
7659 +*/
7660 +
7661 +#ifndef NV_PAIR_LIST_H
7662 +#define NV_PAIR_LIST_H
7663 +
7664 +#include "nv_pair.h"
7665 +#include "void_list.h"
7666 +
7667 +typedef struct nv_pair_list_elt nv_pair_list_elt_t;
7668 +struct nv_pair_list_elt
7669 +{
7670 +    nv_pair_list_elt_t *next;
7671 +    nv_pair_t *data;
7672 +};
7673 +
7674 +typedef struct nv_pair_list nv_pair_list_t;
7675 +struct nv_pair_list
7676 +{
7677 +    nv_pair_list_elt_t pre_head;
7678 +    nv_pair_list_elt_t *head;
7679 +    nv_pair_list_elt_t *tail;
7680 +};
7681 +
7682 +static inline int nv_pair_list_empty(nv_pair_list_t *list)
7683 +{
7684 +     if (list->head == NULL)
7685 +         return 1;
7686 +     else
7687 +         return 0;
7688 +}
7689 +
7690 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data);
7691 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt);
7692 +
7693 +int nv_pair_list_init(nv_pair_list_t *list);
7694 +void nv_pair_list_deinit(nv_pair_list_t *list);
7695 +
7696 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list,
7697 +                              const char *name, const char *value);
7698 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data);
7699 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list);
7700 +char *nv_pair_list_find(nv_pair_list_t *list, char *name);
7701 +
7702 +#endif
7703 +
7704 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.c
7705 --- busybox-1.2.0-orig/archival/libipkg/pkg.c   1970-01-01 01:00:00.000000000 +0100
7706 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.c  2006-07-22 16:31:25.000000000 +0200
7707 @@ -0,0 +1,1754 @@
7708 +/* pkg.c - the itsy package management system
7709 +
7710 +   Carl D. Worth
7711 +
7712 +   Copyright (C) 2001 University of Southern California
7713 +
7714 +   This program is free software; you can redistribute it and/or
7715 +   modify it under the terms of the GNU General Public License as
7716 +   published by the Free Software Foundation; either version 2, or (at
7717 +   your option) any later version.
7718 +
7719 +   This program is distributed in the hope that it will be useful, but
7720 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7721 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7722 +   General Public License for more details.
7723 +*/
7724 +
7725 +#include "ipkg.h"
7726 +#include <ctype.h>
7727 +#include <string.h>
7728 +#include <errno.h>
7729 +
7730 +#include "pkg.h"
7731 +
7732 +#include "pkg_parse.h"
7733 +#include "pkg_extract.h"
7734 +#include "ipkg_message.h"
7735 +#include "ipkg_utils.h"
7736 +
7737 +#include "sprintf_alloc.h"
7738 +#include "file_util.h"
7739 +#include "str_util.h"
7740 +#include "xsystem.h"
7741 +#include "ipkg_conf.h"
7742 +
7743 +typedef struct enum_map enum_map_t;
7744 +struct enum_map
7745 +{
7746 +     int value;
7747 +     char *str;
7748 +};
7749 +
7750 +static const enum_map_t pkg_state_want_map[] = {
7751 +     { SW_UNKNOWN, "unknown"},
7752 +     { SW_INSTALL, "install"},
7753 +     { SW_DEINSTALL, "deinstall"},
7754 +     { SW_PURGE, "purge"}
7755 +};
7756 +
7757 +static const enum_map_t pkg_state_flag_map[] = {
7758 +     { SF_OK, "ok"},
7759 +     { SF_REINSTREQ, "reinstreq"},
7760 +     { SF_HOLD, "hold"},
7761 +     { SF_REPLACE, "replace"},
7762 +     { SF_NOPRUNE, "noprune"},
7763 +     { SF_PREFER, "prefer"},
7764 +     { SF_OBSOLETE, "obsolete"},
7765 +     { SF_USER, "user"},
7766 +};
7767 +
7768 +static const enum_map_t pkg_state_status_map[] = {
7769 +     { SS_NOT_INSTALLED, "not-installed" },
7770 +     { SS_UNPACKED, "unpacked" },
7771 +     { SS_HALF_CONFIGURED, "half-configured" },
7772 +     { SS_INSTALLED, "installed" },
7773 +     { SS_HALF_INSTALLED, "half-installed" },
7774 +     { SS_CONFIG_FILES, "config-files" },
7775 +     { SS_POST_INST_FAILED, "post-inst-failed" },
7776 +     { SS_REMOVAL_FAILED, "removal-failed" }
7777 +};
7778 +
7779 +static int verrevcmp(const char *val, const char *ref);
7780 +
7781 +
7782 +pkg_t *pkg_new(void)
7783 +{
7784 +     pkg_t *pkg;
7785 +
7786 +     pkg = malloc(sizeof(pkg_t));
7787 +     if (pkg == NULL) {
7788 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7789 +         return NULL;
7790 +     }
7791 +
7792 +     pkg_init(pkg);
7793 +
7794 +     return pkg;
7795 +}
7796 +
7797 +int pkg_init(pkg_t *pkg)
7798 +{
7799 +     memset(pkg, 0, sizeof(pkg_t));
7800 +     pkg->name = NULL;
7801 +     pkg->epoch = 0;
7802 +     pkg->version = NULL;
7803 +     pkg->revision = NULL;
7804 +     pkg->familiar_revision = NULL;
7805 +     pkg->dest = NULL;
7806 +     pkg->src = NULL;
7807 +     pkg->architecture = NULL;
7808 +     pkg->maintainer = NULL;
7809 +     pkg->section = NULL;
7810 +     pkg->description = NULL;
7811 +     pkg->state_want = SW_UNKNOWN;
7812 +     pkg->state_flag = SF_OK;
7813 +     pkg->state_status = SS_NOT_INSTALLED;
7814 +     pkg->depends_str = NULL;
7815 +     pkg->provides_str = NULL;
7816 +     pkg->depends_count = 0;
7817 +     pkg->depends = NULL;
7818 +     pkg->suggests_str = NULL;
7819 +     pkg->recommends_str = NULL;
7820 +     pkg->suggests_count = 0;
7821 +     pkg->recommends_count = 0;
7822 +
7823 +     /* Abhaya: added init for conflicts fields */
7824 +     pkg->conflicts = NULL;
7825 +     pkg->conflicts_count = 0;
7826 +
7827 +     /* added for replaces.  Jamey 7/23/2002 */
7828 +     pkg->replaces = NULL;
7829 +     pkg->replaces_count = 0;
7830 +    
7831 +     pkg->pre_depends_count = 0;
7832 +     pkg->pre_depends_str = NULL;
7833 +     pkg->provides_count = 0;
7834 +     pkg->provides = NULL;
7835 +     pkg->filename = NULL;
7836 +     pkg->local_filename = NULL;
7837 +     pkg->tmp_unpack_dir = NULL;
7838 +     pkg->md5sum = NULL;
7839 +     pkg->size = NULL;
7840 +     pkg->installed_size = NULL;
7841 +     pkg->priority = NULL;
7842 +     pkg->source = NULL;
7843 +     conffile_list_init(&pkg->conffiles);
7844 +     pkg->installed_files = NULL;
7845 +     pkg->installed_files_ref_cnt = 0;
7846 +     pkg->essential = 0;
7847 +     pkg->provided_by_hand = 0;
7848 +
7849 +     return 0;
7850 +}
7851 +
7852 +void pkg_deinit(pkg_t *pkg)
7853 +{
7854 +     free(pkg->name);
7855 +     pkg->name = NULL;
7856 +     pkg->epoch = 0;
7857 +     free(pkg->version);
7858 +     pkg->version = NULL;
7859 +     /* revision and familiar_revision share storage with version, so
7860 +       don't free */
7861 +     pkg->revision = NULL;
7862 +     pkg->familiar_revision = NULL;
7863 +     /* owned by ipkg_conf_t */
7864 +     pkg->dest = NULL;
7865 +     /* owned by ipkg_conf_t */
7866 +     pkg->src = NULL;
7867 +     free(pkg->architecture);
7868 +     pkg->architecture = NULL;
7869 +     free(pkg->maintainer);
7870 +     pkg->maintainer = NULL;
7871 +     free(pkg->section);
7872 +     pkg->section = NULL;
7873 +     free(pkg->description);
7874 +     pkg->description = NULL;
7875 +     pkg->state_want = SW_UNKNOWN;
7876 +     pkg->state_flag = SF_OK;
7877 +     pkg->state_status = SS_NOT_INSTALLED;
7878 +     free(pkg->depends_str);
7879 +     pkg->depends_str = NULL;
7880 +     free(pkg->provides_str);
7881 +     pkg->provides_str = NULL;
7882 +     pkg->depends_count = 0;
7883 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->depends ? */
7884 +     pkg->pre_depends_count = 0;
7885 +     free(pkg->pre_depends_str);
7886 +     pkg->pre_depends_str = NULL;
7887 +     pkg->provides_count = 0;
7888 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->provides ? */
7889 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->suggests ? */
7890 +     free(pkg->filename);
7891 +     pkg->filename = NULL;
7892 +     free(pkg->local_filename);
7893 +     pkg->local_filename = NULL;
7894 +     /* CLEANUP: It'd be nice to pullin the cleanup function from
7895 +       ipkg_install.c here. See comment in
7896 +       ipkg_install.c:cleanup_temporary_files */
7897 +     free(pkg->tmp_unpack_dir);
7898 +     pkg->tmp_unpack_dir = NULL;
7899 +     free(pkg->md5sum);
7900 +     pkg->md5sum = NULL;
7901 +     free(pkg->size);
7902 +     pkg->size = NULL;
7903 +     free(pkg->installed_size);
7904 +     pkg->installed_size = NULL;
7905 +     free(pkg->priority);
7906 +     pkg->priority = NULL;
7907 +     free(pkg->source);
7908 +     pkg->source = NULL;
7909 +     conffile_list_deinit(&pkg->conffiles);
7910 +     /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
7911 +       since if they are calling deinit, they should know. Maybe do an
7912 +       assertion here instead? */
7913 +     pkg->installed_files_ref_cnt = 1;
7914 +     pkg_free_installed_files(pkg);
7915 +     pkg->essential = 0;
7916 +}
7917 +
7918 +int pkg_init_from_file(pkg_t *pkg, const char *filename)
7919 +{
7920 +     int err;
7921 +     char **raw;
7922 +     FILE *control_file;
7923 +
7924 +     err = pkg_init(pkg);
7925 +     if (err) { return err; }
7926 +
7927 +     pkg->local_filename = strdup(filename);
7928 +    
7929 +     control_file = tmpfile();
7930 +     err = pkg_extract_control_file_to_stream(pkg, control_file);
7931 +     if (err) { return err; }
7932 +
7933 +     rewind(control_file);
7934 +     raw = read_raw_pkgs_from_stream(control_file);
7935 +     pkg_parse_raw(pkg, &raw, NULL, NULL);
7936 +
7937 +     fclose(control_file);
7938 +
7939 +     return 0;
7940 +}
7941 +
7942 +/* Merge any new information in newpkg into oldpkg */
7943 +/* XXX: CLEANUP: This function shouldn't actually modify anything in
7944 +   newpkg, but should leave it usable. This rework is so that
7945 +   pkg_hash_insert doesn't clobber the pkg that you pass into it. */
7946 +/* 
7947 + * uh, i thought that i had originally written this so that it took 
7948 + * two pkgs and returned a new one?  we can do that again... -sma
7949 + */
7950 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status)
7951 +{
7952 +     if (oldpkg == newpkg) {
7953 +         return 0;
7954 +     }
7955 +
7956 +     if (!oldpkg->src)
7957 +         oldpkg->src = newpkg->src;
7958 +     if (!oldpkg->dest)
7959 +         oldpkg->dest = newpkg->dest;
7960 +     if (!oldpkg->architecture)
7961 +         oldpkg->architecture = str_dup_safe(newpkg->architecture);
7962 +     if (!oldpkg->arch_priority)
7963 +         oldpkg->arch_priority = newpkg->arch_priority;
7964 +     if (!oldpkg->section)
7965 +         oldpkg->section = str_dup_safe(newpkg->section);
7966 +     if(!oldpkg->maintainer)
7967 +         oldpkg->maintainer = str_dup_safe(newpkg->maintainer);
7968 +     if(!oldpkg->description)
7969 +         oldpkg->description = str_dup_safe(newpkg->description);
7970 +     if (set_status) {
7971 +         /* merge the state_flags from the new package */
7972 +         oldpkg->state_want = newpkg->state_want;
7973 +         oldpkg->state_status = newpkg->state_status;
7974 +         oldpkg->state_flag = newpkg->state_flag;
7975 +     } else {
7976 +         if (oldpkg->state_want == SW_UNKNOWN)
7977 +              oldpkg->state_want = newpkg->state_want;
7978 +         if (oldpkg->state_status == SS_NOT_INSTALLED)
7979 +              oldpkg->state_status = newpkg->state_status;
7980 +         oldpkg->state_flag |= newpkg->state_flag;
7981 +     }
7982 +
7983 +     if (!oldpkg->depends_str && !oldpkg->pre_depends_str && !oldpkg->recommends_str && !oldpkg->suggests_str) {
7984 +         oldpkg->depends_str = newpkg->depends_str;
7985 +         newpkg->depends_str = NULL;
7986 +         oldpkg->depends_count = newpkg->depends_count;
7987 +         newpkg->depends_count = 0;
7988 +
7989 +         oldpkg->depends = newpkg->depends;
7990 +         newpkg->depends = NULL;
7991 +
7992 +         oldpkg->pre_depends_str = newpkg->pre_depends_str;
7993 +         newpkg->pre_depends_str = NULL;
7994 +         oldpkg->pre_depends_count = newpkg->pre_depends_count;
7995 +         newpkg->pre_depends_count = 0;
7996 +
7997 +         oldpkg->recommends_str = newpkg->recommends_str;
7998 +         newpkg->recommends_str = NULL;
7999 +         oldpkg->recommends_count = newpkg->recommends_count;
8000 +         newpkg->recommends_count = 0;
8001 +
8002 +         oldpkg->suggests_str = newpkg->suggests_str;
8003 +         newpkg->suggests_str = NULL;
8004 +         oldpkg->suggests_count = newpkg->suggests_count;
8005 +         newpkg->suggests_count = 0;
8006 +     }
8007 +
8008 +     if (!oldpkg->provides_str) {
8009 +         oldpkg->provides_str = newpkg->provides_str;
8010 +         newpkg->provides_str = NULL;
8011 +         oldpkg->provides_count = newpkg->provides_count;
8012 +         newpkg->provides_count = 0;
8013 +
8014 +         oldpkg->provides = newpkg->provides;
8015 +         newpkg->provides = NULL;
8016 +     }
8017 +
8018 +     if (!oldpkg->conflicts_str) {
8019 +         oldpkg->conflicts_str = newpkg->conflicts_str;
8020 +         newpkg->conflicts_str = NULL;
8021 +         oldpkg->conflicts_count = newpkg->conflicts_count;
8022 +         newpkg->conflicts_count = 0;
8023 +
8024 +         oldpkg->conflicts = newpkg->conflicts;
8025 +         newpkg->conflicts = NULL;
8026 +     }
8027 +
8028 +     if (!oldpkg->replaces_str) {
8029 +         oldpkg->replaces_str = newpkg->replaces_str;
8030 +         newpkg->replaces_str = NULL;
8031 +         oldpkg->replaces_count = newpkg->replaces_count;
8032 +         newpkg->replaces_count = 0;
8033 +
8034 +         oldpkg->replaces = newpkg->replaces;
8035 +         newpkg->replaces = NULL;
8036 +     }
8037 +
8038 +     if (!oldpkg->filename)
8039 +         oldpkg->filename = str_dup_safe(newpkg->filename);
8040 +     if (0)
8041 +     fprintf(stdout, "pkg=%s old local_filename=%s new local_filename=%s\n", 
8042 +            oldpkg->name, oldpkg->local_filename, newpkg->local_filename);
8043 +     if (!oldpkg->local_filename)
8044 +         oldpkg->local_filename = str_dup_safe(newpkg->local_filename);
8045 +     if (!oldpkg->tmp_unpack_dir)
8046 +         oldpkg->tmp_unpack_dir = str_dup_safe(newpkg->tmp_unpack_dir);
8047 +     if (!oldpkg->md5sum)
8048 +         oldpkg->md5sum = str_dup_safe(newpkg->md5sum);
8049 +     if (!oldpkg->size)
8050 +         oldpkg->size = str_dup_safe(newpkg->size);
8051 +     if (!oldpkg->installed_size)
8052 +         oldpkg->installed_size = str_dup_safe(newpkg->installed_size);
8053 +     if (!oldpkg->priority)
8054 +         oldpkg->priority = str_dup_safe(newpkg->priority);
8055 +     if (!oldpkg->source)
8056 +         oldpkg->source = str_dup_safe(newpkg->source);
8057 +     if (oldpkg->conffiles.head == NULL){
8058 +         oldpkg->conffiles = newpkg->conffiles;
8059 +         conffile_list_init(&newpkg->conffiles);
8060 +     }
8061 +     if (!oldpkg->installed_files){
8062 +         oldpkg->installed_files = newpkg->installed_files;
8063 +         oldpkg->installed_files_ref_cnt = newpkg->installed_files_ref_cnt;
8064 +         newpkg->installed_files = NULL;
8065 +     }
8066 +     if (!oldpkg->essential)
8067 +         oldpkg->essential = newpkg->essential;
8068 +
8069 +     return 0;
8070 +}
8071 +
8072 +abstract_pkg_t *abstract_pkg_new(void)
8073 +{
8074 +     abstract_pkg_t * ab_pkg;
8075 +
8076 +     ab_pkg = malloc(sizeof(abstract_pkg_t));
8077 +
8078 +     if (ab_pkg == NULL) {
8079 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8080 +         return NULL;
8081 +     }
8082 +
8083 +     if ( abstract_pkg_init(ab_pkg) < 0 ) 
8084 +        return NULL;
8085 +
8086 +     return ab_pkg;
8087 +}
8088 +
8089 +int abstract_pkg_init(abstract_pkg_t *ab_pkg)
8090 +{
8091 +     memset(ab_pkg, 0, sizeof(abstract_pkg_t));
8092 +
8093 +     ab_pkg->provided_by = abstract_pkg_vec_alloc();
8094 +     if (ab_pkg->provided_by==NULL){
8095 +        return -1;
8096 +     }
8097 +     ab_pkg->dependencies_checked = 0;
8098 +     ab_pkg->state_status = SS_NOT_INSTALLED;
8099 +
8100 +     return 0;
8101 +}
8102 +
8103 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg){
8104 +     char * temp_str;
8105 +     char **raw =NULL;
8106 +     char **raw_start=NULL; 
8107 +
8108 +     temp_str = (char *) malloc (strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
8109 +     if (temp_str == NULL ){
8110 +        ipkg_message(conf, IPKG_INFO, "Out of memory in  %s\n", __FUNCTION__);
8111 +        return;
8112 +     }
8113 +     sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
8114 +   
8115 +     raw = raw_start = read_raw_pkgs_from_file(temp_str);
8116 +     if (raw == NULL ){
8117 +        ipkg_message(conf, IPKG_ERROR, "Unable to open the control file in  %s\n", __FUNCTION__);
8118 +        return;
8119 +     }
8120 +
8121 +     while(*raw){
8122 +        if (!pkg_valorize_other_field(pkg, &raw ) == 0) {
8123 +            ipkg_message(conf, IPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
8124 +        }
8125 +     }
8126 +     raw = raw_start;
8127 +     while (*raw) {
8128 +        if (raw!=NULL)
8129 +          free(*raw++);
8130 +     }
8131 +
8132 +     free(raw_start); 
8133 +     free(temp_str);
8134 +
8135 +     return ;
8136 +
8137 +}
8138 +
8139 +char * pkg_formatted_info(pkg_t *pkg )
8140 +{
8141 +     char *line;
8142 +     char * buff;
8143 +
8144 +     buff = malloc(8192);
8145 +     if (buff == NULL) {
8146 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8147 +         return NULL;
8148 +     }
8149 +
8150 +     buff[0] = '\0';
8151 +
8152 +     line = pkg_formatted_field(pkg, "Package");
8153 +     strncat(buff ,line, strlen(line));
8154 +     free(line);
8155 +
8156 +     line = pkg_formatted_field(pkg, "Version");
8157 +     strncat(buff ,line, strlen(line));
8158 +     free(line);
8159 +
8160 +     line = pkg_formatted_field(pkg, "Depends");
8161 +     strncat(buff ,line, strlen(line));
8162 +     free(line);
8163 +     
8164 +     line = pkg_formatted_field(pkg, "Recommends");
8165 +     strncat(buff ,line, strlen(line));
8166 +     free(line);
8167 +
8168 +     line = pkg_formatted_field(pkg, "Suggests");
8169 +     strncat(buff ,line, strlen(line));
8170 +     free(line);
8171 +
8172 +     line = pkg_formatted_field(pkg, "Provides");
8173 +     strncat(buff ,line, strlen(line));
8174 +     free(line);
8175 +
8176 +     line = pkg_formatted_field(pkg, "Replaces");
8177 +     strncat(buff ,line, strlen(line));
8178 +     free(line);
8179 +
8180 +     line = pkg_formatted_field(pkg, "Conflicts");
8181 +     strncat(buff ,line, strlen(line));
8182 +     free(line);
8183 +
8184 +     line = pkg_formatted_field(pkg, "Status");
8185 +     strncat(buff ,line, strlen(line));
8186 +     free(line);
8187 +
8188 +     line = pkg_formatted_field(pkg, "Section");
8189 +     strncat(buff ,line, strlen(line));
8190 +     free(line);
8191 +
8192 +     line = pkg_formatted_field(pkg, "Essential"); /* @@@@ should be removed in future release. *//* I do not agree with this Pigi*/
8193 +     strncat(buff ,line, strlen(line));
8194 +     free(line);
8195 +
8196 +     line = pkg_formatted_field(pkg, "Architecture");
8197 +     strncat(buff ,line, strlen(line));
8198 +     free(line);
8199 +
8200 +     line = pkg_formatted_field(pkg, "Maintainer");
8201 +     strncat(buff ,line, strlen(line));
8202 +     free(line);
8203 +
8204 +     line = pkg_formatted_field(pkg, "MD5sum");
8205 +     strncat(buff ,line, strlen(line));
8206 +     free(line);
8207 +
8208 +     line = pkg_formatted_field(pkg, "Size");
8209 +     strncat(buff ,line, strlen(line));
8210 +     free(line);
8211 +
8212 +     line = pkg_formatted_field(pkg, "Filename");
8213 +     strncat(buff ,line, strlen(line));
8214 +     free(line);
8215 +
8216 +     line = pkg_formatted_field(pkg, "Conffiles");
8217 +     strncat(buff ,line, strlen(line));
8218 +     free(line);
8219 +
8220 +     line = pkg_formatted_field(pkg, "Source");
8221 +     strncat(buff ,line, strlen(line));
8222 +     free(line);
8223 +
8224 +     line = pkg_formatted_field(pkg, "Description");
8225 +     strncat(buff ,line, strlen(line));
8226 +     free(line);
8227 +
8228 +     line = pkg_formatted_field(pkg, "Installed-Time");
8229 +     strncat(buff ,line, strlen(line));
8230 +     free(line);
8231 +
8232 +     return buff;
8233 +}
8234 +
8235 +char * pkg_formatted_field(pkg_t *pkg, const char *field )
8236 +{
8237 +     static size_t LINE_LEN = 128;
8238 +     char * temp = (char *)malloc(1);
8239 +     int len = 0;
8240 +     int flag_provide_false = 0;
8241 +
8242 +/*
8243 +  Pigi: After some discussion with Florian we decided to modify the full procedure in 
8244 +        dynamic memory allocation. This should avoid any other segv in this area ( except for bugs )
8245 +*/
8246 +
8247 +     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8248 +         goto UNKNOWN_FMT_FIELD;
8249 +     }
8250 +
8251 +     temp[0]='\0'; 
8252 +
8253 +     switch (field[0])
8254 +     {
8255 +     case 'a':
8256 +     case 'A':
8257 +         if (strcasecmp(field, "Architecture") == 0) {
8258 +              /* Architecture */
8259 +              if (pkg->architecture) {
8260 +                   temp = (char *)realloc(temp,strlen(pkg->architecture)+17);
8261 +                   if ( temp == NULL ){
8262 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8263 +                     return NULL;
8264 +                   }
8265 +                   temp[0]='\0';
8266 +                   snprintf(temp, (strlen(pkg->architecture)+17), "Architecture: %s\n", pkg->architecture);
8267 +              }
8268 +         } else {
8269 +              goto UNKNOWN_FMT_FIELD;
8270 +         }
8271 +         break;
8272 +     case 'c':
8273 +     case 'C':
8274 +         if (strcasecmp(field, "Conffiles") == 0) {
8275 +              /* Conffiles */
8276 +              conffile_list_elt_t *iter;
8277 +               char confstr[LINE_LEN];
8278 +
8279 +              if (pkg->conffiles.head == NULL) {
8280 +                   return temp;
8281 +              }
8282 +
8283 +               len = 14 ;
8284 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8285 +                   if (iter->data->name && iter->data->value) {
8286 +                       len = len + (strlen(iter->data->name)+strlen(iter->data->value)+5);
8287 +                   }
8288 +              }
8289 +               temp = (char *)realloc(temp,len);
8290 +               if ( temp == NULL ){
8291 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8292 +                 return NULL;
8293 +               }
8294 +               temp[0]='\0';
8295 +               strncpy(temp, "Conffiles:\n", 12);
8296 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8297 +                   if (iter->data->name && iter->data->value) {
8298 +                         snprintf(confstr, LINE_LEN, "%s %s\n", iter->data->name, iter->data->value);
8299 +                         strncat(temp, confstr, strlen(confstr));           
8300 +                   }
8301 +              }
8302 +         } else if (strcasecmp(field, "Conflicts") == 0) {
8303 +              int i;
8304 +
8305 +              if (pkg->conflicts_count) {
8306 +                    char conflictstr[LINE_LEN];
8307 +                    len = 14 ;
8308 +                   for(i = 0; i < pkg->conflicts_count; i++) {
8309 +                        len = len + (strlen(pkg->conflicts_str[i])+5);
8310 +                    }
8311 +                    temp = (char *)realloc(temp,len);
8312 +                    if ( temp == NULL ){
8313 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8314 +                      return NULL;
8315 +                    }
8316 +                    temp[0]='\0';
8317 +                    strncpy(temp, "Conflicts:", 11);
8318 +                   for(i = 0; i < pkg->conflicts_count; i++) {
8319 +                        snprintf(conflictstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->conflicts_str[i]);
8320 +                        strncat(temp, conflictstr, strlen(conflictstr));           
8321 +                    }
8322 +                    strncat(temp, "\n", strlen("\n")); 
8323 +              }
8324 +         } else {
8325 +              goto UNKNOWN_FMT_FIELD;
8326 +         }
8327 +         break;
8328 +     case 'd':
8329 +     case 'D':
8330 +         if (strcasecmp(field, "Depends") == 0) {
8331 +              /* Depends */
8332 +              int i;
8333 +
8334 +              if (pkg->depends_count) {
8335 +                    char depstr[LINE_LEN];
8336 +                    len = 14 ;
8337 +                   for(i = 0; i < pkg->depends_count; i++) {
8338 +                        len = len + (strlen(pkg->depends_str[i])+4);
8339 +                    }
8340 +                    temp = (char *)realloc(temp,len);
8341 +                    if ( temp == NULL ){
8342 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8343 +                      return NULL;
8344 +                    }
8345 +                    temp[0]='\0';
8346 +                    strncpy(temp, "Depends:", 10);
8347 +                   for(i = 0; i < pkg->depends_count; i++) {
8348 +                        snprintf(depstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
8349 +                        strncat(temp, depstr, strlen(depstr));           
8350 +                    }
8351 +                    strncat(temp, "\n", strlen("\n")); 
8352 +              }
8353 +         } else if (strcasecmp(field, "Description") == 0) {
8354 +              /* Description */
8355 +              if (pkg->description) {
8356 +                   temp = (char *)realloc(temp,strlen(pkg->description)+16);
8357 +                   if ( temp == NULL ){
8358 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8359 +                     return NULL;
8360 +                   }
8361 +                   temp[0]='\0';
8362 +                   snprintf(temp, (strlen(pkg->description)+16), "Description: %s\n", pkg->description);
8363 +              }
8364 +         } else {
8365 +              goto UNKNOWN_FMT_FIELD;
8366 +         }
8367 +      break;
8368 +     case 'e':
8369 +     case 'E': {
8370 +         /* Essential */
8371 +         if (pkg->essential) {
8372 +              temp = (char *)realloc(temp,16);
8373 +              if ( temp == NULL ){
8374 +                fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8375 +                return NULL;
8376 +              }
8377 +              temp[0]='\0';
8378 +              snprintf(temp, (16), "Essential: yes\n");
8379 +         }
8380 +     }
8381 +         break;
8382 +     case 'f':
8383 +     case 'F': {
8384 +         /* Filename */
8385 +         if (pkg->filename) {
8386 +              temp = (char *)realloc(temp,strlen(pkg->filename)+12);
8387 +              if ( temp == NULL ){
8388 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8389 +                 return NULL;
8390 +              }
8391 +              temp[0]='\0';
8392 +              snprintf(temp, (strlen(pkg->filename)+12), "Filename: %s\n", pkg->filename);
8393 +         }
8394 +     }
8395 +         break;
8396 +     case 'i':
8397 +     case 'I': {
8398 +         if (strcasecmp(field, "Installed-Size") == 0) {
8399 +              /* Installed-Size */
8400 +               temp = (char *)realloc(temp,strlen(pkg->installed_size)+17);
8401 +               if ( temp == NULL ){
8402 +                  fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8403 +                  return NULL;
8404 +               }
8405 +               temp[0]='\0';
8406 +               snprintf(temp, (strlen(pkg->installed_size)+17), "Installed-Size: %s\n", pkg->installed_size);
8407 +         } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) {
8408 +               temp = (char *)realloc(temp,29);
8409 +               if ( temp == NULL ){
8410 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8411 +                 return NULL;
8412 +               }
8413 +               temp[0]='\0';
8414 +               snprintf(temp, 29, "Installed-Time: %lu\n", pkg->installed_time);
8415 +         }
8416 +     }
8417 +         break;
8418 +     case 'm':
8419 +     case 'M': {
8420 +         /* Maintainer | MD5sum */
8421 +         if (strcasecmp(field, "Maintainer") == 0) {
8422 +              /* Maintainer */
8423 +              if (pkg->maintainer) {
8424 +                   temp = (char *)realloc(temp,strlen(pkg->maintainer)+14);
8425 +                   if ( temp == NULL ){
8426 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8427 +                     return NULL;
8428 +                   }
8429 +                   temp[0]='\0';
8430 +                   snprintf(temp, (strlen(pkg->maintainer)+14), "maintainer: %s\n", pkg->maintainer);
8431 +              }
8432 +         } else if (strcasecmp(field, "MD5sum") == 0) {
8433 +              /* MD5sum */
8434 +              if (pkg->md5sum) {
8435 +                   temp = (char *)realloc(temp,strlen(pkg->md5sum)+11);
8436 +                   if ( temp == NULL ){
8437 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8438 +                     return NULL;
8439 +                   }
8440 +                   temp[0]='\0';
8441 +                   snprintf(temp, (strlen(pkg->md5sum)+11), "MD5Sum: %s\n", pkg->md5sum);
8442 +              }
8443 +         } else {
8444 +              goto UNKNOWN_FMT_FIELD;
8445 +         }
8446 +     }
8447 +         break;
8448 +     case 'p':
8449 +     case 'P': {
8450 +         if (strcasecmp(field, "Package") == 0) {
8451 +              /* Package */
8452 +               temp = (char *)realloc(temp,strlen(pkg->name)+11);
8453 +               if ( temp == NULL ){
8454 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8455 +                 return NULL;
8456 +               }
8457 +               temp[0]='\0';
8458 +               snprintf(temp, (strlen(pkg->name)+11), "Package: %s\n", pkg->name);
8459 +         } else if (strcasecmp(field, "Priority") == 0) {
8460 +              /* Priority */
8461 +               temp = (char *)realloc(temp,strlen(pkg->priority)+12);
8462 +               if ( temp == NULL ){
8463 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8464 +                 return NULL;
8465 +               }
8466 +               temp[0]='\0';
8467 +               snprintf(temp, (strlen(pkg->priority)+12), "Priority: %s\n", pkg->priority);
8468 +         } else if (strcasecmp(field, "Provides") == 0) {
8469 +              /* Provides */
8470 +              int i;
8471 +
8472 +              if (pkg->provides_count) {
8473 +               /* Here we check if the ipkg_internal_use_only is used, and we discard it.*/
8474 +                  for ( i=0; i < pkg->provides_count; i++ ){
8475 +                     if (strstr(pkg->provides_str[i],"ipkg_internal_use_only")!=NULL) {
8476 +                         memset (pkg->provides_str[i],'\x0',strlen(pkg->provides_str[i])); /* Pigi clear my trick flag, just in case */
8477 +                         flag_provide_false = 1;
8478 +                      }
8479 +                  }
8480 +                  if ( !flag_provide_false ||                                             /* Pigi there is not my trick flag */
8481 +                     ((flag_provide_false) &&  (pkg->provides_count > 1))){             /* Pigi There is, but we also have others Provides */
8482 +                     char provstr[LINE_LEN];
8483 +                     len = 15;
8484 +                    for(i = 0; i < pkg->provides_count; i++) {
8485 +                         len = len + (strlen(pkg->provides_str[i])+5);
8486 +                     }
8487 +                     temp = (char *)realloc(temp,len);
8488 +                     if ( temp == NULL ){
8489 +                       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8490 +                       return NULL;
8491 +                     }
8492 +                     temp[0]='\0';
8493 +                     strncpy(temp, "Provides:", 12);
8494 +                    for(i = 0; i < pkg->provides_count; i++) {
8495 +                         if (strlen(pkg->provides_str[i])>0){;
8496 +                            snprintf(provstr, LINE_LEN, "%s %s", i == 1 ? "" : ",", pkg->provides_str[i]);
8497 +                            strncat(temp, provstr, strlen(provstr));           
8498 +                         }
8499 +                     }
8500 +                     strncat(temp, "\n", strlen("\n")); 
8501 +                  }
8502 +               }
8503 +         } else {
8504 +              goto UNKNOWN_FMT_FIELD;
8505 +         }
8506 +     }
8507 +         break;
8508 +     case 'r':
8509 +     case 'R': {
8510 +         int i;
8511 +         /* Replaces | Recommends*/
8512 +         if (strcasecmp (field, "Replaces") == 0) {
8513 +              if (pkg->replaces_count) {
8514 +                    char replstr[LINE_LEN];
8515 +                    len = 14;
8516 +                   for (i = 0; i < pkg->replaces_count; i++) {
8517 +                        len = len + (strlen(pkg->replaces_str[i])+5);
8518 +                    }
8519 +                    temp = (char *)realloc(temp,len);
8520 +                    if ( temp == NULL ){
8521 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8522 +                      return NULL;
8523 +                    }
8524 +                    temp[0]='\0';
8525 +                    strncpy(temp, "Replaces:", 12);
8526 +                   for (i = 0; i < pkg->replaces_count; i++) {
8527 +                        snprintf(replstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->replaces_str[i]);
8528 +                        strncat(temp, replstr, strlen(replstr));           
8529 +                    }
8530 +                    strncat(temp, "\n", strlen("\n")); 
8531 +              }
8532 +         } else if (strcasecmp (field, "Recommends") == 0) {
8533 +              if (pkg->recommends_count) {
8534 +                    char recstr[LINE_LEN];
8535 +                    len = 15;
8536 +                   for(i = 0; i < pkg->recommends_count; i++) {
8537 +                         len = len + (strlen( pkg->recommends_str[i])+5);
8538 +                    }
8539 +                    temp = (char *)realloc(temp,len);
8540 +                   if ( temp == NULL ){
8541 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8542 +                     return NULL;
8543 +                   }
8544 +                    temp[0]='\0';
8545 +                    strncpy(temp, "Recommends:", 13);
8546 +                   for(i = 0; i < pkg->recommends_count; i++) {
8547 +                        snprintf(recstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->recommends_str[i]);
8548 +                        strncat(temp, recstr, strlen(recstr));           
8549 +                    }
8550 +                    strncat(temp, "\n", strlen("\n")); 
8551 +              }
8552 +         } else {
8553 +              goto UNKNOWN_FMT_FIELD;
8554 +         }
8555 +     }
8556 +         break;
8557 +     case 's':
8558 +     case 'S': {
8559 +         /* Section | Size | Source | Status | Suggests */
8560 +         if (strcasecmp(field, "Section") == 0) {
8561 +              /* Section */
8562 +              if (pkg->section) {
8563 +                   temp = (char *)realloc(temp,strlen(pkg->section)+11);
8564 +                   if ( temp == NULL ){
8565 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8566 +                     return NULL;
8567 +                   }
8568 +                   temp[0]='\0';
8569 +                   snprintf(temp, (strlen(pkg->section)+11), "Section: %s\n", pkg->section);
8570 +              }
8571 +         } else if (strcasecmp(field, "Size") == 0) {
8572 +              /* Size */
8573 +              if (pkg->size) {
8574 +                   temp = (char *)realloc(temp,strlen(pkg->size)+8);
8575 +                   if ( temp == NULL ){
8576 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8577 +                     return NULL;
8578 +                   }
8579 +                   temp[0]='\0';
8580 +                   snprintf(temp, (strlen(pkg->size)+8), "Size: %s\n", pkg->size);
8581 +              }
8582 +         } else if (strcasecmp(field, "Source") == 0) {
8583 +              /* Source */
8584 +              if (pkg->source) {
8585 +                   temp = (char *)realloc(temp,strlen(pkg->source)+10);
8586 +                   if ( temp == NULL ){
8587 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8588 +                     return NULL;
8589 +                   }
8590 +                   temp[0]='\0';
8591 +                   snprintf(temp, (strlen(pkg->source)+10), "Source: %s\n", pkg->source);
8592 +               }
8593 +         } else if (strcasecmp(field, "Status") == 0) {
8594 +              /* Status */
8595 +               /* Benjamin Pineau note: we should avoid direct usage of 
8596 +                * strlen(arg) without keeping "arg" for later free()
8597 +                */
8598 +               char *pflag=pkg_state_flag_to_str(pkg->state_flag);
8599 +               char *pstat=pkg_state_status_to_str(pkg->state_status);
8600 +               char *pwant=pkg_state_want_to_str(pkg->state_want);
8601 +
8602 +               size_t sum_of_sizes = (size_t) ( strlen(pwant)+ strlen(pflag)+ strlen(pstat) + 12 );
8603 +               temp = (char *)realloc(temp,sum_of_sizes);
8604 +               if ( temp == NULL ){
8605 +                   fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8606 +                   return NULL;
8607 +                }
8608 +                temp[0]='\0';
8609 +                snprintf(temp, sum_of_sizes , "Status: %s %s %s\n", pwant, pflag, pstat);
8610 +                free(pflag);
8611 +                free(pwant);
8612 +               if(pstat) /* pfstat can be NULL if ENOMEM */
8613 +                   free(pstat);
8614 +         } else if (strcasecmp(field, "Suggests") == 0) {
8615 +              if (pkg->suggests_count) {
8616 +                   int i;
8617 +                    char sugstr[LINE_LEN];
8618 +                    len = 13;
8619 +                   for(i = 0; i < pkg->suggests_count; i++) {
8620 +                        len = len + (strlen(pkg->suggests_str[i])+5);
8621 +                    }
8622 +                    temp = (char *)realloc(temp,len);
8623 +                    if ( temp == NULL ){
8624 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8625 +                      return NULL;
8626 +                    }
8627 +                    temp[0]='\0';
8628 +                    strncpy(temp, "Suggests:", 10);
8629 +                   for(i = 0; i < pkg->suggests_count; i++) {
8630 +                        snprintf(sugstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->suggests_str[i]);
8631 +                        strncat(temp, sugstr, strlen(sugstr));           
8632 +                    }
8633 +                    strncat(temp, "\n", strlen("\n")); 
8634 +              }
8635 +         } else {
8636 +              goto UNKNOWN_FMT_FIELD;
8637 +         }
8638 +     }
8639 +         break;
8640 +     case 'v':
8641 +     case 'V': {
8642 +         /* Version */
8643 +         char *version = pkg_version_str_alloc(pkg);
8644 +          temp = (char *)realloc(temp,strlen(version)+14);
8645 +          if ( temp == NULL ){
8646 +             fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8647 +             return NULL;
8648 +          }
8649 +          temp[0]='\0';
8650 +          snprintf(temp, (strlen(version)+12), "Version: %s\n", version);
8651 +         free(version);
8652 +     }
8653 +         break;
8654 +     default:
8655 +         goto UNKNOWN_FMT_FIELD;
8656 +     }
8657 +
8658 +     if ( strlen(temp)<2 ) {
8659 +          temp[0]='\0';
8660 +     }
8661 +     return temp;
8662 +
8663 + UNKNOWN_FMT_FIELD:
8664 +     fprintf(stderr, "%s: ERROR: Unknown field name: %s\n", __FUNCTION__, field);
8665 +     if ( strlen(temp)<2 ) {
8666 +          temp[0]='\0';
8667 +     }
8668 +
8669 +     return temp;
8670 +}
8671 +
8672 +void pkg_print_info(pkg_t *pkg, FILE *file)
8673 +{
8674 +     char * buff;
8675 +     if (pkg == NULL) {
8676 +       return;
8677 +     }
8678 +
8679 +     buff = pkg_formatted_info(pkg);
8680 +     if ( buff == NULL ) 
8681 +         return;
8682 +     if (strlen(buff)>2){
8683 +         fwrite(buff, 1, strlen(buff), file);
8684 +     } 
8685 +     free(buff);
8686 +}
8687 +
8688 +void pkg_print_status(pkg_t * pkg, FILE * file)
8689 +{
8690 +     if (pkg == NULL) {
8691 +         return;
8692 +     }
8693 +
8694 +     /* XXX: QUESTION: Do we actually want more fields here? The
8695 +       original idea was to save space by installing only what was
8696 +       needed for actual computation, (package, version, status,
8697 +       essential, conffiles). The assumption is that all other fields
8698 +       can be found in th available file.
8699 +
8700 +       But, someone proposed the idea to make it possible to
8701 +       reconstruct a .ipk from an installed package, (ie. for beaming
8702 +       from one handheld to another). So, maybe we actually want a few
8703 +       more fields here, (depends, suggests, etc.), so that that would
8704 +       be guaranteed to work even in the absence of more information
8705 +       from the available file.
8706 +
8707 +       28-MAR-03: kergoth and I discussed this yesterday.  We think
8708 +       the essential info needs to be here for all installed packages
8709 +       because they may not appear in the Packages files on various
8710 +       feeds.  Furthermore, one should be able to install from URL or
8711 +       local storage without requiring a Packages file from any feed.
8712 +       -Jamey
8713 +     */
8714 +     pkg_print_field(pkg, file, "Package");
8715 +     pkg_print_field(pkg, file, "Version");
8716 +     pkg_print_field(pkg, file, "Depends");
8717 +     pkg_print_field(pkg, file, "Recommends");
8718 +     pkg_print_field(pkg, file, "Suggests");
8719 +     pkg_print_field(pkg, file, "Provides");
8720 +     pkg_print_field(pkg, file, "Replaces");
8721 +     pkg_print_field(pkg, file, "Conflicts");
8722 +     pkg_print_field(pkg, file, "Status");
8723 +     pkg_print_field(pkg, file, "Essential"); /* @@@@ should be removed in future release. */
8724 +     pkg_print_field(pkg, file, "Architecture");
8725 +     pkg_print_field(pkg, file, "Conffiles");
8726 +     pkg_print_field(pkg, file, "Installed-Time");
8727 +     fputs("\n", file);
8728 +}
8729 +
8730 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field)
8731 +{
8732 +     char *buff;
8733 +     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8734 +       fprintf(stderr, "%s: ERROR: Unknown field name: %s\n",
8735 +            __FUNCTION__, field);
8736 +     }
8737 +     buff = pkg_formatted_field(pkg, field);
8738 +     if (strlen(buff)>2) {
8739 +       fprintf(file, "%s", buff);
8740 +       fflush(file);
8741 +     }
8742 +     free(buff);
8743 +     return;
8744 +}
8745 +
8746 +/*
8747 + * libdpkg - Debian packaging suite library routines
8748 + * vercmp.c - comparison of version numbers
8749 + *
8750 + * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
8751 + */
8752 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg)
8753 +{
8754 +     int r;
8755 +
8756 +     if (pkg->epoch > ref_pkg->epoch) {
8757 +         return 1;
8758 +     }
8759 +
8760 +     if (pkg->epoch < ref_pkg->epoch) {
8761 +         return -1;
8762 +     }
8763 +
8764 +     r = verrevcmp(pkg->version, ref_pkg->version);
8765 +     if (r) {
8766 +         return r;
8767 +     }
8768 +
8769 +#ifdef USE_DEBVERSION
8770 +     r = verrevcmp(pkg->revision, ref_pkg->revision);
8771 +     if (r) {
8772 +         return r;
8773 +     }
8774 +
8775 +     r = verrevcmp(pkg->familiar_revision, ref_pkg->familiar_revision);
8776 +#endif
8777 +
8778 +     return r;
8779 +}
8780 +
8781 +int verrevcmp(const char *val, const char *ref)
8782 +{
8783 +     int vc, rc;
8784 +     long vl, rl;
8785 +     const char *vp, *rp;
8786 +     const char *vsep, *rsep;
8787 +    
8788 +     if (!val) val= "";
8789 +     if (!ref) ref= "";
8790 +     for (;;) {
8791 +         vp= val;  while (*vp && !isdigit(*vp)) vp++;
8792 +         rp= ref;  while (*rp && !isdigit(*rp)) rp++;
8793 +         for (;;) {
8794 +              vc= (val == vp) ? 0 : *val++;
8795 +              rc= (ref == rp) ? 0 : *ref++;
8796 +              if (!rc && !vc) break;
8797 +              if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
8798 +              if (rc && !isalpha(rc)) rc += 256;
8799 +              if (vc != rc) return vc - rc;
8800 +         }
8801 +         val= vp;
8802 +         ref= rp;
8803 +         vl=0;  if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
8804 +         rl=0;  if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
8805 +         if (vl != rl) return vl - rl;
8806 +
8807 +         vc = *val;
8808 +         rc = *ref;
8809 +         vsep = strchr(".-", vc);
8810 +         rsep = strchr(".-", rc);
8811 +         if (vsep && !rsep) return -1;
8812 +         if (!vsep && rsep) return +1;
8813 +
8814 +         if (!*val && !*ref) return 0;
8815 +         if (!*val) return -1;
8816 +         if (!*ref) return +1;
8817 +     }
8818 +}
8819 +
8820 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op)
8821 +{
8822 +     int r;
8823 +
8824 +     r = pkg_compare_versions(it, ref);
8825 +
8826 +     if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
8827 +         return r <= 0;
8828 +     }
8829 +
8830 +     if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
8831 +         return r >= 0;
8832 +     }
8833 +
8834 +     if (strcmp(op, "<<") == 0) {
8835 +         return r < 0;
8836 +     }
8837 +
8838 +     if (strcmp(op, ">>") == 0) {
8839 +         return r > 0;
8840 +     }
8841 +
8842 +     if (strcmp(op, "=") == 0) {
8843 +         return r == 0;
8844 +     }
8845 +
8846 +     fprintf(stderr, "unknown operator: %s", op);
8847 +     return 0;
8848 +}
8849 +
8850 +int pkg_name_version_and_architecture_compare(void *p1, void *p2)
8851 +{
8852 +     const pkg_t *a = *(const pkg_t **)p1;
8853 +     const pkg_t *b = *(const pkg_t **)p2;
8854 +     int namecmp;
8855 +     int vercmp;
8856 +     if (!a->name || !b->name) {
8857 +       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->name=%p b=%p b->name=%p\n",
8858 +              a, a->name, b, b->name);
8859 +       return 0;
8860 +     }
8861 +       
8862 +     namecmp = strcmp(a->name, b->name);
8863 +     if (namecmp)
8864 +         return namecmp;
8865 +     vercmp = pkg_compare_versions(a, b);
8866 +     if (vercmp)
8867 +         return vercmp;
8868 +     if (!a->arch_priority || !b->arch_priority) {
8869 +       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->arch_priority=%i b=%p b->arch_priority=%i\n",
8870 +              a, a->arch_priority, b, b->arch_priority);
8871 +       return 0;
8872 +     }
8873 +     if (a->arch_priority > b->arch_priority)
8874 +         return 1;
8875 +     if (a->arch_priority < b->arch_priority)
8876 +         return -1;
8877 +     return 0;
8878 +}
8879 +
8880 +int abstract_pkg_name_compare(void *p1, void *p2)
8881 +{
8882 +     const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
8883 +     const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
8884 +     if (!a->name || !b->name) {
8885 +       fprintf(stderr, "abstract_pkg_name_compare: a=%p a->name=%p b=%p b->name=%p\n",
8886 +              a, a->name, b, b->name);
8887 +       return 0;
8888 +     }
8889 +     return strcmp(a->name, b->name);
8890 +}
8891 +
8892 +
8893 +char *pkg_version_str_alloc(pkg_t *pkg)
8894 +{
8895 +     char *complete_version;
8896 +     char *epoch_str;
8897 +#ifdef USE_DEBVERSION
8898 +     char *revision_str;
8899 +     char *familiar_revision_str;
8900 +#endif
8901 +
8902 +     if (pkg->epoch) {
8903 +         sprintf_alloc(&epoch_str, "%d:", pkg->epoch);
8904 +     } else {
8905 +         epoch_str = strdup("");
8906 +     }
8907 +
8908 +#ifdef USE_DEBVERSION
8909 +     if (pkg->revision && strlen(pkg->revision)) {
8910 +         sprintf_alloc(&revision_str, "-%s", pkg->revision);
8911 +     } else {
8912 +         revision_str = strdup("");
8913 +     }
8914 +
8915 +     if (pkg->familiar_revision && strlen(pkg->familiar_revision)) {
8916 +         sprintf_alloc(&familiar_revision_str, "-fam%s", pkg->familiar_revision);
8917 +     } else {
8918 +         familiar_revision_str = strdup("");
8919 +     }
8920 +#endif
8921 +
8922 +#ifdef USE_DEBVERSION
8923 +     sprintf_alloc(&complete_version, "%s%s%s%s",
8924 +                  epoch_str, pkg->version, revision_str, familiar_revision_str);
8925 +#else
8926 +     sprintf_alloc(&complete_version, "%s%s",
8927 +                  epoch_str, pkg->version);
8928 +#endif
8929 +
8930 +     free(epoch_str);
8931 +#ifdef USE_DEBVERSION
8932 +     free(revision_str);
8933 +     free(familiar_revision_str);
8934 +#endif
8935 +
8936 +     return complete_version;
8937 +}
8938 +
8939 +str_list_t *pkg_get_installed_files(pkg_t *pkg)
8940 +{
8941 +     int err;
8942 +     char *list_file_name = NULL;
8943 +     FILE *list_file = NULL;
8944 +     char *line;
8945 +     char *installed_file_name;
8946 +     int rootdirlen;
8947 +
8948 +     pkg->installed_files_ref_cnt++;
8949 +
8950 +     if (pkg->installed_files) {
8951 +         return pkg->installed_files;
8952 +     }
8953 +
8954 +     pkg->installed_files = str_list_alloc();
8955 +     if (pkg->installed_files == NULL) {
8956 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8957 +         return NULL;
8958 +     }
8959 +
8960 +     /* For uninstalled packages, get the file list firectly from the package.
8961 +       For installed packages, look at the package.list file in the database.
8962 +     */
8963 +     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
8964 +         if (pkg->local_filename == NULL) {
8965 +              return pkg->installed_files;
8966 +         }
8967 +         /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
8968 +            file. In other words, change deb_extract so that it can
8969 +            simply return the file list as a char *[] rather than
8970 +            insisting on writing in to a FILE * as it does now. */
8971 +         list_file = tmpfile();
8972 +         err = pkg_extract_data_file_names_to_stream(pkg, list_file);
8973 +         if (err) {
8974 +              fclose(list_file);
8975 +              fprintf(stderr, "%s: Error extracting file list from %s: %s\n",
8976 +                      __FUNCTION__, pkg->local_filename, strerror(err));
8977 +              return pkg->installed_files;
8978 +         }
8979 +         rewind(list_file);
8980 +     } else {
8981 +         sprintf_alloc(&list_file_name, "%s/%s.list",
8982 +                       pkg->dest->info_dir, pkg->name);
8983 +         if (! file_exists(list_file_name)) {
8984 +              free(list_file_name);
8985 +              return pkg->installed_files;
8986 +         }
8987 +
8988 +         list_file = fopen(list_file_name, "r");
8989 +         if (list_file == NULL) {
8990 +              fprintf(stderr, "WARNING: Cannot open %s: %s\n",
8991 +                      list_file_name, strerror(errno));
8992 +              free(list_file_name);
8993 +              return pkg->installed_files;
8994 +         }
8995 +         free(list_file_name);
8996 +     }
8997 +
8998 +     rootdirlen = strlen( pkg->dest->root_dir );
8999 +     while (1) {
9000 +         char *file_name;
9001 +       
9002 +         line = file_read_line_alloc(list_file);
9003 +         if (line == NULL) {
9004 +              break;
9005 +         }
9006 +         str_chomp(line);
9007 +         file_name = line;
9008 +
9009 +         /* Take pains to avoid uglies like "/./" in the middle of file_name. */
9010 +         if( strncmp( pkg->dest->root_dir, 
9011 +                      file_name, 
9012 +                      rootdirlen ) ) {
9013 +              if (*file_name == '.') {
9014 +                   file_name++;
9015 +              }
9016 +              if (*file_name == '/') {
9017 +                   file_name++;
9018 +              }
9019 +
9020 +              /* Freed in pkg_free_installed_files */
9021 +              sprintf_alloc(&installed_file_name, "%s%s", pkg->dest->root_dir, file_name);
9022 +         } else {
9023 +              // already contains root_dir as header -> ABSOLUTE
9024 +              sprintf_alloc(&installed_file_name, "%s", file_name);
9025 +         }
9026 +         str_list_append(pkg->installed_files, installed_file_name);
9027 +         free(line);
9028 +     }
9029 +
9030 +     fclose(list_file);
9031 +
9032 +     return pkg->installed_files;
9033 +}
9034 +
9035 +/* XXX: CLEANUP: This function and it's counterpart,
9036 +   (pkg_get_installed_files), do not match our init/deinit naming
9037 +   convention. Nor the alloc/free convention. But, then again, neither
9038 +   of these conventions currrently fit the way these two functions
9039 +   work. */
9040 +int pkg_free_installed_files(pkg_t *pkg)
9041 +{
9042 +     str_list_elt_t *iter;
9043 +
9044 +     pkg->installed_files_ref_cnt--;
9045 +     if (pkg->installed_files_ref_cnt > 0) {
9046 +         return 0;
9047 +     }
9048 +
9049 +     if (pkg->installed_files) {
9050 +
9051 +         for (iter = pkg->installed_files->head; iter; iter = iter->next) {
9052 +              /* malloced in pkg_get_installed_files */
9053 +              free (iter->data);
9054 +              iter->data = NULL;
9055 +         }
9056 +
9057 +         str_list_deinit(pkg->installed_files);
9058 +     }
9059 +
9060 +     pkg->installed_files = NULL;
9061 +
9062 +     return 0;
9063 +}
9064 +
9065 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg)
9066 +{
9067 +     int err;
9068 +     char *list_file_name;
9069 +
9070 +     //I don't think pkg_free_installed_files should be called here. Jamey
9071 +     //pkg_free_installed_files(pkg);
9072 +
9073 +     sprintf_alloc(&list_file_name, "%s/%s.list",
9074 +                  pkg->dest->info_dir, pkg->name);
9075 +     if (!conf->noaction) {
9076 +         err = unlink(list_file_name);
9077 +         free(list_file_name);
9078 +
9079 +         if (err) {
9080 +              return errno;
9081 +         }
9082 +     }
9083 +     return 0;
9084 +}
9085 +
9086 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name)
9087 +{
9088 +     conffile_list_elt_t *iter;
9089 +     conffile_t *conffile;
9090 +
9091 +     if (pkg == NULL) {
9092 +         return NULL;
9093 +     }
9094 +
9095 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
9096 +         conffile = iter->data;
9097 +
9098 +         if (strcmp(conffile->name, file_name) == 0) {
9099 +              return conffile;
9100 +         }
9101 +     }
9102 +
9103 +     return NULL;
9104 +}
9105 +
9106 +int pkg_run_script(ipkg_conf_t *conf, pkg_t *pkg,
9107 +                  const char *script, const char *args)
9108 +{
9109 +     int err;
9110 +     char *path;
9111 +     char *cmd;
9112 +
9113 +     /* XXX: FEATURE: When conf->offline_root is set, we should run the
9114 +       maintainer script within a chroot environment. */
9115 +
9116 +     /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
9117 +       have scripts in pkg->tmp_unpack_dir. */
9118 +     if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
9119 +         if (pkg->dest == NULL) {
9120 +              fprintf(stderr, "%s: ERROR: installed package %s has a NULL dest\n",
9121 +                      __FUNCTION__, pkg->name);
9122 +              return EINVAL;
9123 +         }
9124 +         sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, script);
9125 +     } else {
9126 +         if (pkg->tmp_unpack_dir == NULL) {
9127 +              fprintf(stderr, "%s: ERROR: uninstalled package %s has a NULL tmp_unpack_dir\n",
9128 +                      __FUNCTION__, pkg->name);
9129 +              return EINVAL;
9130 +         }
9131 +         sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script);
9132 +     }
9133 +
9134 +     ipkg_message(conf, IPKG_INFO, "Running script %s\n", path);
9135 +     if (conf->noaction) return 0;
9136 +
9137 +     /* XXX: CLEANUP: There must be a better way to handle maintainer
9138 +       scripts when running with offline_root mode and/or a dest other
9139 +       than '/'. I've been playing around with some clever chroot
9140 +       tricks and I might come up with something workable. */
9141 +     if (conf->offline_root) {
9142 +         setenv("IPKG_OFFLINE_ROOT", conf->offline_root, 1);
9143 +     }
9144 +
9145 +     setenv("PKG_ROOT",
9146 +           pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, 1);
9147 +
9148 +     if (! file_exists(path)) {
9149 +         free(path);
9150 +         return 0;
9151 +     }
9152 +
9153 +     if (conf->offline_root) {
9154 +         fprintf(stderr, "(offline root mode: not running %s.%s)\n", pkg->name, script);
9155 +         free(path);
9156 +         return 0;
9157 +     }
9158 +
9159 +     sprintf_alloc(&cmd, "%s %s", path, args);
9160 +     free(path);
9161 +
9162 +     err = xsystem(cmd);
9163 +     free(cmd);
9164 +
9165 +     if (err) {
9166 +         fprintf(stderr, "%s script returned status %d\n", script, err);
9167 +         return err;
9168 +     }
9169 +
9170 +     return 0;
9171 +}
9172 +
9173 +char *pkg_state_want_to_str(pkg_state_want_t sw)
9174 +{
9175 +     int i;
9176 +
9177 +     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
9178 +         if (pkg_state_want_map[i].value == sw) {
9179 +              return strdup(pkg_state_want_map[i].str);
9180 +         }
9181 +     }
9182 +
9183 +     fprintf(stderr, "%s: ERROR: Illegal value for state_want: %d\n",
9184 +            __FUNCTION__, sw);
9185 +     return strdup("<STATE_WANT_UNKNOWN>");
9186 +}
9187 +
9188 +pkg_state_want_t pkg_state_want_from_str(char *str)
9189 +{
9190 +     int i;
9191 +
9192 +     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
9193 +         if (strcmp(str, pkg_state_want_map[i].str) == 0) {
9194 +              return pkg_state_want_map[i].value;
9195 +         }
9196 +     }
9197 +
9198 +     fprintf(stderr, "%s: ERROR: Illegal value for state_want string: %s\n",
9199 +            __FUNCTION__, str);
9200 +     return SW_UNKNOWN;
9201 +}
9202 +
9203 +char *pkg_state_flag_to_str(pkg_state_flag_t sf)
9204 +{
9205 +     int i;
9206 +     int len = 3; /* ok\000 is minimum */
9207 +     char *str = NULL;
9208 +
9209 +     /* clear the temporary flags before converting to string */
9210 +     sf &= SF_NONVOLATILE_FLAGS;
9211 +
9212 +     if (sf == 0) {
9213 +         return strdup("ok");
9214 +     } else {
9215 +
9216 +         for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9217 +              if (sf & pkg_state_flag_map[i].value) {
9218 +                   len += strlen(pkg_state_flag_map[i].str) + 1;
9219 +              }
9220 +         }
9221 +         str = malloc(len);
9222 +          if ( str == NULL ) {
9223 +             fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9224 +              return NULL;
9225 +          }
9226 +         str[0] = 0;
9227 +         for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9228 +              if (sf & pkg_state_flag_map[i].value) {
9229 +                   strcat(str, pkg_state_flag_map[i].str);
9230 +                   strcat(str, ",");
9231 +              }
9232 +         }
9233 +         len = strlen(str);
9234 +         str[len-1] = 0; /* squash last comma */
9235 +         return str;
9236 +     }
9237 +}
9238 +
9239 +pkg_state_flag_t pkg_state_flag_from_str(char *str)
9240 +{
9241 +     int i;
9242 +     int sf = SF_OK;
9243 +
9244 +     if (strcmp(str, "ok") == 0) {
9245 +         return SF_OK;
9246 +     }
9247 +     for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9248 +         const char *sfname = pkg_state_flag_map[i].str;
9249 +         int sfname_len = strlen(sfname);
9250 +         if (strncmp(str, sfname, sfname_len) == 0) {
9251 +              sf |= pkg_state_flag_map[i].value;
9252 +              str += sfname_len;
9253 +              if (str[0] == ',') {
9254 +                   str++;
9255 +              } else {
9256 +                   break;
9257 +              }
9258 +         }
9259 +     }
9260 +
9261 +     return sf;
9262 +}
9263 +
9264 +char *pkg_state_status_to_str(pkg_state_status_t ss)
9265 +{
9266 +     int i;
9267 +
9268 +     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9269 +         if (pkg_state_status_map[i].value == ss) {
9270 +              return strdup(pkg_state_status_map[i].str);
9271 +         }
9272 +     }
9273 +
9274 +     fprintf(stderr, "%s: ERROR: Illegal value for state_status: %d\n",
9275 +            __FUNCTION__, ss);
9276 +     return strdup("<STATE_STATUS_UNKNOWN>");
9277 +}
9278 +
9279 +pkg_state_status_t pkg_state_status_from_str(char *str)
9280 +{
9281 +     int i;
9282 +
9283 +     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9284 +         if (strcmp(str, pkg_state_status_map[i].str) == 0) {
9285 +              return pkg_state_status_map[i].value;
9286 +         }
9287 +     }
9288 +
9289 +     fprintf(stderr, "%s: ERROR: Illegal value for state_status string: %s\n",
9290 +            __FUNCTION__, str);
9291 +     return SS_NOT_INSTALLED;
9292 +}
9293 +
9294 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg)
9295 +{
9296 +     nv_pair_list_elt_t *l;
9297 +
9298 +     if (!pkg->architecture)
9299 +         return 1;
9300 +
9301 +     l = conf->arch_list.head;
9302 +
9303 +     while (l) {
9304 +         nv_pair_t *nv = l->data;
9305 +         if (strcmp(nv->name, pkg->architecture) == 0) {
9306 +              ipkg_message(conf, IPKG_DEBUG, "arch %s (priority %s) supported for pkg %s\n", nv->name, nv->value, pkg->name);
9307 +              return 1;
9308 +         }
9309 +         l = l->next;
9310 +     }
9311 +
9312 +     ipkg_message(conf, IPKG_DEBUG, "arch %s unsupported for pkg %s\n", pkg->architecture, pkg->name);
9313 +     return 0;
9314 +}
9315 +
9316 +int pkg_get_arch_priority(ipkg_conf_t *conf, const char *archname)
9317 +{
9318 +     nv_pair_list_elt_t *l;
9319 +
9320 +     l = conf->arch_list.head;
9321 +
9322 +     while (l) {
9323 +         nv_pair_t *nv = l->data;
9324 +         if (strcmp(nv->name, archname) == 0) {
9325 +              int priority = strtol(nv->value, NULL, 0);
9326 +              return priority;
9327 +         }
9328 +         l = l->next;
9329 +     }
9330 +     return 0;
9331 +}
9332 +
9333 +int pkg_info_preinstall_check(ipkg_conf_t *conf)
9334 +{
9335 +     int i;
9336 +     hash_table_t *pkg_hash = &conf->pkg_hash;
9337 +     pkg_vec_t *available_pkgs = pkg_vec_alloc();
9338 +     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9339 +
9340 +     ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: updating arch priority for each package\n");
9341 +     pkg_hash_fetch_available(pkg_hash, available_pkgs);
9342 +     /* update arch_priority for each package */
9343 +     for (i = 0; i < available_pkgs->len; i++) {
9344 +         pkg_t *pkg = available_pkgs->pkgs[i];
9345 +         int arch_priority = 1;
9346 +         if (!pkg)
9347 +              continue;
9348 +         // ipkg_message(conf, IPKG_DEBUG2, " package %s version=%s arch=%p:", pkg->name, pkg->version, pkg->architecture);
9349 +         if (pkg->architecture) 
9350 +              arch_priority = pkg_get_arch_priority(conf, pkg->architecture);
9351 +         else 
9352 +              ipkg_message(conf, IPKG_ERROR, "pkg_info_preinstall_check: no architecture for package %s\n", pkg->name);
9353 +         // ipkg_message(conf, IPKG_DEBUG2, "%s arch_priority=%d\n", pkg->architecture, arch_priority);
9354 +         pkg->arch_priority = arch_priority;
9355 +     }
9356 +
9357 +     for (i = 0; i < available_pkgs->len; i++) {
9358 +         pkg_t *pkg = available_pkgs->pkgs[i];
9359 +         if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
9360 +              /* clear flags and want for any uninstallable package */
9361 +              ipkg_message(conf, IPKG_NOTICE, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n", 
9362 +                           pkg->name, pkg->arch_priority, pkg->state_flag, pkg->state_want);
9363 +              pkg->state_want = SW_UNKNOWN;
9364 +              pkg->state_flag = 0;
9365 +         }
9366 +     }
9367 +     pkg_vec_free(available_pkgs);
9368 +
9369 +     /* update the file owner data structure */
9370 +     ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: update file owner list\n");
9371 +     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9372 +     for (i = 0; i < installed_pkgs->len; i++) {
9373 +         pkg_t *pkg = installed_pkgs->pkgs[i];
9374 +         str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
9375 +         str_list_elt_t *iter;
9376 +         if (installed_files == NULL) {
9377 +              ipkg_message(conf, IPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
9378 +              break;
9379 +         }
9380 +         for (iter = installed_files->head; iter; iter = iter->next) {
9381 +              char *installed_file = iter->data;
9382 +              // ipkg_message(conf, IPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
9383 +              file_hash_set_file_owner(conf, installed_file, pkg);
9384 +         }
9385 +     }
9386 +     pkg_vec_free(installed_pkgs);
9387 +
9388 +     return 0;
9389 +}
9390 +
9391 +struct pkg_write_filelist_data {
9392 +     ipkg_conf_t *conf;
9393 +     pkg_t *pkg;
9394 +     FILE *stream;
9395 +};
9396 +
9397 +void pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
9398 +{
9399 +     struct pkg_write_filelist_data *data = data_;
9400 +     pkg_t *entry = entry_;
9401 +     if (entry == data->pkg) {
9402 +         fprintf(data->stream, "%s\n", key);
9403 +     }
9404 +}
9405 +
9406 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg)
9407 +{
9408 +     struct pkg_write_filelist_data data;
9409 +     char *list_file_name = NULL;
9410 +     int err = 0;
9411 +
9412 +     if (!pkg) {
9413 +         ipkg_message(conf, IPKG_ERROR, "Null pkg\n");
9414 +         return -EINVAL;
9415 +     }
9416 +     ipkg_message(conf, IPKG_INFO,
9417 +                 "    creating %s.list file\n", pkg->name);
9418 +     sprintf_alloc(&list_file_name, "%s/%s.list", pkg->dest->info_dir, pkg->name);
9419 +     if (!list_file_name) {
9420 +         ipkg_message(conf, IPKG_ERROR, "Failed to alloc list_file_name\n");
9421 +         return -ENOMEM;
9422 +     }
9423 +     ipkg_message(conf, IPKG_INFO,
9424 +                 "    creating %s file for pkg %s\n", list_file_name, pkg->name);
9425 +     data.stream = fopen(list_file_name, "w");
9426 +     if (!data.stream) {
9427 +         ipkg_message(conf, IPKG_ERROR, "Could not open %s for writing: %s\n",
9428 +                      list_file_name, strerror(errno));
9429 +                      return errno;
9430 +     }
9431 +     data.pkg = pkg;
9432 +     data.conf = conf;
9433 +     hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
9434 +     fclose(data.stream);
9435 +     free(list_file_name);
9436 +
9437 +     return err;
9438 +}
9439 +
9440 +int pkg_write_changed_filelists(ipkg_conf_t *conf)
9441 +{
9442 +     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9443 +     hash_table_t *pkg_hash = &conf->pkg_hash;
9444 +     int i;
9445 +     int err;
9446 +     if (conf->noaction)
9447 +         return 0;
9448 +
9449 +     ipkg_message(conf, IPKG_INFO, "%s: saving changed filelists\n", __FUNCTION__);
9450 +     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9451 +     for (i = 0; i < installed_pkgs->len; i++) {
9452 +         pkg_t *pkg = installed_pkgs->pkgs[i];
9453 +         if (pkg->state_flag & SF_FILELIST_CHANGED) {
9454 +               ipkg_message(conf, IPKG_DEBUG, "Calling pkg_write_filelist for pkg=%s from %s\n", pkg->name, __FUNCTION__);
9455 +              err = pkg_write_filelist(conf, pkg);
9456 +              if (err)
9457 +                   ipkg_message(conf, IPKG_NOTICE, "pkg_write_filelist pkg=%s returned %d\n", pkg->name, err);
9458 +         }
9459 +     }
9460 +     return 0;
9461 +}
9462 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_depends.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.c
9463 --- busybox-1.2.0-orig/archival/libipkg/pkg_depends.c   1970-01-01 01:00:00.000000000 +0100
9464 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.c  2006-07-22 16:31:25.000000000 +0200
9465 @@ -0,0 +1,1033 @@
9466 +/* pkg_depends.c - the itsy package management system
9467 +
9468 +   Steven M. Ayer
9469 +   
9470 +   Copyright (C) 2002 Compaq Computer Corporation
9471 +
9472 +   This program is free software; you can redistribute it and/or
9473 +   modify it under the terms of the GNU General Public License as
9474 +   published by the Free Software Foundation; either version 2, or (at
9475 +   your option) any later version.
9476 +
9477 +   This program is distributed in the hope that it will be useful, but
9478 +   WITHOUT ANY WARRANTY; without even the implied warranty of
9479 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9480 +   General Public License for more details.
9481 +*/
9482 +
9483 +#include "ipkg.h"
9484 +#include <errno.h>
9485 +#include <ctype.h>
9486 +   
9487 +#include "pkg.h"
9488 +#include "ipkg_utils.h"
9489 +#include "pkg_hash.h"
9490 +#include "ipkg_message.h"
9491 +#include "pkg_parse.h"
9492 +#include "hash_table.h"
9493 +
9494 +static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str);
9495 +static depend_t * depend_init(void);
9496 +static void depend_deinit(depend_t *d);
9497 +static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
9498 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
9499 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
9500 +
9501 +static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
9502 +{
9503 +     depend_t *depend = (depend_t *)cdata;
9504 +     if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
9505 +         return 1;
9506 +     else
9507 +         return 0;
9508 +}
9509 +
9510 +static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
9511 +{
9512 +     depend_t *depend = (depend_t *)cdata;
9513 +#if 0
9514 +     pkg_t * temp = pkg_new();
9515 +     int comparison;
9516 +     parseVersion(temp, depend->version);
9517 +     comparison = pkg_compare_versions(pkg, temp);
9518 +     free(temp);
9519 +
9520 +     fprintf(stderr, "%s: pkg=%s pkg->version=%s constraint=%p type=%d version=%s comparison=%d satisfied=%d\n", 
9521 +            __FUNCTION__, pkg->name, pkg->version, 
9522 +            depend, depend->constraint, depend->version,
9523 +            comparison, version_constraints_satisfied(depend, pkg));
9524 +#endif
9525 +     if (version_constraints_satisfied(depend, pkg))
9526 +         return 1;
9527 +     else
9528 +         return 0;
9529 +}
9530 +
9531 +/* returns ndependences or negative error value */ 
9532 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, 
9533 +                                           pkg_vec_t *unsatisfied, char *** unresolved)
9534 +{
9535 +     pkg_t * satisfier_entry_pkg;
9536 +     register int i, j, k;
9537 +     int count, found;
9538 +     char ** the_lost;
9539 +     abstract_pkg_t * ab_pkg;
9540 +
9541 +     /* 
9542 +      * this is a setup to check for redundant/cyclic dependency checks, 
9543 +      * which are marked at the abstract_pkg level
9544 +      */
9545 +     if (!(ab_pkg = pkg->parent)) {
9546 +         fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
9547 +         *unresolved = NULL;
9548 +         return 0;
9549 +     }
9550 +     if (ab_pkg->dependencies_checked) {    /* avoid duplicate or cyclic checks */
9551 +         *unresolved = NULL;
9552 +         return 0;
9553 +     } else { 
9554 +         ab_pkg->dependencies_checked = 1;  /* mark it for subsequent visits */
9555 +     }
9556 +     /**/
9557 +
9558 +     count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
9559 +     if (!count){
9560 +         *unresolved = NULL;
9561 +         return 0;
9562 +     }
9563 +
9564 +     the_lost = NULL;
9565 +       
9566 +     /* foreach dependency */
9567 +     for (i = 0; i < count; i++) {
9568 +         compound_depend_t * compound_depend = &pkg->depends[i];
9569 +         depend_t ** possible_satisfiers = compound_depend->possibilities;;
9570 +         found = 0;
9571 +         satisfier_entry_pkg = NULL;
9572 +
9573 +         if (compound_depend->type == GREEDY_DEPEND) {
9574 +              /* foreach possible satisfier */
9575 +              for (j = 0; j < compound_depend->possibility_count; j++) {
9576 +                   /* foreach provided_by, which includes the abstract_pkg itself */
9577 +                   abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
9578 +                   abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
9579 +                   int nposs = ab_provider_vec->len;
9580 +                   abstract_pkg_t **ab_providers = ab_provider_vec->pkgs; 
9581 +                   int l;
9582 +                   for (l = 0; l < nposs; l++) {
9583 +                        pkg_vec_t *test_vec = ab_providers[l]->pkgs;
9584 +                        /* if no depends on this one, try the first package that Provides this one */
9585 +                        if (!test_vec){   /* no pkg_vec hooked up to the abstract_pkg!  (need another feed?) */
9586 +                             continue;
9587 +                        }
9588 +             
9589 +                        /* cruise this possiblity's pkg_vec looking for an installed version */
9590 +                        for (k = 0; k < test_vec->len; k++) {
9591 +                             pkg_t *pkg_scout = test_vec->pkgs[k];
9592 +                             /* not installed, and not already known about? */
9593 +                             if ((pkg_scout->state_want != SW_INSTALL)
9594 +                                 && !pkg_scout->parent->dependencies_checked
9595 +                                 && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
9596 +                                  char ** newstuff = NULL;
9597 +                                  int rc;
9598 +                                  pkg_vec_t *tmp_vec = pkg_vec_alloc ();
9599 +                                  /* check for not-already-installed dependencies */
9600 +                                  rc = pkg_hash_fetch_unsatisfied_dependencies(conf, 
9601 +                                                                               pkg_scout, 
9602 +                                                                               tmp_vec,
9603 +                                                                               &newstuff);
9604 +                                  if (newstuff == NULL) {
9605 +                                       int i;
9606 +                                       int ok = 1;
9607 +                                       for (i = 0; i < rc; i++) {
9608 +                                           pkg_t *p = tmp_vec->pkgs[i];
9609 +                                           if (p->state_want == SW_INSTALL)
9610 +                                               continue;
9611 +                                           ipkg_message(conf, IPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
9612 +                                           ok = 0;
9613 +                                           break;
9614 +                                       }
9615 +                                       pkg_vec_free (tmp_vec);
9616 +                                       if (ok) {
9617 +                                           /* mark this one for installation */
9618 +                                           ipkg_message(conf, IPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
9619 +                                           pkg_vec_insert(unsatisfied, pkg_scout);
9620 +                                       }
9621 +                                  } else  {
9622 +                                       ipkg_message(conf, IPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
9623 +                                       free (newstuff);
9624 +                                  }
9625 +                             }
9626 +                        }
9627 +                   }
9628 +              }
9629 +
9630 +              continue;
9631 +         }
9632 +
9633 +         /* foreach possible satisfier, look for installed package  */
9634 +         for (j = 0; j < compound_depend->possibility_count; j++) {
9635 +              /* foreach provided_by, which includes the abstract_pkg itself */
9636 +              depend_t *dependence_to_satisfy = possible_satisfiers[j];
9637 +              abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9638 +              pkg_t *satisfying_pkg = 
9639 +                   pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
9640 +                                                              pkg_installed_and_constraint_satisfied, 
9641 +                                                              dependence_to_satisfy, 1);
9642 +               /* Being that I can't test constraing in pkg_hash, I will test it here */
9643 +              if (satisfying_pkg != NULL) {
9644 +                  if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9645 +                     satisfying_pkg = NULL;
9646 +                  }
9647 +               }
9648 +              ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
9649 +              if (satisfying_pkg != NULL) {
9650 +                   found = 1;
9651 +                   break;
9652 +              }
9653 +
9654 +         }
9655 +         /* if nothing installed matches, then look for uninstalled satisfier */
9656 +         if (!found) {
9657 +              /* foreach possible satisfier, look for installed package  */
9658 +              for (j = 0; j < compound_depend->possibility_count; j++) {
9659 +                   /* foreach provided_by, which includes the abstract_pkg itself */
9660 +                   depend_t *dependence_to_satisfy = possible_satisfiers[j];
9661 +                   abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9662 +                   pkg_t *satisfying_pkg = 
9663 +                        pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
9664 +                                                                   pkg_constraint_satisfied, 
9665 +                                                                   dependence_to_satisfy, 1);
9666 +                    /* Being that I can't test constraing in pkg_hash, I will test it here too */
9667 +                   if (satisfying_pkg != NULL) {
9668 +                         if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9669 +                            satisfying_pkg = NULL;
9670 +                         }
9671 +                    }
9672 +
9673 +                   /* user request overrides package recommendation */
9674 +                   if (satisfying_pkg != NULL
9675 +                       && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
9676 +                       && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
9677 +                        ipkg_message (conf, IPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
9678 +                                      pkg->name, satisfying_pkg->name);
9679 +                        continue;
9680 +                   }
9681 +
9682 +                   ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
9683 +                   if (satisfying_pkg != NULL) {
9684 +                        satisfier_entry_pkg = satisfying_pkg;
9685 +                        break;
9686 +                   }
9687 +              }
9688 +         }
9689 +
9690 +         /* we didn't find one, add something to the unsatisfied vector */
9691 +         if (!found) {
9692 +              if (!satisfier_entry_pkg) {
9693 +                   /* failure to meet recommendations is not an error */
9694 +                   if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
9695 +                        the_lost = add_unresolved_dep(pkg, the_lost, i);
9696 +                   else
9697 +                        ipkg_message (conf, IPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
9698 +                                      pkg->name, compound_depend->possibilities[0]->pkg->name);
9699 +              }
9700 +              else {
9701 +                   if (compound_depend->type == SUGGEST) {
9702 +                        /* just mention it politely */
9703 +                        ipkg_message (conf, IPKG_NOTICE, "package %s suggests installing %s\n",
9704 +                                      pkg->name, satisfier_entry_pkg->name);
9705 +                   } else {
9706 +                        char ** newstuff = NULL;
9707 +                        
9708 +                        if (satisfier_entry_pkg != pkg &&
9709 +                            !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
9710 +                             pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
9711 +                             pkg_hash_fetch_unsatisfied_dependencies(conf, 
9712 +                                                                     satisfier_entry_pkg, 
9713 +                                                                     unsatisfied,
9714 +                                                                     &newstuff);
9715 +                             the_lost = merge_unresolved(the_lost, newstuff);
9716 +                        }
9717 +                   }
9718 +              }
9719 +         }
9720 +     }
9721 +     *unresolved = the_lost;
9722 +
9723 +     return unsatisfied->len;
9724 +}
9725 +
9726 +/*checking for conflicts !in replaces 
9727 +  If a packages conflicts with another but is also replacing it, I should not consider it a 
9728 +  really conflicts 
9729 +  returns 0 if conflicts <> replaces or 1 if conflicts == replaces 
9730 +*/
9731 +int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
9732 +{
9733 +    int i ;
9734 +    int replaces_count = pkg->replaces_count;
9735 +    abstract_pkg_t **replaces;
9736 +
9737 +    if (pkg->replaces_count==0)    // No replaces, it's surely a conflict
9738 +        return 0;
9739 +
9740 +    replaces = pkg->replaces;
9741 +
9742 +    for (i = 0; i < replaces_count; i++) {
9743 +        if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) {      // Found
9744 +            ipkg_message(NULL, IPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
9745 +            return 1;
9746 +        }
9747 +    }
9748 +    return 0;
9749 +
9750 +}
9751 +
9752 +
9753 +/* Abhaya: added support for conflicts */
9754 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
9755 +{
9756 +    pkg_vec_t * installed_conflicts, * test_vec;
9757 +    compound_depend_t * conflicts;
9758 +    depend_t ** possible_satisfiers;
9759 +    depend_t * possible_satisfier;
9760 +    register int i, j, k;
9761 +    int count;
9762 +    abstract_pkg_t * ab_pkg;
9763 +    pkg_t **pkg_scouts; 
9764 +    pkg_t *pkg_scout; 
9765 +
9766 +    /* 
9767 +     * this is a setup to check for redundant/cyclic dependency checks, 
9768 +     * which are marked at the abstract_pkg level
9769 +     */
9770 +    if(!(ab_pkg = pkg->parent)){
9771 +       fprintf(stderr, "dependency check error.  pkg %s isn't in hash table\n", pkg->name);
9772 +       return (pkg_vec_t *)NULL;
9773 +    }
9774 +
9775 +    conflicts = pkg->conflicts;
9776 +    if(!conflicts){
9777 +       return (pkg_vec_t *)NULL;
9778 +    }
9779 +    installed_conflicts = pkg_vec_alloc();
9780 +
9781 +    count = pkg->conflicts_count;
9782 +
9783 +
9784 +
9785 +    /* foreach conflict */
9786 +    for(i = 0; i < pkg->conflicts_count; i++){
9787 +
9788 +       possible_satisfiers = conflicts->possibilities;
9789 +
9790 +       /* foreach possible satisfier */
9791 +       for(j = 0; j < conflicts->possibility_count; j++){
9792 +            possible_satisfier = possible_satisfiers[j];
9793 +            if (!possible_satisfier)
9794 +                fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
9795 +            if (!possible_satisfier->pkg)
9796 +                fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
9797 +           test_vec = possible_satisfier->pkg->pkgs;
9798 +           if (test_vec) {
9799 +                /* pkg_vec found, it is an actual package conflict
9800 +                * cruise this possiblity's pkg_vec looking for an installed version */
9801 +               pkg_scouts = test_vec->pkgs;
9802 +               for(k = 0; k < test_vec->len; k++){
9803 +                    pkg_scout = pkg_scouts[k];
9804 +                    if (!pkg_scout) {
9805 +                        fprintf(stderr,  "%s: null pkg scout\n", __FUNCTION__);
9806 +                        continue; 
9807 +                    }
9808 +                   if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
9809 +                      version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
9810 +                       if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
9811 +                           pkg_vec_insert(installed_conflicts, pkg_scout);
9812 +                       }
9813 +                   }
9814 +               }
9815 +           }
9816 +       }
9817 +       conflicts++;
9818 +    }
9819 +
9820 +    if (installed_conflicts->len)
9821 +           return installed_conflicts;
9822 +    pkg_vec_free(installed_conflicts);
9823 +       return (pkg_vec_t *)NULL;
9824 +}
9825 +
9826 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
9827 +{
9828 +    pkg_t * temp;
9829 +    int comparison;
9830 +
9831 +    if(depends->constraint == NONE)
9832 +       return 1;
9833 +
9834 +    temp = pkg_new();
9835 +
9836 +    parseVersion(temp, depends->version);
9837 +
9838 +    comparison = pkg_compare_versions(pkg, temp);
9839 +
9840 +    free(temp);
9841 +
9842 +    if((depends->constraint == EARLIER) && 
9843 +       (comparison < 0))
9844 +       return 1;
9845 +    else if((depends->constraint == LATER) && 
9846 +           (comparison > 0))
9847 +       return 1;
9848 +    else if(comparison == 0)
9849 +       return 1;
9850 +    else if((depends->constraint == LATER_EQUAL) && 
9851 +           (comparison >= 0))
9852 +       return 1;
9853 +    else if((depends->constraint == EARLIER_EQUAL) && 
9854 +           (comparison <= 0))
9855 +       return 1;
9856 +    
9857 +    return 0;
9858 +}
9859 +
9860 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend)
9861 +{
9862 +     abstract_pkg_t *apkg = depend->pkg;
9863 +     abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9864 +     int n_providers = provider_apkgs->len;
9865 +     abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9866 +     pkg_vec_t *pkg_vec;
9867 +     int n_pkgs ; 
9868 +     int i;
9869 +     int j;
9870 +
9871 +     for (i = 0; i < n_providers; i++) {
9872 +         abstract_pkg_t *papkg = apkgs[i];
9873 +         pkg_vec = papkg->pkgs;
9874 +         if (pkg_vec) {
9875 +              n_pkgs = pkg_vec->len;
9876 +              for (j = 0; j < n_pkgs; j++) {
9877 +                   pkg_t *pkg = pkg_vec->pkgs[j];
9878 +                   if (version_constraints_satisfied(depend, pkg)) {
9879 +                        return 1;
9880 +                   }
9881 +              }
9882 +         }
9883 +     }
9884 +     return 0;
9885 +}
9886 +
9887 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend)
9888 +{
9889 +     abstract_pkg_t *apkg = depend->pkg;
9890 +     abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9891 +     int n_providers = provider_apkgs->len;
9892 +     abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9893 +     int i;
9894 +     int n_pkgs;
9895 +     int j;
9896 +
9897 +     for (i = 0; i < n_providers; i++) {
9898 +         abstract_pkg_t *papkg = apkgs[i];
9899 +         pkg_vec_t *pkg_vec = papkg->pkgs;
9900 +         if (pkg_vec) {
9901 +              n_pkgs = pkg_vec->len;
9902 +              for (j = 0; j < n_pkgs; j++) {
9903 +                   pkg_t *pkg = pkg_vec->pkgs[j];
9904 +                   if (version_constraints_satisfied(depend, pkg)) {
9905 +                        if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
9906 +                             return 1;
9907 +                   }
9908 +              }
9909 +         }
9910 +     }
9911 +     return 0;
9912 +}
9913 +
9914 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
9915 +{
9916 +    register int i;
9917 +    pkg_t ** pkgs = vec->pkgs;
9918 +
9919 +    for(i = 0; i < vec->len; i++)
9920 +       if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
9921 +          && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
9922 +          && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
9923 +           return 1;
9924 +    return 0;
9925 +}
9926 +
9927 +
9928 +#ifdef DeadCode
9929 +/**
9930 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
9931 + * the same abstract package and 0 otherwise.
9932 + */
9933 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
9934 +{
9935 +     abstract_pkg_t **provides = pkg->provides;
9936 +     int provides_count = pkg->provides_count;
9937 +     abstract_pkg_t **replacee_provides = replacee->provides;
9938 +     int replacee_provides_count = replacee->provides_count;
9939 +     int i, j;
9940 +     for (i = 0; i < provides_count; i++) {
9941 +         abstract_pkg_t *apkg = provides[i];
9942 +         for (j = 0; j < replacee_provides_count; j++) {
9943 +              abstract_pkg_t *replacee_apkg = replacee_provides[i];
9944 +              if (apkg == replacee_apkg)
9945 +                   return 1;
9946 +         }
9947 +     }
9948 +     return 0;
9949 +}
9950 +#endif
9951 +
9952 +/**
9953 + * pkg_provides_abstract returns 1 if pkg->provides contains providee
9954 + * and 0 otherwise.
9955 + */
9956 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
9957 +{
9958 +     abstract_pkg_t **provides = pkg->provides;
9959 +     int provides_count = pkg->provides_count;
9960 +     int i;
9961 +     for (i = 0; i < provides_count; i++) {
9962 +         if (provides[i] == providee)
9963 +              return 1;
9964 +     }
9965 +     return 0;
9966 +}
9967 +
9968 +/**
9969 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
9970 + * otherwise.
9971 + */
9972 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
9973 +{
9974 +     abstract_pkg_t **replaces = pkg->replaces;
9975 +     int replaces_count = pkg->replaces_count;
9976 +     /* abstract_pkg_t **replacee_provides = pkg->provides;
9977 +     int replacee_provides_count = pkg->provides_count; */
9978 +     int i, j;
9979 +     for (i = 0; i < replaces_count; i++) {
9980 +         abstract_pkg_t *abstract_replacee = replaces[i];
9981 +         for (j = 0; j < replaces_count; j++) {
9982 +   /*            ipkg_message(NULL, IPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
9983 +                 pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
9984 +              if (replacee->provides[j] == abstract_replacee)
9985 +                   return 1;
9986 +         }
9987 +     }
9988 +     return 0;
9989 +}
9990 +
9991 +
9992 +/**
9993 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
9994 + * otherwise.
9995 + */
9996 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
9997 +{
9998 +     compound_depend_t *conflicts = pkg->conflicts;
9999 +     int conflicts_count = pkg->conflicts_count;
10000 +     int i, j;
10001 +     for (i = 0; i < conflicts_count; i++) {
10002 +         int possibility_count = conflicts[i].possibility_count;
10003 +         struct depend **possibilities = conflicts[i].possibilities;
10004 +         for (j = 0; j < possibility_count; j++) {
10005 +              if (possibilities[j]->pkg == conflictee) {
10006 +                   return 1;
10007 +              }
10008 +         }
10009 +     }
10010 +     return 0;
10011 +}
10012 +
10013 +/**
10014 + * pkg_conflicts returns 1 if pkg->conflicts contains one of
10015 + * conflictee's provides and 0 otherwise.
10016 + */
10017 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
10018 +{
10019 +     compound_depend_t *conflicts = pkg->conflicts;
10020 +     int conflicts_count = pkg->conflicts_count;
10021 +     abstract_pkg_t **conflictee_provides = conflictee->provides;
10022 +     int conflictee_provides_count = conflictee->provides_count;
10023 +     int i, j, k;
10024 +     int possibility_count;
10025 +     struct depend **possibilities;
10026 +     abstract_pkg_t *possibility ;
10027 +
10028 +     for (i = 0; i < conflicts_count; i++) {
10029 +         possibility_count = conflicts[i].possibility_count;
10030 +         possibilities = conflicts[i].possibilities;
10031 +         for (j = 0; j < possibility_count; j++) {
10032 +              possibility = possibilities[j]->pkg;
10033 +              for (k = 0; k < conflictee_provides_count; k++) {
10034 +                   if (possibility == conflictee_provides[k]) {
10035 +                        return 1;
10036 +                   }
10037 +              }
10038 +         }
10039 +     }
10040 +     return 0;
10041 +}
10042 +
10043 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
10044 +{
10045 +    int oldlen = 0, newlen = 0;
10046 +    char ** result;
10047 +    register int i, j;
10048 +
10049 +    if(!newstuff)
10050 +       return oldstuff;
10051 +    
10052 +    while(oldstuff && oldstuff[oldlen]) oldlen++;
10053 +    while(newstuff && newstuff[newlen]) newlen++;
10054 +    
10055 +    result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
10056 +    if (result == NULL) {
10057 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10058 +        return NULL;
10059 +    }
10060 +    
10061 +    for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
10062 +       *(result + i) = *(newstuff + j);
10063 +    
10064 +    *(result + i) = NULL;
10065 +
10066 +    return result;
10067 +}
10068 +    
10069 +/* 
10070 + * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
10071 + * this is null terminated, no count is carried around 
10072 + */
10073 +char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
10074 +{
10075 +    int count;
10076 +    char ** resized;
10077 +    char *depend_str = pkg_depend_str(pkg, ref_ndx);
10078 +
10079 +    count = 0;
10080 +    while(the_lost && the_lost[count]) count++;
10081 +
10082 +    count++;  /* need one to hold the null */
10083 +    resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
10084 +    if (resized == NULL) {
10085 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10086 +       return NULL;
10087 +    }
10088 +    resized[count - 1] = strdup(depend_str);
10089 +    resized[count] = NULL;
10090 +    
10091 +    return resized;
10092 +}
10093 +       
10094 +void printDepends(pkg_t * pkg)
10095 +{
10096 +    register int i, j;
10097 +    compound_depend_t * depend;
10098 +    int count;
10099 +    
10100 +    count = pkg->pre_depends_count + pkg->depends_count;
10101 +    
10102 +    depend = pkg->depends;
10103 +    if(!depend){
10104 +       fprintf(stderr, "Depends pointer is NULL\n");
10105 +       return;
10106 +    }
10107 +    for(i = 0; i < count; i++){
10108 +       fprintf(stderr, "%s has %d possibilities:\n", 
10109 +               (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
10110 +               depend->possibility_count);
10111 +       for(j = 0; j < depend->possibility_count; j++)
10112 +           fprintf(stderr, "\t%s version %s (%d)\n",
10113 +                   depend->possibilities[j]->pkg->name,
10114 +                   depend->possibilities[j]->version,
10115 +                   depend->possibilities[j]->constraint);
10116 +       depend++;
10117 +    }
10118 +}
10119 +
10120 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
10121 +{
10122 +    register int i, j;
10123 +
10124 +    /* every pkg provides itself */
10125 +    abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
10126 +
10127 +    if (!pkg->provides_count)
10128 +      return 0;
10129 +
10130 +    pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1));
10131 +    if (pkg->provides == NULL) {
10132 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10133 +       return -1 ;
10134 +    }
10135 +    pkg->provides[0] = ab_pkg;
10136 +
10137 +    // if (strcmp(ab_pkg->name, pkg->name))
10138 +    //     fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
10139 +
10140 +    for(i = 0; i < pkg->provides_count; i++){
10141 +        abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
10142 +
10143 +       pkg->provides[i+1] = provided_abpkg;
10144 +
10145 +       j = 0;
10146 +       abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
10147 +    }
10148 +    return 0;
10149 +}
10150 +
10151 +/* Abhaya: added conflicts support */
10152 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
10153 +{
10154 +    register int i;
10155 +    compound_depend_t * conflicts;
10156 +
10157 +    if (!pkg->conflicts_count)
10158 +       return 0;
10159 +
10160 +    conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) *
10161 +                                       pkg->conflicts_count);
10162 +    if (conflicts == NULL) {
10163 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10164 +       return  -1;
10165 +    }
10166 +    for (i = 0; i < pkg->conflicts_count; i++) {
10167 +        conflicts->type = CONFLICTS;
10168 +        parseDepends(conflicts, hash,
10169 +                     pkg->conflicts_str[i]);
10170 +#if 0
10171 +        for (j = 0; j < conflicts->possibility_count; j++) {
10172 +             depend_t *possibility = conflicts->possibilities[j];
10173 +             abstract_pkg_t *conflicting_apkg = possibility->pkg;
10174 +             pkg_add_conflict_pair(ab_pkg, conflicting_apkg);
10175 +        }
10176 +#endif
10177 +        conflicts++;
10178 +    }
10179 +    return 0;
10180 +}
10181 +
10182 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
10183 +{
10184 +     register int i, j;
10185 +
10186 +     if (!pkg->replaces_count)
10187 +         return 0;
10188 +
10189 +     pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count);
10190 +     if (pkg->replaces == NULL) {
10191 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10192 +        return  -1;
10193 +     }
10194 +
10195 +     // if (strcmp(ab_pkg->name, pkg->name))
10196 +     //     fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
10197 +
10198 +     for(i = 0; i < pkg->replaces_count; i++){
10199 +         abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
10200 +
10201 +         pkg->replaces[i] = old_abpkg;
10202 +
10203 +         j = 0;
10204 +         if (!old_abpkg->replaced_by)
10205 +              old_abpkg->replaced_by = abstract_pkg_vec_alloc();
10206 +               if ( old_abpkg->replaced_by == NULL ){
10207 +                  return -1;
10208 +               }
10209 +         /* if a package pkg both replaces and conflicts old_abpkg,
10210 +          * then add it to the replaced_by vector so that old_abpkg
10211 +          * will be upgraded to ab_pkg automatically */
10212 +         if (pkg_conflicts_abstract(pkg, old_abpkg))
10213 +              abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
10214 +     }
10215 +     return 0;
10216 +}
10217 +
10218 +int buildDepends(hash_table_t * hash, pkg_t * pkg)
10219 +{
10220 +     int count;
10221 +     register int i;
10222 +     compound_depend_t * depends;
10223 +
10224 +     if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
10225 +         return 0;
10226 +
10227 +     if (0 && pkg->pre_depends_count)
10228 +         fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", 
10229 +                 pkg->name, pkg->pre_depends_count, pkg->depends_count);
10230 +     depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
10231 +     if (depends == NULL) {
10232 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10233 +        return  -1;
10234 +     }
10235 +     
10236 +
10237 +     for(i = 0; i < pkg->pre_depends_count; i++){
10238 +         parseDepends(depends, hash, pkg->pre_depends_str[i]);
10239 +         if (0 && pkg->pre_depends_count)
10240 +              fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n", 
10241 +                      pkg->pre_depends_str[i], depends, depends->possibility_count);
10242 +         depends->type = PREDEPEND;
10243 +         depends++;
10244 +     }
10245 +
10246 +     for(i = 0; i < pkg->recommends_count; i++){
10247 +         parseDepends(depends, hash, pkg->recommends_str[i]);
10248 +         if (0 && pkg->recommends_count)
10249 +              fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n", 
10250 +                      pkg->recommends_str[i], depends, depends->possibility_count);
10251 +         depends->type = RECOMMEND;
10252 +         depends++;
10253 +     }
10254 +
10255 +     for(i = 0; i < pkg->suggests_count; i++){
10256 +         parseDepends(depends, hash, pkg->suggests_str[i]);
10257 +         if (0 && pkg->suggests_count)
10258 +              fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n", 
10259 +                      pkg->suggests_str[i], depends, depends->possibility_count);
10260 +         depends->type = SUGGEST;
10261 +         depends++;
10262 +     }
10263 +
10264 +     for(i = 0; i < pkg->depends_count; i++){
10265 +         parseDepends(depends, hash, pkg->depends_str[i]);
10266 +         if (0 && pkg->depends_count)
10267 +              fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
10268 +                      pkg->depends_str[i], depends, depends->possibility_count);
10269 +         depends++;
10270 +     }
10271 +     return 0;
10272 +}    
10273 +
10274 +/*
10275 + * pkg_depend_string: returns the depends string specified by index.
10276 + *   All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
10277 + *   [0,npredepends) -> returns pre_depends_str[index]
10278 + *   [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
10279 + *   [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
10280 + *   [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
10281 + */
10282 +char *pkg_depend_str(pkg_t *pkg, int index)
10283 +{
10284 +     if (index < pkg->pre_depends_count) {
10285 +         return pkg->pre_depends_str[index];
10286 +     }
10287 +     index -= pkg->pre_depends_count;
10288 +
10289 +     if (index < pkg->recommends_count) {
10290 +         return pkg->recommends_str[index];
10291 +     }
10292 +     index -= pkg->recommends_count;
10293 +
10294 +     if (index < pkg->suggests_count) {
10295 +         return pkg->suggests_str[index];
10296 +     }
10297 +     index -= pkg->suggests_count;
10298 +
10299 +     if (index < pkg->depends_count) {
10300 +         return pkg->depends_str[index];
10301 +     }
10302 +     fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
10303 +     return NULL;
10304 +}
10305 +
10306 +void freeDepends(pkg_t *pkg)
10307 +{
10308 +    int i;
10309 +
10310 +    if (pkg == NULL || pkg->depends == NULL) {
10311 +       return;
10312 +    }
10313 +
10314 +    fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
10315 +    for (i=0; i < pkg->depends->possibility_count; i++) {
10316 +       depend_deinit(pkg->depends->possibilities[i]);
10317 +    }
10318 +    free(pkg->depends->possibilities);
10319 +    free(pkg->depends);
10320 +    pkg->depends = NULL;
10321 +}
10322 +
10323 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
10324 +{
10325 +     compound_depend_t * depends;
10326 +     int count, othercount;
10327 +     register int i, j;
10328 +     abstract_pkg_t * ab_depend;
10329 +     abstract_pkg_t ** temp;
10330 +
10331 +     count = pkg->pre_depends_count + pkg->depends_count;
10332 +     depends = pkg->depends;
10333 +
10334 +         if (0 && pkg->pre_depends_count)
10335 +              fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
10336 +                      pkg->name, pkg->pre_depends_count, pkg->depends_count);
10337 +     for (i = 0; i < count; i++) {
10338 +         if (0 && pkg->pre_depends_count)
10339 +              fprintf(stderr, "  i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
10340 +         for (j = 0; j < depends->possibility_count; j++){
10341 +              ab_depend = depends->possibilities[j]->pkg;
10342 +              if(!ab_depend->depended_upon_by)
10343 +                   ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
10344 +
10345 +              temp = ab_depend->depended_upon_by;
10346 +              othercount = 1;
10347 +              while(*temp){
10348 +                   temp++;
10349 +                   othercount++;
10350 +              }
10351 +              *temp = ab_pkg;
10352 +
10353 +              ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by, 
10354 +                                                                       (othercount + 1) * sizeof(abstract_pkg_t *));
10355 +              /* the array may have moved */
10356 +              temp = ab_depend->depended_upon_by + othercount;
10357 +              *temp = NULL;
10358 +         }
10359 +         depends++;
10360 +     }
10361 +}
10362 +
10363 +static depend_t * depend_init(void)
10364 +{
10365 +    depend_t * d = (depend_t *)malloc(sizeof(depend_t));    
10366 +    if ( d==NULL ){
10367 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10368 +        return NULL; 
10369 +     }
10370 +    d->constraint = NONE;
10371 +    d->version = NULL;
10372 +    d->pkg = NULL;
10373 +    
10374 +    return d;
10375 +}
10376 +
10377 +static void depend_deinit(depend_t *d)
10378 +{
10379 +    free(d);
10380 +}
10381 +
10382 +static int parseDepends(compound_depend_t *compound_depend, 
10383 +                       hash_table_t * hash, char * depend_str)
10384 +{
10385 +     char * pkg_name, buffer[2048];
10386 +     int num_of_ors = 0;
10387 +     register int i;
10388 +     register char * src, * dest;
10389 +     depend_t ** possibilities;
10390 +
10391 +     /* first count the number of ored possibilities for satisfying dependency */
10392 +     src = depend_str;
10393 +     while(*src)
10394 +         if(*src++ == '|')
10395 +              num_of_ors++;
10396 +
10397 +     compound_depend->type = DEPEND;
10398 +
10399 +     compound_depend->possibility_count = num_of_ors + 1;
10400 +     possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
10401 +     if (!possibilities)
10402 +         return -ENOMEM;
10403 +     compound_depend->possibilities = possibilities;
10404 +
10405 +     src = depend_str;
10406 +     for(i = 0; i < num_of_ors + 1; i++){
10407 +         possibilities[i] = depend_init();
10408 +          if (!possibilities[i])
10409 +              return -ENOMEM;
10410 +         /* gobble up just the name first */
10411 +         dest = buffer;
10412 +         while(*src &&
10413 +               !isspace(*src) &&
10414 +               (*src != '(') &&
10415 +               (*src != '*') &&
10416 +               (*src != '|'))
10417 +              *dest++ = *src++;
10418 +         *dest = '\0';
10419 +         pkg_name = trim_alloc(buffer);
10420 +          if (pkg_name == NULL )
10421 +              return -ENOMEM;
10422 +       
10423 +         /* now look at possible version info */
10424 +       
10425 +         /* skip to next chars */
10426 +         if(isspace(*src))
10427 +              while(*src && isspace(*src)) src++;
10428 +
10429 +         /* extract constraint and version */
10430 +         if(*src == '('){
10431 +              src++;
10432 +              if(!strncmp(src, "<<", 2)){
10433 +                   possibilities[i]->constraint = EARLIER;
10434 +                   src += 2;
10435 +              }
10436 +              else if(!strncmp(src, "<=", 2)){
10437 +                   possibilities[i]->constraint = EARLIER_EQUAL;
10438 +                   src += 2;
10439 +              }
10440 +              else if(!strncmp(src, ">=", 2)){
10441 +                   possibilities[i]->constraint = LATER_EQUAL;
10442 +                   src += 2;
10443 +              }
10444 +              else if(!strncmp(src, ">>", 2)){
10445 +                   possibilities[i]->constraint = LATER;
10446 +                   src += 2;
10447 +              }
10448 +              else if(!strncmp(src, "=", 1)){
10449 +                   possibilities[i]->constraint = EQUAL;
10450 +                   src++;
10451 +              }
10452 +              /* should these be here to support deprecated designations; dpkg does */
10453 +              else if(!strncmp(src, "<", 1)){
10454 +                   possibilities[i]->constraint = EARLIER_EQUAL;
10455 +                   src++;
10456 +              }
10457 +              else if(!strncmp(src, ">", 1)){
10458 +                   possibilities[i]->constraint = LATER_EQUAL;
10459 +                   src++; 
10460 +              }
10461 +
10462 +              /* now we have any constraint, pass space to version string */
10463 +              while(isspace(*src)) src++;
10464 +
10465 +              /* this would be the version string */
10466 +              dest = buffer;
10467 +              while(*src && *src != ')')
10468 +                   *dest++ = *src++;
10469 +              *dest = '\0';
10470 +           
10471 +              possibilities[i]->version = trim_alloc(buffer);
10472 +              /*                   fprintf(stderr, "let's print the depends version string:");
10473 +                                   fprintf(stderr, "version %s\n", possibilities[i]->version);*/
10474 +               if (possibilities[i]->version == NULL )
10475 +                   return -ENOMEM;
10476 +
10477 +        
10478 +         }
10479 +         /* hook up the dependency to its abstract pkg */
10480 +         possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
10481 +
10482 +         free(pkg_name);
10483 +       
10484 +         /* now get past the ) and any possible | chars */
10485 +         while(*src &&
10486 +               (isspace(*src) ||
10487 +                (*src == ')') ||
10488 +                (*src == '|')))
10489 +              src++;
10490 +         if (*src == '*')
10491 +         {
10492 +              compound_depend->type = GREEDY_DEPEND;
10493 +              src++;
10494 +         }
10495 +     }
10496 +
10497 +     return 0;
10498 +}
10499 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_depends.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.h
10500 --- busybox-1.2.0-orig/archival/libipkg/pkg_depends.h   1970-01-01 01:00:00.000000000 +0100
10501 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.h  2006-07-22 16:31:25.000000000 +0200
10502 @@ -0,0 +1,105 @@
10503 +/* pkg_depends.h - the itsy package management system
10504 +
10505 +   Steven M. Ayer
10506 +   
10507 +   Copyright (C) 2002 Compaq Computer Corporation
10508 +
10509 +   This program is free software; you can redistribute it and/or
10510 +   modify it under the terms of the GNU General Public License as
10511 +   published by the Free Software Foundation; either version 2, or (at
10512 +   your option) any later version.
10513 +
10514 +   This program is distributed in the hope that it will be useful, but
10515 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10516 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10517 +   General Public License for more details.
10518 +*/
10519 +
10520 +#ifndef PKG_DEPENDS_H
10521 +#define PKG_DEPENDS_H
10522 +
10523 +#include "pkg.h"
10524 +#include "pkg_hash.h"
10525 +
10526 +enum depend_type {
10527 +    PREDEPEND,
10528 +    DEPEND,
10529 +    CONFLICTS,
10530 +    GREEDY_DEPEND,
10531 +    RECOMMEND,
10532 +    SUGGEST
10533 +};
10534 +typedef enum depend_type depend_type_t;
10535 +
10536 +enum version_constraint {
10537 +    NONE,
10538 +    EARLIER,
10539 +    EARLIER_EQUAL,
10540 +    EQUAL,
10541 +    LATER_EQUAL,
10542 +    LATER
10543 +};
10544 +typedef enum version_constraint version_constraint_t;
10545 +
10546 +struct depend{
10547 +    version_constraint_t constraint;
10548 +    char * version;
10549 +    abstract_pkg_t * pkg;
10550 +};
10551 +typedef struct depend depend_t;
10552 +    
10553 +struct compound_depend{
10554 +    depend_type_t type;
10555 +    int possibility_count;
10556 +    struct depend ** possibilities;
10557 +};
10558 +typedef struct compound_depend compound_depend_t;
10559 +
10560 +#include "hash_table.h"
10561 +
10562 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10563 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10564 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10565 +int buildDepends(hash_table_t * hash, pkg_t * pkg);
10566 +
10567 +/**
10568 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
10569 + * the same abstract package and 0 otherwise.
10570 + */
10571 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee);
10572 +
10573 +/**
10574 + * pkg_provides returns 1 if pkg->provides contains providee and 0
10575 + * otherwise.
10576 + */
10577 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee);
10578 +
10579 +/**
10580 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
10581 + * otherwise.
10582 + */
10583 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee);
10584 +
10585 +/**
10586 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee provides and 0
10587 + * otherwise.
10588 + */
10589 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflicts);
10590 +
10591 +/**
10592 + * pkg_conflicts returns 1 if pkg->conflicts contains one of conflictee's provides and 0
10593 + * otherwise.
10594 + */
10595 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflicts);
10596 +
10597 +char *pkg_depend_str(pkg_t *pkg, int index);
10598 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg);
10599 +void freeDepends(pkg_t *pkg);
10600 +void printDepends(pkg_t * pkg);
10601 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg);
10602 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, pkg_vec_t *depends, char *** unresolved);
10603 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg);
10604 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend);
10605 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend);
10606 +
10607 +#endif
10608 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.c
10609 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest.c      1970-01-01 01:00:00.000000000 +0100
10610 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.c     2006-07-22 16:31:25.000000000 +0200
10611 @@ -0,0 +1,92 @@
10612 +/* pkg_dest.c - the itsy package management system
10613 +
10614 +   Carl D. Worth
10615 +
10616 +   Copyright (C) 2001 University of Southern California
10617 +
10618 +   This program is free software; you can redistribute it and/or
10619 +   modify it under the terms of the GNU General Public License as
10620 +   published by the Free Software Foundation; either version 2, or (at
10621 +   your option) any later version.
10622 +
10623 +   This program is distributed in the hope that it will be useful, but
10624 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10625 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10626 +   General Public License for more details.
10627 +*/
10628 +
10629 +#include "ipkg.h"
10630 +
10631 +#include "pkg_dest.h"
10632 +#include "file_util.h"
10633 +#include "str_util.h"
10634 +#include "sprintf_alloc.h"
10635 +
10636 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir)
10637 +{
10638 +    dest->name = strdup(name);
10639 +
10640 +    /* Guarantee that dest->root_dir ends with a '/' */
10641 +    if (str_ends_with(root_dir, "/")) {
10642 +       dest->root_dir = strdup(root_dir);
10643 +    } else {
10644 +       sprintf_alloc(&dest->root_dir, "%s/", root_dir);
10645 +    }
10646 +    file_mkdir_hier(dest->root_dir, 0755);
10647 +
10648 +    sprintf_alloc(&dest->ipkg_dir, "%s%s",
10649 +                 dest->root_dir, IPKG_STATE_DIR_PREFIX);
10650 +    file_mkdir_hier(dest->ipkg_dir, 0755);
10651 +
10652 +    if (str_starts_with (lists_dir, "/")) 
10653 +        sprintf_alloc(&dest->lists_dir, "%s", lists_dir);
10654 +    else
10655 +        sprintf_alloc(&dest->lists_dir, "/%s", lists_dir);
10656 +
10657 +    file_mkdir_hier(dest->lists_dir, 0755);
10658 +
10659 +    sprintf_alloc(&dest->info_dir, "%s/%s",
10660 +                 dest->ipkg_dir, IPKG_INFO_DIR_SUFFIX);
10661 +    file_mkdir_hier(dest->info_dir, 0755);
10662 +
10663 +    sprintf_alloc(&dest->status_file_name, "%s/%s",
10664 +                 dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10665 +
10666 +    sprintf_alloc(&dest->status_file_tmp_name, "%s/%s.tmp",
10667 +                 dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10668 +
10669 +    dest->status_file = NULL;
10670 +
10671 +    return 0;
10672 +}
10673 +
10674 +void pkg_dest_deinit(pkg_dest_t *dest)
10675 +{
10676 +    free(dest->name);
10677 +    dest->name = NULL;
10678 +
10679 +    free(dest->root_dir);
10680 +    dest->root_dir = NULL;
10681 +
10682 +    free(dest->ipkg_dir);
10683 +    dest->ipkg_dir = NULL;
10684 +
10685 +    free(dest->lists_dir);
10686 +    dest->lists_dir = NULL;
10687 +
10688 +    free(dest->info_dir);
10689 +    dest->info_dir = NULL;
10690 +
10691 +    free(dest->status_file_name);
10692 +    dest->status_file_name = NULL;
10693 +
10694 +    free(dest->status_file_tmp_name);
10695 +    dest->status_file_tmp_name = NULL;
10696 +
10697 +    if (dest->status_file) {
10698 +       fclose(dest->status_file);
10699 +    }
10700 +    dest->status_file = NULL;
10701 +
10702 +    dest->root_dir = NULL;
10703 +}
10704 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.h
10705 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest.h      1970-01-01 01:00:00.000000000 +0100
10706 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.h     2006-07-22 16:31:25.000000000 +0200
10707 @@ -0,0 +1,38 @@
10708 +/* pkg_dest.h - the itsy package management system
10709 +
10710 +   Carl D. Worth
10711 +
10712 +   Copyright (C) 2001 University of Southern California
10713 +
10714 +   This program is free software; you can redistribute it and/or
10715 +   modify it under the terms of the GNU General Public License as
10716 +   published by the Free Software Foundation; either version 2, or (at
10717 +   your option) any later version.
10718 +
10719 +   This program is distributed in the hope that it will be useful, but
10720 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10721 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10722 +   General Public License for more details.
10723 +*/
10724 +
10725 +#ifndef PKG_DEST_H
10726 +#define PKG_DEST_H
10727 +
10728 +typedef struct pkg_dest pkg_dest_t;
10729 +struct pkg_dest
10730 +{
10731 +    char *name;
10732 +    char *root_dir;
10733 +    char *ipkg_dir;
10734 +    char *lists_dir;
10735 +    char *info_dir;
10736 +    char *status_file_name;
10737 +    char *status_file_tmp_name;
10738 +    FILE *status_file;
10739 +};
10740 +
10741 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char *lists_dir);
10742 +void pkg_dest_deinit(pkg_dest_t *dest);
10743 +
10744 +#endif
10745 +
10746 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.c
10747 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.c 1970-01-01 01:00:00.000000000 +0100
10748 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.c        2006-07-22 16:31:25.000000000 +0200
10749 @@ -0,0 +1,85 @@
10750 +/* pkg_dest_list.c - the itsy package management system
10751 +
10752 +   Carl D. Worth
10753 +
10754 +   Copyright (C) 2001 University of Southern California
10755 +
10756 +   This program is free software; you can redistribute it and/or
10757 +   modify it under the terms of the GNU General Public License as
10758 +   published by the Free Software Foundation; either version 2, or (at
10759 +   your option) any later version.
10760 +
10761 +   This program is distributed in the hope that it will be useful, but
10762 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10763 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10764 +   General Public License for more details.
10765 +*/
10766 +
10767 +#include "ipkg.h"
10768 +
10769 +#include "pkg_dest.h"
10770 +#include "void_list.h"
10771 +#include "pkg_dest_list.h"
10772 +
10773 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data)
10774 +{
10775 +    return void_list_elt_init((void_list_elt_t *) elt, data);
10776 +}
10777 +
10778 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt)
10779 +{
10780 +    void_list_elt_deinit((void_list_elt_t *) elt);
10781 +}
10782 +
10783 +int pkg_dest_list_init(pkg_dest_list_t *list)
10784 +{
10785 +    return void_list_init((void_list_t *) list);
10786 +}
10787 +
10788 +void pkg_dest_list_deinit(pkg_dest_list_t *list)
10789 +{
10790 +    pkg_dest_list_elt_t *iter;
10791 +    pkg_dest_t *pkg_dest;
10792 +
10793 +    for (iter = list->head; iter; iter = iter->next) {
10794 +       pkg_dest = iter->data;
10795 +       pkg_dest_deinit(pkg_dest);
10796 +
10797 +       /* malloced in pkg_dest_list_append */
10798 +       free(pkg_dest);
10799 +       iter->data = NULL;
10800 +    }
10801 +    void_list_deinit((void_list_t *) list);
10802 +}
10803 +
10804 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10805 +                                const char *root_dir,const char *lists_dir)
10806 +{
10807 +    int err;
10808 +    pkg_dest_t *pkg_dest;
10809 +
10810 +    /* freed in plg_dest_list_deinit */
10811 +    pkg_dest = malloc(sizeof(pkg_dest_t));
10812 +    if (pkg_dest == NULL) {
10813 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10814 +       return NULL;
10815 +    }
10816 +
10817 +    pkg_dest_init(pkg_dest, name, root_dir,lists_dir);
10818 +    err = void_list_append((void_list_t *) list, pkg_dest);
10819 +    if (err) {
10820 +       return NULL;
10821 +    }
10822 +
10823 +    return pkg_dest;
10824 +}
10825 +
10826 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data)
10827 +{
10828 +    return void_list_push((void_list_t *) list, data);
10829 +}
10830 +
10831 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list)
10832 +{
10833 +    return (pkg_dest_list_elt_t *) void_list_pop((void_list_t *) list);
10834 +}
10835 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.h
10836 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.h 1970-01-01 01:00:00.000000000 +0100
10837 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.h        2006-07-22 16:31:25.000000000 +0200
10838 @@ -0,0 +1,50 @@
10839 +/* pkg_dest_list.h - the itsy package management system
10840 +
10841 +   Carl D. Worth
10842 +
10843 +   Copyright (C) 2001 University of Southern California
10844 +
10845 +   This program is free software; you can redistribute it and/or
10846 +   modify it under the terms of the GNU General Public License as
10847 +   published by the Free Software Foundation; either version 2, or (at
10848 +   your option) any later version.
10849 +
10850 +   This program is distributed in the hope that it will be useful, but
10851 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10852 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10853 +   General Public License for more details.
10854 +*/
10855 +
10856 +#ifndef PKG_DEST_LIST_H
10857 +#define PKG_DEST_LIST_H
10858 +
10859 +#include "pkg_dest.h"
10860 +
10861 +typedef struct pkg_dest_list_elt pkg_dest_list_elt_t;
10862 +struct pkg_dest_list_elt
10863 +{
10864 +    pkg_dest_list_elt_t *next;
10865 +    pkg_dest_t *data;
10866 +};
10867 +
10868 +typedef struct pkg_dest_list pkg_dest_list_t;
10869 +struct pkg_dest_list
10870 +{
10871 +    pkg_dest_list_elt_t pre_head;
10872 +    pkg_dest_list_elt_t *head;
10873 +    pkg_dest_list_elt_t *tail;
10874 +};
10875 +
10876 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data);
10877 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt);
10878 +
10879 +int pkg_dest_list_init(pkg_dest_list_t *list);
10880 +void pkg_dest_list_deinit(pkg_dest_list_t *list);
10881 +
10882 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10883 +                                const char *root_dir,const char* lists_dir);
10884 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data);
10885 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list);
10886 +
10887 +#endif
10888 +
10889 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_extract.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.c
10890 --- busybox-1.2.0-orig/archival/libipkg/pkg_extract.c   1970-01-01 01:00:00.000000000 +0100
10891 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.c  2006-07-30 22:47:49.000000000 +0200
10892 @@ -0,0 +1,224 @@
10893 +/* pkg_extract.c - the itsy package management system
10894 +
10895 +   Carl D. Worth
10896 +
10897 +   Copyright (C) 2001 University of Southern California
10898 +
10899 +   This program is free software; you can redistribute it and/or
10900 +   modify it under the terms of the GNU General Public License as
10901 +   published by the Free Software Foundation; either version 2, or (at
10902 +   your option) any later version.
10903 +
10904 +   This program is distributed in the hope that it will be useful, but
10905 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10906 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10907 +   General Public License for more details.
10908 +*/
10909 +
10910 +#include "ipkg.h"
10911 +#include <errno.h>
10912 +#include <fcntl.h>
10913 +#include <stdio.h>
10914 +
10915 +#include "pkg_extract.h"
10916 +
10917 +#include "libbb.h"
10918 +#include "file_util.h"
10919 +#include "sprintf_alloc.h"
10920 +#include "unarchive.h"
10921 +
10922 +#define IPKG_CONTROL_ARCHIVE  "control.tar.gz"
10923 +#define IPKG_DATA_ARCHIVE  "data.tar.gz"
10924 +#define IPKG_CONTROL_FILE  "control"
10925 +
10926 +static void extract_ipkg_file_to_dir(pkg_t *pkg, const char *dir, const char *filename)
10927 +{
10928 +       archive_handle_t *archive;
10929 +       char *path;
10930 +
10931 +       sprintf_alloc(&path, "%s/", dir);
10932 +       archive = init_handle();
10933 +       archive->src_fd = bb_xopen(pkg->local_filename, O_RDONLY);
10934 +       archive->filter = filter_accept_list;
10935 +       llist_add_to(&(archive->accept), (char *)filename);
10936 +       archive->buffer = path;
10937 +       archive->action_data = data_extract_all_prefix;
10938 +       archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
10939 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10940 +       close(archive->src_fd);
10941 +       free(archive->accept);
10942 +       free(archive);
10943 +       free(path);
10944 +}
10945 +
10946 +static void data_extract_file_name_to_buffer(archive_handle_t *archive)
10947 +{
10948 +       unsigned int size = strlen(archive->file_header->name) + 2;
10949 +
10950 +       if (archive->buffer == NULL) {
10951 +               archive->buffer = xmalloc(size);
10952 +               strcpy(archive->buffer, archive->file_header->name);
10953 +       } else {
10954 +               size += strlen(archive->buffer);
10955 +               archive->buffer = xrealloc(archive->buffer, size);
10956 +               strcat(archive->buffer, archive->file_header->name);
10957 +       } 
10958 +       strcat(archive->buffer, "\n");
10959 +       data_skip(archive);
10960 +}
10961 +
10962 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
10963 +{
10964 +       archive_handle_t *archive;
10965 +       char *name;
10966 +
10967 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE); 
10968 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10969 +       archive = init_handle();
10970 +       archive->src_fd = bb_xopen(name, O_RDONLY);
10971 +       archive->filter = filter_accept_list;
10972 +       llist_add_to(&(archive->accept), "./" IPKG_CONTROL_FILE);
10973 +       archive->action_data = data_extract_to_buffer;
10974 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10975 +       close(archive->src_fd);
10976 +       fputs(archive->buffer, stream);
10977 +       free(archive->buffer);
10978 +       free(archive->accept);
10979 +       free(archive);
10980 +       free(name);
10981 +
10982 +       return 0;
10983 +}
10984 +
10985 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
10986 +{
10987 +    return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
10988 +}
10989 +
10990 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir, const char *prefix)
10991 +{
10992 +       archive_handle_t *archive;
10993 +       char *name;
10994 +       char *path;
10995 +
10996 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE); 
10997 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10998 +       sprintf_alloc(&path, "%s/%s", dir, prefix);
10999 +       archive = init_handle();
11000 +       archive->src_fd = bb_xopen(name, O_RDONLY);
11001 +       archive->filter = filter_accept_all;
11002 +       archive->buffer = path;
11003 +       archive->action_data = data_extract_all_prefix;
11004 +       archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
11005 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
11006 +       close(archive->src_fd);
11007 +       free(archive);
11008 +       free(path);
11009 +       free(name);
11010 +
11011 +       return 0;
11012 +}
11013 +
11014 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
11015 +{
11016 +       archive_handle_t *archive;
11017 +       char *name;
11018 +       char *path;
11019 +
11020 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
11021 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
11022 +       sprintf_alloc(&path, "%s/", dir);
11023 +       archive = init_handle();
11024 +       archive->src_fd = bb_xopen(name, O_RDONLY);
11025 +       archive->filter = filter_accept_all;
11026 +       archive->buffer = path;
11027 +       archive->action_data = data_extract_all_prefix;
11028 +       archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
11029 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
11030 +       close(archive->src_fd);
11031 +       free(archive);
11032 +       free(path);
11033 +       free(name);
11034 +
11035 +       return 0;
11036 +}
11037 +
11038 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
11039 +{
11040 +     int err=0;
11041 +     char *line, *data_file;
11042 +     FILE *file;
11043 +     FILE *tmp;
11044 +
11045 +     file = fopen(file_name, "w");
11046 +     if (file == NULL) {
11047 +         fprintf(stderr, "%s: ERROR: Failed to open %s for writing.\n",
11048 +                 __FUNCTION__, file_name);
11049 +         return EINVAL;
11050 +     }
11051 +
11052 +     tmp = tmpfile();
11053 +     if (pkg->installed_files) {
11054 +         str_list_elt_t *elt;
11055 +         for (elt = pkg->installed_files->head; elt; elt = elt->next) {
11056 +              fprintf(file, "%s\n", elt->data);
11057 +         }
11058 +     } else {
11059 +         err = pkg_extract_data_file_names_to_stream(pkg, tmp);
11060 +         if (err) {
11061 +              fclose(file);
11062 +              fclose(tmp);
11063 +              return err;
11064 +         }
11065 +
11066 +         /* Fixup data file names by removing the initial '.' */
11067 +         rewind(tmp);
11068 +         while (1) {
11069 +              line = file_read_line_alloc(tmp);
11070 +              if (line == NULL) {
11071 +                   break;
11072 +              }
11073 +
11074 +              data_file = line;
11075 +              if (*data_file == '.') {
11076 +                   data_file++;
11077 +              }
11078 +
11079 +              if (*data_file != '/') {
11080 +                   fputs("/", file);
11081 +              }
11082 +
11083 +              /* I have no idea why, but this is what dpkg does */
11084 +              if (strcmp(data_file, "/\n") == 0) {
11085 +                   fputs("/.\n", file);
11086 +              } else {
11087 +                   fputs(data_file, file);
11088 +              }
11089 +         }
11090 +     }
11091 +     fclose(tmp);
11092 +     fclose(file);
11093 +
11094 +     return err;
11095 +}
11096 +
11097 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
11098 +{
11099 +       archive_handle_t *archive;
11100 +       char *name;
11101 +       
11102 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
11103 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
11104 +       archive = init_handle();
11105 +       archive->src_fd = bb_xopen(name, O_RDONLY);
11106 +       archive->filter = filter_accept_all;
11107 +       archive->action_data = data_extract_file_name_to_buffer;
11108 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
11109 +       close(archive->src_fd);
11110 +       fputs(archive->buffer, file);
11111 +       free(archive->buffer);
11112 +       free(archive);
11113 +       free(name);
11114 +       
11115 +       return 0;
11116 +}
11117 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_extract.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.h
11118 --- busybox-1.2.0-orig/archival/libipkg/pkg_extract.h   1970-01-01 01:00:00.000000000 +0100
11119 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.h  2006-07-22 16:31:25.000000000 +0200
11120 @@ -0,0 +1,32 @@
11121 +/* pkg_extract.c - the itsy package management system
11122 +
11123 +   Carl D. Worth
11124 +
11125 +   Copyright (C) 2001 University of Southern California
11126 +
11127 +   This program is free software; you can redistribute it and/or
11128 +   modify it under the terms of the GNU General Public License as
11129 +   published by the Free Software Foundation; either version 2, or (at
11130 +   your option) any later version.
11131 +
11132 +   This program is distributed in the hope that it will be useful, but
11133 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11134 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11135 +   General Public License for more details.
11136 +*/
11137 +
11138 +#ifndef PKG_EXTRACT_H
11139 +#define PKG_EXTRACT_H
11140 +
11141 +#include "pkg.h"
11142 +
11143 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream);
11144 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir);
11145 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
11146 +                                                const char *dir,
11147 +                                                const char *prefix);
11148 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir);
11149 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name);
11150 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file);
11151 +
11152 +#endif
11153 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.h
11154 --- busybox-1.2.0-orig/archival/libipkg/pkg.h   1970-01-01 01:00:00.000000000 +0100
11155 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.h  2006-07-22 16:31:25.000000000 +0200
11156 @@ -0,0 +1,232 @@
11157 +/* pkg.h - the itsy package management system
11158 +
11159 +   Carl D. Worth
11160 +
11161 +   Copyright (C) 2001 University of Southern California
11162 +
11163 +   This program is free software; you can redistribute it and/or
11164 +   modify it under the terms of the GNU General Public License as
11165 +   published by the Free Software Foundation; either version 2, or (at
11166 +   your option) any later version.
11167 +
11168 +   This program is distributed in the hope that it will be useful, but
11169 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11170 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11171 +   General Public License for more details.
11172 +*/
11173 +
11174 +#ifndef PKG_H
11175 +#define PKG_H
11176 +
11177 +#include <sys/types.h>
11178 +#include <sys/stat.h>
11179 +#include <unistd.h>
11180 +
11181 +#include "pkg_vec.h"
11182 +#include "str_list.h"
11183 +#include "pkg_src.h"
11184 +#include "pkg_dest.h"
11185 +#include "ipkg_conf.h"
11186 +#include "conffile_list.h"
11187 +
11188 +struct ipkg_conf;
11189 +
11190 +
11191 +#define ARRAY_SIZE(array) sizeof(array) / sizeof((array)[0])
11192 +
11193 +/* I think "Size" is currently the shortest field name */
11194 +#define PKG_MINIMUM_FIELD_NAME_LEN 4
11195 +
11196 +enum pkg_state_want
11197 +{
11198 +    SW_UNKNOWN = 1,
11199 +    SW_INSTALL,
11200 +    SW_DEINSTALL,
11201 +    SW_PURGE,
11202 +    SW_LAST_STATE_WANT
11203 +};
11204 +typedef enum pkg_state_want pkg_state_want_t;
11205 +
11206 +enum pkg_state_flag
11207 +{
11208 +    SF_OK        = 0,
11209 +    SF_REINSTREQ = 1,
11210 +    SF_HOLD      = 2,  /* do not upgrade version */
11211 +    SF_REPLACE   = 4,  /* replace this package */
11212 +    SF_NOPRUNE   = 8,  /* do not remove obsolete files */
11213 +    SF_PREFER    = 16, /* prefer this version */
11214 +    SF_OBSOLETE  = 32, /* old package in upgrade pair */
11215 +    SF_MARKED    = 64, /* temporary mark */
11216 +    SF_FILELIST_CHANGED = 128, /* needs filelist written */
11217 +    SF_USER      = 256,
11218 +    SF_LAST_STATE_FLAG
11219 +};
11220 +typedef enum pkg_state_flag pkg_state_flag_t;
11221 +#define SF_NONVOLATILE_FLAGS (SF_HOLD|SF_NOPRUNE|SF_PREFER|SF_OBSOLETE|SF_USER)
11222 +
11223 +enum pkg_state_status
11224 +{
11225 +    SS_NOT_INSTALLED = 1,
11226 +    SS_UNPACKED,
11227 +    SS_HALF_CONFIGURED,
11228 +    SS_INSTALLED,
11229 +    SS_HALF_INSTALLED,
11230 +    SS_CONFIG_FILES,
11231 +    SS_POST_INST_FAILED,
11232 +    SS_REMOVAL_FAILED,
11233 +    SS_LAST_STATE_STATUS
11234 +};
11235 +typedef enum pkg_state_status pkg_state_status_t;
11236 +
11237 +struct abstract_pkg{
11238 +    char * name;
11239 +    int dependencies_checked;
11240 +    pkg_vec_t * pkgs;
11241 +    pkg_state_status_t state_status;
11242 +    pkg_state_flag_t state_flag;
11243 +    struct abstract_pkg ** depended_upon_by; /* @@@@ this should be abstract_pkg_vec_t -Jamey */
11244 +    abstract_pkg_vec_t * provided_by;
11245 +    abstract_pkg_vec_t * replaced_by;
11246 +};
11247 +
11248 +#include "pkg_depends.h"
11249 +
11250 +/* XXX: CLEANUP: I'd like to clean up pkg_t in several ways:
11251 +
11252 +   The 3 version fields should go into a single version struct. (This
11253 +   is especially important since, currently, pkg->version can easily
11254 +   be mistaken for pkg_verson_str_alloc(pkg) although they are very
11255 +   distinct. This has been the source of multiple bugs.
11256 +
11257 +   The 3 state fields could possibly also go into their own struct.
11258 +
11259 +   All fields which deal with lists of packages, (Depends,
11260 +   Pre-Depends, Provides, Suggests, Recommends, Enhances), should each
11261 +   be handled by a single struct in pkg_t
11262 +
11263 +   All string fields for which there is a small set of possible
11264 +   values, (section, maintainer, architecture, maybe version?), that
11265 +   are reused among different packages -- for all such packages we
11266 +   should move from "char *"s to some atom datatype to share data
11267 +   storage and use less memory. We might even do reference counting,
11268 +   but probably not since most often we only create new pkg_t structs,
11269 +   we don't often free them.  */
11270 +struct pkg
11271 +{
11272 +     char *name;
11273 +     unsigned long epoch;
11274 +     char *version;
11275 +     char *revision;
11276 +     char *familiar_revision;
11277 +     pkg_src_t *src;
11278 +     pkg_dest_t *dest;
11279 +     char *architecture;
11280 +     char *section;
11281 +     char *maintainer;
11282 +     char *description;
11283 +     pkg_state_want_t state_want;
11284 +     pkg_state_flag_t state_flag;
11285 +     pkg_state_status_t state_status;
11286 +     char **depends_str;
11287 +     int depends_count;
11288 +     char **pre_depends_str;
11289 +     int pre_depends_count;
11290 +     char **recommends_str;
11291 +     int recommends_count;
11292 +     char **suggests_str;
11293 +     int suggests_count;
11294 +     compound_depend_t * depends;
11295 +
11296 +     /* Abhaya: new conflicts */
11297 +     char **conflicts_str;
11298 +     compound_depend_t * conflicts;
11299 +     int conflicts_count;
11300 +       
11301 +     char **replaces_str;
11302 +     int replaces_count;
11303 +     abstract_pkg_t ** replaces;
11304 +
11305 +     char **provides_str;
11306 +     int provides_count;
11307 +     abstract_pkg_t ** provides;
11308 +
11309 +     abstract_pkg_t *parent;
11310 +
11311 +     pkg_t *old_pkg; /* during upgrade, points from installee to previously installed */
11312 +
11313 +     char *filename;
11314 +     char *local_filename;
11315 +     char *url;
11316 +     char *tmp_unpack_dir;
11317 +     char *md5sum;
11318 +     char *size;
11319 +     char *installed_size;
11320 +     char *priority;
11321 +     char *source;
11322 +     conffile_list_t conffiles;
11323 +     time_t installed_time;
11324 +     /* As pointer for lazy evaluation */
11325 +     str_list_t *installed_files;
11326 +     /* XXX: CLEANUP: I'd like to perhaps come up with a better
11327 +       mechanism to avoid the problem here, (which is that the
11328 +       installed_files list was being freed from an inner loop while
11329 +       still being used within an outer loop. */
11330 +     int installed_files_ref_cnt;
11331 +     int essential;
11332 +     int arch_priority;
11333 +/* Adding this flag, to "force" ipkg to choose a "provided_by_hand" package, if there are multiple choice */
11334 +     int provided_by_hand;
11335 +};
11336 +
11337 +pkg_t *pkg_new(void);
11338 +int pkg_init(pkg_t *pkg);
11339 +void pkg_deinit(pkg_t *pkg);
11340 +int pkg_init_from_file(pkg_t *pkg, const char *filename);
11341 +abstract_pkg_t *abstract_pkg_new(void);
11342 +int abstract_pkg_init(abstract_pkg_t *ab_pkg);
11343 +
11344 +/* 
11345 + * merges fields from newpkg into oldpkg.
11346 + * Forcibly sets oldpkg state_status, state_want and state_flags if set_status is nonzero 
11347 + */
11348 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status);
11349 +
11350 +char *pkg_version_str_alloc(pkg_t *pkg);
11351 +
11352 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg);
11353 +int pkg_name_version_and_architecture_compare(void *a, void *b);
11354 +int abstract_pkg_name_compare(void *a, void *b);
11355 +
11356 +char * pkg_formatted_info(pkg_t *pkg );
11357 +char * pkg_formatted_field(pkg_t *pkg, const char *field );
11358 +
11359 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg);
11360 +
11361 +void pkg_print_info(pkg_t *pkg, FILE *file);
11362 +void pkg_print_status(pkg_t * pkg, FILE * file);
11363 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field);
11364 +str_list_t *pkg_get_installed_files(pkg_t *pkg);
11365 +int pkg_free_installed_files(pkg_t *pkg);
11366 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg);
11367 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name);
11368 +int pkg_run_script(struct ipkg_conf *conf, pkg_t *pkg,
11369 +                  const char *script, const char *args);
11370 +
11371 +/* enum mappings */
11372 +char *pkg_state_want_to_str(pkg_state_want_t sw);
11373 +pkg_state_want_t pkg_state_want_from_str(char *str);
11374 +char *pkg_state_flag_to_str(pkg_state_flag_t sf);
11375 +pkg_state_flag_t pkg_state_flag_from_str(char *str);
11376 +char *pkg_state_status_to_str(pkg_state_status_t ss);
11377 +pkg_state_status_t pkg_state_status_from_str(char *str);
11378 +
11379 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op);
11380 +
11381 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg);
11382 +int pkg_info_preinstall_check(ipkg_conf_t *conf);
11383 +int pkg_free_installed_files(pkg_t *pkg);
11384 +
11385 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg);
11386 +int pkg_write_changed_filelists(ipkg_conf_t *conf);
11387 +
11388 +#endif
11389 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_hash.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.c
11390 --- busybox-1.2.0-orig/archival/libipkg/pkg_hash.c      1970-01-01 01:00:00.000000000 +0100
11391 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.c     2006-07-22 16:31:25.000000000 +0200
11392 @@ -0,0 +1,617 @@
11393 +/* ipkg_hash.c - the itsy package management system
11394 +
11395 +   Steven M. Ayer
11396 +   
11397 +   Copyright (C) 2002 Compaq Computer Corporation
11398 +
11399 +   This program is free software; you can redistribute it and/or
11400 +   modify it under the terms of the GNU General Public License as
11401 +   published by the Free Software Foundation; either version 2, or (at
11402 +   your option) any later version.
11403 +
11404 +   This program is distributed in the hope that it will be useful, but
11405 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11406 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11407 +   General Public License for more details.
11408 +*/
11409 +
11410 +#include "ipkg.h"
11411 +#include <errno.h>
11412 +#include <ctype.h>
11413 +#include <stdlib.h>
11414 +#include <string.h>
11415 +
11416 +#include "hash_table.h"
11417 +#include "pkg.h"
11418 +#include "ipkg_message.h"
11419 +#include "pkg_vec.h"
11420 +#include "pkg_hash.h"
11421 +#include "pkg_parse.h"
11422 +#include "ipkg_utils.h"
11423 +
11424 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
11425 +
11426 +/*
11427 + * this will talk to both feeds-lists files and installed status files
11428 + * example api:
11429 + *
11430 + * hash_table_t hash;
11431 + * pkg_hash_init(name, &hash, 1000);
11432 + * pkg_hash_add_from_file(<feed filename>);
11433 + *
11434 + * the query function is just there as a shell to prove to me that this
11435 + * sort of works, but isn't far from doing something useful
11436 + * 
11437 + * -sma, 12/21/01
11438 + * modified: CDW 3 Jan. 2002
11439 + */
11440 +
11441 +
11442 +\f
11443 +int pkg_hash_init(const char *name, hash_table_t *hash, int len)
11444 +{
11445 +  return hash_table_init(name, hash, len);
11446 +}
11447 +
11448 +void pkg_hash_deinit(hash_table_t *hash)
11449 +{
11450 +  hash_table_deinit(hash);
11451 +}
11452 +
11453 +
11454 +/* Find the default arch for a given package status file if none is given. */
11455 +static char *pkg_get_default_arch(ipkg_conf_t *conf)
11456 +{
11457 +     nv_pair_list_elt_t *l;
11458 +     char *def_arch = HOST_CPU_STR;            /* Default arch */
11459 +     int def_prio = 0;                         /* Other archs override this */
11460 +
11461 +     l = conf->arch_list.head;
11462 +
11463 +     while (l) {
11464 +         nv_pair_t *nv = l->data;
11465 +         int priority = strtol(nv->value, NULL, 0);
11466 +
11467 +         /* Check if this arch has higher priority, and is valid */
11468 +         if ((priority > def_prio) &&
11469 +             (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
11470 +              /* Our new default */
11471 +              def_prio = priority;
11472 +              def_arch = nv->name;
11473 +         }
11474 +         l = l->next;
11475 +     }
11476 +
11477 +     return strdup(def_arch);
11478 +}
11479 +
11480 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
11481 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
11482 +{
11483 +     hash_table_t *hash = &conf->pkg_hash;
11484 +     char **raw;
11485 +     char **raw_start;
11486 +     pkg_t *pkg;
11487 +    
11488 +     raw = raw_start = read_raw_pkgs_from_file(file_name);
11489 +     if (!raw)
11490 +        return -ENOMEM;
11491 +
11492 +     while(*raw){         /* don't worry, we'll increment raw in the parsing function */
11493 +         pkg = pkg_new();
11494 +         if (!pkg)
11495 +              return -ENOMEM;
11496 +
11497 +         if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
11498 +              if (!pkg->architecture) {
11499 +                   char *version_str = pkg_version_str_alloc(pkg);
11500 +                   pkg->architecture = pkg_get_default_arch(conf);
11501 +                   ipkg_message(conf, IPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
11502 +                                pkg->name, version_str, pkg->architecture);
11503 +                   free(version_str);
11504 +              }
11505 +              hash_insert_pkg(hash, pkg, is_status_file,conf);
11506 +         } else {
11507 +              free(pkg);
11508 +         }
11509 +     }
11510 +
11511 +     /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
11512 +       memory after read_raw_pkgs_from_file */
11513 +     raw = raw_start;
11514 +     while (*raw) {
11515 +         free(*raw++);
11516 +     }
11517 +     free(raw_start);
11518 +     return 0;
11519 +}
11520 +
11521 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
11522 +{
11523 +  return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
11524 +}
11525 +
11526 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name)
11527 +{
11528 +    abstract_pkg_t *apkg = abstract_pkg_fetch_by_name(hash, name);
11529 +    if (apkg)
11530 +       return NULL;
11531 +    return apkg->provided_by;
11532 +}
11533 +
11534 +
11535 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg, 
11536 +                                                 int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet)
11537 +{
11538 +     int i; 
11539 +     int nprovides = 0;
11540 +     int nmatching = 0;
11541 +     pkg_vec_t *matching_pkgs = pkg_vec_alloc();
11542 +     abstract_pkg_vec_t *matching_apkgs = abstract_pkg_vec_alloc();
11543 +     abstract_pkg_vec_t *provided_apkg_vec;
11544 +     abstract_pkg_t **provided_apkgs;
11545 +     abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
11546 +     pkg_t *latest_installed_parent = NULL;
11547 +     pkg_t *latest_matching = NULL;
11548 +     pkg_t *held_pkg = NULL;
11549 +     pkg_t *good_pkg_by_name = NULL;
11550 +
11551 +     if (matching_apkgs == NULL || providers == NULL || 
11552 +         apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
11553 +         return NULL;
11554 +
11555 +     ipkg_message(conf, IPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
11556 +
11557 +     provided_apkg_vec = apkg->provided_by;
11558 +     nprovides = provided_apkg_vec->len;
11559 +     provided_apkgs = provided_apkg_vec->pkgs;
11560 +     if (nprovides > 1)
11561 +         ipkg_message(conf, IPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
11562 +
11563 +     /* accumulate all the providers */
11564 +     for (i = 0; i < nprovides; i++) {
11565 +         abstract_pkg_t *provider_apkg = provided_apkgs[i];
11566 +         ipkg_message(conf, IPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
11567 +         abstract_pkg_vec_insert(providers, provider_apkg);
11568 +     }
11569 +     nprovides = providers->len;
11570 +
11571 +     for (i = 0; i < nprovides; i++) {
11572 +         abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
11573 +         abstract_pkg_t *replacement_apkg = NULL;
11574 +         pkg_vec_t *vec;
11575 +
11576 +         if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
11577 +              replacement_apkg = provider_apkg->replaced_by->pkgs[0];
11578 +              if (provider_apkg->replaced_by->len > 1) {
11579 +                   ipkg_message(conf, IPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
11580 +                                provider_apkg->name, replacement_apkg->name);
11581 +              }
11582 +         }
11583 +
11584 +         if (replacement_apkg)
11585 +              ipkg_message(conf, IPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
11586 +                           replacement_apkg->name, provider_apkg->name);
11587 +
11588 +         if (replacement_apkg && (replacement_apkg != provider_apkg)) {
11589 +              if (abstract_pkg_vec_contains(providers, replacement_apkg))
11590 +                   continue;
11591 +              else
11592 +                   provider_apkg = replacement_apkg;
11593 +         }
11594 +
11595 +         if (!(vec = provider_apkg->pkgs)) {
11596 +              ipkg_message(conf, IPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
11597 +              continue;
11598 +         }
11599 +    
11600 +
11601 +         /* now check for supported architecture */
11602 +         {
11603 +              int max_count = 0;
11604 +              int i;
11605 +
11606 +              /* count packages matching max arch priority and keep track of last one */
11607 +              for (i = 0; i < vec->len; i++) {
11608 +                   pkg_t *maybe = vec->pkgs[i];
11609 +                   ipkg_message(conf, IPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
11610 +                                maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
11611 +                   if (maybe->arch_priority > 0)  {
11612 +                        max_count++;
11613 +                        abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
11614 +                        pkg_vec_insert(matching_pkgs, maybe);
11615 +                   }
11616 +              }
11617 +         }
11618 +     }
11619 +
11620 +     if (matching_pkgs->len > 1)
11621 +         pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
11622 +     if (matching_apkgs->len > 1)
11623 +         abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
11624 +
11625 +/* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
11626 +   needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
11627 +/* The problem is what to do when there are more than a mathing package, with the same name and several version ?
11628 +   Until now I always got the latest, but that breaks the downgrade option.
11629 +   If I stop at the first one, I would probably miss the new ones 
11630 +   Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
11631 +   or from a Packages feed.
11632 +   It it is from a file it always need to be checked whatever version I have in feeds or everywhere, according to force-down or whatever options*/
11633 +/*Pigi*/
11634 +
11635 +     for (i = 0; i < matching_pkgs->len; i++) {
11636 +         pkg_t *matching = matching_pkgs->pkgs[i];
11637 +          if (constraint_fcn(matching, cdata)) {  /* We found it */
11638 +             ipkg_message(conf, IPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
11639 +             good_pkg_by_name = matching;
11640 +             if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
11641 +                break;                                 
11642 +          }
11643 +     }
11644 +
11645 +
11646 +     for (i = 0; i < matching_pkgs->len; i++) {
11647 +         pkg_t *matching = matching_pkgs->pkgs[i];
11648 +         latest_matching = matching;
11649 +         if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
11650 +              latest_installed_parent = matching;
11651 +         if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
11652 +              if (held_pkg)
11653 +                   ipkg_message(conf, IPKG_ERROR, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
11654 +                                held_pkg->name, matching->name);
11655 +              held_pkg = matching;
11656 +         }
11657 +     }
11658 +
11659 +     if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
11660 +         ipkg_message(conf, IPKG_ERROR, "Package=%s, %d matching providers\n",
11661 +                      apkg->name, matching_apkgs->len);
11662 +         for (i = 0; i < matching_apkgs->len; i++) {
11663 +              abstract_pkg_t *matching = matching_apkgs->pkgs[i];
11664 +              ipkg_message(conf, IPKG_ERROR, "    %s\n", matching->name);
11665 +         }
11666 +         ipkg_message(conf, IPKG_ERROR, "Please select one with ipkg install or ipkg flag prefer\n");
11667 +     }
11668 +
11669 +     if (matching_apkgs->len > 1 && conf->verbosity > 1) {
11670 +         ipkg_message(conf, IPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
11671 +                      __FUNCTION__, apkg->name, matching_pkgs->len);
11672 +         for (i = 0; i < matching_pkgs->len; i++) {
11673 +              pkg_t *matching = matching_pkgs->pkgs[i];
11674 +              ipkg_message(conf, IPKG_INFO, "    %s %s %s\n",
11675 +                           matching->name, matching->version, matching->architecture);
11676 +         }
11677 +     }
11678 +
11679 +     nmatching = matching_apkgs->len;
11680 +
11681 +     pkg_vec_free(matching_pkgs);
11682 +     abstract_pkg_vec_free(matching_apkgs);
11683 +     abstract_pkg_vec_free(providers);
11684 +
11685 +     if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
11686 +         return good_pkg_by_name;
11687 +     }
11688 +     if (held_pkg) {
11689 +         ipkg_message(conf, IPKG_INFO, "  using held package %s\n", held_pkg->name);
11690 +         return held_pkg;
11691 +     }
11692 +     if (latest_installed_parent) {
11693 +         ipkg_message(conf, IPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
11694 +         return latest_installed_parent;
11695 +     }
11696 +     if (nmatching > 1) {
11697 +         ipkg_message(conf, IPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
11698 +         return NULL;
11699 +     }
11700 +     if (latest_matching) {
11701 +         ipkg_message(conf, IPKG_INFO, "  using latest matching %s %s %s\n",
11702 +                      latest_matching->name, latest_matching->version, latest_matching->architecture);
11703 +         return latest_matching;
11704 +     }
11705 +     return NULL;
11706 +}
11707 +
11708 +static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
11709 +{
11710 +     const char *name = (const char *)cdata;
11711 +     if (strcmp(pkg->name, name) == 0)
11712 +         return 1;
11713 +     else
11714 +         return 0;   
11715 +}
11716 +
11717 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name)
11718 +{
11719 +     hash_table_t *hash = &conf->pkg_hash;
11720 +     abstract_pkg_t *apkg = NULL;
11721 +
11722 +     if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
11723 +         return NULL;
11724 +     
11725 +     return pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0);
11726 +}
11727 +
11728 +
11729 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
11730 +                                      const char *pkg_name,
11731 +                                      const char * version)
11732 +{
11733 +    pkg_vec_t * vec;
11734 +    register int i;
11735 +    char *version_str = NULL;
11736 +    
11737 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
11738 +       return NULL;
11739 +    
11740 +    for(i = 0; i < vec->len; i++) {
11741 +       version_str = pkg_version_str_alloc(vec->pkgs[i]);
11742 +       if(!strcmp(version_str, version)) {
11743 +           free(version_str);
11744 +           break;
11745 +       }
11746 +       free(version_str);
11747 +    }
11748 +       
11749 +    if(i == vec->len)
11750 +       return NULL;
11751 +    
11752 +    return vec->pkgs[i];
11753 +}
11754 +
11755 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
11756 +                                            const char *pkg_name,
11757 +                                            pkg_dest_t *dest)
11758 +{
11759 +    pkg_vec_t * vec;
11760 +    register int i;
11761 +
11762 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
11763 +       return NULL;
11764 +    }
11765 +    
11766 +    for(i = 0; i < vec->len; i++)
11767 +       if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
11768 +           return vec->pkgs[i];
11769 +        }
11770 +    return NULL;
11771 +}
11772 +
11773 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
11774 +                                       const char *pkg_name)
11775 +{
11776 +    pkg_vec_t * vec;
11777 +    register int i;
11778 +
11779 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
11780 +       return NULL;
11781 +    } 
11782 +
11783 +    for(i = 0; i < vec->len; i++)
11784 +       if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
11785 +           return vec->pkgs[i];
11786 +        } 
11787 +    
11788 +    return NULL;
11789 +}
11790 +
11791 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
11792 +{
11793 +    abstract_pkg_t * ab_pkg;
11794 +
11795 +    if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
11796 +       return NULL;
11797 +    }
11798 +    
11799 +    if (ab_pkg->pkgs) {
11800 +      return ab_pkg->pkgs;
11801 +    } else if (ab_pkg->provided_by) {
11802 +      abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
11803 +      if (abpkg != NULL){
11804 +         return abpkg->pkgs;
11805 +      } else {
11806 +         return ab_pkg->pkgs;
11807 +      }
11808 +    } else {
11809 +      return NULL;
11810 +    }
11811 +}
11812 +
11813 +static int pkg_compare_names(const void *p1, const void *p2)
11814 +{
11815 +  const pkg_t *pkg1 = *(const pkg_t **)p1;
11816 +  const pkg_t *pkg2 = *(const pkg_t **)p2;
11817 +  if (pkg1->name == NULL)
11818 +    return 1;
11819 +  if (pkg2->name == NULL)
11820 +    return -1;
11821 +  return(strcmp(pkg1->name, pkg2->name));
11822 +}
11823 +
11824 +
11825 +static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
11826 +{
11827 +  int j;
11828 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11829 +  pkg_vec_t *all = (pkg_vec_t *)data;
11830 +  pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11831 +  if (pkg_vec) {
11832 +    for (j = 0; j < pkg_vec->len; j++) {
11833 +      pkg_t *pkg = pkg_vec->pkgs[j];
11834 +      pkg_vec_insert(all, pkg);
11835 +    }
11836 +  }
11837 +}
11838 +
11839 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
11840 +{
11841 +    hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
11842 +    qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
11843 +}
11844 +
11845 +static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
11846 +{
11847 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11848 +  pkg_vec_t *all = (pkg_vec_t *)data;
11849 +  pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11850 +  int j;
11851 +  if (pkg_vec) {
11852 +    for (j = 0; j < pkg_vec->len; j++) {
11853 +      pkg_t *pkg = pkg_vec->pkgs[j];
11854 +      if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
11855 +       pkg_vec_insert(all, pkg);
11856 +      }
11857 +    }
11858 +  }
11859 +}
11860 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
11861 +{
11862 +    hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
11863 +    qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
11864 +}
11865 +
11866 +static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
11867 +{
11868 +  int i;
11869 +  pkg_t *pkg;
11870 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11871 +  ipkg_conf_t *conf = (ipkg_conf_t *)data;
11872 +  abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
11873 +  fprintf(stdout, "%s\n", ab_pkg->name);
11874 +  i = 0;
11875 +  if (dependents != NULL)
11876 +    while (dependents [i] != NULL)
11877 +      printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
11878 +  dependents = ab_pkg->provided_by->pkgs;
11879 +  i = 0;
11880 +  if (dependents != NULL)
11881 +    while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
11882 +      printf ("\tprovided by - %s\n", dependents [i ++]->name);
11883 +  pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name);
11884 +  if (pkg) {
11885 +    i = 0;
11886 +    while (i < pkg->depends_count)
11887 +      printf ("\tdepends on - %s\n", pkg->depends_str [i ++]); 
11888 +  }
11889 +}
11890 +void pkg_hash_dump(hash_table_t *hash, void *data)
11891 +{
11892 +
11893 +  printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
11894 +  hash_table_foreach(hash, pkg_hash_dump_helper, data);
11895 +  printf ("\n+=+%s+=+\n\n", __FUNCTION__);    
11896 +}
11897 +
11898 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11899 +{
11900 +  abstract_pkg_t * ab_pkg;
11901 +
11902 +  if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
11903 +    ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
11904 +
11905 +  return ab_pkg;
11906 +}
11907 +
11908 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
11909 +{
11910 +     abstract_pkg_t * ab_pkg;
11911 +     int arch_priority;
11912 +
11913 +     if(!pkg)
11914 +         return pkg;
11915 +
11916 +     arch_priority = pkg->arch_priority;
11917 +
11918 +     if (buildDepends(hash, pkg)<0){
11919 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11920 +        return NULL;
11921 +     }
11922 +     ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
11923 +
11924 +     if (set_status) {
11925 +         if (pkg->state_status == SS_INSTALLED) {
11926 +              ab_pkg->state_status = SS_INSTALLED;
11927 +         } else if (pkg->state_status == SS_UNPACKED) {
11928 +              ab_pkg->state_status = SS_UNPACKED;
11929 +         }
11930 +     }
11931 +
11932 +     if(!ab_pkg->pkgs)
11933 +         ab_pkg->pkgs = pkg_vec_alloc();
11934 +    
11935 +     /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
11936 +     pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
11937 +     pkg->parent = ab_pkg;
11938 +
11939 +     if (buildProvides(hash, ab_pkg, pkg)<0){
11940 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11941 +        return NULL;
11942 +     }
11943 +     /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
11944 +     if (buildConflicts(hash, ab_pkg, pkg)<0){
11945 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11946 +        return NULL;
11947 +     }
11948 +     if (buildReplaces(hash, ab_pkg, pkg)<0) {
11949 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11950 +        return NULL;
11951 +     }
11952 +    
11953 +     buildDependedUponBy(pkg, ab_pkg);
11954 +     return pkg;
11955 +}
11956 +
11957 +/*
11958 + * this will assume that we've already determined that
11959 + * the abstract pkg doesn't exist, 'cause we should know these things...
11960 + */
11961 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11962 +{
11963 +  abstract_pkg_t * ab_pkg;
11964 +
11965 +  ab_pkg = abstract_pkg_new();
11966 +  if (ab_pkg == NULL) { return NULL; }
11967 +
11968 +  ab_pkg->name = strdup(pkg_name);
11969 +  hash_table_insert(hash, pkg_name, ab_pkg);
11970 +
11971 +  return ab_pkg;
11972 +}
11973 +
11974 +
11975 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name)
11976 +{
11977 +     hash_table_t *file_hash = &conf->file_hash;
11978 +
11979 +     return hash_table_get(file_hash, file_name); 
11980 +}
11981 +
11982 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
11983 +{
11984 +     hash_table_t *file_hash = &conf->file_hash;
11985 +     pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
11986 +     int file_name_len = strlen(file_name);
11987 +
11988 +     if (file_name[file_name_len -1] == '/')
11989 +         return 0;
11990 +
11991 +     if (conf->offline_root) {
11992 +         int len = strlen(conf->offline_root);
11993 +         if (strncmp(file_name, conf->offline_root, len) == 0) {
11994 +              file_name += len;
11995 +         }
11996 +     }
11997 +
11998 +     // ipkg_message(conf, IPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
11999 +     hash_table_insert(file_hash, file_name, owning_pkg); 
12000 +     if (old_owning_pkg) {
12001 +         str_list_remove_elt(old_owning_pkg->installed_files, file_name);
12002 +         /* mark this package to have its filelist written */
12003 +         old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
12004 +         owning_pkg->state_flag |= SF_FILELIST_CHANGED;
12005 +     }
12006 +     return 0;
12007 +}
12008 +
12009 +
12010 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_hash.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.h
12011 --- busybox-1.2.0-orig/archival/libipkg/pkg_hash.h      1970-01-01 01:00:00.000000000 +0100
12012 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.h     2006-07-22 16:31:25.000000000 +0200
12013 @@ -0,0 +1,61 @@
12014 +/* pkg_hash.h - the itsy package management system
12015 +
12016 +   Steven M. Ayer
12017 +   
12018 +   Copyright (C) 2002 Compaq Computer Corporation
12019 +
12020 +   This program is free software; you can redistribute it and/or
12021 +   modify it under the terms of the GNU General Public License as
12022 +   published by the Free Software Foundation; either version 2, or (at
12023 +   your option) any later version.
12024 +
12025 +   This program is distributed in the hope that it will be useful, but
12026 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12027 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12028 +   General Public License for more details.
12029 +*/
12030 +
12031 +#ifndef PKG_HASH_H
12032 +#define PKG_HASH_H
12033 +
12034 +#include "pkg.h"
12035 +#include "pkg_vec.h"
12036 +#include "hash_table.h"
12037 +
12038 +
12039 +int pkg_hash_init(const char *name, hash_table_t *hash, int len);
12040 +void pkg_hash_deinit(hash_table_t *hash);
12041 +void pkg_hash_map(hash_table_t *hash, void (*f)(void *data, void *entry), void *data);
12042 +
12043 +void pkg_hash_dump(hash_table_t *hash, void *data);
12044 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *available);
12045 +
12046 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
12047 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file);
12048 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf);
12049 +
12050 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
12051 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name);
12052 +pkg_vec_t *pkg_hash_fetch_by_name(hash_table_t *hash, const char *pkg_name);
12053 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *installed);
12054 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
12055 +                                      const char *pkg_name,
12056 +                                      const char * version);
12057 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name);
12058 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg, 
12059 +                                                 int (*constraint_fcn)(pkg_t *pkg, void *data), void *cdata, int quiet);
12060 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name);
12061 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
12062 +                                       const char *pkg_name);
12063 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
12064 +                                            const char *pkg_name,
12065 +                                            pkg_dest_t *dest);
12066 +
12067 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name);
12068 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *pkg);
12069 +
12070 +/* XXX: shouldn't this go in pkg_vec.[ch]? */
12071 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name);
12072 +
12073 +#endif
12074 +
12075 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_parse.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.c
12076 --- busybox-1.2.0-orig/archival/libipkg/pkg_parse.c     1970-01-01 01:00:00.000000000 +0100
12077 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.c    2006-07-22 16:31:25.000000000 +0200
12078 @@ -0,0 +1,366 @@
12079 +/* pkg_parse.c - the itsy package management system
12080 +
12081 +   Steven M. Ayer
12082 +   
12083 +   Copyright (C) 2002 Compaq Computer Corporation
12084 +
12085 +   This program is free software; you can redistribute it and/or
12086 +   modify it under the terms of the GNU General Public License as
12087 +   published by the Free Software Foundation; either version 2, or (at
12088 +   your option) any later version.
12089 +
12090 +   This program is distributed in the hope that it will be useful, but
12091 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12092 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12093 +   General Public License for more details.
12094 +*/
12095 +
12096 +#include "ipkg.h"
12097 +#include <errno.h>
12098 +#include <ctype.h>
12099 +   
12100 +#include "pkg.h"
12101 +#include "ipkg_utils.h"
12102 +#include "pkg_parse.h"
12103 +
12104 +int isGenericFieldType(char * type, char * line)
12105 +{
12106 +    if(!strncmp(line, type, strlen(type)))
12107 +       return 1;
12108 +    return 0;
12109 +}
12110 +
12111 +char * parseGenericFieldType(char * type, char * raw)
12112 +{
12113 +    char * field_value = raw + (strlen(type) + 1);
12114 +    return trim_alloc(field_value);
12115 +}
12116 +
12117 +void parseStatus(pkg_t *pkg, char * raw)
12118 +{
12119 +    char sw_str[64], sf_str[64], ss_str[64];
12120 +
12121 +    sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
12122 +    pkg->state_want = pkg_state_want_from_str(sw_str);
12123 +    pkg->state_flag = pkg_state_flag_from_str(sf_str);
12124 +    pkg->state_status = pkg_state_status_from_str(ss_str);
12125 +}
12126 +
12127 +char ** parseDependsString(char * raw, int * depends_count)
12128 +{
12129 +    char ** depends = NULL;
12130 +    int line_count = 0;
12131 +    char buff[2048], * dest;
12132 +
12133 +    while(raw && *raw && !isspace(*raw)) {
12134 +       raw++;
12135 +    }
12136 +
12137 +    if(line_is_blank(raw)){
12138 +       *depends_count = line_count;
12139 +       return NULL;
12140 +    }
12141 +    while(raw && *raw){
12142 +       depends = (char **)realloc(depends, sizeof(char *) * (line_count + 1));
12143 +       
12144 +       while(isspace(*raw)) raw++;
12145 +
12146 +       dest = buff;
12147 +       while((*raw != ',') && *raw)
12148 +           *dest++ = *raw++;
12149 +
12150 +       *dest = '\0';
12151 +       depends[line_count] = trim_alloc(buff);
12152 +       if(depends[line_count] ==NULL)
12153 +          return NULL;
12154 +        line_count++;
12155 +       if(*raw == ',')
12156 +           raw++;
12157 +    }
12158 +    *depends_count = line_count;
12159 +    return depends;
12160 +}
12161 +
12162 +void parseConffiles(pkg_t * pkg, char * raw)
12163 +{
12164 +    char file_name[1048], md5sum[1048];  /* please tell me there aren't any longer that 1k */
12165 +
12166 +    if(!strncmp(raw, "Conffiles:", 10))
12167 +       raw += strlen("Conffiles:");
12168 +
12169 +    while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
12170 +       conffile_list_append(&pkg->conffiles, file_name, md5sum);
12171 +       /*      fprintf(stderr, "%s %s ", file_name, md5sum);*/
12172 +       while (*raw && isspace(*raw)) {
12173 +           raw++;
12174 +       }
12175 +       raw += strlen(file_name);
12176 +       while (*raw && isspace(*raw)) {
12177 +           raw++;
12178 +       }
12179 +       raw += strlen(md5sum);
12180 +    }
12181 +}    
12182 +
12183 +int parseVersion(pkg_t *pkg, char *raw)
12184 +{
12185 +  char *colon, *eepochcolon;
12186 +#ifdef USE_DEBVERSION
12187 +  char *hyphen;
12188 +#endif
12189 +  unsigned long epoch;
12190 +
12191 +  if (!*raw) {
12192 +      fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
12193 +      return EINVAL;
12194 +  }
12195 +
12196 +  if (strncmp(raw, "Version:", 8) == 0) {
12197 +      raw += 8;
12198 +  }
12199 +  while (*raw && isspace(*raw)) {
12200 +      raw++;
12201 +  }
12202 +  
12203 +  colon= strchr(raw,':');
12204 +  if (colon) {
12205 +    epoch= strtoul(raw,&eepochcolon,10);
12206 +    if (colon != eepochcolon) {
12207 +       fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
12208 +       return EINVAL;
12209 +    }
12210 +    if (!*++colon) {
12211 +       fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
12212 +       return EINVAL;
12213 +    }
12214 +    raw= colon;
12215 +    pkg->epoch= epoch;
12216 +  } else {
12217 +    pkg->epoch= 0;
12218 +  }
12219 +
12220 +  pkg->revision = "";
12221 +  pkg->familiar_revision = "";
12222 +
12223 +  pkg->version= malloc(strlen(raw)+1);
12224 +  if ( pkg->version == NULL ) {
12225 +     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12226 +     return ENOMEM;
12227 +  }
12228 +  strcpy(pkg->version, raw);
12229 +
12230 +#ifdef USE_DEBVERSION
12231 +  hyphen= strrchr(pkg->version,'-');
12232 +
12233 +  if (hyphen) {
12234 +    *hyphen++= 0;
12235 +    if (strncmp("fam", hyphen, 3) == 0) {
12236 +      pkg->familiar_revision=hyphen+3;
12237 +      hyphen= strrchr(pkg->version,'-');
12238 +      if (hyphen) {
12239 +       *hyphen++= 0;
12240 +       pkg->revision = hyphen;
12241 +      }
12242 +    } else {
12243 +      pkg->revision = hyphen;
12244 +    }
12245 +  }
12246 +#endif
12247 +
12248 +/*
12249 +  fprintf(stderr,"Parsed version: %lu, %s, %s, %s\n",
12250 +         pkg->epoch,
12251 +         pkg->version,
12252 +         pkg->revision,
12253 +         pkg->familiar_revision);
12254 +*/
12255 +         
12256 +  return 0;
12257 +}
12258 +
12259 +
12260 +/* This code is needed to insert in first position the keyword for the aligning bug */
12261 +
12262 +int alterProvidesLine(char *raw, char *temp)
12263 +{
12264 +
12265 +
12266 +  if (!*raw) {
12267 +      fprintf(stderr, "%s: ERROR: Provides string is empty", __FUNCTION__);
12268 +      return -EINVAL;
12269 +  }
12270 +
12271 +  if ( temp == NULL ) {
12272 +     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12273 +     return -ENOMEM;
12274 +  }
12275 +
12276 +  if (strncmp(raw, "Provides:", 9) == 0) {
12277 +      raw += 9;
12278 +  }
12279 +  while (*raw && isspace(*raw)) {
12280 +      raw++;
12281 +  }      
12282 +  
12283 +  snprintf ( temp, 35, "Provides: ipkg_internal_use_only, ");           /* First part of the line */
12284 +  while (*raw) {
12285 +     strncat( temp, raw++, 1);
12286 +  }
12287 +  return 0;
12288
12289 +}
12290 +
12291 +/* Some random thoughts from Carl:
12292 +
12293 +   This function could be considerably simplified if we just kept
12294 +   an array of all the generic string-valued field names, and looped
12295 +   through those looking for a match. Also, these fields could perhaps
12296 +   be stored in the package as an array as well, (or, probably better,
12297 +   as an nv_pair_list_t).
12298 +
12299 +   Fields which require special parsing or storage, (such as Depends:
12300 +   and Status:) could be handled as they are now. 
12301 +*/
12302 +/* XXX: FEATURE: The Suggests: field needs to be changed from a string
12303 +   to a dependency list. And, since we already have
12304 +   Depends/Pre-Depends and need to add Conflicts, Recommends, and
12305 +   Enhances, perhaps we could generalize all of these and save some
12306 +   code duplication.
12307 +*/
12308 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
12309 +{
12310 +    int reading_conffiles, reading_description;
12311 +    int pkg_false_provides=1;
12312 +    char ** lines;
12313 +    char * provide=NULL;
12314 +
12315 +    pkg->src = src;
12316 +    pkg->dest = dest;
12317 +
12318 +    reading_conffiles = reading_description = 0;
12319 +
12320 +    for (lines = *raw; *lines; lines++) {
12321 +       /*      fprintf(stderr, "PARSING %s\n", *lines);*/
12322 +       if(isGenericFieldType("Package:", *lines)) 
12323 +           pkg->name = parseGenericFieldType("Package", *lines);
12324 +       else if(isGenericFieldType("Architecture:", *lines))
12325 +           pkg->architecture = parseGenericFieldType("Architecture", *lines);
12326 +       else if(isGenericFieldType("Filename:", *lines))
12327 +           pkg->filename = parseGenericFieldType("Filename", *lines);
12328 +       else if(isGenericFieldType("Section:", *lines))
12329 +           pkg->section = parseGenericFieldType("Section", *lines);
12330 +       else if(isGenericFieldType("MD5sum:", *lines))
12331 +           pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
12332 +       /* The old ipkg wrote out status files with the wrong case for MD5sum,
12333 +          let's parse it either way */
12334 +       else if(isGenericFieldType("MD5Sum:", *lines))
12335 +           pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
12336 +       else if(isGenericFieldType("Size:", *lines))
12337 +           pkg->size = parseGenericFieldType("Size", *lines);
12338 +       else if(isGenericFieldType("Source:", *lines))
12339 +           pkg->source = parseGenericFieldType("Source", *lines);
12340 +       else if(isGenericFieldType("Installed-Size:", *lines))
12341 +           pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
12342 +       else if(isGenericFieldType("Installed-Time:", *lines)) {
12343 +            char *time_str = parseGenericFieldType("Installed-Time", *lines);
12344 +            pkg->installed_time = strtoul(time_str, NULL, 0);
12345 +       } else if(isGenericFieldType("Priority:", *lines))
12346 +           pkg->priority = parseGenericFieldType("Priority", *lines);
12347 +       else if(isGenericFieldType("Essential:", *lines)) {
12348 +           char *essential_value;
12349 +           essential_value = parseGenericFieldType("Essential", *lines);
12350 +           if (strcmp(essential_value, "yes") == 0) {
12351 +               pkg->essential = 1;
12352 +           }
12353 +           free(essential_value);
12354 +       }
12355 +       else if(isGenericFieldType("Status", *lines))
12356 +           parseStatus(pkg, *lines);
12357 +       else if(isGenericFieldType("Version", *lines))
12358 +           parseVersion(pkg, *lines);
12359 +       else if(isGenericFieldType("Maintainer", *lines))
12360 +           pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
12361 +       else if(isGenericFieldType("Conffiles", *lines)){
12362 +           parseConffiles(pkg, *lines);
12363 +           reading_conffiles = 1;
12364 +       }
12365 +       else if(isGenericFieldType("Description", *lines)) {
12366 +           pkg->description = parseGenericFieldType("Description", *lines);
12367 +           reading_conffiles = 0;
12368 +           reading_description = 1;
12369 +       }
12370 +
12371 +       else if(isGenericFieldType("Provides", *lines)){
12372 +/* Here we add the internal_use to align the off by one problem between provides_str and provides */
12373 +            provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new ipkg_internal_use_only */
12374 +            if ( alterProvidesLine(*lines,provide) ){
12375 +               return EINVAL;
12376 +            }
12377 +           pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
12378 +/* Let's try to hack a bit here.
12379 +   The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
12380 +   in alot of other places. We will remove it before writing down the status database */
12381 +            pkg_false_provides=0;
12382 +            free(provide);
12383 +        } 
12384 +
12385 +       else if(isGenericFieldType("Depends", *lines))
12386 +           pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
12387 +       else if(isGenericFieldType("Pre-Depends", *lines))
12388 +           pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
12389 +       else if(isGenericFieldType("Recommends", *lines))
12390 +           pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
12391 +       else if(isGenericFieldType("Suggests", *lines))
12392 +           pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
12393 +       /* Abhaya: support for conflicts */
12394 +       else if(isGenericFieldType("Conflicts", *lines))
12395 +           pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
12396 +       else if(isGenericFieldType("Replaces", *lines))
12397 +           pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
12398 +       else if(line_is_blank(*lines)) {
12399 +           lines++;
12400 +           break;
12401 +       }
12402 +       else if(**lines == ' '){
12403 +           if(reading_description) {
12404 +               /* we already know it's not blank, so the rest of description */      
12405 +               pkg->description = realloc(pkg->description,
12406 +                                          strlen(pkg->description)
12407 +                                          + 1 + strlen(*lines) + 1);
12408 +               strcat(pkg->description, "\n");
12409 +               strcat(pkg->description, (*lines));
12410 +           }
12411 +           else if(reading_conffiles)
12412 +               parseConffiles(pkg, *lines);
12413 +       }
12414 +    }
12415 +    *raw = lines;
12416 +/* If the ipk has not a Provides line, we insert our false line */ 
12417 +    if ( pkg_false_provides==1)
12418 +       pkg->provides_str = parseDependsString ((char *)"Provides: ipkg_internal_use_only ", &pkg->provides_count);
12419 +
12420 +    if (pkg->name) {
12421 +       return 0;
12422 +    } else {
12423 +       return EINVAL;
12424 +    }
12425 +}
12426 +
12427 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
12428 +{
12429 +    char ** lines;
12430 +
12431 +    for (lines = *raw; *lines; lines++) {
12432 +       if(isGenericFieldType("Essential:", *lines)) {
12433 +           char *essential_value;
12434 +           essential_value = parseGenericFieldType("Essential", *lines);
12435 +           if (strcmp(essential_value, "yes") == 0) {
12436 +               pkg->essential = 1;
12437 +           }
12438 +           free(essential_value);
12439 +       }
12440 +    }
12441 +    *raw = lines;
12442 +
12443 +    return 0;
12444 +}
12445 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_parse.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.h
12446 --- busybox-1.2.0-orig/archival/libipkg/pkg_parse.h     1970-01-01 01:00:00.000000000 +0100
12447 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.h    2006-07-22 16:31:25.000000000 +0200
12448 @@ -0,0 +1,31 @@
12449 +/* pkg_parse.h - the itsy package management system
12450 +
12451 +   Steven M. Ayer
12452 +   
12453 +   Copyright (C) 2002 Compaq Computer Corporation
12454 +
12455 +   This program is free software; you can redistribute it and/or
12456 +   modify it under the terms of the GNU General Public License as
12457 +   published by the Free Software Foundation; either version 2, or (at
12458 +   your option) any later version.
12459 +
12460 +   This program is distributed in the hope that it will be useful, but
12461 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12462 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12463 +   General Public License for more details.
12464 +*/
12465 +
12466 +#ifndef PKG_PARSE_H
12467 +#define PKG_PARSE_H
12468 +
12469 +int isGenericFieldType(char * type, char * line);
12470 +char * parseGenericFieldType(char * type, char * raw);
12471 +void parseStatus(pkg_t *pkg, char * raw);
12472 +int parseVersion(pkg_t *pkg, char *raw);
12473 +char ** parseDependsString(char * raw, int * depends_count);
12474 +int parseVersion(pkg_t *pkg, char *raw);
12475 +void parseConffiles(pkg_t * pkg, char * raw);
12476 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
12477 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
12478 +
12479 +#endif
12480 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.c
12481 --- busybox-1.2.0-orig/archival/libipkg/pkg_src.c       1970-01-01 01:00:00.000000000 +0100
12482 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.c      2006-07-22 16:31:25.000000000 +0200
12483 @@ -0,0 +1,43 @@
12484 +/* pkg_src.c - the itsy package management system
12485 +
12486 +   Carl D. Worth
12487 +
12488 +   Copyright (C) 2001 University of Southern California
12489 +
12490 +   This program is free software; you can redistribute it and/or
12491 +   modify it under the terms of the GNU General Public License as
12492 +   published by the Free Software Foundation; either version 2, or (at
12493 +   your option) any later version.
12494 +
12495 +   This program is distributed in the hope that it will be useful, but
12496 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12497 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12498 +   General Public License for more details.
12499 +*/
12500 +
12501 +#include "ipkg.h"
12502 +
12503 +#include "pkg_src.h"
12504 +#include "str_util.h"
12505 +
12506 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip)
12507 +{
12508 +    src->gzip = gzip;
12509 +    src->name = str_dup_safe (name);
12510 +    src->value = str_dup_safe (base_url);
12511 +    if (extra_data)
12512 +       src->extra_data = str_dup_safe (extra_data);
12513 +    else
12514 +       src->extra_data = NULL;
12515 +    return 0;
12516 +}
12517 +
12518 +void pkg_src_deinit(pkg_src_t *src)
12519 +{
12520 +    free (src->name);
12521 +    free (src->value);
12522 +    if (src->extra_data)
12523 +       free (src->extra_data);
12524 +}
12525 +
12526 +
12527 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.h
12528 --- busybox-1.2.0-orig/archival/libipkg/pkg_src.h       1970-01-01 01:00:00.000000000 +0100
12529 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.h      2006-07-22 16:31:25.000000000 +0200
12530 @@ -0,0 +1,34 @@
12531 +/* pkg_src.h - the itsy package management system
12532 +
12533 +   Carl D. Worth
12534 +
12535 +   Copyright (C) 2001 University of Southern California
12536 +
12537 +   This program is free software; you can redistribute it and/or
12538 +   modify it under the terms of the GNU General Public License as
12539 +   published by the Free Software Foundation; either version 2, or (at
12540 +   your option) any later version.
12541 +
12542 +   This program is distributed in the hope that it will be useful, but
12543 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12544 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12545 +   General Public License for more details.
12546 +*/
12547 +
12548 +#ifndef PKG_SRC_H
12549 +#define PKG_SRC_H
12550 +
12551 +#include "nv_pair.h"
12552 +
12553 +typedef struct 
12554 +{
12555 +  char *name;
12556 +  char *value;
12557 +  char *extra_data;
12558 +  int gzip;
12559 +} pkg_src_t;
12560 +
12561 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip);
12562 +void pkg_src_deinit(pkg_src_t *src);
12563 +
12564 +#endif
12565 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.c
12566 --- busybox-1.2.0-orig/archival/libipkg/pkg_src_list.c  1970-01-01 01:00:00.000000000 +0100
12567 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.c 2006-07-22 16:31:25.000000000 +0200
12568 @@ -0,0 +1,75 @@
12569 +/* pkg_src_list.c - the itsy package management system
12570 +
12571 +   Carl D. Worth
12572 +
12573 +   Copyright (C) 2001 University of Southern California
12574 +
12575 +   This program is free software; you can redistribute it and/or
12576 +   modify it under the terms of the GNU General Public License as
12577 +   published by the Free Software Foundation; either version 2, or (at
12578 +   your option) any later version.
12579 +
12580 +   This program is distributed in the hope that it will be useful, but
12581 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12582 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12583 +   General Public License for more details.
12584 +*/
12585 +
12586 +#include "ipkg.h"
12587 +
12588 +#include "pkg_src_list.h"
12589 +#include "void_list.h"
12590 +
12591 +int pkg_src_list_init(pkg_src_list_t *list)
12592 +{
12593 +    return void_list_init((void_list_t *) list);
12594 +}
12595 +
12596 +void pkg_src_list_deinit(pkg_src_list_t *list)
12597 +{
12598 +    pkg_src_list_elt_t *iter;
12599 +    pkg_src_t *pkg_src;
12600 +
12601 +    for (iter = list->head; iter; iter = iter->next) {
12602 +      pkg_src = iter->data;
12603 +      pkg_src_deinit(pkg_src);
12604 +
12605 +      /* malloced in pkg_src_list_append */
12606 +      free(pkg_src);
12607 +      iter->data = NULL;
12608 +    }
12609 +    void_list_deinit((void_list_t *) list);
12610 +}
12611 +
12612 +pkg_src_t *pkg_src_list_append(pkg_src_list_t *list,
12613 +                              const char *name, const char *base_url, const char *extra_data,
12614 +                              int gzip)
12615 +{
12616 +    int err;
12617 +
12618 +    /* freed in pkg_src_list_deinit */
12619 +    pkg_src_t *pkg_src = malloc(sizeof(pkg_src_t));
12620 +
12621 +    if (pkg_src == NULL) {
12622 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12623 +       return NULL;
12624 +    }
12625 +    pkg_src_init(pkg_src, name, base_url, extra_data, gzip);
12626 +
12627 +    err = void_list_append((void_list_t *) list, pkg_src);
12628 +    if (err) {
12629 +       return NULL;
12630 +    }
12631 +
12632 +    return pkg_src;
12633 +}
12634 +
12635 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data)
12636 +{
12637 +    return void_list_push((void_list_t *) list, data);
12638 +}
12639 +
12640 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list)
12641 +{
12642 +    return (pkg_src_list_elt_t *) void_list_pop((void_list_t *) list);
12643 +}
12644 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.h
12645 --- busybox-1.2.0-orig/archival/libipkg/pkg_src_list.h  1970-01-01 01:00:00.000000000 +0100
12646 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.h 2006-07-22 16:31:25.000000000 +0200
12647 @@ -0,0 +1,57 @@
12648 +/* pkg_src_list.h - the itsy package management system
12649 +
12650 +   Carl D. Worth
12651 +
12652 +   Copyright (C) 2001 University of Southern California
12653 +
12654 +   This program is free software; you can redistribute it and/or
12655 +   modify it under the terms of the GNU General Public License as
12656 +   published by the Free Software Foundation; either version 2, or (at
12657 +   your option) any later version.
12658 +
12659 +   This program is distributed in the hope that it will be useful, but
12660 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12661 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12662 +   General Public License for more details.
12663 +*/
12664 +
12665 +#ifndef PKG_SRC_LIST_H
12666 +#define PKG_SRC_LIST_H
12667 +
12668 +#include "pkg_src.h"
12669 +
12670 +typedef struct pkg_src_list_elt pkg_src_list_elt_t;
12671 +struct pkg_src_list_elt
12672 +{
12673 +    pkg_src_list_elt_t *next;
12674 +    pkg_src_t *data;
12675 +};
12676 +
12677 +typedef struct pkg_src_list pkg_src_list_t;
12678 +struct pkg_src_list
12679 +{
12680 +    pkg_src_list_elt_t pre_head;
12681 +    pkg_src_list_elt_t *head;
12682 +    pkg_src_list_elt_t *tail;
12683 +};
12684 +
12685 +static inline int pkg_src_list_empty(pkg_src_list_t *list)
12686 +{
12687 +     if (list->head == NULL)
12688 +         return 1;
12689 +     else
12690 +         return 0;
12691 +}
12692 +
12693 +int pkg_src_list_elt_init(pkg_src_list_elt_t *elt, nv_pair_t *data);
12694 +void pkg_src_list_elt_deinit(pkg_src_list_elt_t *elt);
12695 +
12696 +int pkg_src_list_init(pkg_src_list_t *list);
12697 +void pkg_src_list_deinit(pkg_src_list_t *list);
12698 +
12699 +pkg_src_t *pkg_src_list_append(pkg_src_list_t *list, const char *name, const char *root_dir, const char *extra_data, int gzip);
12700 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data);
12701 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list);
12702 +
12703 +#endif
12704 +
12705 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_vec.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.c
12706 --- busybox-1.2.0-orig/archival/libipkg/pkg_vec.c       1970-01-01 01:00:00.000000000 +0100
12707 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.c      2006-07-22 16:31:26.000000000 +0200
12708 @@ -0,0 +1,230 @@
12709 +/* pkg_vec.c - the itsy package management system
12710 +
12711 +   Steven M. Ayer
12712 +   
12713 +   Copyright (C) 2002 Compaq Computer Corporation
12714 +
12715 +   This program is free software; you can redistribute it and/or
12716 +   modify it under the terms of the GNU General Public License as
12717 +   published by the Free Software Foundation; either version 2, or (at
12718 +   your option) any later version.
12719 +
12720 +   This program is distributed in the hope that it will be useful, but
12721 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12722 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12723 +   General Public License for more details.
12724 +*/
12725 +
12726 +#include <stdlib.h>
12727 +#include <fnmatch.h>
12728 +#include "xregex.h"
12729 +#include "ipkg.h"
12730 +#include "pkg.h"
12731 +
12732 +pkg_vec_t * pkg_vec_alloc(void)
12733 +{
12734 +    pkg_vec_t * vec = (pkg_vec_t *)malloc(sizeof(pkg_vec_t));
12735 +    if (!vec) {
12736 +      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12737 +      return NULL;
12738 +    }
12739 +    vec->pkgs = NULL;
12740 +    vec->len = 0;
12741 +
12742 +    return vec;
12743 +}
12744 +
12745 +void pkg_vec_free(pkg_vec_t *vec)
12746 +{
12747 +    free(vec->pkgs);
12748 +    free(vec);
12749 +}
12750 +
12751 +/*
12752 + * assumption: all names in a vector are identical
12753 + * assumption: all version strings are trimmed,
12754 + *             so identical versions have identical version strings,
12755 + *             implying identical packages; let's marry these
12756 + */
12757 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
12758 +{
12759 +     int i;
12760 +     int found = 0;
12761 +
12762 +     /* look for a duplicate pkg by name, version, and architecture */
12763 +     for (i = 0; i < vec->len; i++){
12764 +         ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found pkg=%s version=%s arch=%s cmp=%s version=%s arch=%s \n", 
12765 +                      __FUNCTION__, pkg->name, pkg->version, pkg->architecture, 
12766 +                       vec->pkgs[i]->name, vec->pkgs[i]->version,vec->pkgs[i]->architecture );
12767 +         if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12768 +             && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12769 +             && (strcmp(pkg->architecture, vec->pkgs[i]->architecture) == 0)) {
12770 +              found  = 1;
12771 +               ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found duplicate for pkg=%s version=%s arch=%s\n",
12772 +                             __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12773 +              break;
12774 +         }
12775 +     }
12776 +
12777 +     /* we didn't find one, add it */
12778 +     if (!found){   
12779 +         ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
12780 +                      __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12781 +
12782 +         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12783 +         vec->pkgs[vec->len] = pkg;
12784 +         vec->len++;
12785 +         return pkg;
12786 +     }
12787 +     /* update the one that we have */
12788 +     else {
12789 +          ipkg_message(conf, IPKG_DEBUG2, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
12790 +                        __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12791 +         if (set_status) {
12792 +              /* this is from the status file, so need to merge with existing database */
12793 +               ipkg_message(conf, IPKG_DEBUG2, " with set_status\n");
12794 +              pkg_merge(vec->pkgs[i], pkg, set_status);
12795 +              /* XXX: CLEANUP: It's not so polite to free something here
12796 +                 that was passed in from above. */
12797 +              pkg_deinit(pkg);
12798 +              free(pkg);
12799 +         } else {
12800 +               ipkg_message(conf, IPKG_DEBUG2, " WITHOUT set_status\n");
12801 +              /* just overwrite the old one */
12802 +              pkg_deinit(vec->pkgs[i]);
12803 +              free(vec->pkgs[i]);
12804 +              vec->pkgs[i] = pkg;
12805 +         }
12806 +         return vec->pkgs[i];
12807 +     }
12808 +}
12809 +
12810 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
12811 +{
12812 +     int i;
12813 +     int found = 0;
12814 +
12815 +     /* look for a duplicate pkg by name, version, and architecture */
12816 +     for (i = 0; i < vec->len; i++)
12817 +         if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12818 +             && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12819 +             && (strcmp(pkg->architecture, vec->pkgs[i]->name) == 0)) {
12820 +              found = 1;
12821 +              break;
12822 +         }
12823 +
12824 +     /* we didn't find one, add it */
12825 +     if(!found){   
12826 +         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12827 +         *(const pkg_t **)&vec->pkgs[vec->len] = pkg;
12828 +         vec->len++;
12829 +     }
12830 +}
12831 +
12832 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
12833 +{
12834 +     int i;
12835 +     for (i = 0; i < vec->len; i++)
12836 +         if (vec->pkgs[i] == apkg)
12837 +              return 1;
12838 +     return 0;
12839 +}
12840 +
12841 +typedef int (*compare_fcn_t)(const void *, const void *);
12842 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *))
12843 +{
12844 +     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12845 +}
12846 +
12847 +int pkg_vec_clear_marks(pkg_vec_t *vec)
12848 +{
12849 +     int npkgs = vec->len;
12850 +     int i;
12851 +     for (i = 0; i < npkgs; i++) {
12852 +         pkg_t *pkg = vec->pkgs[i];
12853 +         pkg->state_flag &= ~SF_MARKED;
12854 +     }
12855 +     return 0;
12856 +}
12857 +
12858 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
12859 +{
12860 +     int matching_count = 0;
12861 +     pkg_t **pkgs = vec->pkgs;
12862 +     int npkgs = vec->len;
12863 +     int i;
12864 +     for (i = 0; i < npkgs; i++) {
12865 +         pkg_t *pkg = pkgs[i];
12866 +         if (fnmatch(pattern, pkg->name, 0)==0) {
12867 +              pkg->state_flag |= SF_MARKED;
12868 +              matching_count++;
12869 +         }
12870 +     }
12871 +     return matching_count;
12872 +}
12873 +
12874 +
12875 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
12876 +{
12877 +    abstract_pkg_vec_t * vec ; 
12878 +    vec = (abstract_pkg_vec_t *)malloc(sizeof(abstract_pkg_vec_t));
12879 +    if (!vec) {
12880 +      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12881 +      return NULL;
12882 +    }
12883 +    vec->pkgs = NULL;
12884 +    vec->len = 0;
12885 +
12886 +    return vec;
12887 +}
12888 +
12889 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
12890 +{
12891 +    free(vec->pkgs);
12892 +    free(vec);
12893 +}
12894 +
12895 +/*
12896 + * assumption: all names in a vector are unique
12897 + */
12898 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
12899 +{
12900 +    int i;
12901 +
12902 +    /* look for a duplicate pkg by name */
12903 +    for(i = 0; i < vec->len; i++)
12904 +       if (strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12905 +           break;
12906 +
12907 +    /* we didn't find one, add it */
12908 +    if(i == vec->len){   
12909 +       vec->pkgs = 
12910 +         (abstract_pkg_t **)
12911 +           realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
12912 +       vec->pkgs[vec->len] = pkg;
12913 +       vec->len++;
12914 +    }
12915 +}
12916 +
12917 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
12918 +{
12919 +    if (vec->len > i) 
12920 +       return vec->pkgs[i];
12921 +    else
12922 +       return NULL;
12923 +}
12924 +
12925 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
12926 +{
12927 +     int i;
12928 +     for (i = 0; i < vec->len; i++)
12929 +         if (vec->pkgs[i] == apkg)
12930 +              return 1;
12931 +     return 0;
12932 +}
12933 +
12934 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *))
12935 +{
12936 +     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12937 +}
12938 +
12939 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_vec.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.h
12940 --- busybox-1.2.0-orig/archival/libipkg/pkg_vec.h       1970-01-01 01:00:00.000000000 +0100
12941 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.h      2006-07-22 16:31:26.000000000 +0200
12942 @@ -0,0 +1,62 @@
12943 +/* pkg_vec.h - the itsy package management system
12944 +
12945 +   Steven M. Ayer
12946 +   
12947 +   Copyright (C) 2002 Compaq Computer Corporation
12948 +
12949 +   This program is free software; you can redistribute it and/or
12950 +   modify it under the terms of the GNU General Public License as
12951 +   published by the Free Software Foundation; either version 2, or (at
12952 +   your option) any later version.
12953 +
12954 +   This program is distributed in the hope that it will be useful, but
12955 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12956 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12957 +   General Public License for more details.
12958 +*/
12959 +
12960 +#ifndef PKG_VEC_H
12961 +#define PKG_VEC_H
12962 +
12963 +typedef struct pkg pkg_t;
12964 +typedef struct abstract_pkg abstract_pkg_t;
12965 +
12966 +struct pkg_vec
12967 +{
12968 +    pkg_t **pkgs;
12969 +    int len;
12970 +};
12971 +typedef struct pkg_vec pkg_vec_t;
12972 +
12973 +struct abstract_pkg_vec
12974 +{
12975 +    abstract_pkg_t **pkgs;
12976 +    int len;
12977 +};
12978 +typedef struct abstract_pkg_vec abstract_pkg_vec_t;
12979 +
12980 +
12981 +pkg_vec_t * pkg_vec_alloc(void);
12982 +void pkg_vec_free(pkg_vec_t *vec);
12983 +void marry_two_packages(pkg_t * newpkg, pkg_t * oldpkg);
12984 +
12985 +void pkg_vec_add(pkg_vec_t *vec, pkg_t *pkg);
12986 +/* pkg_vec_insert_merge: might munge pkg.
12987 +*  returns the pkg that is in the pkg graph */
12988 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status, ipkg_conf_t *conf);
12989 +/* this one never munges pkg */
12990 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg);
12991 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg);
12992 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *));
12993 +
12994 +int pkg_vec_clear_marks(pkg_vec_t *vec);
12995 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern);
12996 +
12997 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void);
12998 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec);
12999 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg);
13000 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i);
13001 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg);
13002 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *));
13003 +#endif
13004 +
13005 diff -ruN busybox-1.2.0-orig/archival/libipkg/sprintf_alloc.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/sprintf_alloc.h
13006 --- busybox-1.2.0-orig/archival/libipkg/sprintf_alloc.h 1970-01-01 01:00:00.000000000 +0100
13007 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/sprintf_alloc.h        2006-07-22 16:31:26.000000000 +0200
13008 @@ -0,0 +1,25 @@
13009 +/* sprintf_alloca.c -- like sprintf with memory allocation
13010 +
13011 +   Carl D. Worth
13012 +
13013 +   Copyright (C) 2001 University of Southern California
13014 +
13015 +   This program is free software; you can redistribute it and/or modify
13016 +   it under the terms of the GNU General Public License as published by
13017 +   the Free Software Foundation; either version 2, or (at your option)
13018 +   any later version.
13019 +
13020 +   This program is distributed in the hope that it will be useful,
13021 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
13022 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13023 +   GNU General Public License for more details.
13024 +*/
13025 +
13026 +#ifndef SPRINTF_ALLOC_H
13027 +#define SPRINTF_ALLOC_H
13028 +
13029 +#include "libbb.h"
13030 +
13031 +#define sprintf_alloc(str, fmt, args...)  *str = bb_xasprintf(fmt, ## args)
13032 +
13033 +#endif
13034 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.c
13035 --- busybox-1.2.0-orig/archival/libipkg/str_list.c      1970-01-01 01:00:00.000000000 +0100
13036 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.c     2006-07-22 16:31:26.000000000 +0200
13037 @@ -0,0 +1,76 @@
13038 +/* str_list.c - the itsy package management system
13039 +
13040 +   Carl D. Worth
13041 +
13042 +   Copyright (C) 2001 University of Southern California
13043 +
13044 +   This program is free software; you can redistribute it and/or
13045 +   modify it under the terms of the GNU General Public License as
13046 +   published by the Free Software Foundation; either version 2, or (at
13047 +   your option) any later version.
13048 +
13049 +   This program is distributed in the hope that it will be useful, but
13050 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13051 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13052 +   General Public License for more details.
13053 +*/
13054 +
13055 +#include "ipkg.h"
13056 +
13057 +#include "str_list.h"
13058 +
13059 +int str_list_elt_init(str_list_elt_t *elt, char *data)
13060 +{
13061 +    return void_list_elt_init((void_list_elt_t *) elt, data);
13062 +}
13063 +
13064 +void str_list_elt_deinit(str_list_elt_t *elt)
13065 +{
13066 +    void_list_elt_deinit((void_list_elt_t *) elt);
13067 +}
13068 +
13069 +str_list_t *str_list_alloc()
13070 +{
13071 +     str_list_t *list = (str_list_t *)malloc(sizeof(str_list_t));
13072 +     if (list)
13073 +         str_list_init(list);
13074 +     return list;
13075 +}
13076 +
13077 +int str_list_init(str_list_t *list)
13078 +{
13079 +    return void_list_init((void_list_t *) list);
13080 +}
13081 +
13082 +void str_list_deinit(str_list_t *list)
13083 +{
13084 +    void_list_deinit((void_list_t *) list);
13085 +}
13086 +
13087 +int str_list_append(str_list_t *list, char *data)
13088 +{
13089 +    return void_list_append((void_list_t *) list, data);
13090 +}
13091 +
13092 +int str_list_push(str_list_t *list, char *data)
13093 +{
13094 +    return void_list_push((void_list_t *) list, data);
13095 +}
13096 +
13097 +str_list_elt_t *str_list_pop(str_list_t *list)
13098 +{
13099 +    return (str_list_elt_t *) void_list_pop((void_list_t *) list);
13100 +}
13101 +
13102 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter)
13103 +{
13104 +    return (str_list_elt_t *) void_list_remove((void_list_t *) list,
13105 +                                              (void_list_elt_t **) iter);
13106 +}
13107 +
13108 +char *str_list_remove_elt(str_list_t *list, const char *target_str)
13109 +{
13110 +     return (char *)void_list_remove_elt((void_list_t *) list,
13111 +                                        (void *)target_str,
13112 +                                        (void_list_cmp_t)strcmp);
13113 +}
13114 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.h
13115 --- busybox-1.2.0-orig/archival/libipkg/str_list.h      1970-01-01 01:00:00.000000000 +0100
13116 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.h     2006-07-22 16:31:26.000000000 +0200
13117 @@ -0,0 +1,51 @@
13118 +/* str_list.h - the itsy package management system
13119 +
13120 +   Carl D. Worth
13121 +
13122 +   Copyright (C) 2001 University of Southern California
13123 +
13124 +   This program is free software; you can redistribute it and/or
13125 +   modify it under the terms of the GNU General Public License as
13126 +   published by the Free Software Foundation; either version 2, or (at
13127 +   your option) any later version.
13128 +
13129 +   This program is distributed in the hope that it will be useful, but
13130 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13131 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13132 +   General Public License for more details.
13133 +*/
13134 +
13135 +#ifndef STR_LIST_H
13136 +#define STR_LIST_H
13137 +
13138 +#include "void_list.h"
13139 +
13140 +typedef struct str_list_elt str_list_elt_t;
13141 +struct str_list_elt
13142 +{
13143 +    str_list_elt_t *next;
13144 +    char *data;
13145 +};
13146 +
13147 +typedef struct xstr_list str_list_t;
13148 +struct xstr_list
13149 +{
13150 +    str_list_elt_t pre_head;
13151 +    str_list_elt_t *head;
13152 +    str_list_elt_t *tail;
13153 +};
13154 +
13155 +int str_list_elt_init(str_list_elt_t *elt, char *data);
13156 +void str_list_elt_deinit(str_list_elt_t *elt);
13157 +
13158 +str_list_t *str_list_alloc(void);
13159 +int str_list_init(str_list_t *list);
13160 +void str_list_deinit(str_list_t *list);
13161 +
13162 +int str_list_append(str_list_t *list, char *data);
13163 +int str_list_push(str_list_t *list, char *data);
13164 +str_list_elt_t *str_list_pop(str_list_t *list);
13165 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter);
13166 +char *str_list_remove_elt(str_list_t *list, const char *target_str);
13167 +
13168 +#endif
13169 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_util.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.c
13170 --- busybox-1.2.0-orig/archival/libipkg/str_util.c      1970-01-01 01:00:00.000000000 +0100
13171 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.c     2006-07-22 16:31:26.000000000 +0200
13172 @@ -0,0 +1,73 @@
13173 +/* str_utils.c - the itsy package management system
13174 +
13175 +   Carl D. Worth
13176 +
13177 +   Copyright (C) 2001 University of Southern California
13178 +
13179 +   This program is free software; you can redistribute it and/or
13180 +   modify it under the terms of the GNU General Public License as
13181 +   published by the Free Software Foundation; either version 2, or (at
13182 +   your option) any later version.
13183 +
13184 +   This program is distributed in the hope that it will be useful, but
13185 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13186 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13187 +   General Public License for more details.
13188 +*/
13189 +
13190 +#include "ipkg.h"
13191 +
13192 +int str_starts_with(const char *str, const char *prefix)
13193 +{
13194 +    return (strncmp(str, prefix, strlen(prefix)) == 0);
13195 +}
13196 +
13197 +int str_ends_with(const char *str, const char *suffix)
13198 +{
13199 +    int suffix_len;
13200 +    int str_len;
13201 +
13202 +    str_len = strlen(str);
13203 +    suffix_len = strlen(suffix);
13204 +
13205 +    if (str_len < suffix_len) {
13206 +       return 0;
13207 +    }
13208 +
13209 +    return (strcmp(str + str_len - suffix_len, suffix) == 0);
13210 +}
13211 +
13212 +int str_chomp(char *str)
13213 +{
13214 +    if (str[strlen(str) - 1] == '\n') {
13215 +       str[strlen(str) - 1] = '\0';
13216 +       return 1;
13217 +    }
13218 +    return 0;
13219 +}
13220 +
13221 +int str_tolower(char *str)
13222 +{
13223 +    while (*str) {
13224 +       *str = tolower(*str);
13225 +       str++;
13226 +    }
13227 +
13228 +    return 0;
13229 +}
13230 +
13231 +int str_toupper(char *str)
13232 +{
13233 +    while (*str) {
13234 +       *str = toupper(*str);
13235 +       str++;
13236 +    }
13237 +
13238 +    return 0;
13239 +}
13240 +
13241 +char *str_dup_safe(const char *str)
13242 +{
13243 +    return str ? strdup(str) : NULL;
13244 +}
13245 +
13246 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_util.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.h
13247 --- busybox-1.2.0-orig/archival/libipkg/str_util.h      1970-01-01 01:00:00.000000000 +0100
13248 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.h     2006-07-22 16:31:26.000000000 +0200
13249 @@ -0,0 +1,28 @@
13250 +/* str_utils.h - the itsy package management system
13251 +
13252 +   Carl D. Worth
13253 +
13254 +   Copyright (C) 2001 University of Southern California
13255 +
13256 +   This program is free software; you can redistribute it and/or
13257 +   modify it under the terms of the GNU General Public License as
13258 +   published by the Free Software Foundation; either version 2, or (at
13259 +   your option) any later version.
13260 +
13261 +   This program is distributed in the hope that it will be useful, but
13262 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13263 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13264 +   General Public License for more details.
13265 +*/
13266 +
13267 +#ifndef STR_UTILS_H
13268 +#define STR_UTILS_H
13269 +
13270 +int str_starts_with(const char *str, const char *prefix);
13271 +int str_ends_with(const char *str, const char *suffix);
13272 +int str_chomp(char *str);
13273 +int str_tolower(char *str);
13274 +int str_toupper(char *str);
13275 +char *str_dup_safe(const char *str);
13276 +
13277 +#endif
13278 diff -ruN busybox-1.2.0-orig/archival/libipkg/user.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.c
13279 --- busybox-1.2.0-orig/archival/libipkg/user.c  1970-01-01 01:00:00.000000000 +0100
13280 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.c 2006-07-22 16:31:26.000000000 +0200
13281 @@ -0,0 +1,58 @@
13282 +/* user.c - the itsy package management system
13283 +
13284 +   Jamey Hicks
13285 +
13286 +   Copyright (C) 2002 Hewlett Packard Company
13287 +   Copyright (C) 2001 University of Southern California
13288 +
13289 +   This program is free software; you can redistribute it and/or
13290 +   modify it under the terms of the GNU General Public License as
13291 +   published by the Free Software Foundation; either version 2, or (at
13292 +   your option) any later version.
13293 +
13294 +   This program is distributed in the hope that it will be useful, but
13295 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13296 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13297 +   General Public License for more details.
13298 +*/
13299 +
13300 +#include <stdio.h>
13301 +#include <stdarg.h>
13302 +#include "file_util.h"
13303 +#include "str_util.h"
13304 +#ifdef IPKG_LIB        
13305 +#include "libipkg.h"
13306 +#endif 
13307 +
13308 +
13309 +#ifdef IPKG_LIB
13310 +static char *question = NULL;
13311 +static int question_len = 255;
13312 +#endif 
13313 +char *get_user_response(const char *format, ...)
13314 +{
13315 +     int len = question_len;
13316 +     va_list ap;
13317 +     char *response;
13318 +     va_start(ap, format);
13319 +
13320 +#ifndef IPKG_LIB
13321 +     vprintf(format, ap);
13322 +     do {
13323 +         response = file_read_line_alloc(stdin);
13324 +     } while (response == NULL);
13325 +#else
13326 +     do {
13327 +         if (question == NULL || len > question_len) {
13328 +              question = realloc(question, len + 1);
13329 +              question_len = len;
13330 +         }
13331 +         len = vsnprintf(question,question_len,format,ap);
13332 +     } while (len > question_len);
13333 +     response = strdup(ipkg_cb_response(question));
13334 +#endif
13335 +     str_chomp(response);
13336 +     str_tolower(response);
13337 +
13338 +     return response;
13339 +}
13340 diff -ruN busybox-1.2.0-orig/archival/libipkg/user.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.h
13341 --- busybox-1.2.0-orig/archival/libipkg/user.h  1970-01-01 01:00:00.000000000 +0100
13342 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.h 2006-07-22 16:31:26.000000000 +0200
13343 @@ -0,0 +1,23 @@
13344 +/* user.c - the itsy package management system
13345 +
13346 +   Jamey Hicks
13347 +
13348 +   Copyright (C) 2002 Hewlett Packard Company
13349 +   Copyright (C) 2001 University of Southern California
13350 +
13351 +   This program is free software; you can redistribute it and/or
13352 +   modify it under the terms of the GNU General Public License as
13353 +   published by the Free Software Foundation; either version 2, or (at
13354 +   your option) any later version.
13355 +
13356 +   This program is distributed in the hope that it will be useful, but
13357 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13358 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13359 +   General Public License for more details.
13360 +*/
13361 +
13362 +#include <stdio.h>
13363 +#include <stdarg.h>
13364 +
13365 +char *get_user_response(const char *format, ...);
13366 +
13367 diff -ruN busybox-1.2.0-orig/archival/libipkg/void_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.c
13368 --- busybox-1.2.0-orig/archival/libipkg/void_list.c     1970-01-01 01:00:00.000000000 +0100
13369 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.c    2006-07-22 16:31:26.000000000 +0200
13370 @@ -0,0 +1,194 @@
13371 +/* void_list.c - the itsy package management system
13372 +
13373 +   Carl D. Worth
13374 +
13375 +   Copyright (C) 2001 University of Southern California
13376 +
13377 +   This program is free software; you can redistribute it and/or
13378 +   modify it under the terms of the GNU General Public License as
13379 +   published by the Free Software Foundation; either version 2, or (at
13380 +   your option) any later version.
13381 +
13382 +   This program is distributed in the hope that it will be useful, but
13383 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13384 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13385 +   General Public License for more details.
13386 +*/
13387 +
13388 +#include "ipkg.h"
13389 +#include <errno.h>
13390 +
13391 +#include "void_list.h"
13392 +
13393 +int void_list_elt_init(void_list_elt_t *elt, void *data)
13394 +{
13395 +    elt->next = NULL;
13396 +    elt->data = data;
13397 +
13398 +    return 0;
13399 +}
13400 +
13401 +void void_list_elt_deinit(void_list_elt_t *elt)
13402 +{
13403 +    void_list_elt_init(elt, NULL);
13404 +}
13405 +
13406 +int void_list_init(void_list_t *list)
13407 +{
13408 +    void_list_elt_init(&list->pre_head, NULL);
13409 +    list->head = NULL;
13410 +    list->pre_head.next = list->head;
13411 +    list->tail = NULL;
13412 +
13413 +    return 0;
13414 +}
13415 +
13416 +void void_list_deinit(void_list_t *list)
13417 +{
13418 +    void_list_elt_t *elt;
13419 +
13420 +    while (list->head) {
13421 +       elt = void_list_pop(list);
13422 +       void_list_elt_deinit(elt);
13423 +       /* malloced in void_list_append */
13424 +       free(elt);
13425 +    }
13426 +}
13427 +
13428 +int void_list_append(void_list_t *list, void *data)
13429 +{
13430 +    void_list_elt_t *elt;
13431 +
13432 +    /* freed in void_list_deinit */
13433 +    elt = malloc(sizeof(void_list_elt_t));
13434 +    if (elt == NULL) {
13435 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13436 +       return ENOMEM;
13437 +    }
13438 +
13439 +    void_list_elt_init(elt, data);
13440 +
13441 +    if (list->tail) {
13442 +       list->tail->next = elt;
13443 +       list->tail = elt;
13444 +    } else {
13445 +       list->head = elt;
13446 +       list->pre_head.next = list->head;
13447 +       list->tail = elt;
13448 +    }
13449 +
13450 +    return 0;
13451 +}
13452 +
13453 +int void_list_push(void_list_t *list, void *data)
13454 +{
13455 +    void_list_elt_t *elt;
13456 +
13457 +    elt = malloc(sizeof(void_list_elt_t));
13458 +    if (elt == NULL) {
13459 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13460 +       return ENOMEM;
13461 +    }
13462 +
13463 +    void_list_elt_init(elt, data);
13464 +
13465 +    elt->next = list->head;
13466 +    list->head->next = elt;
13467 +    if (list->tail == NULL) {
13468 +       list->tail = list->head;
13469 +    }
13470 +
13471 +    return 0;
13472 +}
13473 +
13474 +void_list_elt_t *void_list_pop(void_list_t *list)
13475 +{
13476 +    void_list_elt_t *elt;
13477 +
13478 +    elt = list->head;
13479 +
13480 +    if (list->head) {
13481 +       list->head = list->head->next;
13482 +       list->pre_head.next = list->head;
13483 +       if (list->head == NULL) {
13484 +           list->tail = NULL;
13485 +       }
13486 +    }
13487 +
13488 +    return elt;
13489 +}
13490 +
13491 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
13492 +{
13493 +    void_list_elt_t *prior;
13494 +    void_list_elt_t *old_elt;
13495 +    void *old_data;
13496 +
13497 +    old_elt = *iter;
13498 +    old_data = old_elt->data;
13499 +
13500 +    if (old_elt == list->head) {
13501 +       prior = &list->pre_head;
13502 +       void_list_pop(list);
13503 +    } else {
13504 +       for (prior = list->head; prior; prior = prior->next) {
13505 +           if (prior->next == old_elt) {
13506 +               break;
13507 +           }
13508 +       }
13509 +       if (prior == NULL || prior->next != old_elt) {
13510 +           fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
13511 +           return NULL;
13512 +       }
13513 +       prior->next = old_elt->next;
13514 +
13515 +       if (old_elt == list->tail) {
13516 +           list->tail = prior;
13517 +       }
13518 +    }
13519 +
13520 +    void_list_elt_deinit(old_elt);
13521 +    *iter = prior;
13522 +
13523 +    return old_data;
13524 +}
13525 +
13526 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13527 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
13528 +{
13529 +     void_list_elt_t *prior;
13530 +     void_list_elt_t *old_elt = NULL;
13531 +     void *old_data = NULL;
13532 +
13533 +     /* first element */
13534 +     if (list->head && list->head->data && (cmp(list->head->data, target_data) == 0)) {
13535 +         old_elt = list->head;
13536 +         old_data = list->head->data;
13537 +         void_list_pop(list);
13538 +     } else {
13539 +         int found = 0;
13540 +         for (prior = list->head; prior && prior->next; prior = prior->next) {
13541 +              if (prior->next->data && (cmp(prior->next->data, target_data) == 0)) {
13542 +                   old_elt = prior->next;
13543 +                   old_data = old_elt->data;
13544 +                   found = 1;
13545 +                   break;
13546 +              }
13547 +         }
13548 +         if (!found) {
13549 +              return NULL;
13550 +         }
13551 +         prior->next = old_elt->next;
13552 +
13553 +         if (old_elt == list->tail) {
13554 +              list->tail = prior;
13555 +         }
13556 +     }
13557 +     if (old_elt)
13558 +         void_list_elt_deinit(old_elt);
13559 +
13560 +     if (old_data)
13561 +         return old_data;
13562 +     else
13563 +         return NULL;
13564 +}
13565 diff -ruN busybox-1.2.0-orig/archival/libipkg/void_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.h
13566 --- busybox-1.2.0-orig/archival/libipkg/void_list.h     1970-01-01 01:00:00.000000000 +0100
13567 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.h    2006-07-22 16:31:26.000000000 +0200
13568 @@ -0,0 +1,59 @@
13569 +/* void_list.h - the itsy package management system
13570 +
13571 +   Carl D. Worth
13572 +
13573 +   Copyright (C) 2001 University of Southern California
13574 +
13575 +   This program is free software; you can redistribute it and/or
13576 +   modify it under the terms of the GNU General Public License as
13577 +   published by the Free Software Foundation; either version 2, or (at
13578 +   your option) any later version.
13579 +
13580 +   This program is distributed in the hope that it will be useful, but
13581 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13582 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13583 +   General Public License for more details.
13584 +*/
13585 +
13586 +#ifndef VOID_LIST_H
13587 +#define VOID_LIST_H
13588 +
13589 +typedef struct void_list_elt void_list_elt_t;
13590 +struct void_list_elt
13591 +{
13592 +    void_list_elt_t *next;
13593 +    void *data;
13594 +};
13595 +
13596 +typedef struct void_list void_list_t;
13597 +struct void_list
13598 +{
13599 +    void_list_elt_t pre_head;
13600 +    void_list_elt_t *head;
13601 +    void_list_elt_t *tail;
13602 +};
13603 +
13604 +static inline int void_list_empty(void_list_t *list)
13605 +{
13606 +     if (list->head == NULL)
13607 +         return 1;
13608 +     else
13609 +         return 0;
13610 +}
13611 +
13612 +int void_list_elt_init(void_list_elt_t *elt, void *data);
13613 +void void_list_elt_deinit(void_list_elt_t *elt);
13614 +
13615 +int void_list_init(void_list_t *list);
13616 +void void_list_deinit(void_list_t *list);
13617 +
13618 +int void_list_append(void_list_t *list, void *data);
13619 +int void_list_push(void_list_t *list, void *data);
13620 +void_list_elt_t *void_list_pop(void_list_t *list);
13621 +
13622 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter);
13623 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13624 +typedef int (*void_list_cmp_t)(const void *, const void *);
13625 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp);
13626 +
13627 +#endif
13628 diff -ruN busybox-1.2.0-orig/archival/libipkg/xsystem.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.c
13629 --- busybox-1.2.0-orig/archival/libipkg/xsystem.c       1970-01-01 01:00:00.000000000 +0100
13630 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.c      2006-07-22 16:31:26.000000000 +0200
13631 @@ -0,0 +1,64 @@
13632 +/* xsystem.c - system(3) with error messages
13633 +
13634 +   Carl D. Worth
13635 +
13636 +   Copyright (C) 2001 University of Southern California
13637 +
13638 +   This program is free software; you can redistribute it and/or
13639 +   modify it under the terms of the GNU General Public License as
13640 +   published by the Free Software Foundation; either version 2, or (at
13641 +   your option) any later version.
13642 +
13643 +   This program is distributed in the hope that it will be useful, but
13644 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13645 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13646 +   General Public License for more details.
13647 +*/
13648 +
13649 +#include "ipkg.h"
13650 +#include <sys/wait.h>
13651 +
13652 +#include "xsystem.h"
13653 +
13654 +/* XXX: FEATURE: I shouldn't actually use system(3) at all. I don't
13655 +   really need the /bin/sh invocation which takes resources and
13656 +   introduces security problems. I should switch all of this to a sort
13657 +   of execl() or execv() interface/implementation.
13658 +*/
13659 +
13660 +/* Like system(3), but with error messages printed if the fork fails
13661 +   or if the child process dies due to an uncaught signal. Also, the
13662 +   return value is a bit simpler:
13663 +
13664 +   -1 if there was any problem
13665 +   Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13666 +   as defined in <sys/wait.h>.
13667 +*/
13668 +int xsystem(const char *cmd)
13669 +{
13670 +    int err;
13671 +
13672 +    err = system(cmd);
13673 +
13674 +    if (err == -1) {
13675 +       fprintf(stderr, "%s: ERROR: fork failed before execution: `%s'\n",
13676 +               __FUNCTION__, cmd);
13677 +       return -1;
13678 +    }
13679 +
13680 +    if (WIFSIGNALED(err)) {
13681 +       fprintf(stderr, "%s: ERROR: Child process died due to signal %d: `%s'\n",
13682 +               __FUNCTION__, WTERMSIG(err), cmd);
13683 +       return -1;
13684 +    }
13685 +
13686 +    if (WIFEXITED(err)) {
13687 +       /* Normal child exit */
13688 +       return WEXITSTATUS(err);
13689 +    }
13690 +
13691 +    fprintf(stderr, "%s: ERROR: Received unintelligible return value from system: %d",
13692 +           __FUNCTION__, err);
13693 +    return -1;
13694 +}
13695 +        
13696 diff -ruN busybox-1.2.0-orig/archival/libipkg/xsystem.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.h
13697 --- busybox-1.2.0-orig/archival/libipkg/xsystem.h       1970-01-01 01:00:00.000000000 +0100
13698 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.h      2006-07-22 16:31:26.000000000 +0200
13699 @@ -0,0 +1,34 @@
13700 +/* xsystem.h - system(3) with error messages
13701 +
13702 +   Carl D. Worth
13703 +
13704 +   Copyright (C) 2001 University of Southern California
13705 +
13706 +   This program is free software; you can redistribute it and/or
13707 +   modify it under the terms of the GNU General Public License as
13708 +   published by the Free Software Foundation; either version 2, or (at
13709 +   your option) any later version.
13710 +
13711 +   This program is distributed in the hope that it will be useful, but
13712 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13713 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13714 +   General Public License for more details.
13715 +*/
13716 +
13717 +#ifndef XSYSTEM_H
13718 +#define XSYSTEM_H
13719 +
13720 +#include <stdlib.h>
13721 +
13722 +/* Like system(3), but with error messages printed if the fork fails
13723 +   or if the child process dies due to an uncaught signal. Also, the
13724 +   return value is a bit simpler:
13725 +
13726 +   -1 if there was any problem
13727 +   Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13728 +   as defined in <sys/wait.h>.
13729 +*/
13730 +int xsystem(const char *cmd);
13731 +
13732 +#endif
13733 +        
13734 diff -ruN busybox-1.2.0-orig/archival/libunarchive/data_extract_all.c busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/data_extract_all.c
13735 --- busybox-1.2.0-orig/archival/libunarchive/data_extract_all.c 2006-07-01 00:42:04.000000000 +0200
13736 +++ busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/data_extract_all.c        2006-07-22 16:31:26.000000000 +0200
13737 @@ -126,3 +126,17 @@
13738                 utime(file_header->name, &t);
13739         }
13740  }
13741 +
13742 +extern void data_extract_all_prefix(archive_handle_t *archive_handle)
13743 +{
13744 +       char *name_ptr = archive_handle->file_header->name;
13745 +
13746 +       name_ptr += strspn(name_ptr, "./");
13747 +       if (name_ptr[0] != '\0') {
13748 +               archive_handle->file_header->name = xmalloc(strlen(archive_handle->buffer) + 1 + strlen(name_ptr) + 1);
13749 +               strcpy(archive_handle->file_header->name, archive_handle->buffer);
13750 +               strcat(archive_handle->file_header->name, name_ptr);
13751 +               data_extract_all(archive_handle);
13752 +       }
13753 +}
13754 +
13755 diff -ruN busybox-1.2.0-orig/archival/libunarchive/Makefile.in busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/Makefile.in
13756 --- busybox-1.2.0-orig/archival/libunarchive/Makefile.in        2006-07-01 00:42:03.000000000 +0200
13757 +++ busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/Makefile.in       2006-07-22 16:31:26.000000000 +0200
13758 @@ -58,6 +58,7 @@
13759  LIBUNARCHIVE-$(CONFIG_FEATURE_DEB_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o
13760  LIBUNARCHIVE-$(CONFIG_GUNZIP) += $(GUNZIP_FILES)
13761  LIBUNARCHIVE-$(CONFIG_FEATURE_GUNZIP_UNCOMPRESS) += decompress_uncompress.o
13762 +LIBUNARCHIVE-$(CONFIG_IPKG) += $(GUNZIP_FILES) get_header_tar.o get_header_tar_gz.o
13763  LIBUNARCHIVE-$(CONFIG_RPM2CPIO) += $(GUNZIP_FILES) get_header_cpio.o
13764  LIBUNARCHIVE-$(CONFIG_RPM) += $(GUNZIP_FILES) get_header_cpio.o
13765  LIBUNARCHIVE-$(CONFIG_TAR) += get_header_tar.o
13766 diff -ruN busybox-1.2.0-orig/archival/Makefile.in busybox-1.2.0+ipkg-0.99.162/archival/Makefile.in
13767 --- busybox-1.2.0-orig/archival/Makefile.in     2006-07-01 00:42:04.000000000 +0200
13768 +++ busybox-1.2.0+ipkg-0.99.162/archival/Makefile.in    2006-07-22 16:31:25.000000000 +0200
13769 @@ -20,6 +20,7 @@
13770  ARCHIVAL-$(CONFIG_DPKG_DEB)    += dpkg_deb.o
13771  ARCHIVAL-$(CONFIG_GUNZIP)      += gunzip.o
13772  ARCHIVAL-$(CONFIG_GZIP)                += gzip.o
13773 +ARCHIVAL-$(CONFIG_IPKG)                += ipkg.o
13774  ARCHIVAL-$(CONFIG_RPM2CPIO)    += rpm2cpio.o
13775  ARCHIVAL-$(CONFIG_RPM)         += rpm.o
13776  ARCHIVAL-$(CONFIG_TAR)         += tar.o
13777 diff -ruN busybox-1.2.0-orig/include/applets.h busybox-1.2.0+ipkg-0.99.162/include/applets.h
13778 --- busybox-1.2.0-orig/include/applets.h        2006-07-01 00:42:10.000000000 +0200
13779 +++ busybox-1.2.0+ipkg-0.99.162/include/applets.h       2006-07-22 16:35:35.000000000 +0200
13780 @@ -152,6 +152,7 @@
13781  USE_IPCALC(APPLET(ipcalc, _BB_DIR_BIN, _BB_SUID_NEVER))
13782  USE_IPCRM(APPLET(ipcrm, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13783  USE_IPCS(APPLET(ipcs, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13784 +USE_IPKG(APPLET(ipkg, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
13785  USE_IPLINK(APPLET(iplink, _BB_DIR_BIN, _BB_SUID_NEVER))
13786  USE_IPROUTE(APPLET(iproute, _BB_DIR_BIN, _BB_SUID_NEVER))
13787  USE_IPTUNNEL(APPLET(iptunnel, _BB_DIR_BIN, _BB_SUID_NEVER))
13788 diff -ruN busybox-1.2.0-orig/include/unarchive.h busybox-1.2.0+ipkg-0.99.162/include/unarchive.h
13789 --- busybox-1.2.0-orig/include/unarchive.h      2006-07-01 00:42:10.000000000 +0200
13790 +++ busybox-1.2.0+ipkg-0.99.162/include/unarchive.h     2006-07-22 16:31:26.000000000 +0200
13791 @@ -77,6 +77,7 @@
13792  
13793  extern void data_skip(archive_handle_t *archive_handle);
13794  extern void data_extract_all(archive_handle_t *archive_handle);
13795 +extern void data_extract_all_prefix(archive_handle_t *archive_handle);
13796  extern void data_extract_to_stdout(archive_handle_t *archive_handle);
13797  extern void data_extract_to_buffer(archive_handle_t *archive_handle);
13798  
13799 diff -ruN busybox-1.2.0-orig/include/usage.h busybox-1.2.0+ipkg-0.99.162/include/usage.h
13800 --- busybox-1.2.0-orig/include/usage.h  2006-07-01 00:42:10.000000000 +0200
13801 +++ busybox-1.2.0+ipkg-0.99.162/include/usage.h 2006-07-22 16:31:26.000000000 +0200
13802 @@ -982,6 +982,82 @@
13803         "$ ls -la /tmp/busybox*\n" \
13804         "-rw-rw-r--    1 andersen andersen   554058 Apr 14 17:49 /tmp/busybox.tar.gz\n"
13805  
13806 +#define ipkg_trivial_usage \
13807 +       "[options]... sub-command [arguments]..."
13808 +#define ipkg_full_usage \
13809 +       "ipkg is an utility to install, remove and manage .ipk packages.\n" \
13810 +       "\n" \
13811 +       "Sub-commands:\n" \
13812 +       "\nPackage Manipulation:\n" \
13813 +       "\tupdate               Update list of available packages\n" \
13814 +       "\tupgrade                      Upgrade all installed packages to latest version\n" \
13815 +       "\tinstall <pkg>                Download and install <pkg> (and dependencies)\n" \
13816 +       "\tinstall <file.ipk>   Install package <file.ipk>\n" \
13817 +       "\tconfigure [<pkg>]    Configure unpacked packages\n" \
13818 +       "\tremove <pkg|regexp>  Remove package <pkg|packages following regexp>\n" \
13819 +       "\tflag <flag> <pkg> ...        Flag package(s) <pkg>\n" \
13820 +       "\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation) \n" \
13821 +       "\n" \
13822 +       "Informational Commands:\n" \
13823 +       "\tlist                 List available packages and descriptions\n" \
13824 +       "\tlist_installed               List all and only the installed packages and description \n" \
13825 +       "\tfiles <pkg>          List all files belonging to <pkg>\n" \
13826 +       "\tsearch <file|regexp>         Search for a package providing <file>\n" \
13827 +       "\tinfo [pkg|regexp [<field>]]  Display all/some info fields for <pkg> or all\n" \
13828 +       "\tstatus [pkg|regexp [<field>]]        Display all/some status fields for <pkg> or all\n" \
13829 +       "\tdownload <pkg>               Download <pkg> to current directory.\n" \
13830 +       "\tcompare_versions <v1> <op> <v2>\n" \
13831 +       "\t                          compare versions using <= < > >= = << >>\n" \
13832 +       "\tprint_architecture      prints the architecture.\n" \
13833 +       "\tprint_installation_architecture\n" \
13834 +       "\twhatdepends [-A] [pkgname|pat]+\n" \
13835 +       "\twhatdependsrec [-A] [pkgname|pat]+\n" \
13836 +       "\twhatprovides [-A] [pkgname|pat]+\n" \
13837 +       "\twhatconflicts [-A] [pkgname|pat]+\n" \
13838 +       "\twhatreplaces [-A] [pkgname|pat]+\n" \
13839 +       "\t                        prints the installation architecture.\n" \
13840 +       "\n" \
13841 +       "\nOptions:\n" \
13842 +       "\t-A                      Query all packages with whatdepends, whatprovides, whatreplaces, whatconflicts\n" \
13843 +       "\t-V <level>               Set verbosity level to <level>. If no value is\n" \
13844 +       "\t--verbosity <level>      provided increase verbosity by one. Verbosity levels:\n" \
13845 +       "\t                         0 errors only\n" \
13846 +       "\t                         1 normal messages (default)\n" \
13847 +       "\t                         2 informative messages\n" \
13848 +       "\t                         3 debug output\n" \
13849 +       "\t-f <conf_file>               Use <conf_file> as the ipkg configuration file\n" \
13850 +       "\t-conf <conf_file>    Default configuration file location\n" \
13851 +       "                               is /etc/ipkg.conf\n" \
13852 +       "\t-d <dest_name>               Use <dest_name> as the the root directory for\n" \
13853 +       "\t-dest <dest_name>    package installation, removal, upgrading.\n" \
13854 +       "                               <dest_name> should be a defined dest name from\n" \
13855 +       "                               the configuration file, (but can also be a\n" \
13856 +       "                               directory name in a pinch).\n" \
13857 +       "\t-o <offline_root>    Use <offline_root> as the root directory for\n" \
13858 +       "\t-offline <offline_root>      offline installation of packages.\n" \
13859 +       "\t-verbose_wget                more wget messages\n" \
13860 +       "\n" \
13861 +       "Force Options (use when ipkg is too smart for its own good):\n" \
13862 +       "\t-force-depends               Make dependency checks warnings instead of errors\n" \
13863 +       "\t                             Install/remove package in spite of failed dependences\n" \
13864 +       "\t-force-defaults              Use default options for questions asked by ipkg.\n" \
13865 +       "                               (no prompts). Note that this will not prevent\n" \
13866 +       "                               package installation scripts from prompting.\n" \
13867 +       "\t-force-reinstall     Allow ipkg to reinstall a package.\n" \
13868 +       "\t-force-overwrite     Allow ipkg to overwrite files from another package during an install.\n" \
13869 +       "\t-force-downgrade     Allow ipkg to downgrade packages.\n" \
13870 +       "\t-force_space            Install even if there does not seem to be enough space.\n" \
13871 +       "\t-noaction               No action -- test only\n" \
13872 +       "\t-nodeps                 Do not follow dependences\n" \
13873 +       "\t-force-removal-of-dependent-packages\n" \
13874 +       "\t-recursive           Allow ipkg to remove package and all that depend on it.\n" \
13875 +       "\t-test                   No action -- test only\n" \
13876 +       "\t-t                   Specify tmp-dir.\n" \
13877 +       "\t--tmp-dir            Specify tmp-dir.\n" \
13878 +       "\n" \
13879 +       "\tregexp could be something like 'pkgname*' '*file*' or similar\n" \
13880 +       "\teg: ipkg info 'libstd*' or ipkg search '*libop*' or ipkg remove 'libncur*'\n"
13881 +
13882  #define halt_trivial_usage \
13883         "[-d<delay>] [-n<nosync>] [-f<force>]"
13884  #define halt_full_usage \
13885 diff -ruN busybox-1.2.0-orig/Makefile busybox-1.2.0+ipkg-0.99.162/Makefile
13886 --- busybox-1.2.0-orig/Makefile 2006-07-01 00:42:13.000000000 +0200
13887 +++ busybox-1.2.0+ipkg-0.99.162/Makefile        2006-07-22 16:31:25.000000000 +0200
13888 @@ -31,7 +31,7 @@
13889  export srctree=$(top_srcdir)
13890  vpath %/Config.in $(srctree)
13891  
13892 -DIRS:=applets archival archival/libunarchive coreutils console-tools \
13893 +DIRS:=applets archival archival/libipkg archival/libunarchive coreutils console-tools \
13894         debianutils editors findutils init miscutils modutils networking \
13895         networking/libiproute networking/udhcp procps loginutils shell \
13896         sysklogd util-linux e2fsprogs libpwdgrp coreutils/libcoreutils libbb