@@ -499,51 +499,17 @@ VAL idris_strlen(VM* vm, VAL l) {
499499 return MKINT((i_int)(idris_utf8_strlen(GETSTR(l))));
500500}
501501
502−#define BUFSIZE 256
503−
504502VAL idris_readStr(VM* vm, FILE* h) {
505−// Modified from 'safe-fgets.c' in the gdb distribution.
506−// (see http://www.gnu.org/software/gdb/current/)
507− char *line_ptr;
508− char* line_buf = (char *) malloc (BUFSIZE);
509− int line_buf_size = BUFSIZE;
510−
511− /* points to last byte */
512− line_ptr = line_buf + line_buf_size - 1;
513−
514− /* so we can see if fgets put a 0 there */
515− *line_ptr = 1;
516− if (fgets (line_buf, line_buf_size, h) == 0)
517− return MKSTR(vm, "");
518−
519− /* we filled the buffer? */
520− while (line_ptr[0] == 0 && line_ptr[-1] != '\n')
521− {
522− /* Make the buffer bigger and read more of the line */
523− line_buf_size += BUFSIZE;
524− { /* limit the scope of new_line_buf to where it's needed for errors*/
525− char *new_line_buf = realloc (line_buf, line_buf_size);
526− if (new_line_buf == NULL)
527− {
528− fprintf(stderr, "Fatal Error: couldn't allocate a larger line buffer.");
529− exit(EXIT_FAILURE);
530− } else {
531− line_buf = new_line_buf;
532− }
533− }
534− /* points to last byte again */
535− line_ptr = line_buf + line_buf_size - 1;
536− /* so we can see if fgets put a 0 there */
537− *line_ptr = 1;
503+ // FIXME (quickly...): This will fail if there is a line longer than
504+ // bufsize characters!
505+ int bufsize = 255;
538506
539− if (fgets (line_buf + line_buf_size - BUFSIZE - 1, BUFSIZE + 1, h) == 0)
540− return MKSTR(vm, "");
507+ char* buffer = malloc(bufsize+1);
508+ if (fgets(buffer, bufsize, h) == 0) {
509+ return MKSTR(vm, "");
510+ } else {
511+ return MKSTR(vm, buffer);
541512 }
542−
543− VAL str = MKSTR(vm, line_buf);
544−// printf("DBG: %s\n", line_buf);
545− free(line_buf);
546− return str;
547513}
548514
549515VAL idris_strHead(VM* vm, VAL str) {