Merge "gloss: _write takes a buffer of arbitrary length, not a C string"
diff --git a/springbok/crt0.S b/springbok/crt0.S
index deee442..f51ac30 100644
--- a/springbok/crt0.S
+++ b/springbok/crt0.S
@@ -69,9 +69,8 @@
         csrw mtvec, a0
 
         #############################################################
-        # Clear BSS, stack and unused DTCM memory, set up sentinels #
+        # Set up stack sentinels                                    #
         #############################################################
-        jal  ra, _clear_mem
         jal  ra, _setup_stack_sentinels
 
         ##########################
@@ -156,33 +155,6 @@
         .word 0x0000307B # finish (encoded as custom3<func3=3>)
         j _finish
 
-_clear_mem:
-        #######################################################################
-        # Clear the data TCM from the start of BSS to the very end of the TCM #
-        #######################################################################
-        la   a0, _sbss
-        la   a1, _end
-1:
-        sw   zero, 0(a0)
-        sw   zero, 4(a0)
-        sw   zero, 8(a0)
-        sw   zero, 12(a0)
-        sw   zero, 16(a0)
-        sw   zero, 20(a0)
-        sw   zero, 24(a0)
-        sw   zero, 28(a0)
-        sw   zero, 32(a0)
-        sw   zero, 36(a0)
-        sw   zero, 40(a0)
-        sw   zero, 44(a0)
-        sw   zero, 48(a0)
-        sw   zero, 52(a0)
-        sw   zero, 56(a0)
-        sw   zero, 60(a0)
-        addi a0, a0, 64
-        bne  a0, a1, 1b
-        ret
-
 _setup_stack_sentinels:
         #######################################
         # Write our stack sentinels to memory #
diff --git a/springbok/springbok_gloss.cpp b/springbok/springbok_gloss.cpp
index f63c101..eb18b71 100644
--- a/springbok/springbok_gloss.cpp
+++ b/springbok/springbok_gloss.cpp
@@ -43,7 +43,7 @@
     return -1;
   }
 
-  if (nbytes == 0) {
+  if (nbytes <= 0) {
     return 0;
   }
 
@@ -55,25 +55,32 @@
   const int buffer_num   = (file == STDOUT_FILENO)? 0 : 1;
   const int buffer_level = (file == STDOUT_FILENO)? SPRINGBOK_SIMPRINT_INFO : SPRINGBOK_SIMPRINT_ERROR;
 
+  int bytes_read = 0;
   char c;
   do {
+    int len = _write_line_buffer_len[buffer_num];
     c = *(buf++);
-    if (c == '\n') {
-      _write_line_buffer[buffer_num][_write_line_buffer_len[buffer_num]] = '\0';
+    bytes_read++;
+
+    if ((c == '\n') || (c == '\0')) {
+      _write_line_buffer[buffer_num][len] = '\0';
       springbok_simprint(buffer_level, _write_line_buffer[buffer_num], buffer_num);
-      _write_line_buffer_len[buffer_num] = 0;
-    } else if (c != '\0') {
-      _write_line_buffer[buffer_num][_write_line_buffer_len[buffer_num]] = c;
-      _write_line_buffer_len[buffer_num]++;
-      if (_write_line_buffer_len[buffer_num] == 255) {
-        _write_line_buffer[buffer_num][_write_line_buffer_len[buffer_num]] = '\0';
+      len = 0;
+    } else {
+      _write_line_buffer[buffer_num][len] = c;
+      len++;
+
+      if (len == 255) {
+        _write_line_buffer[buffer_num][len] = '\0';
         springbok_simprint(buffer_level, _write_line_buffer[buffer_num], buffer_num);
-        _write_line_buffer_len[buffer_num] = 0;
+        len = 0;
       }
     }
-  } while(c != '\0');
 
-  return nbytes;
+    _write_line_buffer_len[buffer_num] = len;
+  } while (bytes_read < nbytes);
+
+  return bytes_read;
 }
 
 extern "C" int _close(int file) {