const std = @import("std"); const bun = @import("root").bun; fn isValid(buf: *[bun.MAX_PATH_BYTES]u8, segment: []const u8, bin: []const u8) ?u16 { bun.copy(u8, buf, segment); buf[segment.len] = std.fs.path.sep; bun.copy(u8, buf[segment.len + 1 ..], bin); buf[segment.len + 1 + bin.len ..][0] = 0; const filepath = buf[0 .. segment.len + 1 + bin.len :0]; if (!checkPath(filepath)) return null; return @as(u16, @intCast(filepath.len)); } extern "C" fn is_executable_file(path: [*:0]const u8) bool; fn checkPath(filepath: [:0]const u8) bool { bun.JSC.markBinding(@src()); return is_executable_file(filepath); } // Like /usr/bin/which but without needing to exec a child process // Remember to resolve the symlink if necessary pub fn which(buf: *[bun.MAX_PATH_BYTES]u8, path: []const u8, cwd: []const u8, bin: []const u8) ?[:0]const u8 { if (bin.len == 0) return null; // handle absolute paths if (std.fs.path.isAbsolute(bin)) { bun.copy(u8, buf, bin); buf[bin.len] = 0; var binZ: [:0]u8 = buf[0..bin.len :0]; if (checkPath(binZ)) return binZ; // note that directories are often executable // TODO: should we return null here? What about the case where ytou have // /foo/bar/baz as a path and you're in /home/jarred? } if (cwd.len > 0) { if (isValid(buf, std.mem.trimRight(u8, cwd, std.fs.path.sep_str), bin)) |len| { return buf[0..len :0]; } } var path_iter = std.mem.tokenize(u8, path, ":"); while (path_iter.next()) |segment| { if (isValid(buf, segment, bin)) |len| { return buf[0..len :0]; } } return null; } test "which" { var buf: [bun.MAX_PATH_BYTES]u8 = undefined; var realpath = bun.getenvZ("PATH") orelse unreachable; var whichbin = which(&buf, realpath, try std.process.getCwdAlloc(std.heap.c_allocator), "which"); try std.testing.expectEqualStrings(whichbin orelse return std.debug.assert(false), "/usr/bin/which"); try std.testing.expect(null == which(&buf, realpath, try std.process.getCwdAlloc(std.heap.c_allocator), "baconnnnnn")); try std.testing.expect(null != which(&buf, realpath, try std.process.getCwdAlloc(std.heap.c_allocator), "zig")); try std.testing.expect(null == which(&buf, realpath, try std.process.getCwdAlloc(std.heap.c_allocator), "bin")); try std.testing.expect(null == which(&buf, realpath, try std.process.getCwdAlloc(std.heap.c_allocator), "usr")); } .resolve Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/src/api (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-01-21Handle string subclasses and new String() in new BufferGravatar Jarred Sumner 1-2/+11
2023-01-21Make Buffer.alloc* 3ns fasterGravatar Jarred Sumner 1-11/+17
2023-01-21[buffer] Make Buffer.from pass more testsGravatar Jarred Sumner 4-92/+179
2023-01-20constructor parameter properties in class expressions (#1867)Gravatar Dylan Conway 2-8/+48
2023-01-20Update transpiler.test.jsbun-v0.5.1Gravatar Jarred Sumner 1-1/+1
2023-01-20Update transpiler.test.jsGravatar Jarred Sumner 1-1/+1
2023-01-20push super before generated statements (#1856)Gravatar Dylan Conway 2-29/+53
2023-01-20Clear the errorsGravatar Jarred Sumner 1-0/+2
2023-01-20one less hash tableGravatar Jarred Sumner 1-2/+13
2023-01-20Add another testGravatar Jarred Sumner 2-1/+11
2023-01-20fix hanging testGravatar Jarred Sumner 1-38/+42
2023-01-20Further cleanup buffer encodingGravatar Jarred Sumner 1-48/+26
2023-01-20Fixes #1855Gravatar Jarred Sumner 2-5/+57
2023-01-20Fix assertion failure with boringssl messagesGravatar Jarred Sumner 3-5/+132
2023-01-19Revert "ignore sighup"Gravatar Jarred Sumner 1-45/+10
2023-01-19ignore sighupGravatar Jarred Sumner 1-10/+45
2023-01-19make this code easier to readGravatar Jarred Sumner 3-29/+26
2023-01-19Update types.zigGravatar Jarred Sumner 1-4/+0
2023-01-19BumpGravatar Jarred Sumner 2-2/+2
2023-01-19Fix buffer encoding bugGravatar Jarred Sumner 2-4/+17
2023-01-19use `String.from()` (#1850)Gravatar Alex Lam S.L 4-5/+12
2023-01-19Bump zigGravatar Jarred Sumner 2-2/+2
2023-01-19make it packedGravatar Jarred Sumner 1-2/+2
2023-01-20Bugfixes to install (#1848)Gravatar Jarred Sumner 5-26/+119
2023-01-19repopulate `alias_map` correctly (#1847)Gravatar Alex Lam S.L 5-70/+240
2023-01-19Add a commentGravatar Jarred Sumner 1-0/+6
2023-01-19Add a debug safety check for UAF in AST nodesGravatar Jarred Sumner 1-0/+5
2023-01-19Fix UAF when opening workspacesGravatar Jarred Sumner 1-2/+0
2023-01-19Improve error message when a workspace is not foundGravatar Jarred Sumner 2-9/+97