summaryrefslogtreecommitdiff
path: root/src/cmd/chdir.zig
blob: fa2da8393b7272ea0df67bf0abff5533dafedf1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const std = @import("std");
const Allocator = std.mem.Allocator;
const print = std.debug.print;

const paths = @import("../paths.zig");
const formatDosPath = paths.formatDosPath;

const types = @import("./types.zig");
const CommandStatus = types.CommandStatus;
const OutputCapture = types.OutputCapture;
const InputSource = types.InputSource;

pub const Chdir = struct {
    path: []const u8,

    pub fn eval(chdir: Chdir, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = input_source;

        if (chdir.path.len == 0) {
            // No arguments - display current directory
            const cwd = std.fs.cwd().realpathAlloc(allocator, ".") catch {
                const error_msg = "Unable to determine current directory\n";
                if (output_capture) |capture| {
                    try capture.write(error_msg);
                } else {
                    print("{s}", .{error_msg});
                }
                return CommandStatus{ .Code = 1 };
            };
            defer allocator.free(cwd);

            const formatted_path = try formatDosPath(allocator, cwd);
            defer allocator.free(formatted_path);

            const output = try std.fmt.allocPrint(allocator, "{s}\n", .{formatted_path});
            defer allocator.free(output);

            if (output_capture) |capture| {
                try capture.write(output);
            } else {
                print("{s}", .{output});
            }
            return CommandStatus{ .Code = 0 };
        } else {
            // Change directory
            const target_path = chdir.path;

            // Handle special cases
            if (std.mem.eql(u8, target_path, "..")) {
                // Go to parent directory
                std.process.changeCurDir("..") catch {
                    const error_msg = "The system cannot find the path specified.\n";
                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                };
            } else if (std.mem.eql(u8, target_path, "\\") or std.mem.eql(u8, target_path, "/")) {
                // Go to root directory - simplified to just go to "/"
                std.process.changeCurDir("/") catch {
                    const error_msg = "The system cannot find the path specified.\n";
                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                };
            } else {
                // Regular directory change
                // Make sure the path doesn't contain null bytes
                for (target_path) |ch| {
                    if (ch == 0) {
                        const error_msg = "Invalid path: contains null character\n";
                        if (output_capture) |capture| {
                            try capture.write(error_msg);
                        } else {
                            print("{s}", .{error_msg});
                        }
                        return CommandStatus{ .Code = 1 };
                    }
                }

                std.process.changeCurDir(target_path) catch {
                    const error_msg = "The system cannot find the path specified.\n";
                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                };
            }
            return CommandStatus{ .Code = 0 };
        }
    }
};