From 4fcff0ecc6f94dbc079f29b9636d810a16ef2ed7 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Fri, 5 Aug 2022 19:54:22 +0200 Subject: user/libc: fseek + ftell for getting a file's size --- src/user/app/vterm/font.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/user/app/vterm') diff --git a/src/user/app/vterm/font.c b/src/user/app/vterm/font.c index d9a4e3c..5a9dd24 100644 --- a/src/user/app/vterm/font.c +++ b/src/user/app/vterm/font.c @@ -13,13 +13,24 @@ void font_load(const char *path) { exit(1); } - const size_t cap = 8 * 1024; // TODO get file size - void *buf = malloc(cap); + void *buf; + long buflen; + + fseek(f, 0, SEEK_END); + buflen = ftell(f); + if (buflen < 0) { + eprintf("can't get the size of \"%s\"", path); + exit(1); + } + + buf = malloc(buflen); if (!buf) { eprintf("out of memory"); exit(1); } - fread(buf, 1, cap, f); + + fseek(f, 0, SEEK_SET); + fread(buf, 1, buflen, f); if (ferror(f)) { eprintf("error reading file"); exit(1); -- cgit v1.2.3