Skip to content

Commit 17b3ef4

Browse files
committed
- Fix builtin gets() emulation (hopefully).
1 parent 137a290 commit 17b3ef4

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

main/streams.c

+3-11
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,13 @@ PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRML
250250
return NULL;
251251
} else {
252252
/* unbuffered fgets - poor performance ! */
253-
size_t n = 1;
254253
char *c = buf;
255254

256255
/* TODO: look at error returns? */
257256

258-
while(n < maxlen && stream->ops->read(stream, c, 1 TSRMLS_CC) > 0) {
259-
n++;
260-
if (*c == '\n') {
261-
c++;
262-
break;
263-
}
264-
c++;
265-
}
266-
*c = 0;
267-
return buf;
257+
while (--maxlen > 0 && stream->ops->read(stream, buf, 1 TSRMLS_CC) == 1 && *buf++ != '\n');
258+
*buf = '\0';
259+
return c == buf && maxlen > 0 ? NULL : c;
268260
}
269261
}
270262

0 commit comments

Comments
 (0)