diff options
author | Alexey Samsonov <samsonov@google.com> | 2013-09-03 13:20:48 +0000 |
---|---|---|
committer | Alexey Samsonov <samsonov@google.com> | 2013-09-03 13:20:48 +0000 |
commit | 1dcd1d9d1c86ab9b728386d261fbebe00b32e097 (patch) | |
tree | 5d8dd78f23d0fd4cc6bdd445dbda53e61c0223ea /lib/sanitizer_common/sanitizer_posix.cc | |
parent | 72870db5f2e0e83b2e925eef594dacfae275a8de (diff) |
[Sanitizer] Add the way to find binary in PATH
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@189799 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/sanitizer_common/sanitizer_posix.cc')
-rw-r--r-- | lib/sanitizer_common/sanitizer_posix.cc | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/sanitizer_common/sanitizer_posix.cc b/lib/sanitizer_common/sanitizer_posix.cc index 02f031b6a..0977f2c9f 100644 --- a/lib/sanitizer_common/sanitizer_posix.cc +++ b/lib/sanitizer_common/sanitizer_posix.cc @@ -173,6 +173,30 @@ const char *GetPwd() { return GetEnv("PWD"); } +char *FindPathToBinary(const char *name) { + const char *path = GetEnv("PATH"); + if (!path) + return 0; + uptr name_len = internal_strlen(name); + InternalScopedBuffer<char> buffer(kMaxPathLength); + const char *beg = path; + while (true) { + const char *end = internal_strchrnul(beg, ':'); + uptr prefix_len = end - beg; + if (prefix_len + name_len + 2 <= kMaxPathLength) { + internal_memcpy(buffer.data(), beg, prefix_len); + buffer[prefix_len] = '/'; + internal_memcpy(&buffer[prefix_len + 1], name, name_len); + buffer[prefix_len + 1 + name_len] = '\0'; + if (FileExists(buffer.data())) + return internal_strdup(buffer.data()); + } + if (*end == '\0') break; + beg = end + 1; + } + return 0; +} + } // namespace __sanitizer #endif // SANITIZER_LINUX || SANITIZER_MAC |