Add pipes to shell, add wc

This commit is contained in:
2026-07-16 21:39:00 +03:00
parent 4ad32dda17
commit 85cfbb2050
17 changed files with 293 additions and 63 deletions
+59
View File
@@ -0,0 +1,59 @@
#include "src/lib/string.h"
#include "src/lib/syscall.h"
#include "src/lib/util.h"
#include "src/user/syscall.h"
#define BLOCK_SIZE 65536
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
exit_code_t pass(uint64_t fd) {
static char buffer[BLOCK_SIZE];
uint64_t bytes;
uint64_t total = 0;
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
if (bytes == (uint64_t)-1) {
PRINT_S("wc: could not read file\n");
return EXIT_CODE_GENERAL_FAILURE;
} else {
total += bytes;
}
}
uint64_t l = string_format("%d\n", BLOCK_SIZE, buffer, total);
write(1, buffer, l);
return EXIT_CODE_OK;
}
exit_code_t wc(const char *path) {
uint64_t fd = open(path, OPEN_FILE);
if (fd == (uint64_t)-1) {
PRINT_S("wc: path does not exist\n");
return EXIT_CODE_GENERAL_FAILURE;
}
exit_code_t code = pass(fd);
close(fd);
return code;
}
exit_code_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return pass(0);
}
uint64_t code = EXIT_CODE_OK;
for (uint64_t i = 1; i < argc; i++) {
exit_code_t c = wc(argv[i]);
if (c != EXIT_CODE_OK) {
code = c;
}
}
return code;
}