Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Leont committed May 2, 2021
1 parent cd2ea7b commit 0a23d16
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions sv.c
Expand Up @@ -8432,6 +8432,8 @@ in the SV (typically, C<SvCUR(sv)> is a suitable choice).
=cut
*/

#define DEFAULT_BUFFER_SIZE 8192

char *
Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
{
Expand All @@ -8444,8 +8446,10 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
#ifdef USE_HEAP_INSTEAD_OF_STACK /* Slower way. */
STDCHAR *buf = NULL;
#else
STDCHAR buf[8192];
STDCHAR buf[DEFAULT_BUFFER_SIZE];
#endif
char *bufptr = NULL;
size_t bufsize = 0;


PERL_ARGS_ASSERT_SV_GETS;
Expand Down Expand Up @@ -8499,10 +8503,13 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
#ifdef PERL_COPY_ON_WRITE
/* Add an extra byte for the sake of copy-on-write's
* buffer reference count. */
(void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
STRLEN length = (st.st_size - offset) + append + 2;
#else
(void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
STRLEN length = (st.st_size - offset) + append + 1;
#endif
(void) SvGROW(sv, length);
bufptr = SvPVX(sv);
bufsize = length;
}
}
rsptr = NULL;
Expand Down Expand Up @@ -8553,30 +8560,34 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
}

#ifdef USE_HEAP_INSTEAD_OF_STACK /* Slower way. */
Newx(buf, 8192, STDCHAR);
Newx(buf, DEFAULT_BUFFER_SIZE, STDCHAR);
assert(buf);
#endif
if (bufptr == NULL) {
bufptr = (char*)buf;
bufsize = DEFAULT_BUFFER_SIZE;
}

screamer:
if (rslen)
cnt = PerlIO_readdelim(fp, buf, sizeof(buf), rslast);
cnt = PerlIO_readdelim(fp, bufptr, bufsize, rslast);
else
cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
cnt = PerlIO_read(fp, bufptr, bufsize);

/* Accommodate broken VAXC compiler, which applies U8 cast to
* both args of ?: operator, causing EOF to change into 255
*/
if (cnt > 0)
i = (U8)buf[cnt - 1];
i = (U8)bufptr[cnt - 1];
else
i = EOF;

if (cnt < 0)
cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
if (append)
sv_catpvn_nomg(sv, (char *) buf, cnt);
sv_catpvn_nomg(sv, (char *) bufptr, cnt);
else
sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */
sv_setpvn(sv, (char *) bufptr, cnt); /* "nomg" is implied */

if (cnt > 0 && /* joy */
(!rslen ||
Expand All @@ -8595,7 +8606,7 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
*
* - jik 9/25/96
*/
if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
if (!(cnt < (I32)bufsize && PerlIO_eof(fp)))
goto screamer;
}

Expand Down

0 comments on commit 0a23d16

Please sign in to comment.