commit c09c3902c441bbf5dd1421f6ccd9f0dac5c6d96e (tree)
parent 48985a7e684ff32eb5d419a89eac3b92c08c3ee9
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Mon, 18 Jun 2018 12:16:47 -0400
Merge branch 'fix-1117-macos-realpath' of https://github.com/binary132/zig into binary132-fix-1117-macos-realpath
Diffstat:
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/os.cpp b/src/os.cpp
@@ -989,12 +989,29 @@ int os_self_exe_path(Buf *out_path) {
}
#elif defined(ZIG_OS_DARWIN)
+ // How long is the executable's path?
uint32_t u32_len = 0;
int ret1 = _NSGetExecutablePath(nullptr, &u32_len);
assert(ret1 != 0);
- buf_resize(out_path, u32_len);
- int ret2 = _NSGetExecutablePath(buf_ptr(out_path), &u32_len);
+
+ // Make a buffer having room for the temp path.
+ Buf *tmp = buf_alloc_fixed(u32_len);
+
+ // Fill the executable path.
+ int ret2 = _NSGetExecutablePath(buf_ptr(tmp), &u32_len);
assert(ret2 == 0);
+
+ // Resolve the real path from that.
+ buf_resize(out_path, PATH_MAX);
+ char *real_path = realpath(buf_ptr(tmp), buf_ptr(out_path));
+ assert(real_path == buf_ptr(out_path));
+
+ // Deallocate our scratch space.
+ buf_deinit(tmp);
+
+ // Resize out_path for the correct length.
+ buf_resize(out_path, strlen(buf_ptr(out_path)));
+
return 0;
#elif defined(ZIG_OS_LINUX)
buf_resize(out_path, 256);