diff --git a/ulib/Makefile b/ulib/Makefile index 00af7b4..b91ce0d 100644 --- a/ulib/Makefile +++ b/ulib/Makefile @@ -15,6 +15,7 @@ SRCFILES := $(call GRABSRC, \ util \ ubsan \ umalloc \ + fs \ ) CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \ diff --git a/ulib/fs/path.c b/ulib/fs/path.c new file mode 100644 index 0000000..44251c8 --- /dev/null +++ b/ulib/fs/path.c @@ -0,0 +1,60 @@ +#include +#include + +void path_parse(const char *in, char *mp, char *path) { + if (in == 0 || *in == 0) { + mp[0] = 0; + path[0] = 0; + return; + } + + int i = 0, j = 0, colonfound = 0; + + while (in[i]) { + if (in[i] == ':') { + if (colonfound) { + mp[0] = 0; + path[0] = 0; + return; + } + colonfound = 1; + mp[i] = 0; + i++; + continue; + } + + if (!colonfound) { + mp[i] = in[i]; + } else { + path[j++] = in[i]; + } + + i++; + } + + if (!colonfound) { + mp[i] = 0; + path[0] = 0; + } else { + path[j] = 0; + } +} + +const char *path_basename(const char *path) { + if (!path) { + return NULL; + } + + const char *aftercolon = string_strchr(path, ':'); + if (aftercolon) { + path = aftercolon + 1; + } + const char *lastslash = NULL; + for (const char *p = path; *p; p++) { + if (*p == '/') { + lastslash = p; + } + } + return lastslash ? lastslash + 1 : path; +} + diff --git a/ulib/fs/path.h b/ulib/fs/path.h new file mode 100644 index 0000000..ae3b41e --- /dev/null +++ b/ulib/fs/path.h @@ -0,0 +1,7 @@ +#ifndef ULIB_FS_PATH_H_ +#define ULIB_FS_PATH_H_ + +void path_parse(const char *in, char *mp, char *path); +const char *path_basename(const char *path); + +#endif // ULIB_FS_PATH_H_ diff --git a/ulib/string/string.c b/ulib/string/string.c index 483be7a..075e5db 100644 --- a/ulib/string/string.c +++ b/ulib/string/string.c @@ -172,3 +172,14 @@ char *string_tokenizealloc(char *s, char *delim) { return w; } + +// https://stackoverflow.com/questions/2488563/strcat-implementation +char *string_combine(char *dest, const char *src) { + size_t i, j; + for(i = 0; dest[i] != '\0'; i++); + for(j = 0; src[j] != '\0'; j++) { + dest[i+j] = src[j]; + } + dest[i+j] = '\0'; + return dest; +} diff --git a/ulib/string/string.h b/ulib/string/string.h index 31d3e8f..92186f4 100644 --- a/ulib/string/string.h +++ b/ulib/string/string.h @@ -17,6 +17,7 @@ char *string_strcpy(char *dest, const char *src); char *string_strchr(const char *s, int c); int string_strncmp(const char * s1, const char * s2, size_t n); char *string_tokenizealloc(char *s, char *delim); +char *string_combine(char *dest, const char *src); #endif diff --git a/ulib/ulib.h b/ulib/ulib.h index 7eb6a7e..f759138 100644 --- a/ulib/ulib.h +++ b/ulib/ulib.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include diff --git a/user/fs/main.c b/user/fs/main.c index 689f38a..07f3539 100644 --- a/user/fs/main.c +++ b/user/fs/main.c @@ -2,8 +2,9 @@ #include #include -#define CMDS(X) \ - X(fetch) X(mkf) X(mkd) +#define CMDS(X) \ + X(fetch) X(mkf) X(mkd) \ + X(tree) void main(void) { if (argslen() == 0) { diff --git a/user/fs/tree.c b/user/fs/tree.c new file mode 100644 index 0000000..ff5dbc2 --- /dev/null +++ b/user/fs/tree.c @@ -0,0 +1,59 @@ +#include +#include +#include + +int showtree(char *root, int indent) { + #define INDENT() for (size_t i = 0; i < indent; i++) uprintf(" ") + + IoctlStat statbuf; ZERO(&statbuf); + if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)root, 0) != E_OK) { + uprintf("fs: could not stat %s\n", root); + return -1; + } + + const char *bn = path_basename(root); + if (string_len(bn) > 0) { + INDENT(); + uprintf("%s\n", bn); + } + + + if (statbuf.type == IOCTLSTAT_FILE) { + return 0; + } + if (statbuf.type == IOCTLSTAT_DIR) { + IoctlDirent *dirents = (IoctlDirent *)umalloc(statbuf.size * sizeof(*dirents)); + + for (size_t i = 0; i < statbuf.size; i++) { + ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)root, (uint64_t)&dirents[i], i); + + IoctlDirent *dirent = &dirents[i]; + + if (string_strcmp(dirent->name, ".") == 0 || string_strcmp(dirent->name, "..") == 0) { + continue; + } + + char tmpbuf[1024]; ZERO(tmpbuf); + string_strcpy(tmpbuf, root); + if (root[string_len(root) - 1] != '/') { + string_combine(tmpbuf, "/"); + } + string_combine(tmpbuf, dirent->name); + showtree(tmpbuf, indent+1); + } + + ufree(dirents); + } + return 0; +} + +void fs_tree(void) { + if (argslen() < 2) { + uprintf("fs: Not enough arguments\n"); + return; + } + + char *path = *(args()+1); + + showtree(path, 0); +}