From d42b397adfae6ed5ba591e300b53da8b30bfad43 Mon Sep 17 00:00:00 2001 From: ciao Date: Sun, 13 Apr 2025 08:33:20 +0800 Subject: [PATCH 1/2] feat: init commit for coreutils project --- README.md | 28 +++++++++++++++++++++++++--- build.zig | 25 ++++++++++++------------- src/{ => wc}/main.cpp | 0 src/{ => wc}/options.cpp | 0 src/{ => wc}/options.hpp | 0 src/{ => wc}/params.cpp | 0 src/{ => wc}/params.hpp | 0 src/{ => wc}/wc.cpp | 0 src/{ => wc}/wc.hpp | 0 9 files changed, 37 insertions(+), 16 deletions(-) rename src/{ => wc}/main.cpp (100%) rename src/{ => wc}/options.cpp (100%) rename src/{ => wc}/options.hpp (100%) rename src/{ => wc}/params.cpp (100%) rename src/{ => wc}/params.hpp (100%) rename src/{ => wc}/wc.cpp (100%) rename src/{ => wc}/wc.hpp (100%) diff --git a/README.md b/README.md index 5e838c2..7df0667 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,18 @@ -A `wc` like program written by `C++` but built by `Zig`. +# Coreutils Implementation + +A project to implement GNU coreutils utilities in `C++` and `Zig`. This is an ongoing effort to recreate the essential Unix/Linux command-line tools. + +## Implemented Utilities + +- `wc`: Word, line, and byte counting utility + +## Planned Utilities + +The project aims to implement all core utilities, including but not limited to: +- File Management: `ls`, `cp`, `mv`, `rm`, `mkdir`, `cat`, etc. +- Text Processing: `head`, `tail`, `sort`, `uniq`, etc. +- System Information: `pwd`, `whoami`, `uname`, etc. +- And many more... ## Installation @@ -16,11 +30,11 @@ cd wc zig build ``` -The executable will be available at `zig-out/bin/wc`. +The executables will be available in `zig-out/bin/`. ## Usage -### Examples +### wc Examples Count lines, words, and characters in a file: ```bash @@ -37,6 +51,14 @@ Count words from standard input: cat file.txt | wc -w ``` +## Contributing + +Contributions are welcome! Feel free to: +- Implement new utilities +- Improve existing implementations +- Report bugs +- Suggest enhancements + ## License Licensed under Apache-2.0 license ([LICENSE](LICENSE) or http://opensource.org/licenses/Apache-2.0) \ No newline at end of file diff --git a/build.zig b/build.zig index c9b307f..0600533 100644 --- a/build.zig +++ b/build.zig @@ -1,40 +1,39 @@ const std = @import("std"); const zcc = @import("compile_commands"); -const executable_name = "wc"; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); var targets = std.ArrayList(*std.Build.Step.Compile).init(b.allocator); - const exe = b.addExecutable(.{ - .name = executable_name, + const wc_exe = b.addExecutable(.{ + .name = "wc", .target = target, .optimize = optimize, }); - targets.append(exe) catch @panic("OOM"); + targets.append(wc_exe) catch @panic("OOM"); const cpp_sources = [_][]const u8{ - "src/main.cpp", - "src/wc.cpp", - "src/options.cpp", - "src/params.cpp", + "src/wc/main.cpp", + "src/wc/wc.cpp", + "src/wc/options.cpp", + "src/wc/params.cpp", }; for (cpp_sources) |source| { - exe.addCSourceFile(.{ + wc_exe.addCSourceFile(.{ .file = b.path(source), .flags = &[_][]const u8{"-std=c++20"}, }); } - exe.linkLibCpp(); - exe.addIncludePath(b.path("src")); + wc_exe.linkLibCpp(); + wc_exe.addIncludePath(b.path("src/wc")); - b.installArtifact(exe); + b.installArtifact(wc_exe); - const run_cmd = b.addRunArtifact(exe); + const run_cmd = b.addRunArtifact(wc_exe); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| { diff --git a/src/main.cpp b/src/wc/main.cpp similarity index 100% rename from src/main.cpp rename to src/wc/main.cpp diff --git a/src/options.cpp b/src/wc/options.cpp similarity index 100% rename from src/options.cpp rename to src/wc/options.cpp diff --git a/src/options.hpp b/src/wc/options.hpp similarity index 100% rename from src/options.hpp rename to src/wc/options.hpp diff --git a/src/params.cpp b/src/wc/params.cpp similarity index 100% rename from src/params.cpp rename to src/wc/params.cpp diff --git a/src/params.hpp b/src/wc/params.hpp similarity index 100% rename from src/params.hpp rename to src/wc/params.hpp diff --git a/src/wc.cpp b/src/wc/wc.cpp similarity index 100% rename from src/wc.cpp rename to src/wc/wc.cpp diff --git a/src/wc.hpp b/src/wc/wc.hpp similarity index 100% rename from src/wc.hpp rename to src/wc/wc.hpp From f050476bbb0b5f09a9552d311155475d195e19b3 Mon Sep 17 00:00:00 2001 From: ciao Date: Sun, 13 Apr 2025 09:11:07 +0800 Subject: [PATCH 2/2] build: update build.zig --- build.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 0600533..92c337b 100644 --- a/build.zig +++ b/build.zig @@ -1,11 +1,11 @@ const std = @import("std"); const zcc = @import("compile_commands"); - pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); var targets = std.ArrayList(*std.Build.Step.Compile).init(b.allocator); + defer targets.deinit(); const wc_exe = b.addExecutable(.{ .name = "wc", @@ -33,6 +33,8 @@ pub fn build(b: *std.Build) void { b.installArtifact(wc_exe); + const run_step = b.step("run-wc", "Run the wc app"); + const run_cmd = b.addRunArtifact(wc_exe); run_cmd.step.dependOn(b.getInstallStep()); @@ -40,7 +42,6 @@ pub fn build(b: *std.Build) void { run_cmd.addArgs(args); } - const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); zcc.createStep(b, "cdb", targets.toOwnedSlice() catch @panic("OOM")); }