Subversion Repositories Kolibri OS

Rev

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

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Platform independent __allocfp() implementation.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include "liballoc.h"
  37. #include "fileacc.h"
  38. #include "rtdata.h"
  39. #include "seterrno.h"
  40. #include "streamio.h"
  41.  
  42.  
  43. #define KEEP_FLAGS (_READ | _WRITE | _DYNAMIC)
  44.  
  45. FILE *__allocfp( int handle )
  46. {
  47.     FILE                *end;
  48.     FILE                *fp;
  49.     __stream_link       *link;
  50.     unsigned            flags;
  51.  
  52.     handle = handle;
  53.     _AccessIOB();
  54.     /* Try and take one off the recently closed list */
  55.     link = _RWD_cstream;
  56.     if( link != NULL ) {
  57.         _RWD_cstream = link->next;
  58.         fp = link->stream;
  59.         flags = (fp->_flag & KEEP_FLAGS) | (_READ | _WRITE);
  60.         goto got_one;
  61.     }
  62.     /* See if there is a static FILE structure available. */
  63.     end = &_RWD_iob[_NFILES];
  64.     for( fp = _RWD_iob; fp < end; ++fp ) {
  65.         if( (fp->_flag & (_READ | _WRITE)) == 0 ) {
  66.             link = lib_malloc( sizeof( __stream_link ) );
  67.             if( link == NULL )
  68.                 goto no_mem;
  69.             flags = _READ | _WRITE;
  70.             goto got_one;
  71.         }
  72.     }
  73.     /* Allocate a new dynamic structure */
  74.     flags = _DYNAMIC | _READ | _WRITE;
  75.     link = lib_malloc( sizeof( __stream_link ) + sizeof( FILE ) );
  76.     if( link == NULL )
  77.         goto no_mem;
  78.     fp = (FILE *)(link + 1);
  79. got_one:
  80.     memset( fp, 0, sizeof( *fp ) );
  81.     fp->_flag = flags;
  82.     link->stream = fp;
  83.     link->stream->_link = link;     /* point back to link structure */
  84.     link->next = _RWD_ostream;
  85.     _RWD_ostream = link;
  86.     _ReleaseIOB();
  87.     return( fp );
  88. no_mem:
  89.     __set_errno( ENOMEM );
  90.     _ReleaseIOB();
  91.     return( NULL );     /* no free slots */
  92. }
  93.