fixed several off-by-one errors in string handling functions

loading and verifying .s19 files from basflash.s19 works!
This commit is contained in:
Markus Fröschle
2013-03-01 19:18:23 +00:00
parent a92c134454
commit fdbcfaf94e
3 changed files with 29 additions and 9 deletions

View File

@@ -13,7 +13,8 @@
extern int strncmp(const char *s1, const char *s2, int max); extern int strncmp(const char *s1, const char *s2, int max);
extern char *strcpy(char *dst, const char *src); extern char *strcpy(char *dst, const char *src);
extern size_t strlen(const char *str); 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); extern int atoi(const char *c);
#define isdigit(c) (((c) >= '0') && ((c) <= '9')) #define isdigit(c) (((c) >= '0') && ((c) <= '9'))

View File

@@ -12,9 +12,9 @@ int strncmp(const char *s1, const char *s2, int max)
int i; int i;
int cmp; 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; if (cmp != 0) return cmp;
} }
return cmp; return cmp;
@@ -46,20 +46,29 @@ size_t strlen(const char *s)
while (*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; int i;
char *ret = dst;
dst = &dst[strlen(dst) + 1]; dst = &dst[strlen(dst)];
for (i = 0; i < max && *src; i++) for (i = 0; i < max && *src; i++)
{ {
*dst++ = *src++; *dst++ = *src++;
} }
*dst++ = '\0'; *dst++ = '\0';
return i; return ret;
} }

View File

@@ -353,19 +353,23 @@ void basflash(void)
* Files located in the BASTEST-folder thus override those in flash. Useful for testing before flashing * Files located in the BASTEST-folder thus override those in flash. Useful for testing before flashing
*/ */
res = disk_status(0); res = disk_status(0);
xprintf("disk_status(0) = %d\r\n", res);
if (res == RES_OK) if (res == RES_OK)
{ {
fres = f_mount(0, &fs); fres = f_mount(0, &fs);
xprintf("f_mount() = %d\r\n", fres);
if (fres == FR_OK) if (fres == FR_OK)
{ {
DIR directory; DIR directory;
fres = f_opendir(&directory, bastest_str); fres = f_opendir(&directory, bastest_str);
xprintf("f_opendir() = %d\r\n", fres);
if (fres == FR_OK) if (fres == FR_OK)
{ {
FILINFO fileinfo; FILINFO fileinfo;
fres = f_readdir(&directory, &fileinfo); fres = f_readdir(&directory, &fileinfo);
xprintf("f_readdir() = %d\r\n", fres);
while (fres == FR_OK) while (fres == FR_OK)
{ {
const char *srec_ext = ".S19"; const char *srec_ext = ".S19";
@@ -373,25 +377,31 @@ void basflash(void)
if (fileinfo.fname[0] != '\0') /* found a file */ 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 * build path + filename
*/ */
strcpy(path, bastest_str); strcpy(path, bastest_str);
strcat(path, "\\");
strncat(path, fileinfo.fname, 13); strncat(path, fileinfo.fname, 13);
xprintf("loading file %s\n", path); xprintf("loading file %s\r\n", path);
/* /*
* load file * load file
*/ */
if (srec_load(path) != OK) if (srec_load(path) != OK)
{ {
xprintf("failed to load file %s\r\n", path);
// error handling // error handling
} }
} }
} }
else
break;
fres = f_readdir(&directory, &fileinfo); fres = f_readdir(&directory, &fileinfo);
xprintf("f_readdir() = %d\r\n", fres);
} }
} }
else else