Author: akhaldi
Date: Thu May 22 16:13:44 2014
New Revision: 63412
URL:
http://svn.reactos.org/svn/reactos?rev=63412&view=rev
Log:
[CRT]
* Update fputs().
CORE-8080
Modified:
trunk/reactos/lib/sdk/crt/stdio/file.c
Modified: trunk/reactos/lib/sdk/crt/stdio/file.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/stdio/file.c?r…
==============================================================================
--- trunk/reactos/lib/sdk/crt/stdio/file.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/stdio/file.c [iso-8859-1] Thu May 22 16:13:44 2014
@@ -551,6 +551,29 @@
file->_ptr = file->_base;
file->_cnt = 0;
return TRUE;
+}
+
+/* INTERNAL: Allocate temporary buffer for stdout and stderr */
+static BOOL add_std_buffer(FILE *file)
+{
+ static char buffers[2][BUFSIZ];
+
+ if((file->_file!=STDOUT_FILENO && file->_file!=STDERR_FILENO)
+ || !_isatty(file->_file) || file->_bufsiz)
+ return FALSE;
+
+ file->_ptr = file->_base = buffers[file->_file == STDOUT_FILENO ? 0 : 1];
+ file->_bufsiz = file->_cnt = BUFSIZ;
+ return TRUE;
+}
+
+/* INTERNAL: Removes temporary buffer from stdout or stderr */
+/* Only call this function when add_std_buffer returned TRUE */
+static void remove_std_buffer(FILE *file)
+{
+ flush_buffer(file);
+ file->_ptr = file->_base = NULL;
+ file->_bufsiz = file->_cnt = 0;
}
/* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */
@@ -3427,23 +3450,13 @@
*/
int CDECL fputs(const char *s, FILE* file)
{
- size_t i, len = strlen(s);
+ size_t len = strlen(s);
int ret;
_lock_file(file);
- if (!(get_ioinfo(file->_file)->wxflag & WX_TEXT)) {
- ret = fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
- _unlock_file(file);
- return ret;
- }
- for (i=0; i<len; i++)
- if (fputc(s[i], file) == EOF) {
- _unlock_file(file);
- return EOF;
- }
-
+ ret = fwrite(s, sizeof(*s), len, file) == len ? 0 : EOF;
_unlock_file(file);
- return 0;
+ return ret;
}
/*********************************************************************
@@ -3452,6 +3465,7 @@
int CDECL fputws(const wchar_t *s, FILE* file)
{
size_t i, len = strlenW(s);
+ BOOL tmp_buf;
int ret;
_lock_file(file);
@@ -3460,14 +3474,17 @@
_unlock_file(file);
return ret;
}
+
+ tmp_buf = add_std_buffer(file);
for (i=0; i<len; i++) {
- if (((s[i] == '\n') && (fputc('\r', file) == EOF))
- || fputwc(s[i], file) == WEOF) {
+ if(fputwc(s[i], file) == WEOF) {
+ if(tmp_buf) remove_std_buffer(file);
_unlock_file(file);
return WEOF;
}
}
+ if(tmp_buf) remove_std_buffer(file);
_unlock_file(file);
return 0;
}