summaryrefslogtreecommitdiff
path: root/src/cmd/chdir.zig
blob: 1adfdd80e429c87e1f431bf003ddad022de97cf0 (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
const std = @import("std");
const Allocator = std.mem.Allocator;

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

const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
const CommandContext = types.CommandContext;

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

    pub fn eval(chdir: Chdir, ctx: CommandContext) !CommandStatus {
        if (chdir.path.len == 0) {
            // No arguments - display current directory
            const cwd = std.fs.cwd().realpathAlloc(ctx.allocator, ".") catch {
                const error_msg = "Unable to determine current directory\n";
                var writer = ctx.output_writer;
                try writer.write(error_msg);
                return CommandStatus{ .Code = 1 };
            };
            defer ctx.allocator.free(cwd);

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

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

            var writer = ctx.output_writer;
            try writer.write(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";
                    var writer = ctx.output_writer;
                    try writer.write(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";
                    var writer = ctx.output_writer;
                    try writer.write(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";
                        var writer = ctx.output_writer;
                        try writer.write(error_msg);
                        return CommandStatus{ .Code = 1 };
                    }
                }

                std.process.changeCurDir(target_path) catch {
                    const error_msg = "The system cannot find the path specified.\n";
                    var writer = ctx.output_writer;
                    try writer.write(error_msg);
                    return CommandStatus{ .Code = 1 };
                };
            }
            return CommandStatus{ .Code = 0 };
        }
    }
};