1
2
3
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& dir, const string& fileName, const 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(dir, entry.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(dir, fileName, fileNameX);
48 if (!p.IsEmpty())
49 {
50 return p;
51 }
52 }
53 return fileName;
54 }
55 }