9
0
Fork 0

- fix memory hole

- fix reading of empty lines
- put the command prompt at the beginning of the line when exiting edit
This commit is contained in:
Sascha Hauer 2007-07-13 20:42:18 +02:00
parent ef54cffe97
commit 6f779cc1c5
1 changed files with 33 additions and 5 deletions

View File

@ -185,7 +185,7 @@ static int edit_read_file(const char *path)
struct line *line;
struct line *lastline = NULL;
char *filebuffer;
char *linestr;
char *linestr, *lineend;
struct stat s;
if (!stat(path, &s)) {
@ -195,8 +195,17 @@ static int edit_read_file(const char *path)
return -1;
}
linestr = strtok(filebuffer, "\r\n");
while (linestr) {
linestr = filebuffer;
while (1) {
if (!*linestr)
break;
lineend = strchr(linestr, '\n');
if (!lineend && !*linestr)
break;
*lineend = 0;
line = line_realloc(strlen(linestr) + 1, NULL);
if (!buffer)
buffer = line;
@ -206,8 +215,13 @@ static int edit_read_file(const char *path)
lastline->next = line;
line->next = 0;
lastline = line;
linestr = strtok(NULL, "\r\n");
if (!lineend)
break;
linestr = lineend + 1;
}
free(filebuffer);
}
if (!buffer) {
@ -218,6 +232,19 @@ static int edit_read_file(const char *path)
return 0;
}
static void free_buffer(void)
{
struct line *line, *tmp;
line = buffer;
while(line) {
tmp = line->next;
line_free(line);
line = tmp;
}
}
static int save_file(const char *path)
{
struct line *line, *tmp;
@ -235,7 +262,6 @@ static int save_file(const char *path)
tmp = line->next;
write(fd, line->data, strlen(line->data));
write(fd, "\n", 1);
line_free(line);
line = tmp;
}
close(fd);
@ -489,7 +515,9 @@ int do_edit(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
}
}
out:
free_buffer();
printf("%c[2J", 27);
printf("\n");
return 0;
}