Annotation of coherent/b/lib/libc/stdio/ungetc.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * libc/stdio/ungetc.c
        !             3:  * ANSI-compliant C standard i/o library.
        !             4:  * ungetc()
        !             5:  * ANSI 4.9.7.11.
        !             6:  * Unget a character.
        !             7:  */
        !             8: 
        !             9: #include <stdio.h>
        !            10: #include <string.h>
        !            11: #include "stdio.int.h"
        !            12: 
        !            13: extern int     _fungotc();
        !            14: extern int     _funungetc();
        !            15: 
        !            16: int
        !            17: ungetc(c, stream) int c; register FILE *stream;
        !            18: {
        !            19:        register _FILE2 *f2p;
        !            20: 
        !            21:        f2p = stream->_f2p;
        !            22:        if (c == EOF || (stream->_ff1 & _FWONLY))
        !            23:                return EOF;             /* Leave input stream unchanged */
        !            24:        f2p->_gt = &_fungotc;           /* Replace get function */
        !            25:        if (!(stream->_ff1 & _FRONLY))
        !            26:                f2p->_pt = &_funungetc; /* Replace put function */
        !            27:        stream->_cc = 0;
        !            28:        stream->_ff2 |= _FUNGOT;        /* Set ungot flag */
        !            29:        stream->_ff1 &= ~_FEOF;         /* ANSI 4.9.7.11 (24) */
        !            30:        f2p->_uc = c;                   /* Store ungot character */
        !            31:        return c;
        !            32: }
        !            33: 
        !            34: /*
        !            35:  * Get ungetc character.
        !            36:  * Restore the appropriate get and put functions.
        !            37:  */
        !            38: static
        !            39: int
        !            40: _fungotc(fp) register FILE *fp;
        !            41: {
        !            42:        register _FILE2 *f2p;
        !            43: 
        !            44:        f2p = fp->_f2p;
        !            45: 
        !            46: #if    _ASCII
        !            47:        register int isascii;
        !            48: 
        !            49:        isascii = fp->_ff2 & _FASCII;
        !            50: #endif
        !            51: 
        !            52:        switch (fp->_mode) {
        !            53:        case _MODE_UNINIT:              /* Uninitialized */
        !            54:                f2p->_gt = &_fginit;
        !            55:                f2p->_pt = &_fpinit;
        !            56:                break;
        !            57: 
        !            58:        case _MODE_FBUF:
        !            59:        case _MODE_LBUF:
        !            60:                /* Fully buffered or line buffered */
        !            61:                if (fp->_mode == _MODE_FBUF) {
        !            62:                        /* Fully buffered output */
        !            63: #if    _ASCII
        !            64:                        f2p->_pt = isascii ? &_fputba : &_fputb;
        !            65: #else
        !            66:                        f2p->_pt = &_fputb;
        !            67: #endif
        !            68:                } else {
        !            69:                        /* Line buffered output */
        !            70: #if    _ASCII
        !            71:                        f2p->_pt = isascii ? &_fputta : &_fputt;
        !            72: #else
        !            73:                        f2p->_pt = &_fputt;
        !            74: #endif
        !            75:                }
        !            76: #if    _ASCII
        !            77:                f2p->_gt = isascii ? &_fgetba : &_fgetb;
        !            78: #else
        !            79:                f2p->_gt = &_fgetb;
        !            80: #endif
        !            81:                if ((fp->_cc = -(fp->_cp - f2p->_dp)) < 0)
        !            82:                        fp->_cc = f2p->_ep - fp->_cp;
        !            83:                break;
        !            84: 
        !            85:        case _MODE_NBUF:
        !            86:                /* Unbuffered */
        !            87: #if    _ASCII
        !            88:                f2p->_gt = isascii ? &_fgetca : &_fgetc;
        !            89:                f2p->_pt = isascii ? &_fputca : &_fputc;
        !            90: #else
        !            91:                f2p->_gt = &_fgetc;
        !            92:                f2p->_pt = &_fputc;
        !            93: #endif
        !            94:                break;
        !            95: 
        !            96:        case _MODE_STR:
        !            97:                /* String */
        !            98:                f2p->_gt = &_fgetstr;
        !            99:                fp->_cc = strlen(fp->_cp);
        !           100:                break;
        !           101:        }
        !           102: 
        !           103:        if (fp->_ff1 & _FRONLY)
        !           104:                f2p->_pt = &_fpute;
        !           105:        fp->_ff2 &= ~_FUNGOT;           /* Clear the ungot flag */
        !           106:        return f2p->_uc;                /* Return the ungot character */
        !           107: }
        !           108: 
        !           109: /*
        !           110:  * Undo unget.
        !           111:  * This occurs after an ungetc()
        !           112:  * when put function is done before get function.
        !           113:  */
        !           114: static
        !           115: int
        !           116: _funungetc(c, fp) register int c; register FILE *fp;
        !           117: {
        !           118:        register _FILE2 *f2p;
        !           119: 
        !           120:        f2p = fp->_f2p;
        !           121:        (*f2p->_gt)(fp);                /* Undo the unget */
        !           122:        return putc(c, fp);             /* and do the put */
        !           123: }
        !           124: 
        !           125: /* end of libc/stdio/ungetc.c */

unix.superglobalmegacorp.com

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