Annotation of cci/usr/src/lib/libc/stdio/rdwr.c, revision 1.1.1.2

1.1.1.2 ! root        1: /*
        !             2:  * Modified by Gershon Shamay, 31 March 1986. To move whole
        !             3:  * strings at a time in 'fread' instead of a character at a time
        !             4:  * which is killing us for many bytes/call.
        !             5:  */
1.1       root        6: #include       <stdio.h>
                      7: 
1.1.1.2 ! root        8: #define        MIN(a,b)        ((a)<(b) ? (a):(b))
        !             9: 
1.1       root       10: fread(ptr, size, count, iop)
1.1.1.2 ! root       11: register unsigned size;
1.1       root       12: register char *ptr;
                     13: register FILE *iop;
                     14: {
1.1.1.2 ! root       15:        register int chars_to_read = size * count;
        !            16:        register int chars_read = 0;
        !            17:        register int this_transfer;
1.1       root       18: 
1.1.1.2 ! root       19: /*
        !            20:  * easy case: data to be read is already in file buffer.
        !            21:  */
        !            22:        if (chars_to_read <= iop->_cnt) {
        !            23:                bcopy(iop->_ptr, ptr, chars_to_read);
        !            24:                iop->_ptr += chars_to_read;
        !            25:                iop->_cnt -= chars_to_read;
        !            26:                return(count);
        !            27:        }
        !            28: /*
        !            29:  * else we have to sweat a bit ....
        !            30:  */
        !            31:        while (chars_read < chars_to_read) {
        !            32:                if (iop->_cnt <= 0) {
        !            33:                        if (_filbuf(iop) < 0 ) /* Out of input data */
        !            34:                                return(chars_read/size);
        !            35:                        else {
        !            36:                                iop->_cnt++;  /* Put back first char */
        !            37:                                iop->_ptr--;
        !            38:                        }
        !            39:                }
        !            40:                this_transfer = MIN(iop->_cnt, chars_to_read - chars_read);
        !            41:                bcopy(iop->_ptr, ptr, this_transfer);
        !            42:                chars_read += this_transfer;
        !            43:                ptr += this_transfer;
        !            44:                iop->_ptr += this_transfer;
        !            45:                iop->_cnt -= this_transfer;
1.1       root       46:        }
1.1.1.2 ! root       47:        return(chars_read/size);
1.1       root       48: }
                     49: 
                     50: fwrite(ptr, size, count, iop)
1.1.1.2 ! root       51: register unsigned size, count;
1.1       root       52: register char *ptr;
                     53: register FILE *iop;
                     54: {
1.1.1.2 ! root       55:        register int chars_to_write = size * count;
        !            56:        register int chars_written = 0;
        !            57:        register int this_transfer;
1.1       root       58: 
1.1.1.2 ! root       59: /*
        !            60:  * easy case: check if there's enough room in buffer for entire
        !            61:  *             operation.
        !            62:  */
        !            63:        if (chars_to_write <= iop->_cnt) {
        !            64:                bcopy(ptr, iop->_ptr, chars_to_write);
        !            65:                iop->_ptr += chars_to_write;
        !            66:                iop->_cnt -= chars_to_write;
        !            67:                return(count);
        !            68:        }
        !            69: /*
        !            70:  * else we have to sweat a bit ....
        !            71:  */
        !            72:        while (chars_written < chars_to_write) {
        !            73:                if (iop->_cnt <= 0) {
        !            74:                        _flsbuf((unsigned)*ptr, iop);   /* Full buffer */
        !            75:                        if (ferror(iop))
        !            76:                                break;
        !            77:                        else {
        !            78:                                iop->_cnt++;
        !            79:                                iop->_ptr--;
        !            80:                        }
        !            81:                }
        !            82:                this_transfer = MIN(iop->_cnt, chars_to_write - chars_written);
        !            83:                bcopy(ptr, iop->_ptr, this_transfer);
        !            84:                chars_written += this_transfer;
        !            85:                ptr += this_transfer;
        !            86:                iop->_ptr += this_transfer;
        !            87:                iop->_cnt -= this_transfer;
1.1       root       88:        }
1.1.1.2 ! root       89:        return(chars_written/size);
1.1       root       90: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.