1 // =================================
 2 // Copyright (c) 2022 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Collections;
 8 using System.IO;
 9 using System.Os;
10 
11 namespace paths
12 {
13     string ReadDir(const string& dirconst string& fileNameconst string& fileNameX)
14     {
15         if (DirectoryExists(dir))
16         {
17             DirectoryEntry entry;
18             FileStatus status;
19             DirectoryReader reader(dir);
20             while (reader.Read(entry))
21             {
22                 if (entry.IsDot() || entry.IsDotDot())
23                 {
24                     continue;
25                 }
26                 string p = Path.Combine(direntry.name);
27                 Stat(p.Chars()status);
28                 if (status.fileType == FileType.regular && (status.ownerAccess & Access.execute) != Access.none)
29                 {
30                     if (entry.name == fileName || entry.name == fileNameX)
31                     {
32                         return p;
33                     }
34                 }
35             }
36         }
37         return string();
38     }
39 
40     public string GetProgramFilePath(const string& fileName)
41     {
42         string fileNameX = fileName + ".x";
43         string path = GetEnv("PATH");
44         List<string> pathDirs = path.Split(":");
45         for (const string& dir : pathDirs)
46         {
47             string p = ReadDir(dirfileNamefileNameX);
48             if (!p.IsEmpty())
49             {
50                 return p;
51             }
52         }
53         return fileName;
54     }
55 }