Add file descriptors and basic cat
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#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));
|
||||
|
||||
uint64_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;
|
||||
}
|
||||
|
||||
static char buffer[BLOCK_SIZE];
|
||||
uint64_t bytes;
|
||||
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
|
||||
if (bytes == (uint64_t)-1) {
|
||||
PRINT_S("cat: could not read file\n");
|
||||
close(fd);
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
write(1, buffer, bytes);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t main(uint64_t argc, const char **argv) {
|
||||
if (argc == 1) {
|
||||
return cat(".");
|
||||
}
|
||||
|
||||
uint64_t code = 0;
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (cat(argv[i]) == (uint64_t)-1) {
|
||||
code = (uint64_t)-1;
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "src/lib/string.h"
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
#define WRITE_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define WRITE_D(s) write(1, s, string_length(s));
|
||||
#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) {
|
||||
uint64_t i = 0;
|
||||
@@ -13,11 +13,11 @@ uint64_t ls(const char *path) {
|
||||
return 0;
|
||||
}
|
||||
if (s == (uint64_t)-1) {
|
||||
WRITE_S("ls: path does not exist\n");
|
||||
PRINT_S("ls: path does not exist\n");
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
WRITE_D(out);
|
||||
WRITE_S("\n");
|
||||
PRINT_D(out);
|
||||
PRINT_S("\n");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -30,9 +30,9 @@ uint64_t main(uint64_t argc, const char **argv) {
|
||||
uint64_t code = 0;
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (argc > 2) {
|
||||
WRITE_S("\n");
|
||||
WRITE_D(argv[i]);
|
||||
WRITE_S(":\n");
|
||||
PRINT_S("\n");
|
||||
PRINT_D(argv[i]);
|
||||
PRINT_S(":\n");
|
||||
}
|
||||
if (ls(argv[i]) == (uint64_t)-1) {
|
||||
code = (uint64_t)-1;
|
||||
|
||||
@@ -8,8 +8,8 @@ static void cd(uint8_t argc, char **argv);
|
||||
|
||||
#define PATH "/bin/"
|
||||
|
||||
#define WRITE_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define WRITE_D(s) write(1, s, string_length(s));
|
||||
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define PRINT_D(s) write(1, s, string_length(s));
|
||||
|
||||
#define PROMPT_LENGTH 255
|
||||
|
||||
@@ -34,25 +34,25 @@ static app_entry_t builtins[] = {
|
||||
|
||||
static void help(uint8_t argc, __attribute__((unused)) char **argv) {
|
||||
if (argc) {
|
||||
WRITE_S("help: expects no arguments");
|
||||
PRINT_S("help: expects no arguments");
|
||||
return;
|
||||
}
|
||||
|
||||
WRITE_S("Available commands:\n");
|
||||
PRINT_S("Available commands:\n");
|
||||
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
|
||||
WRITE_D(builtins[i].name);
|
||||
WRITE_S("\n");
|
||||
PRINT_D(builtins[i].name);
|
||||
PRINT_S("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void cd(uint8_t argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
WRITE_S("cd: requires a single path\n");
|
||||
PRINT_S("cd: requires a single path\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (chdir(argv[1]) == (uint64_t)-1) {
|
||||
WRITE_S("cd: path does not exist\n");
|
||||
PRINT_S("cd: path does not exist\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,9 +62,9 @@ static void print_prompt() {
|
||||
char *components[16];
|
||||
uint8_t cl = string_split(path, '/', 16, components);
|
||||
|
||||
WRITE_S("[")
|
||||
WRITE_D(pl == 1 ? "/" : components[cl - 1]);
|
||||
WRITE_S("]$ ");
|
||||
PRINT_S("[")
|
||||
PRINT_D(pl == 1 ? "/" : components[cl - 1]);
|
||||
PRINT_S("]$ ");
|
||||
}
|
||||
|
||||
static void on_home_pressed() {
|
||||
@@ -109,7 +109,7 @@ static void on_enter_pressed() {
|
||||
char *argv[16];
|
||||
uint8_t argc = (uint8_t)string_split(prompt, ' ', 16, argv);
|
||||
|
||||
WRITE_S("\n");
|
||||
PRINT_S("\n");
|
||||
|
||||
uint8_t i;
|
||||
for (i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
|
||||
@@ -124,7 +124,7 @@ static void on_enter_pressed() {
|
||||
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) {
|
||||
WRITE_S("terminal: program not found\n");
|
||||
PRINT_S("terminal: program not found\n");
|
||||
}
|
||||
memory_free(path);
|
||||
}
|
||||
@@ -133,13 +133,13 @@ static void on_enter_pressed() {
|
||||
}
|
||||
|
||||
static void on_cancel_pressed() {
|
||||
WRITE_S("^C\n");
|
||||
PRINT_S("^C\n");
|
||||
prompt_length = prompt_offset = 0;
|
||||
print_prompt();
|
||||
}
|
||||
|
||||
static void on_disconnect_pressed() {
|
||||
WRITE_S("^D\n");
|
||||
PRINT_S("^D\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
@@ -269,7 +269,7 @@ uint64_t main() {
|
||||
memory_set('\b', PROMPT_LENGTH, (char *)bs);
|
||||
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
||||
|
||||
WRITE_D("Welcome to FreywarOS v" VERSION "!\n\n");
|
||||
PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n");
|
||||
|
||||
print_prompt();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user