From eac30107d3cdf14b5443d6ea43d436cd1c32f814 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 31 Aug 2017 22:02:10 +0000 Subject: [PATCH] Issue #216 - read-line remove trailing newlines --- CHANGELOG.md | 4 ++++ runtime.c | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8021975..dcbece7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ Features - Allow the compiler to optimize calls to `+`, `-`, `*`, and `/` that accept more than 2 arguments. - Added support for bignums to `bitwise-if` from SRFI 60. +Bug Fixes + +- Fix `read-line` to remove trailing newlines. Thanks to wasamasa for the bug report! + ## 0.6.2 - August 25, 2017 Features diff --git a/runtime.c b/runtime.c index af68507e..ed55eaf4 100644 --- a/runtime.c +++ b/runtime.c @@ -6174,7 +6174,7 @@ 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 len; Cyc_check_port(data, port); if (stream == NULL) { @@ -6183,8 +6183,15 @@ object Cyc_io_read_line(void *data, object cont, object port) set_thread_blocked(data, cont); errno = 0; if (fgets(buf, 1023, stream) != NULL) { - make_string(s, buf); - return_thread_runnable(data, &s); + len = strlen(buf); + { + // Remove trailing newline + if (len > 0 && buf[len - 1] == '\n') { + buf[len - 1] = '\0'; + } + make_string_noalloc(s, buf, len); + return_thread_runnable(data, &s); + } } else { if (feof(stream)) { return_thread_runnable(data, Cyc_EOF);