|
|
1.1 root 1: /* $Header: /kernel/kersrc/coh.386/RCS/fd.c,v 1.2 92/08/04 12:30:49 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.2 92/08/04 12:30:49 bin
21: * changed for kernel 59
22: *
23: * Revision 1.3 92/06/10 12:52:39 hal
24: * First record locking changes.
25: *
26: * Revision 1.2 92/01/06 11:59:07 hal
27: * Compile with cc.mwc.
28: *
29: * Revision 1.1 88/03/24 16:13:43 src
30: * Initial revision
31: *
32: */
33: #include <sys/coherent.h>
34: #include <errno.h>
35: #include <fcntl.h>
36: #include <sys/fd.h>
37: #include <sys/inode.h>
38:
39: /*
40: * Given a file number, return the file descriptor.
41: */
42: FD *
43: fdget(fd)
44: register unsigned fd;
45: {
46: register FD *fdp;
47:
48: if (fd>=NUFILE || (fdp=u.u_filep[fd])==NULL) {
49: u.u_error = EBADF;
50: return (NULL);
51: }
52: return (fdp);
53: }
54:
55: /*
56: * Given an inode, and a mode containing permission flags, open the
57: * inode with the appropriate permissions and return a file descriptor
58: * containing it.
59: */
60: fdopen(ip, mode)
61: register INODE *ip;
62: {
63: register FD **fdpp;
64: register FD *fdp;
65:
66: for (fdpp=u.u_filep; fdpp<&u.u_filep[NUFILE]; fdpp++) {
67: if (*fdpp != NULL)
68: continue;
69: if ((fdp=kalloc(sizeof(FD))) == NULL)
70: return (-1);
71: iopen(ip, mode);
72: if (u.u_error) {
73: kfree(fdp);
74: return (-1);
75: }
76: fdp->f_flag = mode;
77: fdp->f_flag2 = 0;
78: fdp->f_refc = 1;
79: fdp->f_seek = 0;
80: fdp->f_ip = ip;
81: *fdpp = fdp;
82: return (fdpp-u.u_filep);
83: }
84: u.u_error = EMFILE;
85: return (-1);
86: }
87:
88: /*
89: * Close the given file number.
90: */
91: fdclose(fd)
92: register unsigned fd;
93: {
94: register FD *fdp;
95: static FLOCK flock = { F_UNLCK, 0, 0, 0 };
96:
97: if (fd>=NUFILE || (fdp=u.u_filep[fd])==NULL) {
98: u.u_error = EBADF;
99: return;
100: }
101: if (fdp->f_ip->i_rl != NULL)
102: rlock(fdp, F_SETLK, &flock); /* delete all record locks */
103: u.u_filep[fd] = NULL;
104: if (fdp->f_refc == 0)
105: panic("fdclose()");
106: if (--fdp->f_refc == 0) {
107: iclose(fdp->f_ip);
108: kfree(fdp);
109: }
110: }
111:
112: /*
113: * Assuming we have made a copy of the user area, increment the reference
114: * of all open files. (used in fork).
115: */
116: fdadupl()
117: {
118: register FD **fdpp;
119: register FD *fdp;
120:
121: for (fdpp=u.u_filep; fdpp<&u.u_filep[NUFILE]; fdpp++) {
122: if ((fdp=*fdpp) == NULL)
123: continue;
124: fdp->f_refc++;
125: }
126: }
127:
128: /*
129: * Close all open files in the current process.
130: */
131: fdaclose()
132: {
133: register int fd;
134:
135: for (fd=0; fd<NUFILE; fd++) {
136: if (u.u_filep[fd] == NULL)
137: continue;
138: fdclose(fd);
139: }
140: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.