* @brief Read data from a file descriptor with line handling * * @param[in] fd File descriptor to read from * @param[in] maxbytes Maximum bytes to read (buffer size - 1) * @param[out] buffer Output buffer for the read data (null-terminated) * @param[out] p_readlen Pointer to store actual bytes read (optional) * * @return int Status code: * - READFILE_STAT_NEXTLINE_REACHED when newline fo
| 602 | * position for partial reads. |
| 603 | */ |
| 604 | static int _readfile(int fd, size_t maxbytes, char *buffer, int *p_readlen) |
| 605 | { |
| 606 | int readlen; |
| 607 | int stat; |
| 608 | char *nlp; |
| 609 | |
| 610 | readlen = read(fd, buffer, maxbytes - 1); |
| 611 | if (readlen <= 0) |
| 612 | { |
| 613 | /* eof, failed */ |
| 614 | stat = READFILE_STAT_EOF_REACHED; |
| 615 | buffer[0] = '\0'; |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | if ((nlp = strchr(buffer, '\n')) == NULL) |
| 620 | { |
| 621 | if (readlen == maxbytes - 1) |
| 622 | { |
| 623 | int tailing_wordlen = 0; |
| 624 | char *cp = buffer + readlen - 1; |
| 625 | for (; *cp && *cp != ' ' && *cp != '\t'; cp--, tailing_wordlen++) |
| 626 | ; |
| 627 | if (tailing_wordlen) |
| 628 | { |
| 629 | lseek(fd, -tailing_wordlen, SEEK_CUR); |
| 630 | readlen -= tailing_wordlen; |
| 631 | stat = READFILE_STAT_TRUNCATED; |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | stat = READFILE_STAT_EOF_REACHED; |
| 636 | } |
| 637 | } |
| 638 | else |
| 639 | { |
| 640 | stat = READFILE_STAT_EOF_REACHED; |
| 641 | } |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | stat = READFILE_STAT_NEXTLINE_REACHED; |
| 646 | readlen = nlp - buffer; |
| 647 | } |
| 648 | buffer[readlen] = '\0'; |
| 649 | } |
| 650 | |
| 651 | if (p_readlen) |
| 652 | *p_readlen = readlen; |
| 653 | return stat; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * @brief Find the start of the next word in a string |
no test coverage detected