ReactOS  0.4.15-dev-3331-g8ebe441
_flsbuf.c File Reference
#include <precomp.h>
Include dependency graph for _flsbuf.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

BOOL __cdecl msvcrt_alloc_buffer (FILE *stream)
 
int __cdecl _flsbuf (int ch, FILE *stream)
 

Function Documentation

◆ _flsbuf()

int __cdecl _flsbuf ( int  ch,
FILE stream 
)

Definition at line 14 of file _flsbuf.c.

15 {
16  int count, written;
17 
18  /* Check if the stream supports flushing */
19  if ((stream->_flag & _IOSTRG) || !(stream->_flag & (_IORW|_IOWRT)))
20  {
21  stream->_flag |= _IOERR;
22  return EOF;
23  }
24 
25  /* Always reset _cnt */
26  stream->_cnt = 0;
27 
28  /* Check if this was a read buffer */
29  if (stream->_flag & _IOREAD)
30  {
31  /* Must be at the end of the file */
32  if (!(stream->_flag & _IOEOF))
33  {
34  stream->_flag |= _IOERR;
35  return EOF;
36  }
37 
38  /* Reset buffer */
39  stream->_ptr = stream->_base;
40  }
41 
42  /* Fixup flags */
43  stream->_flag &= ~(_IOREAD|_IOEOF);
44  stream->_flag |= _IOWRT;
45 
46  /* Check if should get a buffer */
47  if (!(stream->_flag & _IONBF) && stream != stdout && stream != stderr)
48  {
49  /* If we have no buffer, try to allocate one */
50  if (!stream->_base) msvcrt_alloc_buffer(stream);
51  }
52 
53  /* Check if we can use a buffer now */
54  if (stream->_base && !(stream->_flag & _IONBF))
55  {
56  /* We can, check if there is something to write */
57  count = (int)(stream->_ptr - stream->_base);
58  if (count > 0)
59  written = _write(stream->_file, stream->_base, count);
60  else
61  written = 0;
62 
63  /* Reset buffer and put the char into it */
64  stream->_ptr = stream->_base + sizeof(TCHAR);
65  stream->_cnt = stream->_bufsiz - sizeof(TCHAR);
66  *(TCHAR*)stream->_base = ch;
67  }
68  else
69  {
70  /* There is no buffer, write the char directly */
71  count = sizeof(TCHAR);
72  written = _write(stream->_file, &ch, sizeof(TCHAR));
73  }
74 
75  /* Check for failure */
76  if (written != count)
77  {
78  stream->_flag |= _IOERR;
79  return EOF;
80  }
81 
82  return ch & (sizeof(TCHAR) > sizeof(char) ? 0xffff : 0xff);
83 }
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
GLuint GLuint GLsizei count
Definition: gl.h:1545
BOOL __cdecl msvcrt_alloc_buffer(FILE *stream)
Definition: file.c:573
#define _IOSTRG
Definition: stdio.h:134
FILE * stdout
#define _IOERR
Definition: stdio.h:133
#define _IOREAD
Definition: stdio.h:124
#define _IOEOF
Definition: stdio.h:132
char TCHAR
Definition: xmlstorage.h:189
#define _IOWRT
Definition: stdio.h:125
Definition: parse.h:22
#define EOF
Definition: stdio.h:24
FILE * stderr
#define _IONBF
Definition: stdio.h:129
#define _IORW
Definition: stdio.h:135
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31

Referenced by fputc(), fwrite(), test_flsbuf(), and test_invalid_stdin_child().

◆ msvcrt_alloc_buffer()

BOOL __cdecl msvcrt_alloc_buffer ( FILE stream)

Definition at line 573 of file file.c.

574 {
575  if((file->_file==STDOUT_FILENO || file->_file==STDERR_FILENO)
576  && _isatty(file->_file))
577  return FALSE;
578 
579  file->_base = calloc(1, MSVCRT_INTERNAL_BUFSIZ);
580  if(file->_base) {
581  file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
582  file->_flag |= _IOMYBUF;
583  } else {
584  file->_base = (char*)(&file->_charbuf);
585  file->_bufsiz = 2;
586  file->_flag |= _IONBF;
587  }
588  file->_ptr = file->_base;
589  file->_cnt = 0;
590  return TRUE;
591 }
#define TRUE
Definition: types.h:120
int CDECL _isatty(int fd)
Definition: file.c:565
#define FALSE
Definition: types.h:117
#define STDOUT_FILENO
Definition: syshdrs.h:89
#define _IOMYBUF
Definition: stdio.h:131
#define MSVCRT_INTERNAL_BUFSIZ
Definition: file.c:115
#define calloc
Definition: rosglue.h:14
#define _IONBF
Definition: stdio.h:129
#define STDERR_FILENO
Definition: syshdrs.h:90
Definition: fci.c:126

Referenced by _filbuf(), _flsbuf(), fread(), and ungetc().