From: lwang Date: Tue, 17 May 2005 08:43:37 +0000 (+0000) Subject: fix up for readline (the old one can not handle long input) X-Git-Tag: v1_7_100~1^25~8^2~118 X-Git-Url: https://git.whamcloud.com/gitweb?a=commitdiff_plain;h=6168c82141071699c87c84e14b73dd9c695c0993;p=fs%2Flustre-release.git fix up for readline (the old one can not handle long input) --- diff --git a/lustre/utils/parser.c b/lustre/utils/parser.c index 26f66d8..9c23e77 100644 --- a/lustre/utils/parser.c +++ b/lustre/utils/parser.c @@ -341,16 +341,46 @@ int init_input() #define add_history(s) char * readline(char * prompt) { - char line[2048]; - int n = 0; + int size = 2048; + char *line = malloc(size); + char *ptr = line; + int c; + + if (line == NULL) + return NULL; if (prompt) printf ("%s", prompt); - if (fgets(line, sizeof(line), stdin) == NULL) - return (NULL); - n = strlen(line); - if (n && line[n-1] == '\n') - line[n-1] = '\0'; - return strdup(line); + + while (1) { + if ((c = fgetc(stdin)) != EOF) { + if (c == '\n') + goto out; + *ptr++ = c; + + if (ptr - line >= size - 1) { + char *tmp; + + size *= 2; + tmp = malloc(size); + if (tmp == NULL) + goto outfree; + memcpy(tmp, line, ptr - line); + ptr = tmp + (ptr - line); + free(line); + line = tmp; + } + } else { + if (ferror(stdin)) + goto outfree; + goto out; + } + } +out: + *ptr = 0; + return line; +outfree: + free(line); + return NULL; } #endif