Fork me on GitHub

root/fs/fd.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_new_fd
  2. release_fd
  3. fd_init

   1 /*
   2  * fiwix/fs/fd.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/errno.h>
   9 #include <fiwix/types.h>
  10 #include <fiwix/fs.h>
  11 #include <fiwix/sleep.h>
  12 #include <fiwix/stdio.h>
  13 #include <fiwix/string.h>
  14 
  15 struct fd *fd_table;
  16 
  17 static struct resource fd_resource = { NULL, NULL };
  18 
  19 int get_new_fd(struct inode *i)
  20 {
  21         unsigned int n;
  22 
  23         lock_resource(&fd_resource);
  24 
  25         for(n = 1; n < NR_OPENS; n++) {
  26                 if(fd_table[n].count == 0) {
  27                         memset_b(&fd_table[n], NULL, sizeof(struct fd));
  28                         fd_table[n].inode = i;
  29                         fd_table[n].count = 1;
  30                         unlock_resource(&fd_resource);
  31                         return n;
  32                 }
  33         }
  34 
  35         unlock_resource(&fd_resource);
  36 
  37         return -ENFILE;
  38 }
  39 
  40 void release_fd(unsigned int fd)
  41 {
  42         lock_resource(&fd_resource);
  43         fd_table[fd].count = 0;
  44         unlock_resource(&fd_resource);
  45 }
  46 
  47 void fd_init(void)
  48 {
  49         memset_b(fd_table, NULL, fd_table_size);
  50 }

/* [previous][next][first][last][top][bottom][index][help] */