|
|
1.1 root 1: /* @(#)fwrite.c 3.6 */
2: /*LINTLIBRARY*/
3: /*
4: * This version writes directly to the buffer rather than looping on putc.
5: * Ptr args aren't checked for NULL because the program would be a
6: * catastrophic mess anyway. Better to abort than just to return NULL.
7: *
8: * This version does buffered writes larger than BUFSIZ directly, when
9: * the buffer is empty.
10: */
11: #include <stdio.h>
12: #include "stdiom.h"
13:
14: #define MIN(x, y) (x < y ? x : y)
15:
16: extern char *memcpy();
17:
18: int
19: fwrite(ptr, size, count, iop)
20: char *ptr;
21: int size, count;
22: register FILE *iop;
23: {
24: register unsigned nleft;
25: register int n;
26: register unsigned char *cptr, *bufend;
27:
28: if (size <= 0 || count <= 0 || _WRTCHK(iop))
29: return (0);
30:
31: bufend = _bufend(iop);
32: nleft = count*size;
33:
34: /* if the file is unbuffered, or if the iop->ptr = iop->base, and there
35: is > BUFSZ chars to write, we can do a direct write */
36: if (iop->_base >= iop->_ptr) { /*this covers the unbuffered case, too*/
37: if (((iop->_flag & _IONBF) != 0) || (nleft >= BUFSIZ)) {
38: if ((n=write(fileno(iop),ptr,nleft)) != nleft)
39: {
40: iop->_flag |= _IOERR;
41: n = (n >= 0) ? n : 0;
42: }
43: return n/size;
44: }
45: }
46: /* Put characters in the buffer */
47: /* note that the meaning of n when just starting this loop is
48: irrelevant. It is defined in the loop */
49: for (; ; ptr += n) {
50: while ((n = bufend - (cptr = iop->_ptr)) <= 0) /* full buf */
51: if (_xflsbuf(iop) == EOF)
52: return (count - (nleft + size - 1)/size);
53: n = MIN(nleft, n);
54: (void) memcpy((char *) cptr, ptr, n);
55: iop->_cnt -= n;
56: iop->_ptr += n;
57: _BUFSYNC(iop);
58: /* done; flush if linebuffered with a newline */
59: if ((nleft -= n) == 0) {
60: if (iop->_flag & (_IOLBF | _IONBF)) {
61: if ((iop->_flag & _IONBF) || (memchr(iop->_base,
62: '\n',count * size) != NULL)) {
63: (void) _xflsbuf(iop);
64: }
65: }
66: return (count);
67: }
68: }
69: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.