Skip to content

Commit 19f08cb

Browse files
refactor: start over
1 parent be469a6 commit 19f08cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1343
-1061
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
zig-cache
1+
.zig-cache
22
zig-out
3+
result
4+
result-cache

applets.zig

-10
This file was deleted.

applets/arch.zig

-7
This file was deleted.

applets/arch/main.zig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
4+
pub fn main() !void {
5+
const stdout = std.io.getStdOut().writer().any();
6+
try stdout.writeAll(@tagName(builtin.cpu.arch) ++ "\n");
7+
}

applets/basename/main.zig

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
const native_os = builtin.os.tag;
4+
5+
var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
6+
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
7+
8+
pub fn main() !void {
9+
const gpa, const gpa_deinit = gpa: {
10+
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
11+
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
12+
break :gpa switch (builtin.mode) {
13+
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
14+
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
15+
};
16+
};
17+
defer if (gpa_deinit) {
18+
_ = debug_allocator.deinit();
19+
};
20+
21+
var args = try std.process.argsWithAllocator(gpa);
22+
defer args.deinit();
23+
24+
_ = args.skip();
25+
26+
if (args.next()) |comp| {
27+
const basename = std.fs.path.basename(comp);
28+
const stdout = std.io.getStdOut().writer().any();
29+
try stdout.writeAll(if (basename.len == 0) "/" else basename);
30+
try stdout.writeByte('\n');
31+
} else {
32+
const stderr = std.io.getStdErr().writer().any();
33+
try stderr.writeAll("basename: path is required\n");
34+
std.process.exit(1);
35+
}
36+
}

applets/cal.zig

-158
This file was deleted.

applets/cat.zig

-58
This file was deleted.

applets/cat/main.zig

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
const native_os = builtin.os.tag;
4+
5+
var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
6+
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
7+
8+
pub fn main() !void {
9+
const gpa, const gpa_deinit = gpa: {
10+
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
11+
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
12+
break :gpa switch (builtin.mode) {
13+
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
14+
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
15+
};
16+
};
17+
defer if (gpa_deinit) {
18+
_ = debug_allocator.deinit();
19+
};
20+
21+
var args = try std.process.argsWithAllocator(gpa);
22+
defer args.deinit();
23+
24+
_ = args.skip();
25+
26+
var i: usize = 0;
27+
28+
const raw_stdout = std.io.getStdOut().writer().any();
29+
var buffered_stdout = std.io.bufferedWriter(raw_stdout);
30+
defer buffered_stdout.flush() catch |err| std.debug.panic("Failed to flush stdout: {}", .{err});
31+
const stdout = buffered_stdout.writer().any();
32+
33+
while (args.next()) |p| : (i += 1) {
34+
var file = if (std.fs.path.isAbsolute(p)) try std.fs.openFileAbsolute(p, .{}) else try std.fs.cwd().openFile(p, .{});
35+
defer file.close();
36+
37+
const reader = file.reader();
38+
39+
while (reader.readByte() catch null) |b| try stdout.writeByte(b);
40+
}
41+
42+
if (i == 0) {
43+
const stderr = std.io.getStdErr().writer().any();
44+
try stderr.writeAll("cat: path is required\n");
45+
std.process.exit(1);
46+
}
47+
}

applets/chmod/main.zig

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
const native_os = builtin.os.tag;
4+
5+
var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
6+
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
7+
8+
pub fn main() !void {
9+
const gpa, const gpa_deinit = gpa: {
10+
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
11+
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
12+
break :gpa switch (builtin.mode) {
13+
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
14+
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
15+
};
16+
};
17+
defer if (gpa_deinit) {
18+
_ = debug_allocator.deinit();
19+
};
20+
21+
var args = try std.process.argsWithAllocator(gpa);
22+
defer args.deinit();
23+
24+
_ = args.skip();
25+
}

0 commit comments

Comments
 (0)