From a95fbb0f497680cce0a097aa3a143e84943b903f Mon Sep 17 00:00:00 2001
From: Hartmut Goebel <h.goebel@crazy-compilers.com>
Date: Sun, 3 May 2020 15:26:43 +0200
Subject: [PATCH] Bootloader: Reimplement pyi_search_path using strtok().

This avoids some places where we need to cope with strncpy(= and related. Also
the implementation becomes much simpler.
---
 bootloader/src/pyi_path.c | 34 +++++++---------------------------
 1 file changed, 7 insertions(+), 27 deletions(-)

diff --git a/bootloader/src/pyi_path.c b/bootloader/src/pyi_path.c
index 8736de8cc5..fb417e9269 100644
--- a/bootloader/src/pyi_path.c
+++ b/bootloader/src/pyi_path.c
@@ -205,40 +205,20 @@ pyi_path_exists(char * path)
 int
 pyi_search_path(char * result, const char * appname)
 {
-    char * path = pyi_getenv("PATH");
-    char dirname[PATH_MAX + 1];
-    char filename[PATH_MAX + 1];
+    char *path = pyi_getenv("PATH"); // returns a copy
+    char *dirname;
 
     if (NULL == path) {
         return -1;
     }
 
-    while (1) {
-        char *delim = strchr(path, PYI_PATHSEP);
-
-        if (delim) {
-            size_t len = delim - path;
-
-            if (len > PATH_MAX) {
-                len = PATH_MAX;
-            }
-            strncpy(dirname, path, len);
-            *(dirname + len) = '\0';
-        }
-        else {  /* last $PATH element */
-            strncpy(dirname, path, PATH_MAX);
-        }
-        pyi_path_join(filename, dirname, appname);
-
-        if (pyi_path_exists(filename)) {
-            strncpy(result, filename, PATH_MAX);
+    dirname = strtok(path, PYI_PATHSEPSTR);
+    while (dirname != NULL) {
+        pyi_path_join(result, dirname, appname);
+        if (pyi_path_exists(result)) {
             return 0;
         }
-
-        if (!delim) {
-            break;
-        }
-        path = delim + 1;
+        dirname = strtok(NULL, PYI_PATHSEPSTR);
     }
     return -1;
 }
