9bec2f12db849e8fc14cfc7d845c2f3e9bf66c7c
1 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
6 int cntcr(char* bufp
, int bufsiz
);
7 int convert(char* endp
, int bufsiz
, int n
);
8 int _writecnv(int fn
, void* buf
, size_t bufsiz
);
14 int _flsbuf(int c
, FILE* f
)
21 if (!OPEN4WRITING(f
)) {
26 // no file associated with buffer, this is a memory stream
27 if (_fileno(f
) == -1) {
31 /* if the buffer is not yet allocated, allocate it */
32 if ((base
= f
->_base
) == NULL
&& (f
->_flag
& _IONBF
) == 0) {
34 if ((f
->_base
= base
= malloc(size
)) == NULL
) {
36 f
->_flag
&= ~(_IOFBF
|_IO_LBF
);
39 f
->_cnt
= f
->_bufsiz
= size
;
42 if (f
== stdout
&& _isatty(_fileno(stdout
))) {
48 if (f
->_flag
& _IO_LBF
) {
49 /* in line-buffering mode we get here on each character */
52 if (c
== '\n' || rn
>= f
->_bufsiz
) {
53 /* time for real flush */
57 /* we got here because _cnt is wrong, so fix it */
58 /* Negative _cnt causes all output functions to call */
59 /* _flsbuf for each character, thus realizing line-buffering */
63 } else if (f
->_flag
& _IONBF
) {
71 if ((f
->_flag
& _IOAHEAD
) == _IOAHEAD
)
72 _lseek(_fileno(f
), -(rn
+f
->_cnt
), SEEK_CUR
);
74 f
->_flag
&= ~_IOAHEAD
;
76 f
->_flag
&= ~_IODIRTY
;
78 n
= _write(_fileno(f
), base
, rn
);
86 if ((f
->_flag
& (_IO_LBF
|_IONBF
)) == 0) {
94 wint_t _flswbuf(wchar_t c
, FILE* fp
)
98 result
= _flsbuf((int)c
, fp
);
101 return (wint_t)result
;
104 int _writecnv(int fn
, void* buf
, size_t siz
)
106 char* bufp
= (char*)buf
;
113 cr1
= cntcr(bufp
, bufsiz
);
115 memcpy(tmp
, bufp
+ bufsiz
- cr1
, cr1
);
116 cr2
= cntcr(tmp
, cr1
);
117 convert(bufp
, bufsiz
- cr2
, cr1
- cr2
);
118 n
= _write(fn
, bufp
, bufsiz
+ cr1
);
119 convert(tmp
, cr1
, cr2
);
120 n
+= _write(fn
, tmp
, cr1
+ cr2
);
125 int convert(char* endp
, int bufsiz
, int n
)
127 endp
= endp
+ bufsiz
+ n
;
141 int cntcr(char* bufp
, int bufsiz
)