Author: tkreuzer Date: Tue Jan 4 22:22:13 2011 New Revision: 50290
URL: http://svn.reactos.org/svn/reactos?rev=50290&view=rev Log: [CRT] - Rewrite broken _flsbuf and implement _flswbuf - use _flswbuf in wstreamout - Fixes broken text output in dwnl etc
Added: trunk/reactos/lib/sdk/crt/stdio/_flsbuf.c (with props) trunk/reactos/lib/sdk/crt/stdio/_flswbuf.c (with props) Modified: trunk/reactos/lib/sdk/crt/crt.rbuild trunk/reactos/lib/sdk/crt/printf/streamout.c trunk/reactos/lib/sdk/crt/stdio/file.c
Modified: trunk/reactos/lib/sdk/crt/crt.rbuild URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/crt.rbuild?rev=... ============================================================================== --- trunk/reactos/lib/sdk/crt/crt.rbuild [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/crt.rbuild [iso-8859-1] Tue Jan 4 22:22:13 2011 @@ -354,6 +354,8 @@ <file>xcptinfo.c</file> </directory> <directory name="stdio"> + <file>_flsbuf.c</file> + <file>_flswbuf.c</file> <file>access.c</file> <file>file.c</file> <file>find.c</file>
Modified: trunk/reactos/lib/sdk/crt/printf/streamout.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/streamou... ============================================================================== --- trunk/reactos/lib/sdk/crt/printf/streamout.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/printf/streamout.c [iso-8859-1] Tue Jan 4 22:22:13 2011 @@ -16,6 +16,8 @@ #ifdef _UNICODE #define streamout wstreamout #define format_float format_floatw +#define _flsbuf _flswbuf +int __cdecl _flwsbuf(int ch, FILE *stream); #endif
#define MB_CUR_MAX 10 @@ -68,7 +70,8 @@ va_arg(argptr, double)
#ifdef _LIBCNT_ -# define _flsbuf(chr, stream) 0 +# undef _flsbuf +# define _flsbuf(chr, stream) _TEOF #endif
#define get_exp(f) floor(f == 0 ? 0 : (f >= 0 ? log10(f) : log10(-f)))
Added: trunk/reactos/lib/sdk/crt/stdio/_flsbuf.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/stdio/_flsbuf.c... ============================================================================== --- trunk/reactos/lib/sdk/crt/stdio/_flsbuf.c (added) +++ trunk/reactos/lib/sdk/crt/stdio/_flsbuf.c [iso-8859-1] Tue Jan 4 22:22:13 2011 @@ -1,0 +1,82 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/stdio/_flsbuf.c + * PURPOSE: Implementation of _flsbuf / _flswbuf + * PROGRAMMER: Timo Kreuzer + */ + +#include <stdio.h> +#include <io.h> +#include <tchar.h> + +void __cdecl alloc_buffer(FILE *stream); + +int __cdecl +_flsbuf(int ch, FILE *stream) +{ + int count, written; + + /* Check if the stream supports flushing */ + if ((stream->_flag & _IOSTRG) || !(stream->_flag & (_IORW|_IOWRT))) + { + stream->_flag |= _IOERR; + return EOF; + } + + /* Is this was a read buffer */ + if (stream->_flag & _IOREAD) + { + /* Must be at the end of the file */ + if (!(stream->_flag & _IOEOF)) + { + stream->_flag |= _IOERR; + stream->_cnt = 0; + return EOF; + } + + /* Reset buffer */ + stream->_ptr = stream->_base; + } + + /* Fixup flags */ + stream->_flag &= ~(_IOREAD|_IOEOF); + stream->_flag |= _IOWRT; + + /* If we have no buffer, try to allocate one */ + if (!stream->_base && stream != stdout && stream != stderr) + { + alloc_buffer(stream); + } + + /* Check if we have a buffer now */ + if (stream->_base) + { + /* We have one, check if there is something to write */ + count = stream->_ptr - stream->_base; + if (count > 0) + written = _write(stream->_file, stream->_base, count); + else + written = 0; + + /* Reset buffer and put the char into it */ + stream->_ptr = stream->_base + sizeof(TCHAR); + stream->_cnt = stream->_bufsiz - sizeof(TCHAR); + *(TCHAR*)stream->_base = ch; + } + else + { + /* There is no buffer, write the char directly */ + count = sizeof(TCHAR); + written = _write(stream->_file, &ch, sizeof(TCHAR)); + } + + /* Check for failure */ + if (written != count) + { + stream->_flag |= _IOERR; + return EOF; + } + + return (TCHAR)ch; +}
Propchange: trunk/reactos/lib/sdk/crt/stdio/_flsbuf.c ------------------------------------------------------------------------------ svn:eol-style = native
Added: trunk/reactos/lib/sdk/crt/stdio/_flswbuf.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/stdio/_flswbuf.... ============================================================================== --- trunk/reactos/lib/sdk/crt/stdio/_flswbuf.c (added) +++ trunk/reactos/lib/sdk/crt/stdio/_flswbuf.c [iso-8859-1] Tue Jan 4 22:22:13 2011 @@ -1,0 +1,11 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/stdio/_flswbuf + * PURPOSE: Implementation of _flswbuf + * PROGRAMMER: Timo Kreuzer + */ + +#define _UNICODE +#define _flsbuf _flswbuf +#include "_flsbuf.c"
Propchange: trunk/reactos/lib/sdk/crt/stdio/_flswbuf.c ------------------------------------------------------------------------------ svn:eol-style = native
Modified: trunk/reactos/lib/sdk/crt/stdio/file.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/stdio/file.c?re... ============================================================================== --- trunk/reactos/lib/sdk/crt/stdio/file.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/stdio/file.c [iso-8859-1] Tue Jan 4 22:22:13 2011 @@ -88,7 +88,7 @@ typedef struct { HANDLE handle; unsigned char wxflag; - DWORD unkn[7]; /* critical section and init flag */ + DWORD unkn[7]; /* critical section and init flag */ } ioinfo;
ioinfo fdesc[MAX_FILES]; @@ -292,12 +292,12 @@ *handle_ptr = INVALID_HANDLE_VALUE; } wxflag_ptr++; handle_ptr++; - } + } return TRUE; }
-/* INTERNAL: Set up all file descriptors, - * as well as default streams (stdin, stderr and stdout) +/* INTERNAL: Set up all file descriptors, + * as well as default streams (stdin, stderr and stdout) */ void msvcrt_init_io(void) { @@ -342,7 +342,7 @@ { #ifndef __REACTOS__ DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE), - GetCurrentProcess(), &fdesc[0].handle, 0, TRUE, + GetCurrentProcess(), &fdesc[0].handle, 0, TRUE, DUPLICATE_SAME_ACCESS); #else fdesc[0].handle = GetStdHandle(STD_INPUT_HANDLE); @@ -355,7 +355,7 @@ { #ifndef __REACTOS__ DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE), - GetCurrentProcess(), &fdesc[1].handle, 0, TRUE, + GetCurrentProcess(), &fdesc[1].handle, 0, TRUE, DUPLICATE_SAME_ACCESS); #else fdesc[1].handle = GetStdHandle(STD_OUTPUT_HANDLE); @@ -368,7 +368,7 @@ { #ifndef __REACTOS__ DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), - GetCurrentProcess(), &fdesc[2].handle, 0, TRUE, + GetCurrentProcess(), &fdesc[2].handle, 0, TRUE, DUPLICATE_SAME_ACCESS); #else fdesc[2].handle = GetStdHandle(STD_ERROR_HANDLE); @@ -409,7 +409,7 @@ }
/* INTERNAL: Allocate stdio file buffer */ -static void alloc_buffer(FILE* file) +void alloc_buffer(FILE* file) { file->_base = calloc(BUFSIZ,1); if(file->_base) { @@ -722,7 +722,7 @@ int CDECL _dup(int od) { int fd, ret; - + LOCK_FILES(); fd = fdstart; if (_dup2(od, fd) == 0) @@ -1393,7 +1393,7 @@ else creation = OPEN_EXISTING; } - + switch( shflags ) { case _SH_DENYRW: @@ -2030,12 +2030,12 @@ wcp = (char *)&wc; for(i=0; i<sizeof(wc); i++) { - if (file->_cnt>0) + if (file->_cnt>0) { file->_cnt--; chp = file->_ptr++; wcp[i] = *chp; - } + } else { j = _filbuf(file); @@ -2050,7 +2050,7 @@ } return wc; } - + c = fgetc(file); if ((*__p___mb_cur_max() > 1) && isleadbyte(c)) { @@ -2287,37 +2287,6 @@ return c & 0xff; } else { return _flsbuf(c, file); - } -} - -/********************************************************************* - * _flsbuf (MSVCRT.@) - */ -int CDECL _flsbuf(int c, FILE* file) -{ - /* Flush output buffer */ - if(file->_bufsiz == 0 && !(file->_flag & _IONBF)) { - alloc_buffer(file); - } - if(!(file->_flag & _IOWRT)) { - if(file->_flag & _IORW) { - file->_flag |= _IOWRT; - } else { - return EOF; - } - } - if(file->_bufsiz) { - int res=flush_buffer(file); - return res?res : fputc(c, file); - } else { - unsigned char cc=c; - int len; - /* set _cnt to 0 for unbuffered FILEs */ - file->_cnt = 0; - len = _write(file->_file, &cc, 1); - if (len == 1) return c & 0xff; - file->_flag |= _IOERR; - return EOF; } }
@@ -2487,7 +2456,7 @@ /* Discard buffered input */ file->_cnt = 0; file->_ptr = file->_base; - + /* Reset direction of i/o */ if(file->_flag & _IORW) { file->_flag &= ~(_IOREAD|_IOWRT); @@ -2560,7 +2529,7 @@ if (!(fdesc[file->_file].wxflag & WX_TEXT)) return fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF; for (i=0; i<len; i++) - if (fputc(s[i], file) == EOF) + if (fputc(s[i], file) == EOF) return EOF; return 0; } @@ -2578,7 +2547,7 @@ if ((s[i] == '\n') && (fputc('\r', file) == EOF)) return WEOF; if (fputwc(s[i], file) == WEOF) - return WEOF; + return WEOF; } return 0; }