diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-14 12:55:53 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-14 12:55:53 +0200 |
commit | a2057d6a815cfe4d4d3a57a375f170f7888bcbf4 (patch) | |
tree | 0e92563df20658881e0f6803796016b07a33daf2 | |
parent | aab94c7cf995fb846060c758dd78dbfa009c6f10 (diff) |
DIR: Add correct bytes free.
-rw-r--r-- | src/eval.zig | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/eval.zig b/src/eval.zig index bc39887..88c27b7 100644 --- a/src/eval.zig +++ b/src/eval.zig @@ -4,6 +4,11 @@ const ArrayList = std.ArrayList; const Allocator = std.mem.Allocator; const Thread = std.Thread; const Mutex = std.Thread.Mutex; +const c = @cImport({ + @cInclude("sys/statvfs.h"); + @cInclude("errno.h"); + @cInclude("stdio.h"); +}); const syntax = @import("syntax.zig"); const Command = syntax.Command; @@ -297,8 +302,12 @@ fn executeCommandWithOutput(command: Command, allocator: Allocator, output_captu } } - // Get free disk space (simplified - just use a placeholder) - const bytes_free: u64 = 1024 * 1024 * 1024; // 1GB placeholder + // Get free disk space using statvfs + const path = try std.fs.cwd().realpathAlloc(allocator, dir.path); + defer allocator.free(path); + const bytes_free = getFreeDiskSpace(path) catch |err| switch (err) { + error.AccessDenied => 0, + }; try output_buffer.writer().print(" {d} File(s) {d:>14} bytes\n", .{ file_count, total_file_bytes }); try output_buffer.writer().print(" {d} Dir(s) {d:>14} bytes free\n", .{ dir_count, bytes_free }); @@ -1077,3 +1086,12 @@ fn executeCommandWithOutput(command: Command, allocator: Allocator, output_captu fn isLeapYear(year: u32) bool { return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0); } + +fn getFreeDiskSpace(path: []const u8) !u64 { + var stat: c.struct_statvfs = undefined; + if (c.statvfs(path.ptr, &stat) != 0) { + _ = c.perror("statvfs"); + return error.AccessDenied; + } + return stat.f_bsize * stat.f_bfree; +} |