diff --git a/src/lib.zig b/src/lib.zig index b3b361a276f620a8bee6443ebe4daedba46ffee8..8357e0b390ae04436523f24c1887fa3f29d86bd2 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -1,6 +1,7 @@ const std = @import("std"); const string = []const u8; const assert = std.debug.assert; +const builtin = @import("builtin"); pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string { return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZYRQ"); @@ -833,3 +834,16 @@ pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { } }; } + +pub fn fd_realpath(fd: std.os.fd_t) ![std.fs.MAX_PATH_BYTES:0]u8 { + switch (builtin.os.tag) { + .linux => { + var buf = std.mem.zeroes([64]u8); + var res = std.mem.zeroes([std.fs.MAX_PATH_BYTES:0]u8); + const str = try std.fmt.bufPrint(&buf, "/proc/self/fd/{d}", .{fd}); + _ = try std.os.readlink(str, &res); + return res; + }, + else => @compileError("not implemented!"), + } +}