summaryrefslogtreecommitdiff
path: root/lang/gnu-apl/files/patch-src_LibPaths.cc
blob: b894ebb0a56c59f2b7eca3610073627952e8a22a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--- src/LibPaths.cc.orig	2019-06-23 12:39:20 UTC
+++ src/LibPaths.cc
@@ -70,33 +70,22 @@ LibPaths::compute_bin_path(const char * argv0, bool lo
          //
          // we fix this by searching argv0 in $PATH
          //
-         const char * path = getenv("PATH");   // must NOT be modified
-
-         if (path)
+         if (const char * paths = getenv("PATH"))
             {
               logit && CERR << "initializing paths from  $PATH = "
-                            << path << endl;
+                            << paths << endl;
 
-              // we must not modify path, so we copy it to path1 and
-              // replace the semicolons in path1 by 0. That converts
-              // p1;p2; ... into a sequence of 0-terminated strings
-              // p1 p2 ... The variable next points to the start of each
-              // string.
-              //
-              const size_t plen = strlen(path);
-              std::string   path1;
-              path1.reserve(plen + 1);
-              loop(p, (plen + 1))   path1 += path[p];
-              char * next = &path1[0];
-              for (;;)
-                  {
-                    char * semi = strchr(next, ':');
-                    if (semi)   *semi = 0;
-                    UTF8_string filename;
-                    for (const char * n = next; *n; ++n)   filename += *n;
-                    filename += '/';
-                    for (const char * a = argv0; *a; ++a)   filename += *a;
+              while (*paths)
+                    {
+                      size_t dir_len;
+                      if (const char * colon = strchr(paths, ':'))
+                         dir_len = colon - paths;
+                      else
+                         dir_len = strlen(paths);
 
+                    std::string filename(paths, dir_len);
+                    filename += '/';
+                    filename.append(argv0);
                     if (access(filename.c_str(), X_OK) == 0)
                        {
                          strncpy(APL_bin_path, filename.c_str(),
@@ -110,8 +99,7 @@ LibPaths::compute_bin_path(const char * argv0, bool lo
                          goto done;
                        }
 
-                    if (semi == 0)   break;
-                    next = semi + 1;
+                    paths += dir_len + 1;   // next $PATH item
                   }
             }
            else