diff options
author | Douglas Gregor <dgregor@apple.com> | 2011-02-09 22:11:23 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2011-02-09 22:11:23 +0000 |
commit | 529329e859a0028f2074aa1963cb86be7a14425e (patch) | |
tree | ede4141cbca2e93e4d6e2046a2e338a10eb01ea2 /llvm/lib/Support/Unix/PathV2.inc | |
parent | 0ce4a83c4482841d58a7e8c5db9a213258d6867b (diff) | |
download | bcm5719-llvm-529329e859a0028f2074aa1963cb86be7a14425e.tar.gz bcm5719-llvm-529329e859a0028f2074aa1963cb86be7a14425e.zip |
Add llvm::sys::path::canonical(), which provides the canonicalized
name of a path, after resolving symbolic links and eliminating excess
path elements such as "foo/../" and "./".
This routine still needs a Windows implementation, but I don't have a
Windows machine available. Help? Please?
llvm-svn: 125228
Diffstat (limited to 'llvm/lib/Support/Unix/PathV2.inc')
-rw-r--r-- | llvm/lib/Support/Unix/PathV2.inc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/llvm/lib/Support/Unix/PathV2.inc b/llvm/lib/Support/Unix/PathV2.inc index 03ff28367e4..7fa838b16b7 100644 --- a/llvm/lib/Support/Unix/PathV2.inc +++ b/llvm/lib/Support/Unix/PathV2.inc @@ -503,5 +503,35 @@ error_code get_magic(const Twine &path, uint32_t len, } } // end namespace fs + +namespace path { + +void canonical(const char *path, SmallVectorImpl<char> &buffer) { + buffer.resize(PATH_MAX); + char *result = realpath(path, buffer.data()); + if (result) { + buffer.resize(strlen(result)); + return; + } + + // A common extension is to support memory allocation of the result when + // passing NULL as the second argument. + result = realpath(path, 0); + if (result) { + size_t length = strlen(result); + buffer.resize(length); + memcpy(buffer.data(), result, length); + free(result); + return buffer.data(); + } + + size_t length = strlen(path); + buffer.resize(length); + memcpy(buffer.data(), path, length); + return path; +} + +} // end namespace path + } // end namespace sys } // end namespace llvm |