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