[flashrom] [PATCH 08/10] print.c: Fix multiline support.
Stefan Tauner
stefan.tauner at student.tuwien.ac.at
Wed Jul 10 21:17:53 CEST 2013
- Use the reentrant tokenizer version strtok_r to break up vendor and model
names in print.c
- Add implementation of strtok_r for mingw (posix only)
- Free allocated temporary memory again.
Signed-off-by: Stefan Tauner <stefan.tauner at student.tuwien.ac.at>
---
print.c | 49 ++++++++++++++++++++++++++++++++++++-------------
1 file changed, 36 insertions(+), 13 deletions(-)
diff --git a/print.c b/print.c
index 6ca2a57..b163cbf 100644
--- a/print.c
+++ b/print.c
@@ -25,6 +25,26 @@
#include "flash.h"
#include "programmer.h"
+#ifdef __MINGW32__
+static char* strtok_r(char *str, const char *delim, char **nextp)
+{
+ if (str == NULL)
+ str = *nextp;
+
+ str += strspn(str, delim); /* Skip leading delimiters */
+ if (*str == '\0')
+ return NULL;
+
+ char *ret = str;
+ str += strcspn(str, delim); /* Find end of token */
+ if (*str != '\0')
+ *str++ = '\0';
+
+ *nextp = str;
+ return ret;
+}
+#endif
+
/*
* Return a string corresponding to the bustype parameter.
* Memory is obtained with malloc() and must be freed with free() by the caller.
@@ -69,7 +89,8 @@ static int print_supported_chips(void)
int maxtypelen = strlen("Type") + 1;
const struct flashchip *chip;
char *s;
- char *tmpven, *tmpdev;
+ char *ven, *dev;
+ char *tmpven, *tmpdev, *tmpven_save, *tmpdev_save;
int tmpvenlen, tmpdevlen, curvenlen, curdevlen;
/* calculate maximum column widths and by iterating over all chips */
@@ -179,17 +200,17 @@ static int print_supported_chips(void)
* - after all other values are printed print the surplus tokens
* on fresh lines
*/
- tmpven = malloc(strlen(chip->vendor) + 1);
- if (tmpven == NULL) {
+ ven = malloc(strlen(chip->vendor) + 1);
+ if (ven == NULL) {
msg_gerr("Out of memory!\n");
return 1;
}
- strcpy(tmpven, chip->vendor);
+ strcpy(ven, chip->vendor);
- tmpven = strtok(tmpven, delim);
+ tmpven = strtok_r(ven, delim, &tmpven_save);
msg_ginfo("%s", tmpven);
curvenlen = strlen(tmpven);
- while ((tmpven = strtok(NULL, delim)) != NULL) {
+ while ((tmpven = strtok_r(NULL, delim, &tmpven_save)) != NULL) {
msg_ginfo("%s", delim);
curvenlen++;
tmpvenlen = strlen(tmpven);
@@ -203,17 +224,17 @@ static int print_supported_chips(void)
msg_ginfo(" ");
/* support for multiline device names as above */
- tmpdev = malloc(strlen(chip->name) + 1);
- if (tmpdev == NULL) {
+ dev = malloc(strlen(chip->name) + 1);
+ if (dev == NULL) {
msg_gerr("Out of memory!\n");
return 1;
}
- strcpy(tmpdev, chip->name);
+ strcpy(dev, chip->name);
- tmpdev = strtok(tmpdev, delim);
+ tmpdev = strtok_r(dev, delim, &tmpdev_save);
msg_ginfo("%s", tmpdev);
curdevlen = strlen(tmpdev);
- while ((tmpdev = strtok(NULL, delim)) != NULL) {
+ while ((tmpdev = strtok_r(NULL, delim, &tmpdev_save)) != NULL) {
msg_ginfo("%s", delim);
curdevlen++;
tmpdevlen = strlen(tmpdev);
@@ -287,7 +308,7 @@ static int print_supported_chips(void)
if (tmpven != NULL){
msg_ginfo("%s", tmpven);
curvenlen = strlen(tmpven);
- while ((tmpven = strtok(NULL, delim)) != NULL) {
+ while ((tmpven = strtok_r(NULL, delim, &tmpven_save)) != NULL) {
msg_ginfo("%s", delim);
curvenlen++;
tmpvenlen = strlen(tmpven);
@@ -306,7 +327,7 @@ static int print_supported_chips(void)
if (tmpdev != NULL){
msg_ginfo("%s", tmpdev);
curdevlen = strlen(tmpdev);
- while ((tmpdev = strtok(NULL, delim)) != NULL) {
+ while ((tmpdev = strtok_r(NULL, delim, &tmpdev_save)) != NULL) {
msg_ginfo("%s", delim);
curdevlen++;
tmpdevlen = strlen(tmpdev);
@@ -319,6 +340,8 @@ static int print_supported_chips(void)
}
}
msg_ginfo("\n");
+ free(ven);
+ free(dev);
}
return 0;
--
Kind regards, Stefan Tauner
More information about the flashrom
mailing list