~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Minix Cross Reference
Minix/fs/filedes.c


  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  *===========================================================================*/
 17 PUBLIC int get_fd(start, bits, k, fpt)
 18 int start;                      /* start of search (used for F_DUPFD) */
 19 mode_t bits;                    /* mode of the file to be created (RWX bits) */
 20 int *k;                         /* place to return file descriptor */
 21 struct filp **fpt;              /* place to return filp slot */
 22 {
 23 /* Look for a free file descriptor and a free filp slot.  Fill in the mode word
 24  * in the latter, but don't claim either one yet, since the open() or creat()
 25  * may yet fail.
 26  */
 27 
 28   register struct filp *f;
 29   register int i;
 30 
 31   *k = -1;                      /* we need a way to tell if file desc found */
 32 
 33   /* Search the fproc fp_filp table for a free file descriptor. */
 34   for (i = start; i < OPEN_MAX; i++) {
 35         if (fp->fp_filp[i] == NIL_FILP) {
 36                 /* A file descriptor has been located. */
 37                 *k = i;
 38                 break;
 39         }
 40   }
 41 
 42   /* Check to see if a file descriptor has been found. */
 43   if (*k < 0) return(EMFILE);   /* this is why we initialized k to -1 */
 44 
 45   /* Now that a file descriptor has been found, look for a free filp slot. */
 46   for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
 47         if (f->filp_count == 0) {
 48                 f->filp_mode = bits;
 49                 f->filp_pos = 0L;
 50                 f->filp_flags = 0;
 51                 *fpt = f;
 52                 return(OK);
 53         }
 54   }
 55 
 56   /* If control passes here, the filp table must be full.  Report that back. */
 57   return(ENFILE);
 58 }
 59 
 60 
 61 /*===========================================================================*
 62  *                              get_filp                                     *
 63  *===========================================================================*/
 64 PUBLIC struct filp *get_filp(fild)
 65 int fild;                       /* file descriptor */
 66 {
 67 /* See if 'fild' refers to a valid file descr.  If so, return its filp ptr. */
 68 
 69   err_code = EBADF;
 70   if (fild < 0 || fild >= OPEN_MAX ) return(NIL_FILP);
 71   return(fp->fp_filp[fild]);    /* may also be NIL_FILP */
 72 }
 73 
 74 
 75 /*===========================================================================*
 76  *                              find_filp                                    *
 77  *===========================================================================*/
 78 PUBLIC struct filp *find_filp(rip, bits)
 79 register struct inode *rip;     /* inode referred to by the filp to be found */
 80 Mode_t bits;                    /* mode of the filp to be found (RWX bits) */
 81 {
 82 /* Find a filp slot that refers to the inode 'rip' in a way as described
 83  * by the mode bit 'bits'. Used for determining whether somebody is still
 84  * interested in either end of a pipe.  Also used when opening a FIFO to
 85  * find partners to share a filp field with (to shared the file position).
 86  * Like 'get_fd' it performs its job by linear search through the filp table.
 87  */
 88 
 89   register struct filp *f;
 90 
 91   for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
 92         if (f->filp_count != 0 && f->filp_ino == rip && (f->filp_mode & bits)){
 93                 return(f);
 94         }
 95   }
 96 
 97   /* If control passes here, the filp wasn't there.  Report that back. */
 98   return(NIL_FILP);
 99 }
100 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.