2 # STDIO/FWRITE.D - output
7 File.fwrite(void *buf, size_t bytes)
11 # If a regular file requires a flush prior to turn-around to
12 # ensure that any read-back sees the write.
14 if (this.flags & F_ISREG) {
15 if ((this.flags & F_OUT) == 0) {
23 * If unbuffered just write the data directly
24 * (we are guarenteed to be flushed and it doesn't
25 * matter if we are in input mode).
27 while (this.mode == File.M_NONE) {
28 size_t n = this.fs.write1(buf, bytes);
30 if (n < 0 && this.fs.error == Sys.EINTR)
32 return r; # early return
34 buf = (char *)buf + n;
41 * If we need an output buffer create one
43 if (this.out.buf == NULL) {
48 * Copy to the buffer until the buffer is full
51 size_t n = this.out.size - this.out.index;
54 if (this.fflush() < 0)
60 Sys.bcopy((char *)buf + r,
61 &this.out.buf[this.out.index], n);
71 if (this.mode == File.M_LINE &&
72 this.out.index != 0L &&
73 this.out.buf[this.out.index - 1L] == '\n'
77 if ((r2 = this.fflush()) < 0)
87 if (this.mode == File.M_NONE) {
88 if (this.fs.write(&c, 1) == 1L) {
96 * If we need an output buffer create one
98 if (this.out.buf == NULL)
102 * Copy data to buffer
104 if (this.out.index == this.out.size) {
105 if (this.fflush() < 0)
108 this.out.buf[this.out.index] = c;
115 if (this.mode == File.M_LINE && c == '\n')
116 return(this.fflush());
127 if (this.out.index != 0L) {
128 while (r != this.out.index) {
131 n = this.fs.write(&this.out.buf[0] + r,
137 if (r != 0L && r != this.out.index) {
138 Sys.bcopy(&this.out.buf[r], &this.out.buf[0],