From 630ff582fbe52a92a05d99f17f00e6578e5f2f70 Mon Sep 17 00:00:00 2001 From: rgimad Date: Sun, 23 Mar 2025 22:28:52 +0300 Subject: [PATCH] add reading file --- kpack_c.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/kpack_c.c b/kpack_c.c index 71ca5c6..194e889 100644 --- a/kpack_c.c +++ b/kpack_c.c @@ -9,6 +9,11 @@ static const char* str_usage = "Usage: %s [--nologo / -n] [--kernel / -k] [--unpack / -u] []\n"; +static void error_load_infile() { + printf("Error: cannot load input file\n"); + exit(1); +} + int main(int argc, char *argv[]) { bool flag_nologo = false; @@ -75,8 +80,7 @@ int main(int argc, char *argv[]) struct stat64 statbuf; int res_stat = stat64(infile_name, &statbuf); if (res_stat == -1) { - printf("Error: cannot load input file\n"); - return 1; + error_load_infile(); } // printf("statbuf.st_size = %lld\n", statbuf.st_size); @@ -89,9 +93,23 @@ int main(int argc, char *argv[]) FILE *infile_handle = fopen(infile_name, "rb"); if (!infile_handle) { - printf("Error: cannot load input file\n"); + error_load_infile(); + } + + unsigned char *infile_buf = malloc(infile_size); + if (infile_buf == NULL) { + printf("Error: memory allocation failed\n"); return 1; } + size_t bytes_read = fread(infile_buf, 1, infile_size, infile_handle); + if (bytes_read != infile_size) { + error_load_infile(); + } + fclose(infile_handle); + + // printf("file contents = %s\n", infile_buf); + + // TODO ... return 0; } \ No newline at end of file