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