Add basic ls
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "src/lib/string.h"
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
int main(uint64_t argc, const char **argv) {
|
||||
uint64_t main(uint64_t argc, const char **argv) {
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (i > 1) {
|
||||
write(1, " ", 1);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#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));
|
||||
|
||||
uint64_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;
|
||||
}
|
||||
if (s == (uint64_t)-1) {
|
||||
WRITE_S("ls: path does not exist\n");
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
WRITE_D(out);
|
||||
WRITE_S("\n");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t main(uint64_t argc, const char **argv) {
|
||||
if (argc == 1) {
|
||||
return ls(".");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
if (ls(argv[i]) == (uint64_t)-1) {
|
||||
code = (uint64_t)-1;
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
static void help(uint8_t argc, char **argv);
|
||||
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));
|
||||
|
||||
@@ -117,9 +119,14 @@ static void on_enter_pressed() {
|
||||
}
|
||||
}
|
||||
if (i == sizeof(builtins) / sizeof(app_entry_t)) {
|
||||
if (spawn(argv[0], argc, (const char **)argv) == (uint64_t)-1){
|
||||
uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1;
|
||||
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) {
|
||||
WRITE_S("terminal: program not found\n");
|
||||
}
|
||||
memory_free(path);
|
||||
}
|
||||
prompt_length = prompt_offset = 0;
|
||||
print_prompt();
|
||||
@@ -258,7 +265,7 @@ static void on_char_received(char c) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
uint64_t main() {
|
||||
memory_set('\b', PROMPT_LENGTH, (char *)bs);
|
||||
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user