# # STDIO/FWRITE.D - output # public method size_t File.fwrite(void *buf, size_t bytes) { size_t r = 0; # If a regular file requires a flush prior to turn-around to # ensure that any read-back sees the write. # if (this.flags & F_ISREG) { if ((this.flags & F_OUT) == 0) { this.flags |= F_OUT; this.in.base = 0; this.in.index = 0; } } /* * If unbuffered just write the data directly * (we are guarenteed to be flushed and it doesn't * matter if we are in input mode). */ while (this.mode == File.M_NONE) { size_t n = this.fs.write1(buf, bytes); if (n <= 0L) { if (n < 0 && this.fs.error == Sys.EINTR) continue; return r; # early return } buf = (char *)buf + n; r += n; bytes -= n; this.pos += (off_t)n; } /* * If we need an output buffer create one */ if (this.out.buf == NULL) { this.out.initbuf(); } /* * Copy to the buffer until the buffer is full */ while (bytes != 0L) { size_t n = this.out.size - this.out.index; if (n == 0L) { if (this.fflush() < 0) break; continue; } if (n > bytes) n = bytes; Sys.bcopy((char *)buf + r, &this.out.buf[this.out.index], n); r += n; bytes -= n; this.pos += (off_t)n; this.out.index += n; } /* * Line mode flush */ if (this.mode == File.M_LINE && this.out.index != 0L && this.out.buf[this.out.index - 1L] == '\n' ) { int r2; if ((r2 = this.fflush()) < 0) r = (size_t)r2; } return(r); } public method int File.fputc(char c) { if (this.mode == File.M_NONE) { if (this.fs.write(&c, 1) == 1L) { ++this.pos; return(0); } return(-1); } /* * If we need an output buffer create one */ if (this.out.buf == NULL) this.out.initbuf(); /* * Copy data to buffer */ if (this.out.index == this.out.size) { if (this.fflush() < 0) return(-1); } this.out.buf[this.out.index] = c; ++this.out.index; ++this.pos; /* * Line mode flush */ if (this.mode == File.M_LINE && c == '\n') return(this.fflush()); else return(0); } public method int File.fflush() { size_t r = 0; if (this.out.index != 0L) { while (r != this.out.index) { size_t n; n = this.fs.write(&this.out.buf[0] + r, this.out.index - r); if (n < 0L) return((int)n); r += n; } if (r != 0L && r != this.out.index) { Sys.bcopy(&this.out.buf[r], &this.out.buf[0], this.out.index - r); } this.out.index -= r; } return(0); }