Convert file descriptors to generic streams

This commit is contained in:
2026-06-18 21:46:18 +03:00
parent bcf2e5bac6
commit 9761e3e870
20 changed files with 150 additions and 79 deletions
+9 -7
View File
@@ -1,3 +1,4 @@
#include "src/lib/util.h"
#include "src/user/syscall.h"
#define BLOCK_SIZE 65536
@@ -5,12 +6,12 @@
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
uint64_t cat(const char *path) {
exit_code_t cat(const char *path) {
uint64_t fd = open(path);
if (fd == (uint64_t)-1) {
PRINT_S("cat: path does not exist\n");
return (uint64_t)-1;
return EXIT_CODE_GENERAL_FAILURE;
}
static char buffer[BLOCK_SIZE];
@@ -19,7 +20,7 @@ uint64_t cat(const char *path) {
if (bytes == (uint64_t)-1) {
PRINT_S("cat: could not read file\n");
close(fd);
return (uint64_t)-1;
return EXIT_CODE_GENERAL_FAILURE;
}
write(1, buffer, bytes);
}
@@ -29,15 +30,16 @@ uint64_t cat(const char *path) {
return 0;
}
uint64_t main(uint64_t argc, const char **argv) {
exit_code_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return cat(".");
}
uint64_t code = 0;
uint64_t code = EXIT_CODE_OK;
for (uint64_t i = 1; i < argc; i++) {
if (cat(argv[i]) == (uint64_t)-1) {
code = (uint64_t)-1;
exit_code_t c = cat(argv[i]);
if (c != EXIT_CODE_OK) {
code = c;
}
}
+3 -2
View File
@@ -1,7 +1,8 @@
#include "src/lib/string.h"
#include "src/lib/util.h"
#include "src/user/syscall.h"
uint64_t main(uint64_t argc, const char **argv) {
exit_code_t main(uint64_t argc, const char **argv) {
for (uint64_t i = 1; i < argc; i++) {
if (i > 1) {
write(1, " ", 1);
@@ -9,5 +10,5 @@ uint64_t main(uint64_t argc, const char **argv) {
write(1, argv[i], string_length(argv[i]));
}
write(1, "\n", 1);
return 0;
return EXIT_CODE_OK;
}
+9 -7
View File
@@ -1,20 +1,21 @@
#include "src/lib/string.h"
#include "src/lib/util.h"
#include "src/user/syscall.h"
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define PRINT_D(s) write(1, s, string_length(s));
uint64_t ls(const char *path) {
exit_code_t ls(const char *path) {
uint64_t i = 0;
while (1) {
char out[100];
uint64_t s = readdir(path, i, 100, out);
if (!s) {
return 0;
return EXIT_CODE_OK;
}
if (s == (uint64_t)-1) {
PRINT_S("ls: path does not exist\n");
return (uint64_t)-1;
return EXIT_CODE_GENERAL_FAILURE;
}
PRINT_D(out);
PRINT_S("\n");
@@ -22,20 +23,21 @@ uint64_t ls(const char *path) {
}
}
uint64_t main(uint64_t argc, const char **argv) {
exit_code_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return ls(".");
}
uint64_t code = 0;
exit_code_t code = EXIT_CODE_OK;
for (uint64_t i = 1; i < argc; i++) {
if (argc > 2) {
PRINT_S("\n");
PRINT_D(argv[i]);
PRINT_S(":\n");
}
if (ls(argv[i]) == (uint64_t)-1) {
code = (uint64_t)-1;
exit_code_t c = ls(argv[i]);
if (c != EXIT_CODE_OK) {
code = c;
}
}
+3 -2
View File
@@ -1,5 +1,6 @@
#include "src/lib/memory.h"
#include "src/lib/string.h"
#include "src/lib/util.h"
#include "src/user/syscall.h"
#include <stdint.h>
@@ -123,7 +124,7 @@ static void on_enter_pressed() {
char *path = memory_allocate(size);
memory_copy(PATH, sizeof(PATH), path);
memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1);
if (spawn(path, argc, (const char **)argv) == (uint64_t)-1) {
if (spawn(path, argc, (const char **)argv) == EXIT_CODE_NOT_FOUND) {
PRINT_S("terminal: program not found\n");
}
memory_free(path);
@@ -140,7 +141,7 @@ static void on_cancel_pressed() {
static void on_disconnect_pressed() {
PRINT_S("^D\n");
exit();
exit(EXIT_CODE_OK);
}
static void on_ctrl_character_pressed(char c) {