diff options
Diffstat (limited to 'src/user/knob')
-rw-r--r-- | src/user/knob/block.c | 2 | ||||
-rw-r--r-- | src/user/knob/file.c | 44 | ||||
-rw-r--r-- | src/user/knob/quit.c | 2 | ||||
-rw-r--r-- | src/user/knob/user.c | 17 |
4 files changed, 47 insertions, 18 deletions
diff --git a/src/user/knob/block.c b/src/user/knob/block.c index e890fa1..7524ad3 100644 --- a/src/user/knob/block.c +++ b/src/user/knob/block.c @@ -9,7 +9,7 @@ void blockcpy(void *to, const void *from, uint32_t size) { //unsophisticated, should check by dwords wheere available __attribute__ ((__pure__)) -bool blockequ(void *a, void *b, uint32_t size) { +bool blockequ(const void *a, const void *b, uint32_t size) { for (uint32_t i = 0; i < size; ++i) if (*(uint8_t *)(a++) != *(uint8_t *)(b++)) return false; 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; diff --git a/src/user/knob/quit.c b/src/user/knob/quit.c index 7c20bdd..98881b7 100644 --- a/src/user/knob/quit.c +++ b/src/user/knob/quit.c @@ -1,6 +1,8 @@ #include <pland/syscall.h> +#include <knob/file.h> __attribute__ ((noreturn)) void quit() { + _close_all_files(); _exit_task(); }
\ No newline at end of file diff --git a/src/user/knob/user.c b/src/user/knob/user.c index c84977c..b642f79 100644 --- a/src/user/knob/user.c +++ b/src/user/knob/user.c @@ -134,23 +134,6 @@ void tell_user_sz(const char *sz) { _log_string(sz); } -void tell_user_n(uint32_t n) { - char buf[11]; - char *buf_ptr = buf; - bool zero_yet = false; - for (uint32_t d = 1000000000U; d; d /= 10) { - uint8_t v = (n / d ) % 10; - if (v || zero_yet) { - zero_yet = true; - *buf_ptr++ = v | '0'; - } - } - if (buf_ptr == buf) - *buf_ptr++ = '0'; - *buf_ptr = '\0'; - tell_user_sz(buf); -} - //return value and max_length don't include null terminator uint32_t ask_user_line_sz(char *sz, uint32_t max_length) { char log_buf[2]; |