opkg: remove redundant OPKG_LIB conditional code
[oweals/opkg-lede.git] / libopkg / opkg_cmd.c
1 /* opkg_cmd.c - the itsy package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include <string.h>
19
20 #include "opkg.h"
21 #include <libgen.h>
22 #include <glob.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <dirent.h>
29
30 #include "opkg_conf.h"
31 #include "opkg_cmd.h"
32 #include "opkg_message.h"
33 #include "pkg.h"
34 #include "pkg_dest.h"
35 #include "pkg_parse.h"
36 #include "sprintf_alloc.h"
37 #include "pkg.h"
38 #include "file_util.h"
39 #include "str_util.h"
40 #include "libbb/libbb.h"
41
42 #include <fnmatch.h>
43
44
45 #include "opkg_download.h"
46 #include "opkg_install.h"
47 #include "opkg_upgrade.h"
48 #include "opkg_remove.h"
49 #include "opkg_configure.h"
50 #include "opkg_message.h"
51
52 #include "libopkg.h"
53 static void *p_userdata = NULL;
54
55 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv);
56 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv);
57 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv);
58 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv);
59 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv);
60 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv);
61 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv);
62 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv);
63 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv);
64 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv);
65 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv);
66 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv);
67 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv);
68 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv);
69 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv);
70 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv);
71 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv);
72 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv);
73 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv);
74 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv);
75 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv);
76 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv);
77 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv);
78 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv);
79 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv);
80
81 /* XXX: CLEANUP: The usage strings should be incorporated into this
82    array for easier maintenance */
83 static opkg_cmd_t cmds[] = {
84      {"update", 0, (opkg_cmd_fun_t)opkg_update_cmd}, 
85      {"upgrade", 0, (opkg_cmd_fun_t)opkg_upgrade_cmd},
86      {"list", 0, (opkg_cmd_fun_t)opkg_list_cmd},
87      {"list_installed", 0, (opkg_cmd_fun_t)opkg_list_installed_cmd},
88      {"info", 0, (opkg_cmd_fun_t)opkg_info_cmd},
89      {"flag", 1, (opkg_cmd_fun_t)opkg_flag_cmd},
90      {"status", 0, (opkg_cmd_fun_t)opkg_status_cmd},
91      {"install_pending", 0, (opkg_cmd_fun_t)opkg_install_pending_cmd},
92      {"install", 1, (opkg_cmd_fun_t)opkg_install_cmd},
93      {"remove", 1, (opkg_cmd_fun_t)opkg_remove_cmd},
94      {"purge", 1, (opkg_cmd_fun_t)opkg_purge_cmd},
95      {"configure", 0, (opkg_cmd_fun_t)opkg_configure_cmd},
96      {"files", 1, (opkg_cmd_fun_t)opkg_files_cmd},
97      {"search", 1, (opkg_cmd_fun_t)opkg_search_cmd},
98      {"download", 1, (opkg_cmd_fun_t)opkg_download_cmd},
99      {"compare_versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
100      {"compare-versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
101      {"print-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
102      {"print_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
103      {"print-installation-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
104      {"print_installation_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
105      {"depends", 1, (opkg_cmd_fun_t)opkg_depends_cmd},
106      {"whatdepends", 1, (opkg_cmd_fun_t)opkg_whatdepends_cmd},
107      {"whatdependsrec", 1, (opkg_cmd_fun_t)opkg_whatdepends_recursively_cmd},
108      {"whatrecommends", 1, (opkg_cmd_fun_t)opkg_whatrecommends_cmd},
109      {"whatsuggests", 1, (opkg_cmd_fun_t)opkg_whatsuggests_cmd},
110      {"whatprovides", 1, (opkg_cmd_fun_t)opkg_whatprovides_cmd},
111      {"whatreplaces", 1, (opkg_cmd_fun_t)opkg_whatreplaces_cmd},
112      {"whatconflicts", 1, (opkg_cmd_fun_t)opkg_whatconflicts_cmd},
113 };
114
115 int opkg_state_changed;
116 static void write_status_files_if_changed(opkg_conf_t *conf)
117 {
118      if (opkg_state_changed && !conf->noaction) {
119           opkg_message(conf, OPKG_INFO,
120                        "  writing status file\n");
121           opkg_conf_write_status_files(conf);
122           pkg_write_changed_filelists(conf);
123      } else { 
124           opkg_message(conf, OPKG_NOTICE, "Nothing to be done\n");
125      }
126 }
127
128
129 static int num_cmds = sizeof(cmds) / sizeof(opkg_cmd_t);
130
131 opkg_cmd_t *opkg_cmd_find(const char *name)
132 {
133      int i;
134      opkg_cmd_t *cmd;
135
136      for (i=0; i < num_cmds; i++) {
137           cmd = &cmds[i];
138           if (strcmp(name, cmd->name) == 0) {
139                return cmd;
140           }
141      }
142
143      return NULL;
144 }
145
146 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
147 {
148         int result;
149         p_userdata = userdata;
150       
151
152         result = (cmd->fun)(conf, argc, argv);
153
154         if ( result != 0 ) {
155            opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
156         }
157
158         if ( error_list ) {
159            reverse_error_list(&error_list);
160
161            opkg_message(conf, OPKG_NOTICE, "Collected errors:\n");
162            /* Here we print the errors collected and free the list */
163            while (error_list != NULL) {
164                  opkg_message(conf, OPKG_NOTICE, "%s",error_list->errmsg);
165                  error_list = error_list->next;
166
167            }
168            free_error_list(&error_list);
169
170         }
171    
172         p_userdata = NULL;
173         return result;
174 }
175
176 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
177 {
178      char *tmp;
179      int err;
180      int failures;
181      char *lists_dir;
182      pkg_src_list_elt_t *iter;
183      pkg_src_t *src;
184
185  
186     sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
187  
188     if (! file_is_dir(lists_dir)) {
189           if (file_exists(lists_dir)) {
190                opkg_message(conf, OPKG_ERROR,
191                             "%s: ERROR: %s exists, but is not a directory\n",
192                             __FUNCTION__, lists_dir);
193                free(lists_dir);
194                return EINVAL;
195           }
196           err = file_mkdir_hier(lists_dir, 0755);
197           if (err) {
198                opkg_message(conf, OPKG_ERROR,
199                             "%s: ERROR: failed to make directory %s: %s\n",
200                             __FUNCTION__, lists_dir, strerror(errno));
201                free(lists_dir);
202                return EINVAL;
203           }     
204      } 
205
206      failures = 0;
207
208
209      tmp = strdup ("/tmp/opkg.XXXXXX");
210
211      if (mkdtemp (tmp) == NULL) {
212          perror ("mkdtemp");
213          failures++;
214      }
215
216
217      for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
218           char *url, *list_file_name;
219
220           src = iter->data;
221
222           if (src->extra_data)  /* debian style? */
223               sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, 
224                             src->gzip ? "Packages.gz" : "Packages");
225           else
226               sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
227
228           sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
229           if (src->gzip) {
230               char *tmp_file_name;
231               FILE *in, *out;
232               
233               sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
234               err = opkg_download(conf, url, tmp_file_name);
235               if (err == 0) {
236                    opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url);
237                    in = fopen (tmp_file_name, "r");
238                    out = fopen (list_file_name, "w");
239                    if (in && out)
240                         unzip (in, out);
241                    else
242                         err = 1;
243                    if (in)
244                         fclose (in);
245                    if (out)
246                         fclose (out);
247                    unlink (tmp_file_name);
248               }
249           } else
250               err = opkg_download(conf, url, list_file_name);
251           if (err) {
252                failures++;
253           } else {
254                opkg_message(conf, OPKG_NOTICE,
255                             "Updated list of available packages in %s\n",
256                             list_file_name);
257           }
258           free(url);
259
260           /* download detached signitures to verify the package lists */
261           /* get the url for the sig file */
262           if (src->extra_data)  /* debian style? */
263               sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
264                             "Packages.sig");
265           else
266               sprintf_alloc(&url, "%s/%s", src->value, "Packages.sig");
267
268           /* create temporary file for it */
269           char *tmp_file_name;
270
271           sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
272
273           err = opkg_download(conf, url, tmp_file_name);
274           if (err) {
275             failures++;
276                 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
277           } else {
278             int err;
279             err = opkg_verify_file (list_file_name, tmp_file_name);
280             if (err == 0)
281                 opkg_message (conf, OPKG_NOTICE, "Signature check passed\n");
282             else
283                 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
284           }
285           unlink (tmp_file_name);
286           free (tmp_file_name);
287
288           free (url);
289           free(list_file_name);
290      }
291      rmdir (tmp);
292      free (tmp);
293      free(lists_dir);
294
295 #ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
296 #warning here
297      /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
298       * this is a hack to work around poor bookkeeping in old opkg upgrade code 
299       * -Jamey 3/1/03
300       */
301      {
302           int i;
303           int changed = 0;
304           pkg_vec_t *available = pkg_vec_alloc();
305           pkg_hash_fetch_available(&conf->pkg_hash, available);
306           opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
307           for (i = 0; i < available->len; i++) {
308                pkg_t *pkg = available->pkgs[i];
309                if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
310                     opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
311                     pkg->state_want = SW_UNKNOWN;
312                     changed = 1;
313                }
314           }
315           pkg_vec_free(available);
316           if (changed) {
317                write_status_files_if_changed(conf);
318           }
319      }
320 #endif
321
322      return failures;
323 }
324
325
326 /* scan the args passed and cache the local filenames of the packages */
327 int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
328 {
329      int i;
330      int err;
331     
332      /* 
333       * First scan through package names/urls
334       * For any urls, download the packages and install in database.
335       * For any files, install package info in database.
336       */
337      for (i = 0; i < argc; i ++) {
338           char *filename = argv [i];
339           //char *tmp = basename (tmp);
340           //int tmplen = strlen (tmp);
341
342           //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
343           //     continue;
344           //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
345           //     continue;
346         
347           opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s  \n",filename );
348
349           err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
350           if (err)
351             return err;
352      }
353      return 0;
354 }
355
356 struct opkg_intercept
357 {
358     char *oldpath;
359     char *statedir;
360 };
361
362 typedef struct opkg_intercept *opkg_intercept_t;
363
364 opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
365 {
366     opkg_intercept_t ctx;
367     char *newpath;
368     int gen;
369
370     ctx = malloc (sizeof (*ctx));
371     ctx->oldpath = strdup (getenv ("PATH"));
372
373     sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
374     setenv ("PATH", newpath, 1);
375     free (newpath);
376     
377     gen = 0;
378  retry:
379     sprintf_alloc (&ctx->statedir, "/tmp/opkg-intercept-%d-%d", getpid (), gen);
380     if (mkdir (ctx->statedir, 0770) < 0) {
381         if (errno == EEXIST) {
382             free (ctx->statedir);
383             gen++;
384             goto retry;
385         }
386         perror (ctx->statedir);
387         return NULL;
388     }
389     setenv ("OPKG_INTERCEPT_DIR", ctx->statedir, 1);
390     return ctx;
391 }
392
393 int opkg_finalize_intercepts(opkg_intercept_t ctx)
394 {
395     char *cmd;
396     DIR *dir;
397     int err = 0;
398
399     setenv ("PATH", ctx->oldpath, 1);
400     free (ctx->oldpath);
401
402     dir = opendir (ctx->statedir);
403     if (dir) {
404         struct dirent *de;
405         while (de = readdir (dir), de != NULL) {
406             char *path;
407             
408             if (de->d_name[0] == '.')
409                 continue;
410             
411             sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
412             if (access (path, X_OK) == 0) {
413                 if (system (path)) {
414                     err = errno;
415                     perror (de->d_name);
416                 }
417             }
418             free (path);
419         }
420     } else
421         perror (ctx->statedir);
422         
423     sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
424     system (cmd);
425     free (cmd);
426
427     free (ctx->statedir);
428     free (ctx);
429
430     return err;
431 }
432
433 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
434 {
435      pkg_vec_t *all;
436      int i;
437      pkg_t *pkg;
438      opkg_intercept_t ic;
439      int r, err = 0;
440
441      opkg_message(conf, OPKG_INFO,
442                   "Configuring unpacked packages\n");
443      fflush( stdout );
444
445      all = pkg_vec_alloc();
446      pkg_hash_fetch_available(&conf->pkg_hash, all);
447
448      ic = opkg_prep_intercepts (conf);
449     
450      for(i = 0; i < all->len; i++) {
451           pkg = all->pkgs[i];
452
453           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
454                continue;
455
456           if (pkg->state_status == SS_UNPACKED) {
457                opkg_message(conf, OPKG_NOTICE,
458                             "Configuring %s\n", pkg->name);
459                fflush( stdout );
460                r = opkg_configure(conf, pkg);
461                if (r == 0) {
462                     pkg->state_status = SS_INSTALLED;
463                     pkg->parent->state_status = SS_INSTALLED;
464                     pkg->state_flag &= ~SF_PREFER;
465                } else {
466                     if (!err)
467                         err = r;
468                }
469           }
470      }
471
472      r = opkg_finalize_intercepts (ic);
473      if (r && !err)
474          err = r;
475
476      pkg_vec_free(all);
477      return err;
478 }
479
480 static opkg_conf_t *global_conf;
481
482 static void sigint_handler(int sig)
483 {
484      signal(sig, SIG_DFL);
485      opkg_message(NULL, OPKG_NOTICE,
486                   "opkg: interrupted. writing out status database\n");
487      write_status_files_if_changed(global_conf);
488      exit(128 + sig);
489 }
490
491 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv)
492 {
493      int i;
494      char *arg;
495      int err=0;
496
497      global_conf = conf;
498      signal(SIGINT, sigint_handler);
499
500      /*
501       * Now scan through package names and install
502       */
503      for (i=0; i < argc; i++) {
504           arg = argv[i];
505
506           opkg_message(conf, OPKG_DEBUG2, "Debug install_cmd: %s  \n",arg );
507           err = opkg_prepare_url_for_install(conf, arg, &argv[i]);
508           if (err != EINVAL && err != 0)
509               return err;
510      }
511      pkg_info_preinstall_check(conf);
512
513      for (i=0; i < argc; i++) {
514           arg = argv[i];
515           if (conf->multiple_providers)
516                err = opkg_install_multi_by_name(conf, arg);
517           else{
518                err = opkg_install_by_name(conf, arg);
519           }
520           if (err == OPKG_PKG_HAS_NO_CANDIDATE) {
521                opkg_message(conf, OPKG_ERROR,
522                             "Cannot find package %s.\n"
523                             "Check the spelling or perhaps run 'opkg update'\n",
524                             arg);
525           }
526      }
527
528      /* recheck to verify that all dependences are satisfied */
529      if (0) opkg_satisfy_all_dependences(conf);
530
531      opkg_configure_packages(conf, NULL);
532
533      write_status_files_if_changed(conf);
534
535      return err;
536 }
537
538 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv)
539 {
540      int i;
541      pkg_t *pkg;
542      int err;
543
544      global_conf = conf;
545      signal(SIGINT, sigint_handler);
546
547      if (argc) {
548           for (i=0; i < argc; i++) {
549                char *arg = argv[i];
550
551                err = opkg_prepare_url_for_install(conf, arg, &arg);
552                if (err != EINVAL && err != 0)
553                    return err;
554           }
555           pkg_info_preinstall_check(conf);
556
557           for (i=0; i < argc; i++) {
558                char *arg = argv[i];
559                if (conf->restrict_to_default_dest) {
560                     pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
561                                                                 argv[i],
562                                                                 conf->default_dest);
563                     if (pkg == NULL) {
564                          opkg_message(conf, OPKG_NOTICE,
565                                       "Package %s not installed in %s\n",
566                                       argv[i], conf->default_dest->name);
567                          continue;
568                     }
569                } else {
570                     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
571                                                            argv[i]);
572                }
573                if (pkg)
574                     opkg_upgrade_pkg(conf, pkg);
575                else {
576                     opkg_install_by_name(conf, arg);
577                }
578           }
579      } else {
580           pkg_vec_t *installed = pkg_vec_alloc();
581
582           pkg_info_preinstall_check(conf);
583
584           pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
585           for (i = 0; i < installed->len; i++) {
586                pkg = installed->pkgs[i];
587                opkg_upgrade_pkg(conf, pkg);
588           }
589           pkg_vec_free(installed);
590      }
591
592      /* recheck to verify that all dependences are satisfied */
593      if (0) opkg_satisfy_all_dependences(conf);
594
595      opkg_configure_packages(conf, NULL);
596
597      write_status_files_if_changed(conf);
598
599      return 0;
600 }
601
602 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv)
603 {
604      int i, err;
605      char *arg;
606      pkg_t *pkg;
607
608      pkg_info_preinstall_check(conf);
609      for (i = 0; i < argc; i++) {
610           arg = argv[i];
611
612           pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
613           if (pkg == NULL) {
614                opkg_message(conf, OPKG_ERROR,
615                             "Cannot find package %s.\n"
616                             "Check the spelling or perhaps run 'opkg update'\n",
617                             arg);
618                continue;
619           }
620
621           err = opkg_download_pkg(conf, pkg, ".");
622
623           if (err) {
624                opkg_message(conf, OPKG_ERROR,
625                             "Failed to download %s\n", pkg->name);
626           } else {
627                opkg_message(conf, OPKG_NOTICE,
628                             "Downloaded %s as %s\n",
629                             pkg->name, pkg->local_filename);
630           }
631      }
632
633      return 0;
634 }
635
636
637 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv)
638 {
639      int i ;
640      pkg_vec_t *available;
641      pkg_t *pkg;
642      char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
643      char *newline;
644      char *pkg_name = NULL;
645      char *version_str;
646
647      if (argc > 0) {
648           pkg_name = argv[0];
649      }
650      available = pkg_vec_alloc();
651      pkg_hash_fetch_available(&conf->pkg_hash, available);
652      for (i=0; i < available->len; i++) {
653           pkg = available->pkgs[i];
654           /* if we have package name or pattern and pkg does not match, then skip it */
655           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
656                continue;
657           if (pkg->description) {
658                strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
659           } else {
660                desc_short[0] = '\0';
661           }
662           desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
663           newline = strchr(desc_short, '\n');
664           if (newline) {
665                *newline = '\0';
666           }
667           if (opkg_cb_list) {
668                 version_str = pkg_version_str_alloc(pkg);
669                 opkg_cb_list(pkg->name,desc_short,
670                                              version_str,
671                                          pkg->state_status,
672                                          p_userdata);
673                 free(version_str);
674           }
675      }
676      pkg_vec_free(available);
677
678      return 0;
679 }
680
681
682 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv)
683 {
684      int i ;
685      pkg_vec_t *available;
686      pkg_t *pkg;
687      char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
688      char *newline;
689      char *pkg_name = NULL;
690      char *version_str;
691
692      if (argc > 0) {
693           pkg_name = argv[0];
694      }
695      available = pkg_vec_alloc();
696      pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
697      for (i=0; i < available->len; i++) {
698           pkg = available->pkgs[i];
699           /* if we have package name or pattern and pkg does not match, then skip it */
700           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
701                continue;
702           if (pkg->description) {
703                strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
704           } else {
705                desc_short[0] = '\0';
706           }
707           desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
708           newline = strchr(desc_short, '\n');
709           if (newline) {
710                *newline = '\0';
711           }
712           if (opkg_cb_list) {
713                 version_str = pkg_version_str_alloc(pkg);
714                 opkg_cb_list(pkg->name,desc_short,
715                                              version_str,
716                                          pkg->state_status,
717                                          p_userdata);
718                 free(version_str);
719           }
720      }
721
722      return 0;
723 }
724
725 static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int installed_only)
726 {
727      int i;
728      pkg_vec_t *available;
729      pkg_t *pkg;
730      char *pkg_name = NULL;
731      char **pkg_fields = NULL;
732      int n_fields = 0;
733      char *buff ; // = (char *)malloc(1);
734
735      if (argc > 0) {
736           pkg_name = argv[0];
737      }
738      if (argc > 1) {
739           pkg_fields = &argv[1];
740           n_fields = argc - 1;
741      }
742
743      available = pkg_vec_alloc();
744      if (installed_only)
745           pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
746      else
747           pkg_hash_fetch_available(&conf->pkg_hash, available);
748      for (i=0; i < available->len; i++) {
749           pkg = available->pkgs[i];
750           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
751                continue;
752           }
753
754           buff = pkg_formatted_info(pkg);
755           if ( buff ) {
756                if (opkg_cb_status) opkg_cb_status(pkg->name,
757                                                   pkg->state_status,
758                                                   buff,
759                                                   p_userdata);
760 /* 
761    We should not forget that actually the pointer is allocated. 
762    We need to free it :)  ( Thanks florian for seeing the error )
763 */
764                free(buff);
765           }
766           if (conf->verbosity > 1) {
767                conffile_list_elt_t *iter;
768                for (iter = pkg->conffiles.head; iter; iter = iter->next) {
769                     conffile_t *cf = iter->data;
770                     int modified = conffile_has_been_modified(conf, cf);
771                     opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
772                                  cf->name, cf->value, modified);
773                }
774           }
775      }
776      pkg_vec_free(available);
777
778      return 0;
779 }
780
781 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv)
782 {
783      return opkg_info_status_cmd(conf, argc, argv, 0);
784 }
785
786 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv)
787 {
788      return opkg_info_status_cmd(conf, argc, argv, 1);
789 }
790
791 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv)
792 {
793      
794      int err;
795      if (argc > 0) {
796           char *pkg_name = NULL;
797
798           pkg_name = argv[0];
799
800           err = opkg_configure_packages (conf, pkg_name);
801      
802      } else {
803           err = opkg_configure_packages (conf, NULL);
804      }
805
806      write_status_files_if_changed(conf);
807
808      return err;
809 }
810
811 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv)
812 {
813      int i, err;
814      char *globpattern;
815      glob_t globbuf;
816     
817      sprintf_alloc(&globpattern, "%s/*" OPKG_PKG_EXTENSION, conf->pending_dir);
818      err = glob(globpattern, 0, NULL, &globbuf);
819      free(globpattern);
820      if (err) {
821           return 0;
822      }
823
824      opkg_message(conf, OPKG_NOTICE,
825                   "The following packages in %s will now be installed.\n",
826                   conf->pending_dir);
827      for (i = 0; i < globbuf.gl_pathc; i++) {
828           opkg_message(conf, OPKG_NOTICE,
829                        "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
830      }
831      opkg_message(conf, OPKG_NOTICE, "\n");
832      for (i = 0; i < globbuf.gl_pathc; i++) {
833           err = opkg_install_from_file(conf, globbuf.gl_pathv[i]);
834           if (err == 0) {
835                err = unlink(globbuf.gl_pathv[i]);
836                if (err) {
837                     opkg_message(conf, OPKG_ERROR,
838                                  "%s: ERROR: failed to unlink %s: %s\n",
839                                  __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
840                     return err;
841                }
842           }
843      }
844      globfree(&globbuf);
845
846      return err;
847 }
848
849 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
850 {
851      int i,a,done;
852      pkg_t *pkg;
853      pkg_t *pkg_to_remove;
854      pkg_vec_t *available;
855      char *pkg_name = NULL;
856      global_conf = conf;
857      signal(SIGINT, sigint_handler);
858
859 // ENH: Add the "no pkg removed" just in case.
860
861     done = 0;
862
863      available = pkg_vec_alloc();
864      pkg_info_preinstall_check(conf);
865      if ( argc > 0 ) {
866         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
867         for (i=0; i < argc; i++) {
868            pkg_name = malloc(strlen(argv[i])+2);
869            strcpy(pkg_name,argv[i]);
870            for (a=0; a < available->len; a++) {
871                pkg = available->pkgs[a];
872                if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
873                   continue;
874                }
875                if (conf->restrict_to_default_dest) {
876                     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
877                                                                 pkg->name,
878                                                                 conf->default_dest);
879                } else {
880                     pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
881                }
882         
883                if (pkg == NULL) {
884                     opkg_message(conf, OPKG_ERROR, "Package %s is not installed.\n", pkg->name);
885                     continue;
886                }
887                if (pkg->state_status == SS_NOT_INSTALLED) {    // Added the control, so every already removed package could be skipped
888                     opkg_message(conf, OPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
889                     continue;
890                }
891                opkg_remove_pkg(conf, pkg_to_remove,0);
892                done = 1;
893            }
894            free (pkg_name);
895         }
896         pkg_vec_free(available);
897      } else {
898           pkg_vec_t *installed_pkgs = pkg_vec_alloc();
899           int i;
900           int flagged_pkg_count = 0;
901           int removed;
902
903           pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
904
905           for (i = 0; i < installed_pkgs->len; i++) {
906                pkg_t *pkg = installed_pkgs->pkgs[i];
907                if (pkg->state_flag & SF_USER) {
908                     flagged_pkg_count++;
909                } else {
910                     if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
911                          opkg_message(conf, OPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
912                }
913           }
914           if (!flagged_pkg_count) {
915                opkg_message(conf, OPKG_NOTICE, "No packages flagged as installed by user, \n"
916                             "so refusing to uninstall unflagged non-leaf packages\n");
917                return 0;
918           }
919
920           /* find packages not flagged SF_USER (i.e., installed to
921            * satisfy a dependence) and not having any dependents, and
922            * remove them */
923           do {
924                removed = 0;
925                for (i = 0; i < installed_pkgs->len; i++) {
926                     pkg_t *pkg = installed_pkgs->pkgs[i];
927                     if (!(pkg->state_flag & SF_USER)
928                         && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
929                          removed++;
930                          opkg_message(conf, OPKG_NOTICE, "Removing non-user leaf package %s\n");
931                          opkg_remove_pkg(conf, pkg,0);
932                          done = 1;
933                     }
934                }
935           } while (removed);
936           pkg_vec_free(installed_pkgs);
937      }
938
939      if ( done == 0 ) 
940         opkg_message(conf, OPKG_NOTICE, "No packages removed.\n");
941
942      write_status_files_if_changed(conf);
943      return 0;
944 }
945
946 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv)
947 {
948      int i;
949      pkg_t *pkg;
950
951      global_conf = conf;
952      signal(SIGINT, sigint_handler);
953
954      pkg_info_preinstall_check(conf);
955
956      for (i=0; i < argc; i++) {
957           if (conf->restrict_to_default_dest) {
958                pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
959                                                            argv[i],
960                                                            conf->default_dest);
961           } else {
962                pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
963           }
964
965           if (pkg == NULL) {
966                opkg_message(conf, OPKG_ERROR,
967                             "Package %s is not installed.\n", argv[i]);
968                continue;
969           }
970           opkg_purge_pkg(conf, pkg);
971      }
972
973      write_status_files_if_changed(conf);
974      return 0;
975 }
976
977 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv)
978 {
979      int i;
980      pkg_t *pkg;
981      const char *flags = argv[0];
982     
983      global_conf = conf;
984      signal(SIGINT, sigint_handler);
985
986      for (i=1; i < argc; i++) {
987           if (conf->restrict_to_default_dest) {
988                pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
989                                                            argv[i],
990                                                            conf->default_dest);
991           } else {
992                pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
993           }
994
995           if (pkg == NULL) {
996                opkg_message(conf, OPKG_ERROR,
997                             "Package %s is not installed.\n", argv[i]);
998                continue;
999           }
1000           if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1001               ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1002               pkg->state_flag = pkg_state_flag_from_str(flags);
1003           }
1004 /* pb_ asked this feature 03292004 */
1005 /* Actually I will use only this two, but this is an open for various status */
1006           if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1007               pkg->state_status = pkg_state_status_from_str(flags);
1008           }
1009           opkg_state_changed++;
1010           opkg_message(conf, OPKG_NOTICE,
1011                        "Setting flags for package %s to %s\n",
1012                        pkg->name, flags);
1013      }
1014
1015      write_status_files_if_changed(conf);
1016      return 0;
1017 }
1018
1019 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv)
1020 {
1021      pkg_t *pkg;
1022      str_list_t *installed_files;
1023      str_list_elt_t *iter;
1024      char *pkg_version;
1025      size_t buff_len = 8192;
1026      size_t used_len;
1027      char *buff ;
1028
1029      buff = (char *)malloc(buff_len);
1030      if ( buff == NULL ) {
1031         fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1032         return ENOMEM;
1033      }
1034  
1035      if (argc < 1) {
1036           return EINVAL;
1037      }
1038
1039      pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1040                                             argv[0]);
1041      if (pkg == NULL) {
1042           opkg_message(conf, OPKG_ERROR,
1043                        "Package %s not installed.\n", argv[0]);
1044           return 0;
1045      }
1046
1047      installed_files = pkg_get_installed_files(pkg);
1048      pkg_version = pkg_version_str_alloc(pkg);
1049
1050      if (buff) {
1051      try_again:
1052           used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1053                               pkg->name, pkg_version, pkg->dest->name) + 1;
1054           if (used_len > buff_len) {
1055                buff_len *= 2;
1056                buff = realloc (buff, buff_len);
1057                goto try_again;
1058           }
1059           for (iter = installed_files->head; iter; iter = iter->next) {
1060                used_len += strlen (iter->data) + 1;
1061                while (buff_len <= used_len) {
1062                     buff_len *= 2;
1063                     buff = realloc (buff, buff_len);
1064                }
1065                strncat(buff, iter->data, buff_len);
1066                strncat(buff, "\n", buff_len);
1067           } 
1068           if (opkg_cb_list) opkg_cb_list(pkg->name,
1069                                          buff,
1070                                          pkg_version_str_alloc(pkg),
1071                                          pkg->state_status,
1072                                          p_userdata);
1073           free(buff);
1074      }
1075
1076      free(pkg_version);
1077      pkg_free_installed_files(pkg);
1078
1079      return 0;
1080 }
1081
1082 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv)
1083 {
1084
1085      if (argc > 0) {
1086           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1087           const char *rel_str = "depends on";
1088           int i;
1089      
1090           pkg_info_preinstall_check(conf);
1091
1092           if (conf->query_all)
1093                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1094           else
1095                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1096           for (i = 0; i < argc; i++) {
1097                const char *target = argv[i];
1098                int j;
1099
1100                opkg_message(conf, OPKG_ERROR, "target=%s\n", target);
1101
1102                for (j = 0; j < available_pkgs->len; j++) {
1103                     pkg_t *pkg = available_pkgs->pkgs[j];
1104                     if (fnmatch(target, pkg->name, 0) == 0) {
1105                          int k;
1106                          int count = pkg->depends_count + pkg->pre_depends_count;
1107                          opkg_message(conf, OPKG_ERROR, "What %s (arch=%s) %s\n",
1108                                       target, pkg->architecture, rel_str);
1109                          for (k = 0; k < count; k++) {
1110                               compound_depend_t *cdepend = &pkg->depends[k];
1111                               int l;
1112                               for (l = 0; l < cdepend->possibility_count; l++) {
1113                                    depend_t *possibility = cdepend->possibilities[l];
1114                                    opkg_message(conf, OPKG_ERROR, "    %s", possibility->pkg->name);
1115                                    if (conf->verbosity > 0) {
1116                                         // char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 
1117                                         opkg_message(conf, OPKG_NOTICE, " %s", possibility->version);
1118                                         if (possibility->version) {
1119                                              char *typestr = NULL;
1120                                              switch (possibility->constraint) {
1121                                              case NONE: typestr = "none"; break;
1122                                              case EARLIER: typestr = "<"; break;
1123                                              case EARLIER_EQUAL: typestr = "<="; break;
1124                                              case EQUAL: typestr = "="; break;
1125                                              case LATER_EQUAL: typestr = ">="; break;
1126                                              case LATER: typestr = ">"; break;
1127                                              }
1128                                              opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1129                                         }
1130                                         // free(ver);
1131                                    }
1132                                    opkg_message(conf, OPKG_ERROR, "\n");
1133                               }
1134                          }
1135                     }
1136                }
1137           }
1138           pkg_vec_free(available_pkgs);
1139      }
1140      return 0;
1141 }
1142
1143 enum what_field_type {
1144   WHATDEPENDS,
1145   WHATCONFLICTS,
1146   WHATPROVIDES,
1147   WHATREPLACES,
1148   WHATRECOMMENDS,
1149   WHATSUGGESTS
1150 };
1151
1152 static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1153 {
1154
1155      if (argc > 0) {
1156           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1157           const char *rel_str = NULL;
1158           int i;
1159           int changed;
1160
1161           switch (what_field_type) {
1162           case WHATDEPENDS: rel_str = "depends on"; break;
1163           case WHATCONFLICTS: rel_str = "conflicts with"; break;
1164           case WHATSUGGESTS: rel_str = "suggests"; break;
1165           case WHATRECOMMENDS: rel_str = "recommends"; break;
1166           case WHATPROVIDES: rel_str = "provides"; break;
1167           case WHATREPLACES: rel_str = "replaces"; break;
1168           }
1169      
1170           if (conf->query_all)
1171                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1172           else
1173                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1174
1175           /* mark the root set */
1176           pkg_vec_clear_marks(available_pkgs);
1177           opkg_message(conf, OPKG_NOTICE, "Root set:\n");
1178           for (i = 0; i < argc; i++) {
1179                const char *dependee_pattern = argv[i];
1180                pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1181           }
1182           for (i = 0; i < available_pkgs->len; i++) {
1183                pkg_t *pkg = available_pkgs->pkgs[i];
1184                if (pkg->state_flag & SF_MARKED) {
1185                     /* mark the parent (abstract) package */
1186                     pkg_mark_provides(pkg);
1187                     opkg_message(conf, OPKG_NOTICE, "  %s\n", pkg->name);
1188                }
1189           }
1190
1191           opkg_message(conf, OPKG_NOTICE, "What %s root set\n", rel_str);
1192           do {
1193                int j;
1194                changed = 0;
1195
1196                for (j = 0; j < available_pkgs->len; j++) {
1197                     pkg_t *pkg = available_pkgs->pkgs[j];
1198                     int k;
1199                     int count = ((what_field_type == WHATCONFLICTS)
1200                                  ? pkg->conflicts_count
1201                                  : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1202                     /* skip this package if it is already marked */
1203                     if (pkg->parent->state_flag & SF_MARKED) {
1204                          continue;
1205                     }
1206                     for (k = 0; k < count; k++) {
1207                          compound_depend_t *cdepend = 
1208                               (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1209                          int l;
1210                          for (l = 0; l < cdepend->possibility_count; l++) {
1211                               depend_t *possibility = cdepend->possibilities[l];
1212                               if (possibility->pkg->state_flag & SF_MARKED) {
1213                                    /* mark the depending package so we won't visit it again */
1214                                    pkg->state_flag |= SF_MARKED;
1215                                    pkg_mark_provides(pkg);
1216                                    changed++;
1217
1218                                    opkg_message(conf, OPKG_NOTICE, "    %s", pkg->name);
1219                                    if (conf->verbosity > 0) {
1220                                         char *ver = pkg_version_str_alloc(pkg); 
1221                                         opkg_message(conf, OPKG_NOTICE, " %s", ver);
1222                                         opkg_message(conf, OPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1223                                         if (possibility->version) {
1224                                              char *typestr = NULL;
1225                                              switch (possibility->constraint) {
1226                                              case NONE: typestr = "none"; break;
1227                                              case EARLIER: typestr = "<"; break;
1228                                              case EARLIER_EQUAL: typestr = "<="; break;
1229                                              case EQUAL: typestr = "="; break;
1230                                              case LATER_EQUAL: typestr = ">="; break;
1231                                              case LATER: typestr = ">"; break;
1232                                              }
1233                                              opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1234                                         }
1235                                         free(ver);
1236                                         if (!pkg_dependence_satisfiable(conf, possibility))
1237                                              opkg_message(conf, OPKG_NOTICE, " unsatisfiable");
1238                                    }
1239                                    opkg_message(conf, OPKG_NOTICE, "\n");
1240                                    goto next_package;
1241                               }
1242                          }
1243                     }
1244                next_package:
1245                     ;
1246                }
1247           } while (changed && recursive);
1248           pkg_vec_free(available_pkgs);
1249      }
1250
1251      return 0;
1252 }
1253
1254 int pkg_mark_provides(pkg_t *pkg)
1255 {
1256      int provides_count = pkg->provides_count;
1257      abstract_pkg_t **provides = pkg->provides;
1258      int i;
1259      pkg->parent->state_flag |= SF_MARKED;
1260      for (i = 0; i < provides_count; i++) {
1261           provides[i]->state_flag |= SF_MARKED;
1262      }
1263      return 0;
1264 }
1265
1266 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv)
1267 {
1268      return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1269 }
1270 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv)
1271 {
1272      return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1273 }
1274
1275 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv)
1276 {
1277      return opkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1278 }
1279
1280 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv)
1281 {
1282      return opkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1283 }
1284
1285 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv)
1286 {
1287      return opkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1288 }
1289
1290 static int opkg_what_provides_replaces_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1291 {
1292
1293      if (argc > 0) {
1294           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1295           const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1296           int i;
1297      
1298           pkg_info_preinstall_check(conf);
1299
1300           if (conf->query_all)
1301                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1302           else
1303                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1304           for (i = 0; i < argc; i++) {
1305                const char *target = argv[i];
1306                int j;
1307
1308                opkg_message(conf, OPKG_ERROR, "What %s %s\n",
1309                             rel_str, target);
1310                for (j = 0; j < available_pkgs->len; j++) {
1311                     pkg_t *pkg = available_pkgs->pkgs[j];
1312                     int k;
1313                     int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1314                     for (k = 0; k < count; k++) {
1315                          abstract_pkg_t *apkg = 
1316                               ((what_field_type == WHATPROVIDES) 
1317                                ? pkg->provides[k]
1318                                : pkg->replaces[k]);
1319                          if (fnmatch(target, apkg->name, 0) == 0) {
1320                               opkg_message(conf, OPKG_ERROR, "    %s", pkg->name);
1321                               if (strcmp(target, apkg->name) != 0)
1322                                    opkg_message(conf, OPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1323                               opkg_message(conf, OPKG_ERROR, "\n");
1324                          }
1325                     }
1326                }
1327           }
1328           pkg_vec_free(available_pkgs);
1329      }
1330      return 0;
1331 }
1332
1333 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv)
1334 {
1335      return opkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1336 }
1337
1338 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv)
1339 {
1340      return opkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1341 }
1342
1343 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv)
1344 {
1345      int i;
1346
1347      pkg_vec_t *installed;
1348      pkg_t *pkg;
1349      str_list_t *installed_files;
1350      str_list_elt_t *iter;
1351      char *installed_file;
1352
1353      if (argc < 1) {
1354           return EINVAL;
1355      }
1356  
1357      installed = pkg_vec_alloc();
1358      pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1359
1360      for (i=0; i < installed->len; i++) {
1361           pkg = installed->pkgs[i];
1362
1363           installed_files = pkg_get_installed_files(pkg);
1364
1365           for (iter = installed_files->head; iter; iter = iter->next) {
1366                installed_file = iter->data;
1367                if (fnmatch(argv[0], installed_file, 0)==0)  {
1368                         if (opkg_cb_list) opkg_cb_list(pkg->name, 
1369                                                        installed_file, 
1370                                                        pkg_version_str_alloc(pkg), 
1371                                                        pkg->state_status, p_userdata);
1372                }                
1373           }
1374
1375           pkg_free_installed_files(pkg);
1376      }
1377
1378      /* XXX: CLEANUP: It's not obvious from the name of
1379         pkg_hash_fetch_all_installed that we need to call
1380         pkg_vec_free to avoid a memory leak. */
1381      pkg_vec_free(installed);
1382
1383      return 0;
1384 }
1385
1386 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv)
1387 {
1388      if (argc == 3) {
1389           /* this is a bit gross */
1390           struct pkg p1, p2;
1391           parseVersion(&p1, argv[0]); 
1392           parseVersion(&p2, argv[2]); 
1393           return pkg_version_satisfied(&p1, &p2, argv[1]);
1394      } else {
1395           opkg_message(conf, OPKG_ERROR,
1396                        "opkg compare_versions <v1> <op> <v2>\n"
1397                        "<op> is one of <= >= << >> =\n");
1398           return -1;
1399      }
1400 }
1401
1402 #ifndef HOST_CPU_STR
1403 #define HOST_CPU_STR__(X) #X
1404 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1405 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1406 #endif
1407
1408 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv)
1409 {
1410      nv_pair_list_elt_t *l;
1411
1412      l = conf->arch_list.head;
1413      while (l) {
1414           nv_pair_t *nv = l->data;
1415           printf("arch %s %s\n", nv->name, nv->value);
1416           l = l->next;
1417      }
1418      return 0;
1419 }
1420
1421