aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAlexandros Naskos <alex_naskos@hotmail.com>2020-10-03 18:29:44 +0300
committerGitHub <noreply@github.com>2020-10-03 18:29:44 +0300
commit4396373ccb7182defa6cd084646b009de82f0b18 (patch)
tree3b91fa13bd7c32d3925a2b59de0799659871519d /lib/std
parente31cc80130ee7e4b61b63d1d1a0fc0a84fbb15ff (diff)
parent276598346a1f04bcfbe4982ea3c766aa79cad681 (diff)
downloadzig-4396373ccb7182defa6cd084646b009de82f0b18.tar.gz
zig-4396373ccb7182defa6cd084646b009de82f0b18.zip
Merge pull request #6503 from kristoff-it/fix-darwin-symlink-exe
fix symlink path not being resolved in darwin
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/c/darwin.zig2
-rw-r--r--lib/std/fs.zig16
-rw-r--r--lib/std/os.zig2
3 files changed, 14 insertions, 6 deletions
diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig
index ed1ddb7d91..976690d6b7 100644
--- a/lib/std/c/darwin.zig
+++ b/lib/std/c/darwin.zig
@@ -12,7 +12,7 @@ usingnamespace @import("../os/bits.zig");
extern "c" fn __error() *c_int;
pub extern "c" fn NSVersionOfRunTimeLibrary(library_name: [*:0]const u8) u32;
-pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int;
+pub extern "c" fn _NSGetExecutablePath(buf: [*:0]u8, bufsize: *u32) c_int;
pub extern "c" fn _dyld_image_count() u32;
pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header;
pub extern "c" fn _dyld_get_image_vmaddr_slide(image_index: u32) usize;
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index 1890d7e136..1d98c03c5c 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -2162,7 +2162,7 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
return openFileAbsoluteZ(buf[0..self_exe_path.len :0].ptr, flags);
}
-pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
+pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathError;
/// `selfExePath` except allocates the result on the heap.
/// Caller owns returned memory.
@@ -2190,10 +2190,18 @@ pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {
/// TODO make the return type of this a null terminated pointer
pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
if (is_darwin) {
- var u32_len: u32 = @intCast(u32, math.min(out_buffer.len, math.maxInt(u32)));
- const rc = std.c._NSGetExecutablePath(out_buffer.ptr, &u32_len);
+ // Note that _NSGetExecutablePath() will return "a path" to
+ // the executable not a "real path" to the executable.
+ var symlink_path_buf: [MAX_PATH_BYTES:0]u8 = undefined;
+ var u32_len: u32 = MAX_PATH_BYTES + 1; // include the sentinel
+ const rc = std.c._NSGetExecutablePath(&symlink_path_buf, &u32_len);
if (rc != 0) return error.NameTooLong;
- return mem.spanZ(@ptrCast([*:0]u8, out_buffer));
+
+ var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
+ const real_path = try std.os.realpathZ(&symlink_path_buf, &real_path_buf);
+ if (real_path.len > out_buffer.len) return error.NameTooLong;
+ std.mem.copy(u8, out_buffer, real_path);
+ return out_buffer[0..real_path.len];
}
switch (builtin.os.tag) {
.linux => return os.readlinkZ("/proc/self/exe", out_buffer),
diff --git a/lib/std/os.zig b/lib/std/os.zig
index c06ce4ed00..ef342edf56 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -3993,7 +3993,7 @@ pub const RealPathError = error{
/// Expands all symbolic links and resolves references to `.`, `..`, and
/// extra `/` characters in `pathname`.
/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.
-/// See also `realpathC` and `realpathW`.
+/// See also `realpathZ` and `realpathW`.
pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
if (builtin.os.tag == .windows) {
const pathname_w = try windows.sliceToPrefixedFileW(pathname);