The existing implementation incorrectly attempted to read the entire stdin
instead of fetching at most the given amount of bytes.
While we're at it, also make the size argument optional and let it default
to Luas internal buffer size.
Suggested-by: Bryan Mayland <bmayland+lede@capnbry.net>
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
int len;
int r;
- len = luaL_checknumber(L, 1);
+ len = luaL_optnumber(L, 1, LUAL_BUFFERSIZE);
luaL_buffinit(L, &B);
while(len > 0) {
char *buf;
buf = luaL_prepbuffer(&B);
- r = read(STDIN_FILENO, buf, LUAL_BUFFERSIZE);
+ r = read(STDIN_FILENO, buf,
+ len < LUAL_BUFFERSIZE ? len : LUAL_BUFFERSIZE);
if (r < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
pfd.revents = 0;
luaL_addsize(&B, r);
data_len += r;
+ len -= r;
if (r != LUAL_BUFFERSIZE)
break;
}