@@ -188,7 +188,42 @@ VAL idris_strlen(VM* vm, VAL l) {
188 return MKINT((i_int)(strlen(GETSTR(l))));
188 return MKINT((i_int)(strlen(GETSTR(l))));
191VAL idris_readStr(VM* vm, FILE* h) {
193VAL idris_readStr(VM* vm, FILE* h) {
194+// Modified from 'safe-fgets.c' in the gdb distribution.
195+// (see http://www.gnu.org/software/gdb/current/)
197+ char* line_buf = (char *) malloc (BUFSIZE);
198+ int line_buf_size = BUFSIZE;
200+ /* points to last byte */
201+ line_ptr = line_buf + line_buf_size - 1;
203+ /* so we can see if fgets put a 0 there */
205+ if (fgets (line_buf, line_buf_size, h) == 0)
206+ return MKSTR(vm, "");
208+ /* we filled the buffer? */
209+ while (line_ptr[0] == 0 && line_ptr[-1] != '\n')
211+ /* Make the buffer bigger and read more of the line */
212+ line_buf_size += BUFSIZE;
213+ line_buf = (char *) realloc (line_buf, line_buf_size);
215+ /* points to last byte again */
216+ line_ptr = line_buf + line_buf_size - 1;
217+ /* so we can see if fgets put a 0 there */
220+ if (fgets (line_buf + line_buf_size - BUFSIZE - 1, BUFSIZE + 1, h) == 0)
221+ return MKSTR(vm, "");
224+ VAL str = MKSTR(vm, line_buf);