use stdlib selfExePath

This commit is contained in:
Mitchell Hashimoto
2023-06-24 12:12:49 -07:00
parent 9c8f784386
commit b2cd2e06de
3 changed files with 1 additions and 44 deletions

View File

@ -1,42 +0,0 @@
const std = @import("std");
const builtin = @import("builtin");
/// Returns the path to the currently executing executable. This may return
/// null if the path cannot be determined. This function is not thread-safe.
///
/// This function can be very slow. The caller can choose to cache the value
/// if they want but this function itself doesn't handle caching.
///
/// From: https://unix.stackexchange.com/questions/317211/absolute-path-to-currently-executing-program
pub fn exePath(buf: []u8) !?[]const u8 {
if (comptime builtin.target.isDarwin()) {
// We put the path into a temporary buffer first because we need
// to call realpath on it to resolve symlinks and expand all ".."
// and such.
var size: u32 = std.math.cast(u32, buf.len) orelse return error.OutOfMemory;
const result = _NSGetExecutablePath(buf.ptr, &size);
if (result == -1) return error.OutOfMemory;
if (result != 0) return error.Unknown;
const path = std.mem.sliceTo(buf, 0);
// Expand.
var realpath_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const realpath = try std.os.realpath(path, &realpath_buf);
if (realpath.len > buf.len) return error.OutOfMemory;
@memcpy(buf[0..realpath.len], realpath);
return buf[0..realpath.len];
}
return null;
}
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
extern "c" fn _NSGetExecutablePath(buf: [*]u8, size: *u32) c_int;
test exePath {
// This just ensures it compiles and runs without crashing. The result
// is allowed to be null for non-supported platforms.
var buf: [4096]u8 = undefined;
_ = try exePath(&buf);
}

View File

@ -1,7 +1,6 @@
//! The "os" package contains utilities for interfacing with the operating //! The "os" package contains utilities for interfacing with the operating
//! system. //! system.
pub usingnamespace @import("exe.zig");
pub usingnamespace @import("file.zig"); pub usingnamespace @import("file.zig");
pub usingnamespace @import("flatpak.zig"); pub usingnamespace @import("flatpak.zig");
pub usingnamespace @import("locale.zig"); pub usingnamespace @import("locale.zig");

View File

@ -775,7 +775,7 @@ const Subprocess = struct {
// Get the path to our running binary // Get the path to our running binary
var exe_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; var exe_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
var exe = (try internal_os.exePath(&exe_buf)) orelse return null; var exe: []const u8 = std.fs.selfExePath(&exe_buf) catch return null;
// We have an exe path! Climb the tree looking for the terminfo // We have an exe path! Climb the tree looking for the terminfo
// bundle as we expect it. // bundle as we expect it.