forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadlink-getfd.zig
66 lines (57 loc) · 2.15 KB
/
readlink-getfd.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const std = @import("std");
const path_handler = @import("../src/resolver/resolve_path.zig");
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
// zig build-exe -Doptimize=ReleaseFast --main-pkg-path ../ ./readlink-getfd.zig
pub fn main() anyerror!void {
var stdout_ = std.io.getStdOut();
var stderr_ = std.io.getStdErr();
var output_source = Output.Source.init(stdout_, stderr_);
Output.Source.set(&output_source);
defer Output.flush();
var args_buffer: [8192 * 2]u8 = undefined;
var fixed_buffer = std.heap.FixedBufferAllocator.init(&args_buffer);
var allocator = fixed_buffer.allocator();
var args = std.mem.bytesAsSlice([]u8, try std.process.argsAlloc(allocator));
const to_resolve = args[args.len - 1];
const cwd = try bun.getcwdAlloc(allocator);
var path: []u8 = undefined;
var out_buffer: bun.PathBuffer = undefined;
var j: usize = 0;
while (j < 1000) : (j += 1) {
var parts = [1][]const u8{to_resolve};
var joined_buf: bun.PathBuffer = undefined;
var joined = path_handler.joinAbsStringBuf(
cwd,
&joined_buf,
&parts,
.loose,
);
joined_buf[joined.len] = 0;
const os = std.posix;
const joined_z: [:0]const u8 = joined_buf[0..joined.len :0];
const O_PATH = if (@hasDecl(bun.O, "PATH")) bun.O.PATH else 0;
var file = std.posix.openZ(joined_z, O_PATH | bun.O.CLOEXEC, 0) catch |err| {
switch (err) {
error.NotDir, error.FileNotFound => {
Output.prettyError("<r><red>404 Not Found<r>: <b>\"{s}\"<r>", .{joined_z});
Global.exit(1);
},
else => {
return err;
},
}
};
path = try std.os.getFdPath(file.handle, &out_buffer);
file.close();
}
Output.print("{s}", .{path});
}