Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2367 → Rev 2368

/branches/fs/uspace/fs/filedes.c
0,0 → 1,87
/* This file contains the procedures that manipulate file descriptors. */
 
/*
* Methods:
* get_fd: look for free file descriptor and free filp slots
* get_filp: look up the filp entry for a given file descriptor
* find_filp: find a filp slot that points to a given inode
*/
 
 
#include "fs.h"
#include "file.h"
#include "fproc.h"
#include "inode.h"
int get_fd(int start, int *k, filp_t **fpt)
{
/* Look for a free file descriptor and a free filp slot. */
register filp_t *f;
register int i;
*k = -1; /* we need a way to tell if file desc found */
/* Search the fproc fp_filp table for a free file descriptor. */
for (i = start; i < OPEN_MAX; i++) {
if (fp->fp_filp[i] == NIL_FILP) {
/* A file descriptor has been located. */
*k = i;
break;
}
}
/* Check to see if a file descriptor has been found. */
if (*k < 0)
return FS_EMFILE; /* this is why we initialized k to -1 */
/* Now that a file descriptor has been found, look for a free filp slot. */
for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
if (f->filp_count == 0) {
f->filp_mode = R_BIT;
f->filp_pos = 0L;
f->filp_flags = 0;
*fpt = f;
return OK;
}
}
/* If control passes here, the filp table must be full. Report that back. */
return FS_ENFILE;
}
filp_t *get_filp(int fild)
{
/* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
err_code = FS_EBADF;
if (fild < 0 || fild >= OPEN_MAX )
return NIL_FILP;
return(fp->fp_filp[fild]); /* may also be NIL_FILP */
}
filp_t *find_filp(register inode_t *rip)
{
/* Find a filp slot that refers to the inode 'rip' in a way as described.
* Used for determining whether somebody is still interested in.
* Like 'get_fd' it performs its job by linear search through the filp table.
*/
register filp_t *f;
for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
if (f->filp_count != 0 && f->filp_ino == rip){
return f;
}
}
/* If control passes here, the filp wasn't there. Report that back. */
return NIL_FILP;
}