Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+proc-executable-file-check.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The private `proc.isExecutableFile` helper holds the executable regular-file probe that `proc.which` runs for each PATH candidate.
14 changes: 9 additions & 5 deletions src/proc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ pub fn which(gpa: Allocator, name: []const u8, path_env: []const u8) Allocator.E
while (it.next()) |dir_raw| {
const cand = try std.fs.path.join(gpa, &.{ if (dir_raw.len == 0) "." else dir_raw, name });
defer gpa.free(cand);
var zb: [sys.path_max:0]u8 = undefined;
const zc = sys.z(&zb, cand) catch continue;
if (linux.errno(linux.faccessat(linux.AT.FDCWD, zc.ptr, linux.X_OK, 0)) != .SUCCESS) continue;
const st = fsx.statAt(fsx.cwd, cand, true) catch continue;
if (!st.isReg()) continue;
if (!isExecutableFile(cand)) continue;
if (cand[0] == '/') return try gpa.dupe(u8, cand);
var rb: [sys.path_max]u8 = undefined;
const dir_abs = fsx.realpath(fsx.cwd, if (dir_raw.len == 0) "." else dir_raw, &rb) catch continue;
Expand All @@ -137,6 +133,14 @@ pub fn which(gpa: Allocator, name: []const u8, path_env: []const u8) Allocator.E
return null;
}

fn isExecutableFile(path: []const u8) bool {
var zb: [sys.path_max:0]u8 = undefined;
const z = sys.z(&zb, path) catch return false;
if (linux.errno(linux.faccessat(linux.AT.FDCWD, z.ptr, linux.X_OK, 0)) != .SUCCESS) return false;
const st = fsx.statAt(fsx.cwd, path, true) catch return false;
return st.isReg();
}

fn nowMs() i64 {
var ts: linux.timespec = undefined;
_ = linux.clock_gettime(.MONOTONIC, &ts);
Expand Down
Loading