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:  Handle manager routines.
  28. *
  29. ****************************************************************************/
  30.  
  31. #include <reent.h>
  32. #include <unistd.h>
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <fcntl.h>
  39.  
  40. #define _NFILES   20
  41. #define _DYNAMIC  0x4000 /* FILE is dynamically allocated   */
  42.  
  43. #define _READ   0x0001  /* file opened for reading */
  44. #define _WRITE  0x0002  /* file opened for writing */
  45.  
  46. #define NULL_HANDLE  (int)-1
  47. #define DUMMY_HANDLE (int)-2
  48. #define INVALID_HANDLE_VALUE (int) -1
  49.  
  50. #define _AccessIOB()
  51. #define _ReleaseIOB()
  52.  
  53. #undef __getOSHandle
  54.  
  55. void __ChkTTYIOMode( int handle );
  56.  
  57. void  __grow_iomode( int num );
  58. int   debugwrite(const char *path,const void *buff,
  59.                  size_t offset, size_t count, size_t *writes);
  60.  
  61.  
  62. int _fmode;
  63.  
  64. #define NUM_STD_STREAMS  3
  65. #define _ISTTY           0x2000  /* is console device */
  66.  
  67. unsigned __init_mode[_NFILES] = { /* file mode information (flags) */
  68.         _READ,          /* stdin */
  69.         _WRITE,         /* stdout */
  70.         _WRITE          /* stderr */
  71. };
  72.  
  73. unsigned *__io_mode = __init_mode;      /* initially points to static array */
  74.  
  75. unsigned  __NFiles   = _NFILES;          /* maximum # of files we can open */
  76.  
  77. unsigned  __NHandles = 0;
  78.  
  79. int *__OSHandles = NULL;
  80.  
  81.  
  82. static __file_handle
  83. stdin_handle = {
  84.                     NULL,
  85.                     0,
  86.                     NULL
  87.                 };
  88.  
  89. static __file_handle
  90. stdout_handle =
  91.                 {
  92.                     NULL,
  93.                     0,
  94.                     debugwrite
  95.                 };
  96.  
  97.  
  98. static __file_handle
  99. stderr_handle =
  100.                 {
  101.                     NULL,
  102.                     0,
  103.                     debugwrite
  104.                 };
  105.  
  106.  
  107. unsigned __growPOSIXHandles( unsigned num )
  108. {
  109.     int       *new2;
  110.     unsigned   i;
  111.  
  112.     if( num > __NHandles )
  113.     {
  114.         if( __OSHandles == NULL )
  115.         {
  116.             new2 = malloc( num * sizeof( int ) );
  117.         }
  118.         else
  119.         {
  120.             new2 = realloc( __OSHandles, num * sizeof( int ) );
  121.         }
  122.         if( new2 == NULL )
  123.         {
  124. //            __set_errno( ENOMEM );
  125.             num = __NHandles;
  126.         }
  127.         else
  128.         {
  129.             for( i = __NHandles; i < num; i++ )
  130.             {
  131.                 new2[ i ] = NULL_HANDLE;
  132.             }
  133.             __OSHandles = new2;
  134.             __NHandles = num;
  135.         }
  136.     }
  137.     return( __NHandles );
  138. }
  139.  
  140. int __allocPOSIXHandle( int hdl )
  141. {
  142.     int i;
  143.  
  144.     for( i = 0; i < __NHandles; i++ )
  145.     {
  146.         if( __OSHandles[i] == NULL_HANDLE ) break;
  147.     }
  148.     if( i >= __NHandles )
  149.     {
  150.                                 // 20 -> (20+10+1) -> 31
  151.                                 // 31 -> (31+15+1) -> 47
  152.                                 // 47 -> (47+23+1) -> 71
  153.         __growPOSIXHandles( i + (i >> 1) + 1 );
  154.         // keep iomode array in sync
  155.         if( __NHandles > __NFiles ) __grow_iomode( __NHandles );
  156.         for( ; i < __NHandles; i++ )
  157.         {
  158.             if( __OSHandles[i] == NULL_HANDLE ) break;
  159.         }
  160.     }
  161.     if( i >= __NHandles )
  162.     {
  163.         i = -1;
  164.     } else {
  165.         __OSHandles[i] = hdl;
  166.     }
  167.     return( i );
  168. }
  169.  
  170. void __freePOSIXHandle( int hid )
  171. {
  172.     __OSHandles[ hid ] = NULL_HANDLE;
  173. }
  174.  
  175.  
  176. int __getOSHandle( int hid )
  177. {
  178.     return( __OSHandles[ hid ] );
  179. }
  180.  
  181.  
  182. int __setOSHandle( unsigned hid, int hdl )
  183. {
  184.     // call the Win32 API for a standard file handle
  185.     switch( hid ) {
  186.         case STDIN_FILENO:
  187. //        SetStdHandle( STD_INPUT_HANDLE, hdl );
  188.         break;
  189.     case STDOUT_FILENO:
  190. //        SetStdHandle( STD_OUTPUT_HANDLE, hdl );
  191.         break;
  192.     case STDERR_FILENO:
  193. //        SetStdHandle( STD_ERROR_HANDLE, hdl );
  194.         break;
  195.     }
  196.     if( hid < __NHandles )
  197.     {
  198.         __OSHandles[ hid ] = hdl;
  199.     }
  200.     else
  201.     {
  202.         hid = (unsigned)-1;     // this should never happen
  203.     }
  204.     return( hid );
  205. }
  206.  
  207. // called from library startup code
  208.  
  209.  
  210. void __initPOSIXHandles( void )
  211. {
  212.     int h;
  213.  
  214.     _fmode = O_BINARY;
  215.  
  216.     __growPOSIXHandles( __NFiles );
  217.  
  218.     h = (int)&stdin_handle;
  219.     __allocPOSIXHandle( h );        // should return 0==STDIN_FILENO
  220.     h = (int)&stdout_handle;
  221.     __allocPOSIXHandle( h );        // should return 1==STDOUT_FILENO
  222.     h = (int)&stderr_handle;
  223.     __allocPOSIXHandle( h );        // should return 3==STDERR_FILENO
  224. }
  225.  
  226. /*
  227. static void __finiPOSIXHandles( void )
  228. {
  229.     if( __OSHandles != NULL ) {
  230.         free( __OSHandles );
  231.         __OSHandles = NULL;
  232.     }
  233.     if( __FakeHandles != NULL )
  234.     {
  235.         int i;
  236.         for( i = 0 ; i < __topFakeHandle ; i++ )
  237.         {
  238.           //  CloseHandle( __FakeHandles[i] );
  239.         }
  240.         free( __FakeHandles );
  241.         __FakeHandles = 0;
  242.     }
  243. }
  244. */
  245.  
  246.  
  247. void __set_handles( int num )
  248. {
  249.     __NHandles = num;
  250. }
  251.  
  252. int _grow_handles( int num )
  253. {
  254.     if( num > __NHandles )
  255.     {
  256.         num = __growPOSIXHandles( num );
  257.  
  258.         if( num > __NFiles ) {
  259.             __grow_iomode( num );   // sets new __NFiles if successful
  260.         }
  261.         __NHandles = num;
  262.     }
  263.     return( __NHandles );
  264. }
  265.  
  266.  
  267.  
  268. static  unsigned _init_NFiles;          // original __NFiles value;
  269.  
  270. void __grow_iomode( int num )
  271. {
  272.     unsigned    *new;
  273.  
  274.     _AccessIOB();
  275.     if( __io_mode == __init_mode )
  276.     {
  277.         _init_NFiles = __NFiles;
  278.         new = (unsigned *) malloc( num * sizeof( unsigned ) );
  279.         if( new != NULL ) {
  280.             memcpy( new, __init_mode, __NFiles * sizeof(unsigned) );
  281.         }
  282.     }
  283.     else
  284.     {
  285.         new = (unsigned *) realloc( __io_mode, num * sizeof( unsigned ) );
  286.     }
  287.     if( new == NULL )
  288.     {
  289. //        __set_errno( ENOMEM );
  290.     }
  291.     else
  292.     {
  293.         memset( &new[__NFiles], 0, (num-__NFiles)*sizeof(unsigned) );
  294.         __io_mode = new;
  295.         __NFiles = num;
  296.     }
  297.     _ReleaseIOB();
  298. }
  299.  
  300. void __shrink_iomode( void )
  301. {
  302.     _AccessIOB();
  303.     // free any malloc'd iomode array
  304.     if( __io_mode != __init_mode )
  305.     {
  306.         free( __io_mode );
  307.         __io_mode = __init_mode;
  308.         __NFiles = _init_NFiles;
  309.     }
  310.     _ReleaseIOB();
  311. }
  312.  
  313. #define _INITIALIZED    _DYNAMIC
  314.  
  315. signed __SetIOMode( int handle, unsigned value )
  316. {
  317.     int         i;
  318.  
  319.     if( handle >= __NFiles )
  320.     {
  321.         i = __NFiles;           // 20 -> (20+10+1) -> 31
  322.                                 // 31 -> (31+15+1) -> 47
  323.                                 // 47 -> (47+23+1) -> 71
  324.         __grow_iomode( i + (i > 1) + 1 );
  325.     }
  326.     if( handle >= __NFiles )
  327.     {
  328.         // return an error indication (errno should be set to ENOMEM)
  329.         return( -1 );
  330.     }
  331.     else
  332.     {
  333.         if( value != 0 )
  334.         {
  335.             __ChkTTYIOMode( handle );
  336.             __io_mode[handle] = value | _INITIALIZED;
  337.         }
  338.         else
  339.         {
  340.             __io_mode[handle] = value;    /* we're closing it; smite _INITIALIZED */
  341.         }
  342.         return( handle );
  343.     }
  344. }
  345.  
  346. int _isatty( int hid )
  347. {
  348.     return( 0 );
  349. }
  350.  
  351. void __ChkTTYIOMode( int handle )
  352. {
  353.     if( handle < NUM_STD_STREAMS && !(__io_mode[handle] & _INITIALIZED) )
  354.     {
  355.         __io_mode[handle] |= _INITIALIZED;
  356.         if( _isatty( handle ) )
  357.         {
  358.             __io_mode[handle] |= _ISTTY;
  359.         }
  360.     }
  361. }
  362.  
  363. unsigned __GetIOMode( int handle )
  364. {
  365.     if( handle >= __NFiles )
  366.     {
  367.         return( 0 );
  368.     }
  369.     return( __io_mode[handle] );
  370. };
  371.  
  372. void __SetIOMode_nogrow( int handle, unsigned value )
  373. {
  374.     if( handle < __NFiles )
  375.     {
  376.         __io_mode[handle] = value;    /* we're closing it; smite _INITIALIZED */
  377.     }
  378. }
  379.  
  380.