summaryrefslogtreecommitdiff
path: root/src/user/app/yosh/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/app/yosh/main.cpp')
-rw-r--r--src/user/app/yosh/main.cpp94
1 files changed, 42 insertions, 52 deletions
diff --git a/src/user/app/yosh/main.cpp b/src/user/app/yosh/main.cpp
index 37b557d..fc9580c 100644
--- a/src/user/app/yosh/main.cpp
+++ b/src/user/app/yosh/main.cpp
@@ -4,8 +4,9 @@
#include <stdio.h>
#include <readline.h>
#include <IO/IOStream.h>
+#include <IO/Dir.h>
-FILE cwd_f;
+Dir *cwd_f;
String cwd;
void about() {
@@ -43,52 +44,46 @@ void cd(String *args) {
String newcwd = path_cat(cwd, args[1]);
- file_info info;
- int i = libc::stat(newcwd.c_str(), &info);
- if (i == E_NOT_FOUND) {
+ Dir *newdir = new Dir(newcwd.c_str(), 0);
+ if (newdir->error == E_NOT_FOUND) {
stdio << "No such file or directory.\n";
- } else if (i < 0) {
- stdio.printf("Error stating: %i\n", i);
- } else if ((info.type & FT_DIR) == 0) {
+ } else if (newdir->error == E_INVALID_TYPE) {
stdio << "Not a directory.\n";
+ } else if (newdir->error != 0) {
+ stdio.printf("Error stating: %i\n", newdir->error);
} else {
- FILE nf = libc::open(newcwd.c_str(), 0);
- if (nf < 0) {
- stdio.printf("Error opening: %i\n", nf);
- } else {
- cwd = newcwd;
- libc::close(cwd_f);
- cwd_f = nf;
- }
+ cwd_f->close();
+ cwd = newcwd;
+ cwd_f = newdir;
}
}
-void ls_dir(FILE f) {
- file_info info;
- int i = 0;
- char buf[256];
- while (libc::read(f, i, 256, buf) > 0) {
- i++;
+void ls_dir(Dir *d) {
+ if (d->error != 0) {
+ return;
+ }
+ d->pos = 0;
+ for (String name = d->read_ent(); name; name = d->read_ent()) {
+ if (name == "." || name == "..") continue;
- if (libc::strcmp(buf, ".") == 0 || libc::strcmp(buf, "..") == 0) continue;
+ stdio.printf(" %s", name.c_str());
- stdio.printf(" %s", buf);
+ Node child(d->fd, name.c_str(), 0);
- libc::stat_relative(f, buf, &info);
- if (info.type & FT_DIR) stdio << "/";
+ if (child.info.type & FT_DIR) stdio << "/";
stdio << " \t";
- if (info.type & FT_FILE) stdio << "file ";
- if (info.type & FT_DIR) stdio << "dir ";
- if (info.type & FT_SYMLINK) stdio << "symlink ";
- if (info.type & FT_DEV) stdio << "dev ";
- if (info.type & FT_TERMINAL) stdio << "term ";
+ if (child.info.type & FT_FILE) stdio << "file ";
+ if (child.info.type & FT_DIR) stdio << "dir ";
+ if (child.info.type & FT_SYMLINK) stdio << "symlink ";
+ if (child.info.type & FT_DEV) stdio << "dev ";
+ if (child.info.type & FT_TERMINAL) stdio << "term ";
stdio << "\t";
- if (info.type & FT_TERMINAL) {
- stdio.printf("%ix%i", info.size >> 16, info.size & 0xFFFF);
- } else if ((info.type & FT_DEV) == 0) {
- stdio.printf("%i", info.size);
+ if (child.info.type & FT_TERMINAL) {
+ stdio.printf("%ix%i", child.info.size >> 16, child.info.size & 0xFFFF);
+ } else if ((child.info.type & FT_DEV) == 0) {
+ stdio.printf("%i", child.info.size);
}
stdio << "\n";
@@ -103,22 +98,16 @@ void ls(String *args) {
for (i = 1; args[i]; i++) {
stdio.printf("Contents of %s :\n", args[i].c_str());
String d = path_cat(cwd, args[i].c_str());
- file_info info;
- int e = libc::stat(d.c_str(), &info);
- if (e == E_NOT_FOUND) {
+ Dir dir(d.c_str(), 0);
+ if (dir.error == E_NOT_FOUND) {
stdio << " No such file or directory\n";
- } else if (e < 0) {
- stdio.printf(" Error stating: %i\n", e);
- } else if ((info.type & FT_DIR) == 0) {
+ } else if (dir.error == E_INVALID_TYPE) {
stdio.printf(" Not a directory.\n");
+ } else if (dir.error < 0) {
+ stdio.printf(" Error stating: %i\n", dir.error);
} else {
- FILE ff = libc::open(d.c_str(), 0);
- if (ff < 0) {
- stdio.printf(" Error opening: %i\n", ff);
- } else {
- ls_dir(ff);
- libc::close(ff);
- }
+ ls_dir(&dir);
+ dir.close();
}
}
}
@@ -165,7 +154,7 @@ int Main(String *sh_args) {
about();
cwd = "/";
- cwd_f = libc::open(cwd.c_str(), 0);
+ cwd_f = new Dir("/", 0);
String path = path_cat(sh_args[0], "..");
@@ -219,7 +208,7 @@ int Main(String *sh_args) {
cat(c_args);
} else {
FILE vt = term->fd;
- String *first_arg = c_args + 1;
+ String *first_arg = c_args ;
String t;
if (c_args[0] == "on") {
@@ -237,11 +226,12 @@ int Main(String *sh_args) {
}
String c;
- if (libc::strchr(c_args[0].c_str(), '/')) {
- c = path_cat(cwd, c_args[0], false);
+ if (libc::strchr(first_arg->c_str(), '/')) {
+ c = path_cat(cwd, *first_arg, false);
} else {
- c = path_cat(path, c_args[0], false);
+ c = path_cat(path, *first_arg, false);
}
+ first_arg++;
const char *run_args[15] = {0};
for (int i = 0; first_arg[i]; i++) run_args[i] = first_arg[i].c_str();