|
|
1.1 root 1: /* $Header: /newbits/kernel/USRSRC/coh/RCS/fd.c,v 1.4 91/07/24 07:50:35 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.4 91/07/24 07:50:35 bin
21: * update prov by hal
22: *
23: *
24: * Revision 1.1 88/03/24 16:13:43 src
25: * Initial revision
26: *
27: */
28: #include <sys/coherent.h>
29: #include <errno.h>
30: #include <sys/fd.h>
31: #include <sys/inode.h>
32: #include <sys/uproc.h>
33:
34: /*
35: * Given a file number, return the file descriptor.
36: */
37: FD *
38: fdget(fd)
39: register unsigned fd;
40: {
41: register FD *fdp;
42:
43: if (fd>=NUFILE || (fdp=u.u_filep[fd])==NULL) {
44: u.u_error = EBADF;
45: return (NULL);
46: }
47: return (fdp);
48: }
49:
50: /*
51: * Duplicate a file descriptor number. This has the same calling
52: * sequence as the dup2 system call and even uses the silly DUP2 bit.
53: */
54: fddup(ofd, nfd)
55: register unsigned ofd;
56: register unsigned nfd;
57: {
58: register FD *fdp;
59:
60: if ((fdp=fdget(ofd&~DUP2)) == NULL)
61: return (-1);
62: if ((ofd&DUP2) != 0) {
63: if (nfd >= NUFILE) {
64: u.u_error = EBADF;
65: return (-1);
66: }
67: ofd &= ~DUP2;
68: if (ofd == nfd)
69: return (nfd);
70: if (u.u_filep[nfd] != NULL) {
71: fdclose(nfd);
72: if (u.u_error)
73: return (-1);
74: }
75: } else {
76: for (nfd=0; nfd<NUFILE; nfd++)
77: if (u.u_filep[nfd] == NULL)
78: break;
79: if (nfd == NUFILE) {
80: u.u_error = EMFILE;
81: return (-1);
82: }
83: }
84: u.u_filep[nfd] = fdp;
85: fdp->f_refc++;
86: return (nfd);
87: }
88:
89: /*
90: * Given an inode, and a mode containing permission flags, open the
91: * inode with the appropriate permissions and return a file descriptor
92: * containing it.
93: */
94: fdopen(ip, mode)
95: register INODE *ip;
96: {
97: register FD **fdpp;
98: register FD *fdp;
99:
100: for (fdpp=u.u_filep; fdpp<&u.u_filep[NUFILE]; fdpp++) {
101: if (*fdpp != NULL)
102: continue;
103: if ((fdp=kalloc(sizeof(FD))) == NULL)
104: return (-1);
105: iopen(ip, mode);
106: if (u.u_error) {
107: kfree(fdp);
108: return (-1);
109: }
110: fdp->f_flag = mode;
111: fdp->f_refc = 1;
112: fdp->f_seek = 0;
113: fdp->f_ip = ip;
114: *fdpp = fdp;
115: return (fdpp-u.u_filep);
116: }
117: u.u_error = EMFILE;
118: return (-1);
119: }
120:
121: /*
122: * Close the given file number.
123: */
124: fdclose(fd)
125: register unsigned fd;
126: {
127: register FD *fdp;
128:
129: if (fd>=NUFILE || (fdp=u.u_filep[fd])==NULL) {
130: u.u_error = EBADF;
131: return;
132: }
133: u.u_filep[fd] = NULL;
134: if (fdp->f_refc == 0)
135: panic("fdclose()");
136: if (--fdp->f_refc == 0) {
137: iclose(fdp->f_ip);
138: kfree(fdp);
139: }
140: }
141:
142: /*
143: * Assuming we have made a copy of the user area, increment the reference
144: * of all open files. (used in fork).
145: */
146: fdadupl()
147: {
148: register FD **fdpp;
149: register FD *fdp;
150:
151: for (fdpp=u.u_filep; fdpp<&u.u_filep[NUFILE]; fdpp++) {
152: if ((fdp=*fdpp) == NULL)
153: continue;
154: fdp->f_refc++;
155: }
156: }
157:
158: /*
159: * Close all open files in the current process.
160: */
161: fdaclose()
162: {
163: register int fd;
164:
165: for (fd=0; fd<NUFILE; fd++) {
166: if (u.u_filep[fd] == NULL)
167: continue;
168: fdclose(fd);
169: }
170: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.