Use fgets for read-line

This commit is contained in:
Justin Ethier 2016-07-26 21:36:33 -04:00
parent fa24f4db37
commit 9c04662938
2 changed files with 32 additions and 12 deletions

21
examples/cat.scm Normal file
View file

@ -0,0 +1,21 @@
(import (scheme base)
(scheme file)
(scheme write))
(define (cat fname)
(call-with-input-file
fname
(lambda (fp)
(define (loop)
(let ((s (read-line fp)))
(cond
((not (eof-object? s))
(display s)
;(newline)
(loop)))))
(loop))))
(cat
"cyclone.scm"
;"scheme/base.sld"
)

View file

@ -2415,22 +2415,21 @@ object Cyc_io_read_line(void *data, object cont, object port)
{
FILE *stream = ((port_type *) port)->fp;
char buf[1024];
int i = 0, c;
//int i = 0, c;
set_thread_blocked(data, cont);
while (1) {
c = fgetc(stream);
if (c == EOF && i == 0) {
errno = 0;
if (fgets(buf, 1023, stream) != NULL) {
make_string(s, buf);
return_thread_runnable(data, &s);
} else {
if (feof(stream)) {
return_thread_runnable(data, Cyc_EOF);
} else {
// TODO: can't do this because we said thread could be blocked
//Cyc_rt_raise2(data, "Error reading from file: ", obj_int2obj(errno));
return_thread_runnable(data, Cyc_EOF);
} else if (c == EOF || i == 1023 || c == '\n') {
buf[i] = '\0';
{
make_string(s, buf);
return_thread_runnable(data, &s);
}
}
buf[i++] = c;
}
return NULL;
}