source: branches/minix3-book/servers/fs/filedes.c@ 4

Last change on this file since 4 was 4, checked in by Mattia Monga, 13 years ago

Importazione sorgenti libro

File size: 3.0 KB
Line 
1/* This file contains the procedures that manipulate file descriptors.
2 *
3 * The entry points into this file are
4 * get_fd: look for free file descriptor and free filp slots
5 * get_filp: look up the filp entry for a given file descriptor
6 * find_filp: find a filp slot that points to a given inode
7 */
8
9#include "fs.h"
10#include "file.h"
11#include "fproc.h"
12#include "inode.h"
13
14/*===========================================================================*
15 * get_fd *
16 *===========================================================================*/
17PUBLIC int get_fd(int start, mode_t bits, int *k, struct filp **fpt)
18{
19/* Look for a free file descriptor and a free filp slot. Fill in the mode word
20 * in the latter, but don't claim either one yet, since the open() or creat()
21 * may yet fail.
22 */
23
24 register struct filp *f;
25 register int i;
26
27 *k = -1; /* we need a way to tell if file desc found */
28
29 /* Search the fproc fp_filp table for a free file descriptor. */
30 for (i = start; i < OPEN_MAX; i++) {
31 if (fp->fp_filp[i] == NIL_FILP) {
32 /* A file descriptor has been located. */
33 *k = i;
34 break;
35 }
36 }
37
38 /* Check to see if a file descriptor has been found. */
39 if (*k < 0) return(EMFILE); /* this is why we initialized k to -1 */
40
41 /* Now that a file descriptor has been found, look for a free filp slot. */
42 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
43 if (f->filp_count == 0) {
44 f->filp_mode = bits;
45 f->filp_pos = 0L;
46 f->filp_selectors = 0;
47 f->filp_select_ops = 0;
48 f->filp_pipe_select_ops = 0;
49 f->filp_flags = 0;
50 *fpt = f;
51 return(OK);
52 }
53 }
54
55 /* If control passes here, the filp table must be full. Report that back. */
56 return(ENFILE);
57}
58
59/*===========================================================================*
60 * get_filp *
61 *===========================================================================*/
62PUBLIC struct filp *get_filp(fild)
63int fild; /* file descriptor */
64{
65/* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
66
67 err_code = EBADF;
68 if (fild < 0 || fild >= OPEN_MAX ) return(NIL_FILP);
69 return(fp->fp_filp[fild]); /* may also be NIL_FILP */
70}
71
72/*===========================================================================*
73 * find_filp *
74 *===========================================================================*/
75PUBLIC struct filp *find_filp(register struct inode *rip, mode_t bits)
76{
77/* Find a filp slot that refers to the inode 'rip' in a way as described
78 * by the mode bit 'bits'. Used for determining whether somebody is still
79 * interested in either end of a pipe. Also used when opening a FIFO to
80 * find partners to share a filp field with (to shared the file position).
81 * Like 'get_fd' it performs its job by linear search through the filp table.
82 */
83
84 register struct filp *f;
85
86 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
87 if (f->filp_count != 0 && f->filp_ino == rip && (f->filp_mode & bits)){
88 return(f);
89 }
90 }
91
92 /* If control passes here, the filp wasn't there. Report that back. */
93 return(NIL_FILP);
94}
Note: See TracBrowser for help on using the repository browser.