improvements in the memory deallocation in updatestring()

updatestring() now returns an integer to handle canceling
This commit is contained in:
Frederic Culot 2006-12-14 08:28:21 +00:00
parent ae24e6ccc3
commit 6d1838df86

View File

@ -1,4 +1,4 @@
/* $calcurse: utils.c,v 1.17 2006/12/13 09:34:09 culot Exp $ */ /* $calcurse: utils.c,v 1.18 2006/12/14 08:28:21 culot Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -268,13 +268,14 @@ int getstring(WINDOW *win, int colr, char *str, int l, int x, int y)
} }
/* Update an already existing string. */ /* Update an already existing string. */
void updatestring(WINDOW *win, int colr, char **str, int x, int y) { int updatestring(WINDOW *win, int colr, char **str, int x, int y) {
char *newstr; char *newstr;
int len; int escape, len = strlen(*str) + 1;
newstr = (char *) malloc(MAX_LENGTH); newstr = (char *) malloc(MAX_LENGTH);
(void)memcpy(newstr, *str, strlen(*str) + 1); (void)memcpy(newstr, *str, len);
if (getstring(win, colr, newstr, MAX_LENGTH, x, y) == 0) { escape = getstring(win, colr, newstr, MAX_LENGTH, x, y);
if (!escape) {
len = strlen(newstr) + 1; len = strlen(newstr) + 1;
if ((*str = (char *) realloc(*str, len)) == NULL) { if ((*str = (char *) realloc(*str, len)) == NULL) {
/* NOTREACHED */ /* NOTREACHED */
@ -284,6 +285,8 @@ void updatestring(WINDOW *win, int colr, char **str, int x, int y) {
} else } else
(void)memcpy(*str, newstr, len); (void)memcpy(*str, newstr, len);
} }
free(newstr);
return escape;
} }
/* checks if a string is only made of digits */ /* checks if a string is only made of digits */