** This routine reads a line of text from FILE in, stores ** the text in memory obtained from malloc() and returns a pointer ** to the text. NULL is returned at end of file, or if malloc() ** fails. ** ** If zLine is not NULL then it is a malloced buffer returned from ** a previous call to this routine that may be reused. */
| 522 | ** a previous call to this routine that may be reused. |
| 523 | */ |
| 524 | static char *local_getline(char *zLine, FILE *in) { |
| 525 | idx_t nLine = zLine == 0 ? 0 : 100; |
| 526 | idx_t n = 0; |
| 527 | |
| 528 | while (1) { |
| 529 | if (n + 100 > nLine) { |
| 530 | nLine = nLine * 2 + 100; |
| 531 | zLine = (char *)realloc(zLine, nLine); |
| 532 | if (!zLine) { |
| 533 | shell_out_of_memory(); |
| 534 | } |
| 535 | } |
| 536 | if (fgets(&zLine[n], nLine - n, in) == 0) { |
| 537 | if (n == 0) { |
| 538 | free(zLine); |
| 539 | return 0; |
| 540 | } |
| 541 | zLine[n] = 0; |
| 542 | break; |
| 543 | } |
| 544 | while (zLine[n]) |
| 545 | n++; |
| 546 | if (n > 0 && zLine[n - 1] == '\n') { |
| 547 | n--; |
| 548 | if (n > 0 && zLine[n - 1] == '\r') |
| 549 | n--; |
| 550 | zLine[n] = 0; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | return zLine; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | ** Retrieve a single line of input text. |
no test coverage detected