From 81da833c5b947e3814968f3d4b481a617772af0c Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 18:07:47 +0100 Subject: [PATCH] TFTP Fix error of not writing the index.txt correctly --- project/Core/Src/tftp.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 61ba699..70423df 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -13,7 +13,28 @@ extern struct llfs_data_file* llfs_root; static llfs_file_t virt_file[2] = { {.name = "index.txt", .data = "test", .len = 4}, {.name = "virtImage.raw", .data = "test", .len = 4} - }; + }; + +int str_cat_str(char* dest, int dest_size, const char* src) { + int dest_len = strlen(dest); + int src_len = strlen(src); + if (dest_len + src_len > dest_size) { + return -1; + } + memcpy(dest + dest_len, src, src_len); + return 0; +} + +int str_cat(char* dest, int dest_size, char c) +{ + int dest_len = strlen(dest); + if (dest_len + 1 > dest_size) { + return -1; + } + dest[dest_len] = c; + dest[dest_len + 1] = '\0'; + return 0; +} /** * @brief tftp helper functions @@ -72,10 +93,13 @@ int tftp_read(void* handle, void* buf, int bytes) { if (file == &virt_file[0]) { const struct llfs_data_file* root = llfs_root; - snprintf(buf, bytes, "%s\n", virt_file[0].name); - snprintf(buf, bytes, "%s\n", virt_file[1].name); + str_cat_str(buf, bytes, virt_file[0].name); + str_cat(buf, bytes, '\n'); + str_cat_str(buf, bytes, virt_file[1].name); + str_cat(buf, bytes, '\n'); while(root != NULL) { - snprintf(buf, bytes, "%s\n", root->name); + str_cat(buf, bytes, root->name); + str_cat(buf, bytes, '\n'); file = root->next; } } else if (file == &virt_file[1]) {