authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-02 00:55:05 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-02 00:55:05 -08:00
log905e29b6760035e63b7dc027a4c355ff392bb454
tree3119e659cf34f4578ff10534a83aba09b1afc9fe
parent1f7f862f7d7b4c7de5b61668a2b5c86db173275b

add fd_realpath


1 files changed, 14 insertions(+), 0 deletions(-)

src/lib.zig+14
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const builtin = @import("builtin");
45
5pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string {6pub fn fmtByteCountIEC(alloc: std.mem.Allocator, b: u64) !string {
6 return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZYRQ");7 return try reduceNumber(alloc, b, 1024, "B", "KMGTPEZYRQ");
...@@ -833,3 +834,16 @@ pub fn RingBuffer(comptime T: type, comptime capacity: usize) type {...@@ -833,3 +834,16 @@ pub fn RingBuffer(comptime T: type, comptime capacity: usize) type {
833 }834 }
834 };835 };
835}836}
837
838pub fn fd_realpath(fd: std.os.fd_t) ![std.fs.MAX_PATH_BYTES:0]u8 {
839 switch (builtin.os.tag) {
840 .linux => {
841 var buf = std.mem.zeroes([64]u8);
842 var res = std.mem.zeroes([std.fs.MAX_PATH_BYTES:0]u8);
843 const str = try std.fmt.bufPrint(&buf, "/proc/self/fd/{d}", .{fd});
844 _ = try std.os.readlink(str, &res);
845 return res;
846 },
847 else => @compileError("not implemented!"),
848 }
849}