Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <libc/file.h>
  6. #include <libc/local.h>
  7. #include <libc/stdiohk.h>
  8.  
  9. FILE *__alloc_file(void)
  10. {
  11.   __file_rec *fr = __file_rec_list;
  12.   __file_rec **last_fr = &__file_rec_list;
  13.   FILE *rv=0;
  14.   int i;
  15.  
  16.   /* Try to find an empty slot */
  17.   while (fr)
  18.   {
  19.     last_fr = &(fr->next);
  20.  
  21.     /* If one of the existing slots is available, return it */
  22.     for (i=0; i<fr->count; i++)
  23.       if (fr->files[i]->_flag == 0)
  24.         return fr->files[i];
  25.  
  26.     /* If this one is full, go to the next */
  27.     if (fr->count == __FILE_REC_MAX)
  28.       fr = fr->next;
  29.     else
  30.       /* it isn't full, we can add to it */
  31.       break;
  32.   }
  33.   if (!fr)
  34.   {
  35.     /* add another one to the end, make it empty */
  36.     fr = *last_fr = (__file_rec *)malloc(sizeof(__file_rec));
  37.     if (fr == 0)
  38.       return 0;
  39.     fr->next = 0;
  40.     fr->count = 0;
  41.   }
  42.   /* fr is a pointer to a rec with empty slots in it */
  43.   rv = fr->files[fr->count] = (FILE *)malloc(sizeof(FILE));
  44.   if (rv == 0)
  45.     return 0;
  46.   memset(rv, 0, sizeof(FILE));
  47.   fr->count ++;
  48.   return rv;
  49. }
  50.