diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2007-07-13 20:42:18 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2007-07-13 20:42:18 +0200 |
commit | 6f779cc1c5438939a5cbb2dfd197b8f7abb1f2a4 (patch) | |
tree | c5adf7d2b42d2ee552f064398e60004b2069871e | |
parent | ef54cffe97fcb4ce68b07d717ee3a98c3451ba3a (diff) | |
download | barebox-6f779cc1c5438939a5cbb2dfd197b8f7abb1f2a4.tar.gz barebox-6f779cc1c5438939a5cbb2dfd197b8f7abb1f2a4.tar.xz |
- fix memory hole
- fix reading of empty lines
- put the command prompt at the beginning of the line when exiting edit
-rw-r--r-- | commands/edit.c | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/commands/edit.c b/commands/edit.c index fae2bd36da..efc74c752f 100644 --- a/commands/edit.c +++ b/commands/edit.c @@ -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; } |