Fix: return value for GCC _write and _read is written/read chars

When using ARMClang compiler the functions _sys_write and _sys_read
should return the number of unprocessed characters, whilst when using
GCC the functions _write and _read should return the number of
processed (i.e., written or read) chars.

The code was returning the unprocessed characters value in both cases
(i.e., 0) making the calling code to exit early with error and stop
further processing when using GCC. Hence no subsequent calls to write
would be executed causing missing output on the console.  For example,
string truncation has been seen when printing on stderr.

Note that the bug was not really visible when printing to stdout, but
it was immediately exposed when using stderr.  This was likely due to
the fact that libc buffers the stdout output and flushes it only when
encountering a new line char (\n), whilst no buffering is done on
stderr.  Indeed by printing whole buffers the problem was worked
around and went unseen.

Change-Id: I5a48d6a29441175b2a950716997332a8b9c34e10
diff --git a/targets/corstone-300/retarget.c b/targets/corstone-300/retarget.c
index 13cf68b..4bde44d 100644
--- a/targets/corstone-300/retarget.c
+++ b/targets/corstone-300/retarget.c
@@ -33,7 +33,8 @@
 #define STDOUT 0x8002
 #define STDERR 0x8003
 
-#define RETARGET(fun) _sys##fun
+#define RETARGET(fun)  _sys##fun
+#define IO_OUTPUT(len) 0
 
 #else
 /*
@@ -52,7 +53,8 @@
 #define STDOUT 0x01
 #define STDERR 0x02
 
-#define RETARGET(fun) fun
+#define RETARGET(fun)  fun
+#define IO_OUTPUT(len) len
 
 #endif
 
@@ -90,15 +92,16 @@
     case STDOUT:
     case STDERR: {
         int c;
+        unsigned int i;
 
-        while (len-- > 0) {
-            c = fputc(*buf++, stdout);
+        for (i = 0; i < len; i++) {
+            c = fputc(buf[i], stdout);
             if (c == EOF) {
                 return EOF;
             }
         }
 
-        return 0;
+        return IO_OUTPUT(len);
     }
     default:
         return EOF;
@@ -111,17 +114,18 @@
     switch (fh) {
     case STDIN: {
         int c;
+        unsigned int i;
 
-        while (len-- > 0) {
+        for (i = 0; i < len; i++) {
             c = fgetc(stdin);
             if (c == EOF) {
                 return EOF;
             }
 
-            *buf++ = (unsigned char)c;
+            buf[i] = (unsigned char)c;
         }
 
-        return 0;
+        return IO_OUTPUT(len);
     }
     default:
         return EOF;