Merge pull request #2981 from jtojanen/bugfix_fgetl

Fix fgetl() logic
pull/2999/head
Jens Steube 3 years ago committed by GitHub
commit 660e451689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -982,54 +982,38 @@ void hc_fclose (HCFILE *fp)
size_t fgetl (HCFILE *fp, char *line_buf, const size_t line_sz) size_t fgetl (HCFILE *fp, char *line_buf, const size_t line_sz)
{ {
size_t line_truncated = 0; int c;
size_t line_len = 0; size_t line_len = 0;
while (!hc_feof (fp)) size_t line_truncated = 0;
{
const int c = hc_fgetc (fp);
if (c == EOF) break; while ((c = hc_fgetc (fp)) != EOF)
{
if (c == '\n') break;
if (line_len == line_sz) if (line_len == line_sz)
{ {
line_truncated++; line_truncated++;
continue;
} }
else
{
line_buf[line_len] = (char) c;
line_buf[line_len] = (char) c; line_len++;
}
line_len++;
if (c == '\n') break;
} }
if (line_truncated > 0) if (line_truncated > 0)
{ {
fprintf (stderr, "\nOversized line detected! Truncated %" PRIu64 " bytes\n", (u64) line_truncated); fprintf (stderr, "\nOversized line detected! Truncated %" PRIu64 " bytes\n", (u64) line_truncated);
} }
else
if (line_len == 0) return 0;
while (line_len)
{ {
if (line_buf[line_len - 1] == '\n') while (line_len > 0 && line_buf[line_len - 1] == '\r')
{ {
line_len--; line_len--;
continue;
} }
if (line_buf[line_len - 1] == '\r')
{
line_len--;
continue;
}
break;
} }
line_buf[line_len] = 0; line_buf[line_len] = 0;

Loading…
Cancel
Save