Subversion Repositories HelenOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2368 konopa 1
/* This file contains the procedures that manipulate file descriptors. */
2
 
3
/*   
4
 * Methods:
5
 * get_fd:    look for free file descriptor and free filp slots
6
 * get_filp:  look up the filp entry for a given file descriptor
7
 * find_filp: find a filp slot that points to a given inode
8
 */
9
 
10
 
11
#include "fs.h"
12
#include "file.h"
13
#include "fproc.h"
14
#include "inode.h"
15
 
16
 
17
int get_fd(int start, int *k, filp_t **fpt)
18
{
19
    /* Look for a free file descriptor and a free filp slot. */
20
 
21
    register filp_t *f;
22
    register int i;
23
 
24
    *k = -1;                      /* we need a way to tell if file desc found */
25
 
26
    /* Search the fproc fp_filp table for a free file descriptor. */
27
    for (i = start; i < OPEN_MAX; i++) {
28
        if (fp->fp_filp[i] == NIL_FILP) {
29
                /* A file descriptor has been located. */
30
                *k = i;
31
                    break;
32
        }
33
    }
34
 
35
    /* Check to see if a file descriptor has been found. */
36
    if (*k < 0)
37
        return FS_EMFILE;   /* this is why we initialized k to -1 */
38
 
39
    /* Now that a file descriptor has been found, look for a free filp slot. */
40
    for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
41
        if (f->filp_count == 0) {
42
                f->filp_mode = R_BIT;
43
            f->filp_pos = 0L;
44
                    f->filp_flags = 0;
45
                    *fpt = f;
46
                    return OK;
47
            }
48
    }
49
 
50
    /* If control passes here, the filp table must be full.  Report that back. */
51
    return FS_ENFILE;
52
}
53
 
54
 
55
filp_t *get_filp(int fild)
56
{
57
 
58
    /* See if 'fild' refers to a valid file descr.  If so, return its filp ptr. */
59
 
60
    err_code = FS_EBADF;
61
    if (fild < 0 || fild >= OPEN_MAX )
62
        return NIL_FILP;
63
 
64
    return(fp->fp_filp[fild]);    /* may also be NIL_FILP */
65
}
66
 
67
 
68
filp_t *find_filp(register inode_t *rip)
69
{
70
 
71
    /* Find a filp slot that refers to the inode 'rip' in a way as described.
72
     * Used for determining whether somebody is still interested in.
73
     * Like 'get_fd' it performs its job by linear search through the filp table.
74
     */
75
 
76
    register filp_t *f;
77
 
78
    for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
79
        if (f->filp_count != 0 && f->filp_ino == rip){
80
                return f;
81
            }
82
    }
83
 
84
    /* If control passes here, the filp wasn't there.  Report that back. */
85
    return NIL_FILP;
86
}
87