zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit dd66fbb96a2e1f85e7dacdce18ebc5f85d26d0cc (tree)
parent e3fec6cce91a6ad5d63f78afe96649dc2aeea6a1
Author: Michael Dusan <michael.dusan@gmail.com>
Date:   Wed, 25 Mar 2020 19:49:52 -0400

Merge pull request #4811 from mikdusan/fix4634

self-hosted: use fs.selfExePathAlloc
Diffstat:
Mlib/std/fs.zig | 17+++++++----------
Msrc-self-hosted/introspect.zig | 2+-
2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/lib/std/fs.zig b/lib/std/fs.zig @@ -1509,6 +1509,13 @@ test "openSelfExe" { pub const SelfExePathError = os.ReadLinkError || os.SysCtlError; +/// `selfExePath` except allocates the result on the heap. +/// Caller owns returned memory. +pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 { + var buf: [MAX_PATH_BYTES]u8 = undefined; + return mem.dupe(allocator, u8, try selfExePath(&buf)); +} + /// Get the path to the current executable. /// If you only need the directory, use selfExeDirPath. /// If you only want an open file handle, use openSelfExe. @@ -1568,16 +1575,6 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 { /// Get the directory path that contains the current executable. /// Returned value is a slice of out_buffer. pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 { - if (builtin.os.tag == .linux) { - // If the currently executing binary has been deleted, - // the file path looks something like `/a/b/c/exe (deleted)` - // This path cannot be opened, but it's valid for determining the directory - // the executable was in when it was run. - const full_exe_path = try os.readlinkC("/proc/self/exe", out_buffer); - // Assume that /proc/self/exe has an absolute path, and therefore dirname - // will not return null. - return path.dirname(full_exe_path).?; - } const self_exe_path = try selfExePath(out_buffer); // Assume that the OS APIs return absolute paths, and therefore dirname // will not return null. diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig @@ -41,7 +41,7 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![ /// Caller must free result pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { - const self_exe_path = try fs.selfExeDirPathAlloc(allocator); + const self_exe_path = try fs.selfExePathAlloc(allocator); defer allocator.free(self_exe_path); var cur_path: []const u8 = self_exe_path;