* Update fputs().
CORE-8080

svn path=/trunk/; revision=63412
This commit is contained in:
Amine Khaldi 2014-05-22 16:13:44 +00:00
parent f537d6e8e4
commit 292420e2af

View file

@ -553,6 +553,29 @@ int CDECL _isatty(int fd)
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 "" */
static int int_to_base32(int num, char *str)
{
@ -3427,23 +3450,13 @@ int CDECL fgetpos(FILE* file, fpos_t *pos)
*/
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 fputs(const char *s, FILE* file)
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 @@ int CDECL fputws(const wchar_t *s, FILE* file)
_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;
}