From 7535bb73339d1239a5fb28436dd9e32ec60b4b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Fr=C3=B6schle?= Date: Fri, 1 Mar 2013 19:18:23 +0000 Subject: [PATCH] fixed several off-by-one errors in string handling functions loading and verifying .s19 files from basflash.s19 works! --- BaS_gcc/include/bas_string.h | 3 ++- BaS_gcc/sources/bas_string.c | 21 +++++++++++++++------ BaS_gcc/sources/basflash.c | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/BaS_gcc/include/bas_string.h b/BaS_gcc/include/bas_string.h index 303c4c4..7f99cad 100644 --- a/BaS_gcc/include/bas_string.h +++ b/BaS_gcc/include/bas_string.h @@ -13,7 +13,8 @@ extern int strncmp(const char *s1, const char *s2, int max); extern char *strcpy(char *dst, const char *src); extern size_t strlen(const char *str); -extern int strncat(char *dst, char *src, int max); +extern char *strcat(char *dst, const char *src); +extern char *strncat(char *dst, const char *src, int max); extern int atoi(const char *c); #define isdigit(c) (((c) >= '0') && ((c) <= '9')) diff --git a/BaS_gcc/sources/bas_string.c b/BaS_gcc/sources/bas_string.c index 0b01add..f8ef732 100644 --- a/BaS_gcc/sources/bas_string.c +++ b/BaS_gcc/sources/bas_string.c @@ -12,9 +12,9 @@ int strncmp(const char *s1, const char *s2, int max) int i; int cmp; - for (i = 0; i < max; i++); + for (i = 0; i < max && *s1++ && *s2++; i++); { - cmp = (*s1++ - *s2++); + cmp = (*s1 - *s2); if (cmp != 0) return cmp; } return cmp; @@ -46,20 +46,29 @@ size_t strlen(const char *s) while (*s++); - return s - start; + return s - start - 1; } -int strncat(char *dst, char *src, int max) +char *strcat(char *dst, const char *src) +{ + char *ret = dst; + dst = &dst[strlen(dst)]; + while ((*dst++ = *src++) != '\0'); + return ret; +} + +char *strncat(char *dst, const char *src, int max) { int i; + char *ret = dst; - dst = &dst[strlen(dst) + 1]; + dst = &dst[strlen(dst)]; for (i = 0; i < max && *src; i++) { *dst++ = *src++; } *dst++ = '\0'; - return i; + return ret; } diff --git a/BaS_gcc/sources/basflash.c b/BaS_gcc/sources/basflash.c index 21aaa0f..98b389b 100644 --- a/BaS_gcc/sources/basflash.c +++ b/BaS_gcc/sources/basflash.c @@ -353,19 +353,23 @@ void basflash(void) * Files located in the BASTEST-folder thus override those in flash. Useful for testing before flashing */ res = disk_status(0); + xprintf("disk_status(0) = %d\r\n", res); if (res == RES_OK) { fres = f_mount(0, &fs); + xprintf("f_mount() = %d\r\n", fres); if (fres == FR_OK) { DIR directory; fres = f_opendir(&directory, bastest_str); + xprintf("f_opendir() = %d\r\n", fres); if (fres == FR_OK) { FILINFO fileinfo; fres = f_readdir(&directory, &fileinfo); + xprintf("f_readdir() = %d\r\n", fres); while (fres == FR_OK) { const char *srec_ext = ".S19"; @@ -373,25 +377,31 @@ void basflash(void) if (fileinfo.fname[0] != '\0') /* found a file */ { - if (strncmp(&fileinfo.fname[13 - 4], srec_ext, 4) == 0) /* we have a .S19 file */ + xprintf("check file %s (%s == %s ?)\r\n", fileinfo.fname, &fileinfo.fname[strlen(fileinfo.fname) - 4], srec_ext); + if (strlen(fileinfo.fname) >= 4 && strncmp(&fileinfo.fname[strlen(fileinfo.fname) - 4], srec_ext, 4) == 0) /* we have a .S19 file */ { /* * build path + filename */ strcpy(path, bastest_str); + strcat(path, "\\"); strncat(path, fileinfo.fname, 13); - xprintf("loading file %s\n", path); + xprintf("loading file %s\r\n", path); /* * load file */ if (srec_load(path) != OK) { + xprintf("failed to load file %s\r\n", path); // error handling } } } + else + break; fres = f_readdir(&directory, &fileinfo); + xprintf("f_readdir() = %d\r\n", fres); } } else