diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-14 16:31:23 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-14 16:31:23 +0200 |
commit | f40779cf8ebe81ffa802967587a83d71c74c1c7e (patch) | |
tree | 2728f9f8c4f90cb471478c1d0fa936ea4fc9e7cd /src/cmd/rename.zig | |
parent | b74311035a816bbdf9ea69f172d7f6538d8ae9f8 (diff) |
Factor out CD, DEL, MD, RD, PATH, DIR, TYPE.
Diffstat (limited to 'src/cmd/rename.zig')
-rw-r--r-- | src/cmd/rename.zig | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/cmd/rename.zig b/src/cmd/rename.zig new file mode 100644 index 0000000..e3401d2 --- /dev/null +++ b/src/cmd/rename.zig @@ -0,0 +1,66 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const print = std.debug.print; + +const syntax = @import("../syntax.zig"); +const FileSpec = syntax.FileSpec; + +const types = @import("./types.zig"); +const CommandStatus = types.CommandStatus; +const OutputCapture = types.OutputCapture; +const InputSource = types.InputSource; + +pub const Rename = struct { + from: FileSpec, + to: FileSpec, + + pub fn eval(rename: Rename, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus { + _ = allocator; + _ = input_source; + + const from_path = switch (rename.from) { + .Con, .Lpt1, .Lpt2, .Lpt3, .Prn => { + const error_msg = "Cannot rename device\n"; + if (output_capture) |capture| { + try capture.write(error_msg); + } else { + print("{s}", .{error_msg}); + } + return CommandStatus{ .Code = 1 }; + }, + .Path => |path| path, + }; + + const to_path = switch (rename.to) { + .Con, .Lpt1, .Lpt2, .Lpt3, .Prn => { + const error_msg = "Cannot rename to device\n"; + if (output_capture) |capture| { + try capture.write(error_msg); + } else { + print("{s}", .{error_msg}); + } + return CommandStatus{ .Code = 1 }; + }, + .Path => |path| path, + }; + + std.fs.cwd().rename(from_path, to_path) catch |err| { + const error_msg = switch (err) { + error.FileNotFound => "The system cannot find the file specified\n", + error.AccessDenied => "Access denied\n", + error.PathAlreadyExists => "A duplicate file name exists, or the file cannot be found\n", + error.RenameAcrossMountPoints => "Cannot rename across different drives\n", + else => "Cannot rename file\n", + }; + if (output_capture) |capture| { + try capture.write(error_msg); + } else { + print("{s}", .{error_msg}); + } + return CommandStatus{ .Code = 1 }; + }; + + // No output for successful rename (DOS style) + return CommandStatus{ .Code = 0 }; + } +}; |