shell: use re-allocated buffer to store additional characters

tags/0.1.0
S.J.R. van Schaik 9 years ago
parent 0e95284835
commit e9a9236fc7
  1. 9
      source/shell/cmd.c

@ -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…
Cancel
Save