shell: use re-allocated buffer to store additional characters
This commit is contained in:
parent
0e95284835
commit
e9a9236fc7
1 changed files with 3 additions and 6 deletions
|
@ -11,7 +11,7 @@ struct cmd main_cmds[] = {
|
|||
|
||||
static char *prompt(const char *prefix)
|
||||
{
|
||||
char *alloc, *line, *s;
|
||||
char *alloc, *line;
|
||||
size_t nbytes = 0, nalloc_bytes = 16;
|
||||
char c;
|
||||
|
||||
|
@ -20,8 +20,6 @@ static char *prompt(const char *prefix)
|
|||
if (!(line = malloc(nalloc_bytes)))
|
||||
return NULL;
|
||||
|
||||
s = line;
|
||||
|
||||
while ((c = getchar()) && c != '\n') {
|
||||
putchar(c);
|
||||
|
||||
|
@ -34,12 +32,11 @@ static char *prompt(const char *prefix)
|
|||
line = alloc;
|
||||
}
|
||||
|
||||
*s++ = c;
|
||||
nbytes++;
|
||||
line[nbytes++] = c;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
*s = '\0';
|
||||
line[nbytes] = '\0';
|
||||
|
||||
return line;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue