Annotation of coherent/d/lib/libc/stdio/_fopen.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Standard I/O Library Internals
        !             3:  * Open file
        !             4:  */
        !             5: 
        !             6: #include <stdio.h>
        !             7: 
        !             8: #define        CRMODE  0666    /* default access permissions on create */
        !             9: 
        !            10: FILE *
        !            11: _fopen(name, type, fp, fd)
        !            12: char   *name,
        !            13:        *type;
        !            14: register FILE  *fp;
        !            15: register int   fd;
        !            16: {
        !            17:        extern  int     _fginit();
        !            18:        extern  int     _fpinit();
        !            19:        register int    mode = 1;
        !            20:        int     truncate = 0,
        !            21:                append = 0;
        !            22:        {       register char   c = *type++;
        !            23:                char    cn = 'r';
        !            24: 
        !            25:                if (c=='r') {
        !            26:                        mode = 0;
        !            27:                        cn = 'w';
        !            28:                } else if (c=='w')
        !            29:                        truncate++;
        !            30:                else if (c=='a')
        !            31:                        append++;
        !            32:                else
        !            33:                        return (NULL);
        !            34:                if ((c=*type)=='\0' || c=='b')
        !            35:                        ;
        !            36:                else if (c=='+' || c==cn)
        !            37:                        mode = 2;
        !            38:                else
        !            39:                        return (NULL);
        !            40:        }
        !            41:        if (fd<0 && !truncate)
        !            42:                fd = open(name, mode);
        !            43:        if (fd<0 && (truncate || append)
        !            44:         && (fd=creat(name, CRMODE))>=0 && mode!=1) {
        !            45:                close(fd);
        !            46:                fd = open(name, mode);
        !            47:        }
        !            48:        if (fd<0)
        !            49:                return (NULL);
        !            50:        if (append)
        !            51:                lseek(fd, 0L, SEEK_END);
        !            52:        if (fp==NULL && (fp = (FILE *) malloc(sizeof(FILE)))==NULL) {
        !            53:                close(fd);
        !            54:                return (NULL);
        !            55:        }
        !            56:        fp->_ff = _FINUSE;
        !            57:        fp->_bp = fp->_cp = fp->_dp = NULL;
        !            58:        fp->_cc = 0;
        !            59:        fp->_gt = &_fginit;
        !            60:        fp->_pt = &_fpinit;
        !            61:        fp->_fd = fd;
        !            62:        return (fp);
        !            63: }

unix.superglobalmegacorp.com

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