opkg: skip downloading Packages.sig if GPGME is not enabled at build time
[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 #ifdef HAVE_GPGME
261           /* download detached signitures to verify the package lists */
262           /* get the url for the sig file */
263           if (src->extra_data)  /* debian style? */
264               sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
265                             "Packages.sig");
266           else
267               sprintf_alloc(&url, "%s/%s", src->value, "Packages.sig");
268
269           /* create temporary file for it */
270           char *tmp_file_name;
271
272           sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
273
274           err = opkg_download(conf, url, tmp_file_name);
275           if (err) {
276             failures++;
277                 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
278           } else {
279             int err;
280             err = opkg_verify_file (list_file_name, tmp_file_name);
281             if (err == 0)
282                 opkg_message (conf, OPKG_NOTICE, "Signature check passed\n");
283             else
284                 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
285           }
286           unlink (tmp_file_name);
287           free (tmp_file_name);
288           free (url);
289 #else
290           opkg_message (conf, OPKG_NOTICE, "Signiture check for %s skipped "
291               "because GPG support was not enabled in this build\n", src->name);
292 #endif
293           free(list_file_name);
294      }
295      rmdir (tmp);
296      free (tmp);
297      free(lists_dir);
298
299      return failures;
300 }
301
302
303 /* scan the args passed and cache the local filenames of the packages */
304 int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
305 {
306      int i;
307      int err;
308     
309      /* 
310       * First scan through package names/urls
311       * For any urls, download the packages and install in database.
312       * For any files, install package info in database.
313       */
314      for (i = 0; i < argc; i ++) {
315           char *filename = argv [i];
316           //char *tmp = basename (tmp);
317           //int tmplen = strlen (tmp);
318
319           //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
320           //     continue;
321           //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
322           //     continue;
323         
324           opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s  \n",filename );
325
326           err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
327           if (err)
328             return err;
329      }
330      return 0;
331 }
332
333 struct opkg_intercept
334 {
335     char *oldpath;
336     char *statedir;
337 };
338
339 typedef struct opkg_intercept *opkg_intercept_t;
340
341 opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
342 {
343     opkg_intercept_t ctx;
344     char *newpath;
345     int gen;
346
347     ctx = malloc (sizeof (*ctx));
348     ctx->oldpath = strdup (getenv ("PATH"));
349
350     sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
351     setenv ("PATH", newpath, 1);
352     free (newpath);
353     
354     gen = 0;
355  retry:
356     sprintf_alloc (&ctx->statedir, "/tmp/opkg-intercept-%d-%d", getpid (), gen);
357     if (mkdir (ctx->statedir, 0770) < 0) {
358         if (errno == EEXIST) {
359             free (ctx->statedir);
360             gen++;
361             goto retry;
362         }
363         perror (ctx->statedir);
364         return NULL;
365     }
366     setenv ("OPKG_INTERCEPT_DIR", ctx->statedir, 1);
367     return ctx;
368 }
369
370 int opkg_finalize_intercepts(opkg_intercept_t ctx)
371 {
372     char *cmd;
373     DIR *dir;
374     int err = 0;
375
376     setenv ("PATH", ctx->oldpath, 1);
377     free (ctx->oldpath);
378
379     dir = opendir (ctx->statedir);
380     if (dir) {
381         struct dirent *de;
382         while (de = readdir (dir), de != NULL) {
383             char *path;
384             
385             if (de->d_name[0] == '.')
386                 continue;
387             
388             sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
389             if (access (path, X_OK) == 0) {
390                 if (system (path)) {
391                     err = errno;
392                     perror (de->d_name);
393                 }
394             }
395             free (path);
396         }
397     } else
398         perror (ctx->statedir);
399         
400     sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
401     system (cmd);
402     free (cmd);
403
404     free (ctx->statedir);
405     free (ctx);
406
407     return err;
408 }
409
410 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
411 {
412      pkg_vec_t *all;
413      int i;
414      pkg_t *pkg;
415      opkg_intercept_t ic;
416      int r, err = 0;
417
418      opkg_message(conf, OPKG_INFO,
419                   "Configuring unpacked packages\n");
420      fflush( stdout );
421
422      all = pkg_vec_alloc();
423      pkg_hash_fetch_available(&conf->pkg_hash, all);
424
425      ic = opkg_prep_intercepts (conf);
426     
427      for(i = 0; i < all->len; i++) {
428           pkg = all->pkgs[i];
429
430           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
431                continue;
432
433           if (pkg->state_status == SS_UNPACKED) {
434                opkg_message(conf, OPKG_NOTICE,
435                             "Configuring %s\n", pkg->name);
436                fflush( stdout );
437                r = opkg_configure(conf, pkg);
438                if (r == 0) {
439                     pkg->state_status = SS_INSTALLED;
440                     pkg->parent->state_status = SS_INSTALLED;
441                     pkg->state_flag &= ~SF_PREFER;
442                } else {
443                     if (!err)
444                         err = r;
445                }
446           }
447      }
448
449      r = opkg_finalize_intercepts (ic);
450      if (r && !err)
451          err = r;
452
453      pkg_vec_free(all);
454      return err;
455 }
456
457 static opkg_conf_t *global_conf;
458
459 static void sigint_handler(int sig)
460 {
461      signal(sig, SIG_DFL);
462      opkg_message(NULL, OPKG_NOTICE,
463                   "opkg: interrupted. writing out status database\n");
464      write_status_files_if_changed(global_conf);
465      exit(128 + sig);
466 }
467
468 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv)
469 {
470      int i;
471      char *arg;
472      int err=0;
473
474      global_conf = conf;
475      signal(SIGINT, sigint_handler);
476
477      /*
478       * Now scan through package names and install
479       */
480      for (i=0; i < argc; i++) {
481           arg = argv[i];
482
483           opkg_message(conf, OPKG_DEBUG2, "Debug install_cmd: %s  \n",arg );
484           err = opkg_prepare_url_for_install(conf, arg, &argv[i]);
485           if (err != EINVAL && err != 0)
486               return err;
487      }
488      pkg_info_preinstall_check(conf);
489
490      for (i=0; i < argc; i++) {
491           arg = argv[i];
492           if (conf->multiple_providers)
493                err = opkg_install_multi_by_name(conf, arg);
494           else{
495                err = opkg_install_by_name(conf, arg);
496           }
497           if (err == OPKG_PKG_HAS_NO_CANDIDATE) {
498                opkg_message(conf, OPKG_ERROR,
499                             "Cannot find package %s.\n"
500                             "Check the spelling or perhaps run 'opkg update'\n",
501                             arg);
502           }
503      }
504
505      /* recheck to verify that all dependences are satisfied */
506      if (0) opkg_satisfy_all_dependences(conf);
507
508      opkg_configure_packages(conf, NULL);
509
510      write_status_files_if_changed(conf);
511
512      return err;
513 }
514
515 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv)
516 {
517      int i;
518      pkg_t *pkg;
519      int err;
520
521      global_conf = conf;
522      signal(SIGINT, sigint_handler);
523
524      if (argc) {
525           for (i=0; i < argc; i++) {
526                char *arg = argv[i];
527
528                err = opkg_prepare_url_for_install(conf, arg, &arg);
529                if (err != EINVAL && err != 0)
530                    return err;
531           }
532           pkg_info_preinstall_check(conf);
533
534           for (i=0; i < argc; i++) {
535                char *arg = argv[i];
536                if (conf->restrict_to_default_dest) {
537                     pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
538                                                                 argv[i],
539                                                                 conf->default_dest);
540                     if (pkg == NULL) {
541                          opkg_message(conf, OPKG_NOTICE,
542                                       "Package %s not installed in %s\n",
543                                       argv[i], conf->default_dest->name);
544                          continue;
545                     }
546                } else {
547                     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
548                                                            argv[i]);
549                }
550                if (pkg)
551                     opkg_upgrade_pkg(conf, pkg);
552                else {
553                     opkg_install_by_name(conf, arg);
554                }
555           }
556      } else {
557           pkg_vec_t *installed = pkg_vec_alloc();
558
559           pkg_info_preinstall_check(conf);
560
561           pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
562           for (i = 0; i < installed->len; i++) {
563                pkg = installed->pkgs[i];
564                opkg_upgrade_pkg(conf, pkg);
565           }
566           pkg_vec_free(installed);
567      }
568
569      /* recheck to verify that all dependences are satisfied */
570      if (0) opkg_satisfy_all_dependences(conf);
571
572      opkg_configure_packages(conf, NULL);
573
574      write_status_files_if_changed(conf);
575
576      return 0;
577 }
578
579 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv)
580 {
581      int i, err;
582      char *arg;
583      pkg_t *pkg;
584
585      pkg_info_preinstall_check(conf);
586      for (i = 0; i < argc; i++) {
587           arg = argv[i];
588
589           pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
590           if (pkg == NULL) {
591                opkg_message(conf, OPKG_ERROR,
592                             "Cannot find package %s.\n"
593                             "Check the spelling or perhaps run 'opkg update'\n",
594                             arg);
595                continue;
596           }
597
598           err = opkg_download_pkg(conf, pkg, ".");
599
600           if (err) {
601                opkg_message(conf, OPKG_ERROR,
602                             "Failed to download %s\n", pkg->name);
603           } else {
604                opkg_message(conf, OPKG_NOTICE,
605                             "Downloaded %s as %s\n",
606                             pkg->name, pkg->local_filename);
607           }
608      }
609
610      return 0;
611 }
612
613
614 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv)
615 {
616      int i ;
617      pkg_vec_t *available;
618      pkg_t *pkg;
619      char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
620      char *newline;
621      char *pkg_name = NULL;
622      char *version_str;
623
624      if (argc > 0) {
625           pkg_name = argv[0];
626      }
627      available = pkg_vec_alloc();
628      pkg_hash_fetch_available(&conf->pkg_hash, available);
629      for (i=0; i < available->len; i++) {
630           pkg = available->pkgs[i];
631           /* if we have package name or pattern and pkg does not match, then skip it */
632           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
633                continue;
634           if (pkg->description) {
635                strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
636           } else {
637                desc_short[0] = '\0';
638           }
639           desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
640           newline = strchr(desc_short, '\n');
641           if (newline) {
642                *newline = '\0';
643           }
644           if (opkg_cb_list) {
645                 version_str = pkg_version_str_alloc(pkg);
646                 opkg_cb_list(pkg->name,desc_short,
647                                              version_str,
648                                          pkg->state_status,
649                                          p_userdata);
650                 free(version_str);
651           }
652      }
653      pkg_vec_free(available);
654
655      return 0;
656 }
657
658
659 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv)
660 {
661      int i ;
662      pkg_vec_t *available;
663      pkg_t *pkg;
664      char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
665      char *newline;
666      char *pkg_name = NULL;
667      char *version_str;
668
669      if (argc > 0) {
670           pkg_name = argv[0];
671      }
672      available = pkg_vec_alloc();
673      pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
674      for (i=0; i < available->len; i++) {
675           pkg = available->pkgs[i];
676           /* if we have package name or pattern and pkg does not match, then skip it */
677           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
678                continue;
679           if (pkg->description) {
680                strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
681           } else {
682                desc_short[0] = '\0';
683           }
684           desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
685           newline = strchr(desc_short, '\n');
686           if (newline) {
687                *newline = '\0';
688           }
689           if (opkg_cb_list) {
690                 version_str = pkg_version_str_alloc(pkg);
691                 opkg_cb_list(pkg->name,desc_short,
692                                              version_str,
693                                          pkg->state_status,
694                                          p_userdata);
695                 free(version_str);
696           }
697      }
698
699      return 0;
700 }
701
702 static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int installed_only)
703 {
704      int i;
705      pkg_vec_t *available;
706      pkg_t *pkg;
707      char *pkg_name = NULL;
708      char **pkg_fields = NULL;
709      int n_fields = 0;
710      char *buff ; // = (char *)malloc(1);
711
712      if (argc > 0) {
713           pkg_name = argv[0];
714      }
715      if (argc > 1) {
716           pkg_fields = &argv[1];
717           n_fields = argc - 1;
718      }
719
720      available = pkg_vec_alloc();
721      if (installed_only)
722           pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
723      else
724           pkg_hash_fetch_available(&conf->pkg_hash, available);
725      for (i=0; i < available->len; i++) {
726           pkg = available->pkgs[i];
727           if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
728                continue;
729           }
730
731           buff = pkg_formatted_info(pkg);
732           if ( buff ) {
733                if (opkg_cb_status) opkg_cb_status(pkg->name,
734                                                   pkg->state_status,
735                                                   buff,
736                                                   p_userdata);
737 /* 
738    We should not forget that actually the pointer is allocated. 
739    We need to free it :)  ( Thanks florian for seeing the error )
740 */
741                free(buff);
742           }
743           if (conf->verbosity > 1) {
744                conffile_list_elt_t *iter;
745                for (iter = pkg->conffiles.head; iter; iter = iter->next) {
746                     conffile_t *cf = iter->data;
747                     int modified = conffile_has_been_modified(conf, cf);
748                     opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
749                                  cf->name, cf->value, modified);
750                }
751           }
752      }
753      pkg_vec_free(available);
754
755      return 0;
756 }
757
758 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv)
759 {
760      return opkg_info_status_cmd(conf, argc, argv, 0);
761 }
762
763 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv)
764 {
765      return opkg_info_status_cmd(conf, argc, argv, 1);
766 }
767
768 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv)
769 {
770      
771      int err;
772      if (argc > 0) {
773           char *pkg_name = NULL;
774
775           pkg_name = argv[0];
776
777           err = opkg_configure_packages (conf, pkg_name);
778      
779      } else {
780           err = opkg_configure_packages (conf, NULL);
781      }
782
783      write_status_files_if_changed(conf);
784
785      return err;
786 }
787
788 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv)
789 {
790      int i, err;
791      char *globpattern;
792      glob_t globbuf;
793     
794      sprintf_alloc(&globpattern, "%s/*" OPKG_PKG_EXTENSION, conf->pending_dir);
795      err = glob(globpattern, 0, NULL, &globbuf);
796      free(globpattern);
797      if (err) {
798           return 0;
799      }
800
801      opkg_message(conf, OPKG_NOTICE,
802                   "The following packages in %s will now be installed.\n",
803                   conf->pending_dir);
804      for (i = 0; i < globbuf.gl_pathc; i++) {
805           opkg_message(conf, OPKG_NOTICE,
806                        "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
807      }
808      opkg_message(conf, OPKG_NOTICE, "\n");
809      for (i = 0; i < globbuf.gl_pathc; i++) {
810           err = opkg_install_from_file(conf, globbuf.gl_pathv[i]);
811           if (err == 0) {
812                err = unlink(globbuf.gl_pathv[i]);
813                if (err) {
814                     opkg_message(conf, OPKG_ERROR,
815                                  "%s: ERROR: failed to unlink %s: %s\n",
816                                  __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
817                     return err;
818                }
819           }
820      }
821      globfree(&globbuf);
822
823      return err;
824 }
825
826 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
827 {
828      int i,a,done;
829      pkg_t *pkg;
830      pkg_t *pkg_to_remove;
831      pkg_vec_t *available;
832      char *pkg_name = NULL;
833      global_conf = conf;
834      signal(SIGINT, sigint_handler);
835
836 // ENH: Add the "no pkg removed" just in case.
837
838     done = 0;
839
840      available = pkg_vec_alloc();
841      pkg_info_preinstall_check(conf);
842      if ( argc > 0 ) {
843         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
844         for (i=0; i < argc; i++) {
845            pkg_name = malloc(strlen(argv[i])+2);
846            strcpy(pkg_name,argv[i]);
847            for (a=0; a < available->len; a++) {
848                pkg = available->pkgs[a];
849                if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
850                   continue;
851                }
852                if (conf->restrict_to_default_dest) {
853                     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
854                                                                 pkg->name,
855                                                                 conf->default_dest);
856                } else {
857                     pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
858                }
859         
860                if (pkg == NULL) {
861                     opkg_message(conf, OPKG_ERROR, "Package %s is not installed.\n", pkg->name);
862                     continue;
863                }
864                if (pkg->state_status == SS_NOT_INSTALLED) {    // Added the control, so every already removed package could be skipped
865                     opkg_message(conf, OPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
866                     continue;
867                }
868                opkg_remove_pkg(conf, pkg_to_remove,0);
869                done = 1;
870            }
871            free (pkg_name);
872         }
873         pkg_vec_free(available);
874      } else {
875           pkg_vec_t *installed_pkgs = pkg_vec_alloc();
876           int i;
877           int flagged_pkg_count = 0;
878           int removed;
879
880           pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
881
882           for (i = 0; i < installed_pkgs->len; i++) {
883                pkg_t *pkg = installed_pkgs->pkgs[i];
884                if (pkg->state_flag & SF_USER) {
885                     flagged_pkg_count++;
886                } else {
887                     if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
888                          opkg_message(conf, OPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
889                }
890           }
891           if (!flagged_pkg_count) {
892                opkg_message(conf, OPKG_NOTICE, "No packages flagged as installed by user, \n"
893                             "so refusing to uninstall unflagged non-leaf packages\n");
894                return 0;
895           }
896
897           /* find packages not flagged SF_USER (i.e., installed to
898            * satisfy a dependence) and not having any dependents, and
899            * remove them */
900           do {
901                removed = 0;
902                for (i = 0; i < installed_pkgs->len; i++) {
903                     pkg_t *pkg = installed_pkgs->pkgs[i];
904                     if (!(pkg->state_flag & SF_USER)
905                         && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
906                          removed++;
907                          opkg_message(conf, OPKG_NOTICE, "Removing non-user leaf package %s\n");
908                          opkg_remove_pkg(conf, pkg,0);
909                          done = 1;
910                     }
911                }
912           } while (removed);
913           pkg_vec_free(installed_pkgs);
914      }
915
916      if ( done == 0 ) 
917         opkg_message(conf, OPKG_NOTICE, "No packages removed.\n");
918
919      write_status_files_if_changed(conf);
920      return 0;
921 }
922
923 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv)
924 {
925      int i;
926      pkg_t *pkg;
927
928      global_conf = conf;
929      signal(SIGINT, sigint_handler);
930
931      pkg_info_preinstall_check(conf);
932
933      for (i=0; i < argc; i++) {
934           if (conf->restrict_to_default_dest) {
935                pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
936                                                            argv[i],
937                                                            conf->default_dest);
938           } else {
939                pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
940           }
941
942           if (pkg == NULL) {
943                opkg_message(conf, OPKG_ERROR,
944                             "Package %s is not installed.\n", argv[i]);
945                continue;
946           }
947           opkg_purge_pkg(conf, pkg);
948      }
949
950      write_status_files_if_changed(conf);
951      return 0;
952 }
953
954 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv)
955 {
956      int i;
957      pkg_t *pkg;
958      const char *flags = argv[0];
959     
960      global_conf = conf;
961      signal(SIGINT, sigint_handler);
962
963      for (i=1; i < argc; i++) {
964           if (conf->restrict_to_default_dest) {
965                pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
966                                                            argv[i],
967                                                            conf->default_dest);
968           } else {
969                pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
970           }
971
972           if (pkg == NULL) {
973                opkg_message(conf, OPKG_ERROR,
974                             "Package %s is not installed.\n", argv[i]);
975                continue;
976           }
977           if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
978               ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
979               pkg->state_flag = pkg_state_flag_from_str(flags);
980           }
981 /* pb_ asked this feature 03292004 */
982 /* Actually I will use only this two, but this is an open for various status */
983           if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
984               pkg->state_status = pkg_state_status_from_str(flags);
985           }
986           opkg_state_changed++;
987           opkg_message(conf, OPKG_NOTICE,
988                        "Setting flags for package %s to %s\n",
989                        pkg->name, flags);
990      }
991
992      write_status_files_if_changed(conf);
993      return 0;
994 }
995
996 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv)
997 {
998      pkg_t *pkg;
999      str_list_t *installed_files;
1000      str_list_elt_t *iter;
1001      char *pkg_version;
1002      size_t buff_len = 8192;
1003      size_t used_len;
1004      char *buff ;
1005
1006      buff = (char *)malloc(buff_len);
1007      if ( buff == NULL ) {
1008         fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1009         return ENOMEM;
1010      }
1011  
1012      if (argc < 1) {
1013           return EINVAL;
1014      }
1015
1016      pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1017                                             argv[0]);
1018      if (pkg == NULL) {
1019           opkg_message(conf, OPKG_ERROR,
1020                        "Package %s not installed.\n", argv[0]);
1021           return 0;
1022      }
1023
1024      installed_files = pkg_get_installed_files(pkg);
1025      pkg_version = pkg_version_str_alloc(pkg);
1026
1027      if (buff) {
1028      try_again:
1029           used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1030                               pkg->name, pkg_version, pkg->dest->name) + 1;
1031           if (used_len > buff_len) {
1032                buff_len *= 2;
1033                buff = realloc (buff, buff_len);
1034                goto try_again;
1035           }
1036           for (iter = installed_files->head; iter; iter = iter->next) {
1037                used_len += strlen (iter->data) + 1;
1038                while (buff_len <= used_len) {
1039                     buff_len *= 2;
1040                     buff = realloc (buff, buff_len);
1041                }
1042                strncat(buff, iter->data, buff_len);
1043                strncat(buff, "\n", buff_len);
1044           } 
1045           if (opkg_cb_list) opkg_cb_list(pkg->name,
1046                                          buff,
1047                                          pkg_version_str_alloc(pkg),
1048                                          pkg->state_status,
1049                                          p_userdata);
1050           free(buff);
1051      }
1052
1053      free(pkg_version);
1054      pkg_free_installed_files(pkg);
1055
1056      return 0;
1057 }
1058
1059 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv)
1060 {
1061
1062      if (argc > 0) {
1063           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1064           const char *rel_str = "depends on";
1065           int i;
1066      
1067           pkg_info_preinstall_check(conf);
1068
1069           if (conf->query_all)
1070                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1071           else
1072                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1073           for (i = 0; i < argc; i++) {
1074                const char *target = argv[i];
1075                int j;
1076
1077                opkg_message(conf, OPKG_ERROR, "target=%s\n", target);
1078
1079                for (j = 0; j < available_pkgs->len; j++) {
1080                     pkg_t *pkg = available_pkgs->pkgs[j];
1081                     if (fnmatch(target, pkg->name, 0) == 0) {
1082                          int k;
1083                          int count = pkg->depends_count + pkg->pre_depends_count;
1084                          opkg_message(conf, OPKG_ERROR, "What %s (arch=%s) %s\n",
1085                                       target, pkg->architecture, rel_str);
1086                          for (k = 0; k < count; k++) {
1087                               compound_depend_t *cdepend = &pkg->depends[k];
1088                               int l;
1089                               for (l = 0; l < cdepend->possibility_count; l++) {
1090                                    depend_t *possibility = cdepend->possibilities[l];
1091                                    opkg_message(conf, OPKG_ERROR, "    %s", possibility->pkg->name);
1092                                    if (conf->verbosity > 0) {
1093                                         // char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 
1094                                         opkg_message(conf, OPKG_NOTICE, " %s", possibility->version);
1095                                         if (possibility->version) {
1096                                              char *typestr = NULL;
1097                                              switch (possibility->constraint) {
1098                                              case NONE: typestr = "none"; break;
1099                                              case EARLIER: typestr = "<"; break;
1100                                              case EARLIER_EQUAL: typestr = "<="; break;
1101                                              case EQUAL: typestr = "="; break;
1102                                              case LATER_EQUAL: typestr = ">="; break;
1103                                              case LATER: typestr = ">"; break;
1104                                              }
1105                                              opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1106                                         }
1107                                         // free(ver);
1108                                    }
1109                                    opkg_message(conf, OPKG_ERROR, "\n");
1110                               }
1111                          }
1112                     }
1113                }
1114           }
1115           pkg_vec_free(available_pkgs);
1116      }
1117      return 0;
1118 }
1119
1120 enum what_field_type {
1121   WHATDEPENDS,
1122   WHATCONFLICTS,
1123   WHATPROVIDES,
1124   WHATREPLACES,
1125   WHATRECOMMENDS,
1126   WHATSUGGESTS
1127 };
1128
1129 static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1130 {
1131
1132      if (argc > 0) {
1133           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1134           const char *rel_str = NULL;
1135           int i;
1136           int changed;
1137
1138           switch (what_field_type) {
1139           case WHATDEPENDS: rel_str = "depends on"; break;
1140           case WHATCONFLICTS: rel_str = "conflicts with"; break;
1141           case WHATSUGGESTS: rel_str = "suggests"; break;
1142           case WHATRECOMMENDS: rel_str = "recommends"; break;
1143           case WHATPROVIDES: rel_str = "provides"; break;
1144           case WHATREPLACES: rel_str = "replaces"; break;
1145           }
1146      
1147           if (conf->query_all)
1148                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1149           else
1150                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1151
1152           /* mark the root set */
1153           pkg_vec_clear_marks(available_pkgs);
1154           opkg_message(conf, OPKG_NOTICE, "Root set:\n");
1155           for (i = 0; i < argc; i++) {
1156                const char *dependee_pattern = argv[i];
1157                pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1158           }
1159           for (i = 0; i < available_pkgs->len; i++) {
1160                pkg_t *pkg = available_pkgs->pkgs[i];
1161                if (pkg->state_flag & SF_MARKED) {
1162                     /* mark the parent (abstract) package */
1163                     pkg_mark_provides(pkg);
1164                     opkg_message(conf, OPKG_NOTICE, "  %s\n", pkg->name);
1165                }
1166           }
1167
1168           opkg_message(conf, OPKG_NOTICE, "What %s root set\n", rel_str);
1169           do {
1170                int j;
1171                changed = 0;
1172
1173                for (j = 0; j < available_pkgs->len; j++) {
1174                     pkg_t *pkg = available_pkgs->pkgs[j];
1175                     int k;
1176                     int count = ((what_field_type == WHATCONFLICTS)
1177                                  ? pkg->conflicts_count
1178                                  : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1179                     /* skip this package if it is already marked */
1180                     if (pkg->parent->state_flag & SF_MARKED) {
1181                          continue;
1182                     }
1183                     for (k = 0; k < count; k++) {
1184                          compound_depend_t *cdepend = 
1185                               (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1186                          int l;
1187                          for (l = 0; l < cdepend->possibility_count; l++) {
1188                               depend_t *possibility = cdepend->possibilities[l];
1189                               if (possibility->pkg->state_flag & SF_MARKED) {
1190                                    /* mark the depending package so we won't visit it again */
1191                                    pkg->state_flag |= SF_MARKED;
1192                                    pkg_mark_provides(pkg);
1193                                    changed++;
1194
1195                                    opkg_message(conf, OPKG_NOTICE, "    %s", pkg->name);
1196                                    if (conf->verbosity > 0) {
1197                                         char *ver = pkg_version_str_alloc(pkg); 
1198                                         opkg_message(conf, OPKG_NOTICE, " %s", ver);
1199                                         opkg_message(conf, OPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1200                                         if (possibility->version) {
1201                                              char *typestr = NULL;
1202                                              switch (possibility->constraint) {
1203                                              case NONE: typestr = "none"; break;
1204                                              case EARLIER: typestr = "<"; break;
1205                                              case EARLIER_EQUAL: typestr = "<="; break;
1206                                              case EQUAL: typestr = "="; break;
1207                                              case LATER_EQUAL: typestr = ">="; break;
1208                                              case LATER: typestr = ">"; break;
1209                                              }
1210                                              opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1211                                         }
1212                                         free(ver);
1213                                         if (!pkg_dependence_satisfiable(conf, possibility))
1214                                              opkg_message(conf, OPKG_NOTICE, " unsatisfiable");
1215                                    }
1216                                    opkg_message(conf, OPKG_NOTICE, "\n");
1217                                    goto next_package;
1218                               }
1219                          }
1220                     }
1221                next_package:
1222                     ;
1223                }
1224           } while (changed && recursive);
1225           pkg_vec_free(available_pkgs);
1226      }
1227
1228      return 0;
1229 }
1230
1231 int pkg_mark_provides(pkg_t *pkg)
1232 {
1233      int provides_count = pkg->provides_count;
1234      abstract_pkg_t **provides = pkg->provides;
1235      int i;
1236      pkg->parent->state_flag |= SF_MARKED;
1237      for (i = 0; i < provides_count; i++) {
1238           provides[i]->state_flag |= SF_MARKED;
1239      }
1240      return 0;
1241 }
1242
1243 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv)
1244 {
1245      return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1246 }
1247 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv)
1248 {
1249      return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1250 }
1251
1252 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv)
1253 {
1254      return opkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1255 }
1256
1257 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv)
1258 {
1259      return opkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1260 }
1261
1262 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv)
1263 {
1264      return opkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1265 }
1266
1267 static int opkg_what_provides_replaces_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1268 {
1269
1270      if (argc > 0) {
1271           pkg_vec_t *available_pkgs = pkg_vec_alloc();
1272           const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1273           int i;
1274      
1275           pkg_info_preinstall_check(conf);
1276
1277           if (conf->query_all)
1278                pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1279           else
1280                pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1281           for (i = 0; i < argc; i++) {
1282                const char *target = argv[i];
1283                int j;
1284
1285                opkg_message(conf, OPKG_ERROR, "What %s %s\n",
1286                             rel_str, target);
1287                for (j = 0; j < available_pkgs->len; j++) {
1288                     pkg_t *pkg = available_pkgs->pkgs[j];
1289                     int k;
1290                     int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1291                     for (k = 0; k < count; k++) {
1292                          abstract_pkg_t *apkg = 
1293                               ((what_field_type == WHATPROVIDES) 
1294                                ? pkg->provides[k]
1295                                : pkg->replaces[k]);
1296                          if (fnmatch(target, apkg->name, 0) == 0) {
1297                               opkg_message(conf, OPKG_ERROR, "    %s", pkg->name);
1298                               if (strcmp(target, apkg->name) != 0)
1299                                    opkg_message(conf, OPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1300                               opkg_message(conf, OPKG_ERROR, "\n");
1301                          }
1302                     }
1303                }
1304           }
1305           pkg_vec_free(available_pkgs);
1306      }
1307      return 0;
1308 }
1309
1310 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv)
1311 {
1312      return opkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1313 }
1314
1315 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv)
1316 {
1317      return opkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1318 }
1319
1320 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv)
1321 {
1322      int i;
1323
1324      pkg_vec_t *installed;
1325      pkg_t *pkg;
1326      str_list_t *installed_files;
1327      str_list_elt_t *iter;
1328      char *installed_file;
1329
1330      if (argc < 1) {
1331           return EINVAL;
1332      }
1333  
1334      installed = pkg_vec_alloc();
1335      pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1336
1337      for (i=0; i < installed->len; i++) {
1338           pkg = installed->pkgs[i];
1339
1340           installed_files = pkg_get_installed_files(pkg);
1341
1342           for (iter = installed_files->head; iter; iter = iter->next) {
1343                installed_file = iter->data;
1344                if (fnmatch(argv[0], installed_file, 0)==0)  {
1345                         if (opkg_cb_list) opkg_cb_list(pkg->name, 
1346                                                        installed_file, 
1347                                                        pkg_version_str_alloc(pkg), 
1348                                                        pkg->state_status, p_userdata);
1349                }                
1350           }
1351
1352           pkg_free_installed_files(pkg);
1353      }
1354
1355      /* XXX: CLEANUP: It's not obvious from the name of
1356         pkg_hash_fetch_all_installed that we need to call
1357         pkg_vec_free to avoid a memory leak. */
1358      pkg_vec_free(installed);
1359
1360      return 0;
1361 }
1362
1363 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv)
1364 {
1365      if (argc == 3) {
1366           /* this is a bit gross */
1367           struct pkg p1, p2;
1368           parseVersion(&p1, argv[0]); 
1369           parseVersion(&p2, argv[2]); 
1370           return pkg_version_satisfied(&p1, &p2, argv[1]);
1371      } else {
1372           opkg_message(conf, OPKG_ERROR,
1373                        "opkg compare_versions <v1> <op> <v2>\n"
1374                        "<op> is one of <= >= << >> =\n");
1375           return -1;
1376      }
1377 }
1378
1379 #ifndef HOST_CPU_STR
1380 #define HOST_CPU_STR__(X) #X
1381 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1382 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1383 #endif
1384
1385 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv)
1386 {
1387      nv_pair_list_elt_t *l;
1388
1389      l = conf->arch_list.head;
1390      while (l) {
1391           nv_pair_t *nv = l->data;
1392           printf("arch %s %s\n", nv->name, nv->value);
1393           l = l->next;
1394      }
1395      return 0;
1396 }
1397
1398