Annotation of coherent/d/PS2_KERNEL/coh.286/fd.c, revision 1.1.1.1

1.1       root        1: /* $Header: /kernel/kersrc/coh.286/RCS/fd.c,v 1.1 92/07/17 15:18:02 bin Exp Locker: bin $ */
                      2: /* (lgl-
                      3:  *     The information contained herein is a trade secret of Mark Williams
                      4:  *     Company, and  is confidential information.  It is provided  under a
                      5:  *     license agreement,  and may be  copied or disclosed  only under the
                      6:  *     terms of  that agreement.  Any  reproduction or disclosure  of this
                      7:  *     material without the express written authorization of Mark Williams
                      8:  *     Company or persuant to the license agreement is unlawful.
                      9:  *
                     10:  *     COHERENT Version 2.3.37
                     11:  *     Copyright (c) 1982, 1983, 1984.
                     12:  *     An unpublished work by Mark Williams Company, Chicago.
                     13:  *     All rights reserved.
                     14:  -lgl) */
                     15: /*
                     16:  * Coherent.
                     17:  * File descriptor routines.
                     18:  *
                     19:  * $Log:       fd.c,v $
                     20:  * Revision 1.1  92/07/17  15:18:02  bin
                     21:  * Initial revision
                     22:  * 
                     23:  * Revision 1.1        88/03/24  16:13:43      src
                     24:  * Initial revision
                     25:  * 
                     26:  */
                     27: #include <sys/coherent.h>
                     28: #include <errno.h>
                     29: #include <sys/fd.h>
                     30: #include <sys/inode.h>
                     31: 
                     32: /*
                     33:  * Given a file number, return the file descriptor.
                     34:  */
                     35: FD *
                     36: fdget(fd)
                     37: register unsigned fd;
                     38: {
                     39:        register FD *fdp;
                     40: 
                     41:        if (fd>=NUFILE || (fdp=u.u_filep[fd])==NULL) {
                     42:                u.u_error = EBADF;
                     43:                return (NULL);
                     44:        }
                     45:        return (fdp);
                     46: }
                     47: 
                     48: /*
                     49:  * Duplicate a file descriptor number.  This has the same calling
                     50:  * sequence as the dup2 system call and even uses the silly DUP2 bit.
                     51:  */
                     52: fddup(ofd, nfd)
                     53: register unsigned ofd;
                     54: register unsigned nfd;
                     55: {
                     56:        register FD *fdp;
                     57: 
                     58:        if ((fdp=fdget(ofd&~DUP2)) == NULL)
                     59:                return (-1);
                     60:        if ((ofd&DUP2) != 0) {
                     61:                if (nfd >= NUFILE) {
                     62:                        u.u_error = EBADF;
                     63:                        return (-1);
                     64:                }
                     65:                ofd &= ~DUP2;
                     66:                if (ofd == nfd)
                     67:                        return (nfd);
                     68:                if (u.u_filep[nfd] != NULL) {
                     69:                        fdclose(nfd);
                     70:                        if (u.u_error)
                     71:                                return (-1);
                     72:                }
                     73:        } else {
                     74:                for (nfd=0; nfd<NUFILE; nfd++)
                     75:                        if (u.u_filep[nfd] == NULL)
                     76:                                break;
                     77:                if (nfd == NUFILE) {
                     78:                        u.u_error = EMFILE;
                     79:                        return (-1);
                     80:                }
                     81:        }
                     82:        u.u_filep[nfd] = fdp;
                     83:        fdp->f_refc++;
                     84:        return (nfd);
                     85: }
                     86: 
                     87: /*
                     88:  * Given an inode, and a mode containing permission flags, open the
                     89:  * inode with the appropriate permissions and return a file descriptor
                     90:  * containing it.
                     91:  */
                     92: fdopen(ip, mode)
                     93: register INODE *ip;
                     94: {
                     95:        register FD **fdpp;
                     96:        register FD *fdp;
                     97: 
                     98:        for (fdpp=u.u_filep; fdpp<&u.u_filep[NUFILE]; fdpp++) {
                     99:                if (*fdpp != NULL)
                    100:                        continue;
                    101:                if ((fdp=kalloc(sizeof(FD))) == NULL)
                    102:                        return (-1);
                    103:                iopen(ip, mode);
                    104:                if (u.u_error) {
                    105:                        kfree(fdp);
                    106:                        return (-1);
                    107:                }
                    108:                fdp->f_flag = mode;
                    109:                fdp->f_refc = 1;
                    110:                fdp->f_seek = 0;
                    111:                fdp->f_ip = ip;
                    112:                *fdpp = fdp;
                    113:                return (fdpp-u.u_filep);
                    114:        }
                    115:        u.u_error = EMFILE;
                    116:        return (-1);
                    117: }
                    118: 
                    119: /*
                    120:  * Close the given file number.
                    121:  */
                    122: fdclose(fd)
                    123: register unsigned fd;
                    124: {
                    125:        register FD *fdp;
                    126: 
                    127:        if (fd>=NUFILE || (fdp=u.u_filep[fd])==NULL) {
                    128:                u.u_error = EBADF;
                    129:                return;
                    130:        }
                    131:        u.u_filep[fd] = NULL;
                    132:        if (fdp->f_refc == 0)
                    133:                panic("fdclose()");
                    134:        if (--fdp->f_refc == 0) {
                    135:                iclose(fdp->f_ip);
                    136:                kfree(fdp);
                    137:        }
                    138: }
                    139: 
                    140: /*
                    141:  * Assuming we have made a copy of the user area, increment the reference
                    142:  * of all open files.  (used in fork).
                    143:  */
                    144: fdadupl()
                    145: {
                    146:        register FD **fdpp;
                    147:        register FD *fdp;
                    148: 
                    149:        for (fdpp=u.u_filep; fdpp<&u.u_filep[NUFILE]; fdpp++) {
                    150:                if ((fdp=*fdpp) == NULL)
                    151:                        continue;
                    152:                fdp->f_refc++;
                    153:        }
                    154: }
                    155: 
                    156: /*
                    157:  * Close all open files in the current process.
                    158:  */
                    159: fdaclose()
                    160: {
                    161:        register int fd;
                    162: 
                    163:        for (fd=0; fd<NUFILE; fd++) {
                    164:                if (u.u_filep[fd] == NULL)
                    165:                        continue;
                    166:                fdclose(fd);
                    167:        }
                    168: }

unix.superglobalmegacorp.com

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