Subversion Repositories HelenOS

Rev

Rev 2404 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 1987,1997, Prentice Hall
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use of the MINIX operating system in source and
  6.  * binary forms, with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  
  12.  * - Redistributions in binary form must reproduce the above
  13.  *   copyright notice, this list of conditions and the following
  14.  *   disclaimer in the documentation and/or other materials provided
  15.  *   with the distribution.
  16.  
  17.  * - Neither the name of Prentice Hall nor the names of the software
  18.  *   authors or contributors may be used to endorse or promote
  19.  *   products derived from this software without specific prior
  20.  *   written permission.
  21.  
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  23.  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26.  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  27.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  32.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  33.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  */
  35.  
  36. /** @addtogroup FileSystemImpl
  37. * @{
  38. */
  39.  
  40. /**
  41.  * @file    filedesc.c
  42.  * @brief    This file contains the procedures that manipulate file descriptors.
  43.  */
  44.  
  45. #include "fs.h"
  46. #include "file.h"
  47. #include "fproc.h"
  48. #include "inode.h"
  49.    
  50. /**
  51.  * Look for free file descriptor and free filp slots
  52.  */    
  53. int get_fd(int start, int *k, filp_t **fpt)
  54. {
  55.     /* Look for a free file descriptor and a free filp slot. */
  56.    
  57.     register filp_t *f;
  58.     register int i;
  59.    
  60.     *k = -1;                      /* we need a way to tell if file desc found */
  61.    
  62.     /* Search the fproc fp_filp table for a free file descriptor. */
  63.     for (i = start; i < OPEN_MAX; i++) {
  64.         if (fp->fp_filp[i] == NIL_FILP) {
  65.                 /* A file descriptor has been located. */
  66.                 *k = i;
  67.                     break;
  68.         }
  69.     }
  70.    
  71.     /* Check to see if a file descriptor has been found. */
  72.     if (*k < 0)
  73.         return FS_EMFILE;   /* this is why we initialized k to -1 */
  74.    
  75.     /* Now that a file descriptor has been found, look for a free filp slot. */
  76.     for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
  77.         if (f->filp_count == 0) {
  78.                 f->filp_mode = R_BIT;
  79.             f->filp_pos = 0L;
  80.                     f->filp_flags = 0;
  81.                     *fpt = f;
  82.                     return OK;
  83.             }
  84.     }
  85.    
  86.     /* If control passes here, the filp table must be full.  Report that back. */
  87.     return FS_ENFILE;
  88. }
  89.    
  90. /**
  91.  * Look up the filp entry for a given file descriptor
  92.  */
  93. filp_t *get_filp(int fild)
  94. {
  95.    
  96.     /* See if 'fild' refers to a valid file descr.  If so, return its filp ptr. */
  97.    
  98.     err_code = FS_EBADF;
  99.     if (fild < 0 || fild >= OPEN_MAX )
  100.         return NIL_FILP;
  101.      
  102.     return(fp->fp_filp[fild]);    /* may also be NIL_FILP */
  103. }
  104.    
  105.    
  106. /**
  107.  * Find a filp slot that points to a given inode
  108.  */
  109. filp_t *find_filp(register inode_t *rip)
  110. {
  111.    
  112.     /* Find a filp slot that refers to the inode 'rip' in a way as described.
  113.      * Used for determining whether somebody is still interested in.
  114.      * Like 'get_fd' it performs its job by linear search through the filp table.
  115.      */
  116.    
  117.     register filp_t *f;
  118.    
  119.     for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
  120.         if (f->filp_count != 0 && f->filp_ino == rip){
  121.                 return f;
  122.             }
  123.     }
  124.    
  125.     /* If control passes here, the filp wasn't there.  Report that back. */
  126.     return NIL_FILP;
  127. }
  128.  
  129.  
  130. /**
  131.  * }
  132.  */
  133.  
  134.