diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 08:46:32 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 08:46:32 +0000 |
| commit | 8a2b0d1ccca3a5d0f50b4db1b84687000dc74882 (patch) | |
| tree | 91ff883308422aa65900374bd23e3347406a804b /src/builtins.zig | |
| parent | b92ef57d9cfb3b8b51848d3a2940b0b2251c0529 (diff) | |
| download | shell-zig-8a2b0d1ccca3a5d0f50b4db1b84687000dc74882.tar.gz shell-zig-8a2b0d1ccca3a5d0f50b4db1b84687000dc74882.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/builtins.zig')
| -rw-r--r-- | src/builtins.zig | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index 8ce4a6f..4f8dfb8 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -181,3 +181,41 @@ pub fn executeHistoryWrite(stdout: anytype, file_path: []const u8, history_list: try file.writeAll("\n"); } } + +pub fn executeHistoryAppend(stdout: anytype, file_path: []const u8, history_list: []const []const u8, last_written_index: *usize) !void { + // Open file in append mode, or create if doesn't exist + const file = std.fs.cwd().openFile(file_path, .{ .mode = .write_only }) catch |err| { + if (err == error.FileNotFound) { + // Create the file if it doesn't exist + const new_file = std.fs.cwd().createFile(file_path, .{}) catch { + try stdout.print("history: cannot write to {s}: error\n", .{file_path}); + return; + }; + defer new_file.close(); + + // Write all commands to new file + for (history_list) |cmd| { + try new_file.writeAll(cmd); + try new_file.writeAll("\n"); + } + last_written_index.* = history_list.len; + return; + } else { + try stdout.print("history: cannot open {s}: error\n", .{file_path}); + return; + } + }; + defer file.close(); + + // Seek to end to append + try file.seekFromEnd(0); + + // Append only new commands (commands after last_written_index) + const start_idx = last_written_index.*; + for (history_list[start_idx..]) |cmd| { + try file.writeAll(cmd); + try file.writeAll("\n"); + } + + last_written_index.* = history_list.len; +} |