|
@@ -447,10 +447,39 @@ bool newTask
|
|
|
}
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
+void dumpOut(char* header, char* arr[], uint8_t len, uint8_t max) {
|
|
|
+ uint8_t i=1;
|
|
|
+ uint8_t i2=1;
|
|
|
+ uint8_t hlen=strlen(header);
|
|
|
+ char buf[64];
|
|
|
+
|
|
|
+ sprintf(buf, "%s: %%-%ds ", header, max);
|
|
|
+ printf(buf, arr[0]);
|
|
|
+
|
|
|
+ while (arr[i2]) {
|
|
|
+ if (i == 4) {
|
|
|
+ i=0;
|
|
|
+ putc('\n', stdout);
|
|
|
+ sprintf(buf, "%%-%ds", hlen+2);
|
|
|
+ printf(buf, "");
|
|
|
+ }
|
|
|
+
|
|
|
+ sprintf(buf, "%%-%ds ", max);
|
|
|
+ printf(buf, arr[i2]);
|
|
|
+ i++;
|
|
|
+ i2++;
|
|
|
+ }
|
|
|
+
|
|
|
+ putc('\n', stdout);
|
|
|
+ fflush(stdout);
|
|
|
+}
|
|
|
+
|
|
|
+//Move into SleeperBox, remove heap and destructor from task struct
|
|
|
int _ls(char* args) {
|
|
|
uint8_t len;
|
|
|
- uint8_t i=0; uint8_t i2=0;
|
|
|
- char* res[128]; char buf[64]; char cwd[1024];
|
|
|
+ uint8_t i=0; uint8_t i2=0; uint8_t fi=0; uint8_t di=0; uint8_t oi=0;
|
|
|
+ char* files[64]; char* dirs[64]; char* others[64];
|
|
|
+ char buf[64]; char cwd[1024];
|
|
|
if (!args || !strlen(args)) {
|
|
|
if (!getcwd(cwd, 1024)) {
|
|
|
log_string("ls: couldn't get current working directory: %s", strerror(errno));
|
|
@@ -472,33 +501,71 @@ int _ls(char* args) {
|
|
|
struct dirent *e;
|
|
|
|
|
|
while ((e = readdir(d))) {
|
|
|
- if (i2 > 126) { break; }
|
|
|
+ if (fi > 62 || di > 62 || oi > 62) {
|
|
|
+ log_string("ls: output truncated");
|
|
|
+ break;
|
|
|
+ }
|
|
|
if (e->d_name[0] != '.') {
|
|
|
- res[i2] = e->d_name;
|
|
|
len = strlen(e->d_name);
|
|
|
if (len > max) { max = len; }
|
|
|
- i2++;
|
|
|
+ switch (e->d_type) {
|
|
|
+ case DT_REG:
|
|
|
+ files[fi] = e->d_name;
|
|
|
+ fi++;
|
|
|
+ break;
|
|
|
+ case DT_DIR:
|
|
|
+ dirs[di] = e->d_name;
|
|
|
+ di++;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ others[oi] = e->d_name;
|
|
|
+ oi++;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- res[(i2++)] = NULL;
|
|
|
- i2 = 0;
|
|
|
+ files[(fi++)] = NULL; dirs[(di++)] = NULL; others[(oi++)] = NULL;
|
|
|
|
|
|
- while (res[i2]) {
|
|
|
- if (i == 4) {
|
|
|
- i=0;
|
|
|
- putc('\n', stdout);
|
|
|
- }
|
|
|
+ if (fi > 1) { dumpOut("Files: ", files, fi, max); }
|
|
|
+ if (di > 1) { dumpOut("Directories: ", dirs, di, max); }
|
|
|
+ if (oi > 1) { dumpOut("Other: ", others, oi, max); }
|
|
|
+
|
|
|
+ closedir(d);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
- sprintf(buf, "%%-%ds ", max);
|
|
|
- printf(buf, res[i2]);
|
|
|
- i++;
|
|
|
- i2++;
|
|
|
+int _cat(char* args) {
|
|
|
+ int8_t r;
|
|
|
+ uint8_t buf[4096];
|
|
|
+ uint8_t fd = open(args, O_RDONLY);
|
|
|
+ if (fd < 0) {
|
|
|
+ log_string("cat: couldn't open file '%s': %s", args, strerror(errno));
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
- putc('\n', stdout);
|
|
|
+ while (1) {
|
|
|
+ r = read(fd, buf, 4096);
|
|
|
+ if (r == 0) { break; }
|
|
|
+ if (r == -1) {
|
|
|
+ log_string("cat: couldn't read: %s", strerror(errno));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (write(1, buf, r) != r) {
|
|
|
+ log_string("cat: couldn't write: %s", strerror(errno));
|
|
|
+ }
|
|
|
+ }
|
|
|
fflush(stdout);
|
|
|
-
|
|
|
- closedir(d);
|
|
|
+ if (close(fd) < 0) {
|
|
|
+ log_string("cat: couldn't close FD %d: %s", fd, strerror(errno));
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int _cd(char* args) {
|
|
|
+ if (chdir(args) < 0) {
|
|
|
+ log_string("cd: couldn't change directory to '%s'", args);
|
|
|
+ }
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
@@ -515,16 +582,33 @@ int process_command(char* buf, size_t len) {
|
|
|
}
|
|
|
else { memcpy(cmd, buf, len); }
|
|
|
|
|
|
- if (!strncmp(cmd, "ls", len)) { return _ls(args); }
|
|
|
-
|
|
|
+ if (!strncmp(cmd, "ls", len)) { return _ls(args); }
|
|
|
+ else if (!strncmp(cmd, "cd", len)) { return _cd(args); }
|
|
|
+ else if (!strncmp(cmd, "cat", len)) { return _cat(args); }
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
//Return 0 for error, otherwise length of command
|
|
|
size_t get_command(char* buf, size_t len) {
|
|
|
+ char hostname[HOST_NAME_MAX+1];
|
|
|
+ char cwd[64];
|
|
|
uint8_t i;
|
|
|
uint8_t r;
|
|
|
char* err;
|
|
|
+
|
|
|
+ if (getcwd(cwd, 64) < 0) {
|
|
|
+ log_string("command_loop: couldn't get current working directory: %s", strerror(errno));
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (gethostname(hostname, HOST_NAME_MAX+1) < 0) {
|
|
|
+ log_string("command_loop: couldn't get hostname: %s", strerror(errno));
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ //possibly move setids to after chroot to have own user file
|
|
|
+ printf("%s:%s$ ", hostname, cwd);
|
|
|
+ fflush(stdout);
|
|
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
if ((r = read(0, &(buf[i]), 1) < 1)) {
|
|
@@ -542,6 +626,7 @@ size_t get_command(char* buf, size_t len) {
|
|
|
}
|
|
|
|
|
|
int command_loop() {
|
|
|
+
|
|
|
char buf[32];
|
|
|
size_t len;
|
|
|
|
|
@@ -608,6 +693,7 @@ int main() {
|
|
|
ALLOW_SYSCALL(getdents), ALLOW_SYSCALL(open),
|
|
|
ALLOW_SYSCALL(close), ALLOW_SYSCALL(openat),
|
|
|
ALLOW_SYSCALL(getcwd), ALLOW_SYSCALL(chdir),
|
|
|
+ ALLOW_SYSCALL(uname),
|
|
|
/*ALLOW_SYSCALL(fsync),
|
|
|
#ifdef __amd64__
|
|
|
ALLOW_SYSCALL(pwrite64),
|