summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2011-02-09 22:11:23 +0000
committerDouglas Gregor <dgregor@apple.com>2011-02-09 22:11:23 +0000
commit025feadb973272d5c5097bb72cb2f6fef44474d3 (patch)
tree439433d25e19da46c19b50ef7638090c96526bb8 /lib
parenta372d16f92392bb4cd4184783466f0300a51a9ae (diff)
downloadllvm-025feadb973272d5c5097bb72cb2f6fef44474d3.tar.gz
llvm-025feadb973272d5c5097bb72cb2f6fef44474d3.tar.bz2
llvm-025feadb973272d5c5097bb72cb2f6fef44474d3.tar.xz
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? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125228 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Support/Unix/PathV2.inc30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc
index 03ff28367e..7fa838b16b 100644
--- a/lib/Support/Unix/PathV2.inc
+++ b/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