diff options
author | Benji Dial <benji6283@gmail.com> | 2020-09-13 22:06:40 -0400 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2020-09-13 22:06:40 -0400 |
commit | 44d29a33df81ac07163d5146a9e43a0c4fb80af0 (patch) | |
tree | 94d4fb506a80d73cb53dc9ea66dfc952d7a9432a /src/user/knob/file.c | |
parent | 143156f63e2448733f1a35a74e629fe0ae9bb567 (diff) | |
download | portland-os-44d29a33df81ac07163d5146a9e43a0c4fb80af0.tar.gz |
new shell
Diffstat (limited to 'src/user/knob/file.c')
-rw-r--r-- | src/user/knob/file.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/user/knob/file.c b/src/user/knob/file.c index 0032cf2..23b7564 100644 --- a/src/user/knob/file.c +++ b/src/user/knob/file.c @@ -3,7 +3,21 @@ #include <knob/heap.h> #include <knob/env.h> +struct ofl_node { + struct ofl_node *next; + struct ofl_node *prev; + _file_handle_t handle; +}; + +static struct ofl_node *head_ofl_node = 0; + +void _close_all_files() { + for (struct ofl_node *i = head_ofl_node; i; i = i->next) + _close_file(i->handle); +} + struct file { + struct ofl_node *node; _file_handle_t handle; uint32_t position; uint32_t length; @@ -38,7 +52,16 @@ struct file *open_file(const char *path) { if (!h) return 0; + struct ofl_node *new_node = get_block(sizeof(struct ofl_node)); + new_node->next = head_ofl_node; + new_node->prev = 0; + new_node->handle = h; + if (head_ofl_node) + head_ofl_node->prev = new_node; + head_ofl_node = new_node; + struct file *f = get_block(sizeof(struct file)); + f->node = new_node; f->handle = h; f->position = 0; f->length = _file_size(h); @@ -48,6 +71,14 @@ struct file *open_file(const char *path) { void close_file(struct file *f) { _close_file(f->handle); + struct ofl_node *n = f->node; + if (n->next) + n->next->prev = n->prev; + if (n->prev) + n->prev->next = n->next; + if (n == head_ofl_node) + head_ofl_node = n->next; + free_block(n); free_block(f); } @@ -61,6 +92,19 @@ uint32_t read_from_file(struct file *f, uint32_t max, void *buf) { return read; } +//return value and max_length don't include null terminator +uint32_t read_line_from_file(struct file *f, char *sz, uint32_t max_length) { + uint8_t i; + for (i = 0; i < max_length; ++i) { + char byte; + if (!read_from_file(f, 1, &byte) || (byte == '\n')) + break; + sz[i] = byte; + } + sz[i] = '\0'; + return i; +} + uint32_t seek_file_to(struct file *f, uint32_t to) { if (to > f->length) to = f->length; |