libopkg: add some checks for NULL strings
[oweals/opkg-lede.git] / libopkg / opkg.c
index e2545527a735084130c5bb65c7bc10d2a8d86c3a..b4aff139b3b53b66721e988ae4244c4412c4dde9 100644 (file)
 #include "sprintf_alloc.h"
 #include "file_util.h"
 
-#include "libbb.h"
+#include <libbb/libbb.h>
 
 struct _opkg_t
 {
   args_t *args;
   opkg_conf_t *conf;
-  opkg_option_t *options[];
+  opkg_option_t *options;
 };
 
 /** Private Functions ***/
@@ -89,9 +89,14 @@ opkg_new ()
 {
   opkg_t *opkg;
   opkg = malloc (sizeof (opkg_t));
+
+  opkg->args = malloc (sizeof (args_t));
   args_init (opkg->args);
+
+  opkg->conf = malloc (sizeof (opkg_conf_t));
   opkg_conf_init (opkg->conf, opkg->args);
-  opkg_init_options_array (opkg->conf, opkg->options);
+
+  opkg_init_options_array (opkg->conf, &opkg->options);
   return opkg;
 }
 
@@ -102,11 +107,63 @@ opkg_free (opkg_t *opkg)
   args_deinit (opkg->args);
 }
 
+int
+opkg_read_config_files (opkg_t *opkg)
+{
+  args_t *a = opkg->args;
+  opkg_conf_t *c = opkg->conf;
+
+  /* Unfortunatly, the easiest way to re-read the config files right now is to
+   * throw away opkg->conf and start again */
+
+  /* copy the settings we need to keep */
+  a->autoremove = c->autoremove;
+  a->force_depends = c->force_depends;
+  a->force_defaults = c->force_defaults;
+  a->force_overwrite = c->force_overwrite;
+  a->force_downgrade = c->force_downgrade;
+  a->force_reinstall = c->force_reinstall;
+  a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
+  a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
+  a->nodeps = c->nodeps;
+  a->noaction = c->noaction;
+  a->query_all = c->query_all;
+  a->multiple_providers = c->multiple_providers;
+  a->verbosity = c->verbosity;
+
+  if (c->offline_root)
+  {
+    if (a->offline_root) free (a->offline_root);
+    a->offline_root = strdup (c->offline_root);
+  }
+
+  if (c->offline_root_pre_script_cmd)
+  {
+    if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
+    a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
+  }
+
+  if (c->offline_root_post_script_cmd)
+  {
+    if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
+    a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
+  }
+
+  /* throw away old opkg_conf and start again */
+  opkg_conf_deinit (opkg->conf);
+  opkg_conf_init (opkg->conf, opkg->args);
+
+  free (opkg->options);
+  opkg_init_options_array (opkg->conf, &opkg->options);
+
+  return 0;
+}
+
 void
 opkg_get_option (opkg_t *opkg, char *option, void **value)
 {
   int i = 0;
-  opkg_option_t **options = opkg->options;
+  opkg_option_t *options = opkg->options;
 
   /* can't store a value in a NULL pointer! */
   if (!value)
@@ -115,9 +172,9 @@ opkg_get_option (opkg_t *opkg, char *option, void **value)
   /* look up the option
    * TODO: this would be much better as a hash table
    */
-  while (options[i]->name)
+  while (options[i].name)
   {
-    if (strcmp (options[i]->name, option) != 0)
+    if (strcmp (options[i].name, option) != 0)
     {
       i++;
       continue;
@@ -125,18 +182,18 @@ opkg_get_option (opkg_t *opkg, char *option, void **value)
   }
 
   /* get the option */
-  switch (options[i]->type)
+  switch (options[i].type)
   {
   case OPKG_OPT_TYPE_BOOL:
-    *((int *) value) = *((int *) options[i]->value);
+    *((int *) value) = *((int *) options[i].value);
     return;
 
   case OPKG_OPT_TYPE_INT:
-    *((int *) value) = *((int *) options[i]->value);
+    *((int *) value) = *((int *) options[i].value);
     return;
 
   case OPKG_OPT_TYPE_STRING:
-    *((char **)value) = strdup (options[i]->value);
+    *((char **)value) = strdup (options[i].value);
     return;
   }
 
@@ -146,7 +203,7 @@ void
 opkg_set_option (opkg_t *opkg, char *option, void *value)
 {
   int i = 0;
-  opkg_option_t **options = opkg->options;
+  opkg_option_t *options = opkg->options;
 
   /* NULL values are not defined */
   if (!value)
@@ -155,31 +212,31 @@ opkg_set_option (opkg_t *opkg, char *option, void *value)
   /* look up the option
    * TODO: this would be much better as a hash table
    */
-  while (options[i]->name)
+  while (options[i].name)
   {
-    if (strcmp (options[i]->name, option) != 0)
+    if (strcmp (options[i].name, option) == 0)
     {
-      i++;
-      continue;
+      break;
     }
+    i++;
   }
 
   /* set the option */
-  switch (options[i]->type)
+  switch (options[i].type)
   {
   case OPKG_OPT_TYPE_BOOL:
     if (*((int *) value) == 0)
-      *((int *)options[i]->value) = 0;
+      *((int *)options[i].value) = 0;
     else
-      *((int *)options[i]->value) = 1;
+      *((int *)options[i].value) = 1;
     return;
 
   case OPKG_OPT_TYPE_INT:
-    *((int *) options[i]->value) = *((int *) value);
+    *((int *) options[i].value) = *((int *) value);
     return;
 
   case OPKG_OPT_TYPE_STRING:
-    *((char **)options[i]->value) = strdup (value);
+    *((char **)options[i].value) = strdup (value);
     return;
   }