]> rtime.felk.cvut.cz Git - arc.git/blobdiff - common/printf.c
Merge branch 'mikulka' of git@rtime.felk.cvut.cz:arc into mikulka
[arc.git] / common / printf.c
index 902f16b52a282d4b1a8854b8ff4fa04f71a275b2..be39635ab039acdcbebb8f9952fdf4f5c5818e5f 100644 (file)
  *  - ANSI-C and POSIX, streams and POSIX filenumbers.\r
  *    POSIX file-numbers exist in unistd.h and are only to be used by the porting\r
  *    newlib interface i.e. newlib_port.c.\r
- *    The filenumber is actually just a cast of the steampointer and save in the member\r
- *    _cookie in FILE.\r
+ *\r
+ *\r
+ * NEWLIB: File handles vs files\r
+ *   This printf() family of functions does not use newlib at all.\r
+ *   At this point the following can have happend:\r
+ *   1. A call to any of the file functions in newlib have been called.\r
+ *      This then calls a number of functions (sbrk, __sinit(_impure_ptr), etc ).\r
+ *      __sinit(_impure_ptr) initializes the standard files, stdin, stdout, stderr.\r
+ *      file->_file is the actual posix file number (stdin=0, stdout=1, stderr=2)\r
+ *   2. No calls is done to newlib file functions. The impure_data is then empty and\r
+ *      all fields are 0.\r
+ *\r
+ *  Code for checking if the newlib have initialized (or we have explicitly called __sinit(_impure_ptr)\r
+ *     if( _impure_ptr->__sdidinit == 1 ) {\r
+ *       // We use the real filenumber\r
+ *       arc_putchar((int)(file->_file), c);\r
+ *     )\r
  *\r
  */\r
 \r
@@ -45,7 +60,9 @@
 #include <stdarg.h>\r
 #include <assert.h>\r
 #include <string.h>\r
-\r
+#if defined(USE_NEWLIB) && defined(__GNUC__)\r
+#include "reent.h"\r
+#endif\r
 //#define HOST_TEST    1\r
 \r
 int arc_putchar(int fd, int c);\r
@@ -66,7 +83,7 @@ int printf(const char *format, ...) {
        int rv;\r
 \r
        va_start(ap, format);\r
-       rv = vfprintf(stdout, format, ap);\r
+       rv = vfprintf((FILE *)STDOUT_FILENO, format, ap);\r
        va_end(ap);\r
        return rv;\r
 }\r
@@ -103,7 +120,7 @@ int snprintf(char *buffer, size_t n, const char *format, ...) {
 }\r
 \r
 int vprintf(const char *format, va_list ap) {\r
-       return vfprintf(stdout, format, ap);\r
+       return vfprintf((FILE *)STDOUT_FILENO, format, ap);\r
 }\r
 \r
 int vsprintf(char *buffer, const char *format, va_list ap) {\r
@@ -154,8 +171,21 @@ static inline int emitChar( FILE *file, char **buf, char c, int *left ) {
                putc(c, stdout);\r
                fflush(stdout);\r
 #else\r
-               arc_putchar((int)file->_cookie, c);\r
+#if 0\r
+#if defined(USE_NEWLIB) && defined(__GNUC__)\r
+               /* We are trying to print with newlib file descriptor.\r
+                * That's wrong since we are using the POSIX file numbers here instead\r
+                * Check stdout */\r
+               assert( file != _impure_ptr->_stdout );\r
+#endif\r
 #endif\r
+               if( (unsigned )file > 10UL ) {\r
+                       arc_putchar((int)(file->_file), c);\r
+               } else {\r
+                       arc_putchar((int)(file), c);\r
+               }\r
+\r
+#endif /* HOST_TEST */\r
        } else {\r
                **buf = c;\r
                (*buf)++;\r