diff options
author | Benji Dial <benji6283@gmail.com> | 2021-03-01 22:35:26 -0500 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2021-03-01 22:35:26 -0500 |
commit | 1d69a46f5d9823bbf2e6211ca367b409d2d5f7a7 (patch) | |
tree | a49a5498080551270a827a205cde49477d4d89ff /src/user | |
parent | 6f1b50a4cc6c232ee505a543f006abb1c6cd33cf (diff) | |
download | portland-os-1d69a46f5d9823bbf2e6211ca367b409d2d5f7a7.tar.gz |
minimal file writing, shutdown keybinding (Win+Shift+Q)
Diffstat (limited to 'src/user')
-rw-r--r-- | src/user/filetest/filetest.c | 36 | ||||
-rw-r--r-- | src/user/include/knob/file.h | 2 | ||||
-rw-r--r-- | src/user/include/pland/syscall.h | 12 | ||||
-rw-r--r-- | src/user/knob/file.c | 15 |
4 files changed, 64 insertions, 1 deletions
diff --git a/src/user/filetest/filetest.c b/src/user/filetest/filetest.c new file mode 100644 index 0000000..e7792ff --- /dev/null +++ b/src/user/filetest/filetest.c @@ -0,0 +1,36 @@ +#include <libterm/terminal.h> +#include <knob/file.h> + +#define TEST_FILE "user/test.txt" + +void main() { + struct file *f = open_file(TEST_FILE); + if (!f) { + term_add_sz("Failed to open " TEST_FILE ".\n"); + return; + } + + char ch; + if (!read_from_file(f, 1, &ch)) { + term_add_sz(TEST_FILE " is empty.\n"); + close_file(f); + return; + } + + term_addf(TEST_FILE " contained '%c'.\n", ch); + + if (++ch >= 0x7f) + ch = 0x21; + + seek_file_to(f, 0); + + if (!write_to_file(f, 1, &ch)) { + term_add_sz("Failed to write to " TEST_FILE ".\n"); + close_file(f); + return; + } + + term_addf("Wrote '%c' to " TEST_FILE ".\n", ch); + + close_file(f); +}
\ No newline at end of file diff --git a/src/user/include/knob/file.h b/src/user/include/knob/file.h index 8862098..d283288 100644 --- a/src/user/include/knob/file.h +++ b/src/user/include/knob/file.h @@ -12,12 +12,14 @@ struct file *open_file(const char *path); void close_file(struct file *f); uint32_t read_from_file(struct file *f, uint32_t max, void *buf); +uint32_t write_to_file(struct file *f, uint32_t max, void *buf); //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); uint32_t seek_file_to(struct file *f, uint32_t to); int32_t seek_file_by(struct file *f, int32_t by); uint32_t file_size(struct file *f) __attribute__ ((pure)); +void trunc_file(struct file *f); //return value must be manually freed, unless it is a null pointer _dir_info_entry_t *get_directory_info(const char *path, uint32_t *count_out); diff --git a/src/user/include/pland/syscall.h b/src/user/include/pland/syscall.h index 9518d09..3c4f421 100644 --- a/src/user/include/pland/syscall.h +++ b/src/user/include/pland/syscall.h @@ -51,7 +51,9 @@ enum _scn { _SCN_FIND_UNREAD_IPC, _SCN_WAIT_IPC_READ, _SCN_IS_TASK_RUNNING, - _SCN_GET_TIMESTAMP + _SCN_GET_TIMESTAMP, + _SCN_FILE_WRITE, + _SCN_SET_FILE_SIZE }; static inline uint32_t _sc0(enum _scn eax) { @@ -240,4 +242,12 @@ static inline uint32_t _get_timestamp() { return _sc0(_SCN_GET_TIMESTAMP); } +static inline uint32_t _file_write(_file_handle_t handle, uint32_t offset, uint32_t count, const void *from) { + return _sc4(_SCN_FILE_WRITE, handle, offset, count, (uint32_t)from); +} + +static inline void _set_file_size(_file_handle_t handle, uint32_t new_size) { + _sc2(_SCN_SET_FILE_SIZE, handle, new_size); +} + #endif
\ No newline at end of file diff --git a/src/user/knob/file.c b/src/user/knob/file.c index 3083503..283f984 100644 --- a/src/user/knob/file.c +++ b/src/user/knob/file.c @@ -1,4 +1,5 @@ #include <knob/format.h> +#include <knob/block.h> #include <knob/panic.h> #include <knob/heap.h> @@ -83,6 +84,16 @@ uint32_t read_from_file(struct file *f, uint32_t max, void *buf) { return read; } +uint32_t write_to_file(struct file *f, uint32_t max, void *buf) { + if (f->position + max > f->length) + _set_file_size(f->handle, f->length = f->position + max); + + uint32_t written = _file_write(f->handle, f->position, max, buf); + + f->position += written; + return written; +} + //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; @@ -114,6 +125,10 @@ uint32_t file_size(struct file *f) { return f->length; } +void trunc_file(struct file *f) { + _set_file_size(f->handle, f->length = f->position); +} + //return value must be manually freed, unless it is a null pointer _dir_info_entry_t *get_directory_info(const char *path, uint32_t *count_out) { uint8_t dn; |