[opkg] fix opkg crashes if PATH is unset
[oweals/opkg-lede.git] / libopkg / opkg_cmd.c
index 94cfb9987c4a52567163c0879a01900294054a10..2fe74e9d1394ea17b9a7394b6131651cbe301cec 100644 (file)
@@ -72,6 +72,7 @@ static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv);
 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv);
 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv);
 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv);
+static int pkg_mark_provides(pkg_t *pkg);
 
 /* XXX: CLEANUP: The usage strings should be incorporated into this
    array for easier maintenance */
@@ -138,6 +139,23 @@ opkg_cmd_t *opkg_cmd_find(const char *name)
      return NULL;
 }
 
+void opkg_print_error_list (opkg_conf_t *conf)
+{
+  if ( error_list ) {
+     reverse_error_list(&error_list);
+
+     printf ("Collected errors:\n");
+     /* Here we print the errors collected and free the list */
+     while (error_list != NULL) {
+           printf (" * %s", error_list->errmsg);
+           error_list = error_list->next;
+
+     }
+     free_error_list(&error_list);
+  }
+
+}
+
 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
 {
        int result;
@@ -150,20 +168,8 @@ int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **arg
            opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
         }
 
-        if ( error_list ) {
-           reverse_error_list(&error_list);
+        opkg_print_error_list (conf);
 
-           opkg_message(conf, OPKG_NOTICE, "Collected errors:\n");
-           /* Here we print the errors collected and free the list */
-           while (error_list != NULL) {
-                 opkg_message(conf, OPKG_NOTICE, " * %s", error_list->errmsg);
-                 error_list = error_list->next;
-
-           }
-           free_error_list(&error_list);
-
-        }
-   
        p_userdata = NULL;
        return result;
 }
@@ -295,36 +301,6 @@ static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
 }
 
 
-/* scan the args passed and cache the local filenames of the packages */
-int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
-{
-     int i;
-     int err;
-    
-     /* 
-      * First scan through package names/urls
-      * For any urls, download the packages and install in database.
-      * For any files, install package info in database.
-      */
-     for (i = 0; i < argc; i ++) {
-         char *filename = argv [i];
-         //char *tmp = basename (tmp);
-         //int tmplen = strlen (tmp);
-
-         //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
-         //     continue;
-         //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
-         //     continue;
-       
-          opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s  \n",filename );
-
-         err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
-         if (err)
-           return err;
-     }
-     return 0;
-}
-
 struct opkg_intercept
 {
     char *oldpath;
@@ -333,14 +309,21 @@ struct opkg_intercept
 
 typedef struct opkg_intercept *opkg_intercept_t;
 
-opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
+static opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
 {
     opkg_intercept_t ctx;
+    char *oldpath;
     char *newpath;
     int gen;
 
     ctx = malloc (sizeof (*ctx));
-    ctx->oldpath = strdup (getenv ("PATH"));
+    oldpath = getenv ("PATH");
+    if (oldpath) {
+        ctx->oldpath = strdup (oldpath);
+    } else {
+        ctx->oldpath = 0;
+    }
+
 
     sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
     setenv ("PATH", newpath, 1);
@@ -362,14 +345,18 @@ opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
     return ctx;
 }
 
-int opkg_finalize_intercepts(opkg_intercept_t ctx)
+static int opkg_finalize_intercepts(opkg_intercept_t ctx)
 {
     char *cmd;
     DIR *dir;
     int err = 0;
 
-    setenv ("PATH", ctx->oldpath, 1);
-    free (ctx->oldpath);
+    if (ctx->oldpath) {
+        setenv ("PATH", ctx->oldpath, 1);
+        free (ctx->oldpath);
+    } else {
+        unsetenv("PATH"); 
+    }
 
     dir = opendir (ctx->statedir);
     if (dir) {
@@ -410,7 +397,7 @@ int opkg_finalize_intercepts(opkg_intercept_t ctx)
    used to end recursion and avoid an infinite loop on graph cycles.
    pkg_vec ordered will finally contain the ordered set of packages.
 */
-int opkg_recurse_pkgs_in_order(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *all,
+static int opkg_recurse_pkgs_in_order(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *all,
                                pkg_vec_t *visited, pkg_vec_t *ordered)
 {
     int j,k,l,m;
@@ -492,7 +479,7 @@ int opkg_recurse_pkgs_in_order(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *all,
 
 }
 
-int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
+static int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
 {
     pkg_vec_t *all, *ordered, *visited;
      int i;
@@ -933,9 +920,9 @@ static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
 
     done = 0;
 
-     available = pkg_vec_alloc();
      pkg_info_preinstall_check(conf);
      if ( argc > 0 ) {
+        available = pkg_vec_alloc();
         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
         for (i=0; i < argc; i++) {
            pkg_name = malloc(strlen(argv[i])+2);
@@ -1324,7 +1311,7 @@ static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_ty
      return 0;
 }
 
-int pkg_mark_provides(pkg_t *pkg)
+static int pkg_mark_provides(pkg_t *pkg)
 {
      int provides_count = pkg->provides_count;
      abstract_pkg_t **provides = pkg->provides;