diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 08:50:06 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 08:50:06 +0000 |
| commit | 84de37f25e24f68424e0060de28920bb6a841595 (patch) | |
| tree | aa0aa25e3ccb4d2f66535529fbe1365606513cee /src/main.zig | |
| parent | 8a2b0d1ccca3a5d0f50b4db1b84687000dc74882 (diff) | |
| download | shell-zig-84de37f25e24f68424e0060de28920bb6a841595.tar.gz shell-zig-84de37f25e24f68424e0060de28920bb6a841595.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index d373826..cc272ec 100644 --- a/src/main.zig +++ b/src/main.zig @@ -363,6 +363,37 @@ pub fn main() !void { var last_written_index: usize = 0; + // Load history from HISTFILE if it exists + if (std.posix.getenv("HISTFILE")) |histfile_path| { + const file = std.fs.cwd().openFile(histfile_path, .{}) catch null; + if (file) |f| { + defer f.close(); + + const file_size = f.getEndPos() catch 0; + if (file_size > 0) { + const buffer = allocator.alloc(u8, file_size) catch null; + if (buffer) |buf| { + defer allocator.free(buf); + + const bytes_read = f.readAll(buf) catch 0; + if (bytes_read > 0) { + var line_iter = std.mem.splitScalar(u8, buf[0..bytes_read], '\n'); + while (line_iter.next()) |line| { + const trimmed = std.mem.trim(u8, line, " \r"); + if (trimmed.len > 0) { + const line_copy = allocator.dupe(u8, trimmed) catch continue; + history.append(allocator, line_copy) catch { + allocator.free(line_copy); + continue; + }; + } + } + } + } + } + } + } + const stdout = std.fs.File.stdout(); while (true) { |