/*
* Copyright (c) 1987,1997, Prentice Hall
* All rights reserved.
*
* Redistribution and use of the MINIX operating system in source and
* binary forms, with or without modification, are permitted provided
* that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* - Neither the name of Prentice Hall nor the names of the software
* authors or contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup FileSystemImpl
* @{
*/
/**
* @file filedesc.c
* @brief This file contains the procedures that manipulate file descriptors.
*/
#include "fs.h"
#include "file.h"
#include "fproc.h"
#include "inode.h"
/**
* Look for free file descriptor and free filp slots
*/
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;
}
/**
* Look up the filp entry for a given file descriptor
*/
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 */
}
/**
* Find a filp slot that points to a given inode
*/
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;
}
/**
* }
*/