Annotation of researchv8dc/cmd/cfront/libC/stream/filebuf.c, revision 1.1.1.1

1.1       root        1: #include "stream.h"
                      2: 
                      3: /* define some UNIX calls */
                      4: extern int open (char *, int);
                      5: extern int close (int);
                      6: extern long lseek (int, long, int);
                      7: extern int read (int, char *, unsigned);
                      8: extern int write (int, char *, unsigned);
                      9: extern int creat (char *, int);
                     10: 
                     11: /*
                     12:  *     Open a file with the given mode.
                     13:  *     Return:         NULL if failure
                     14:  *                     this if success
                     15:  */
                     16: filebuf* filebuf.open (char *name, open_mode om)
                     17: {
                     18:        switch (om) {
                     19:        case input:     
                     20:                fd = ::open(name, 0);
                     21:                break;
                     22:        case output:    
                     23:                if ((fd = ::open(name,1)) < 0) fd = creat(name, 664);
                     24:                break;
                     25:        case append:    
                     26:                if ((fd = ::open(name,1)) < 0)
                     27:                        fd = creat(name, 664);
                     28:                else {
                     29:                        if (lseek(fd, 0, 2) < 0) {
                     30:                                (void)::close(fd);
                     31:                                fd = -1;
                     32:                        }
                     33:                }
                     34:                break;
                     35:        }
                     36: 
                     37:        if (fd < 0) return NULL;
                     38: 
                     39:        opened = 1;
                     40:        return this;
                     41: }
                     42: 
                     43: /*
                     44:  *     Empty an output buffer.
                     45:  *     Returns:        EOF on error
                     46:  *                     0 on success
                     47:  */
                     48: int filebuf.overflow(int c)
                     49: {
                     50:        if (!opened || allocate()==EOF) return EOF;
                     51: 
                     52:        if (base == eptr) {
                     53:                /* unbuffered IO */
                     54:                if (c != EOF) {
                     55:                        *pptr = c;
                     56:                        if (write(fd, pptr, 1) != 1) return EOF;
                     57:                }
                     58:        } else {
                     59:                /* buffered IO */
                     60:                if (pptr > base)
                     61:                        if (write(fd, base, pptr-base) != pptr-base) return EOF;
                     62:                pptr = gptr = base;
                     63:                if (c != EOF) *pptr++ = c;
                     64:        }
                     65:        return c & 0377;
                     66: }
                     67: 
                     68: /*
                     69:  *     Fill an input buffer.
                     70:  *     Returns:        EOF on error or end of input
                     71:  *                     next character on success
                     72:  */
                     73: int filebuf.underflow()
                     74: {
                     75:        int count;
                     76: 
                     77:        if (!opened || allocate()==EOF) return EOF;
                     78: 
                     79:        if ((count=read(fd, base, eptr-base)) < 1) return EOF;
                     80: 
                     81:        gptr = base;
                     82:        pptr = base + count;
                     83:        return *gptr & 0377;
                     84: }

unix.superglobalmegacorp.com

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