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
);
11 _flsbuf(int c
, FILE *f
)
20 if (!OPEN4WRITING(f
)) {
25 // no file associated with buffer
26 // this is a memory stream
28 if ( _fileno(f
) == -1 )
31 /* if the buffer is not yet allocated, allocate it */
32 if ((base
= f
->_base
) == NULL
&& (f
->_flag
& _IONBF
) == 0)
35 if ((f
->_base
= base
= malloc (size
)) == NULL
)
38 f
->_flag
&= ~(_IOFBF
|_IOLBF
);
43 f
->_cnt
= f
->_bufsiz
= size
;
46 if (f
== stdout
&& _isatty (_fileno (stdout
)))
51 if (f
->_flag
& _IOLBF
)
53 /* in line-buffering mode we get here on each character */
56 if (c
== '\n' || rn
>= f
->_bufsiz
)
58 /* time for real flush */
64 /* we got here because _cnt is wrong, so fix it */
65 /* Negative _cnt causes all output functions
66 to call _flsbuf for each character, thus realizing line-buffering */
71 else if (f
->_flag
& _IONBF
)
82 if ( (f
->_flag
& _IOAHEAD
) == _IOAHEAD
)
83 _lseek(_fileno(f
),-(rn
+f
->_cnt
), SEEK_CUR
);
85 f
->_flag
&= ~_IOAHEAD
;
90 f
->_flag
&= ~_IODIRTY
;
93 n
= _write(_fileno(f
), base
, rn
);
104 if ((f
->_flag
&(_IOLBF
|_IONBF
)) == 0)
112 wint_t _flswbuf(wchar_t c
,FILE *fp
)
114 return (wint_t )_flsbuf((int)c
,fp
);
118 int _writecnv(int fn
, void *buf
, size_t siz
)
120 char *bufp
= (char *)buf
;
130 cr1
= cntcr(bufp
,bufsiz
);
133 memcpy(tmp
,bufp
+bufsiz
-cr1
,cr1
);
134 cr2
= cntcr(tmp
,cr1
);
136 convert(bufp
,bufsiz
-cr2
,cr1
-cr2
);
137 n
= _write(fn
, bufp
, bufsiz
+ cr1
);
139 convert(tmp
,cr1
,cr2
);
140 n
+= _write(fn
, tmp
, cr1
+ cr2
);
147 int convert(char *endp
, int bufsiz
,int n
)
149 endp
= endp
+ bufsiz
+ n
;
162 int cntcr(char *bufp
, int bufsiz
)