Subversion Repositories Kolibri OS

Rev

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:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
  28. *               DESCRIBE IT HERE!
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #include "variety.h"
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #if defined(__NT__)
  37.  //   #include <windows.h>
  38. #endif
  39. #include <unistd.h>
  40. #include "fcntl.h"
  41. #include "dos.h"
  42. #include "dosfunc.h"
  43. #include "fileacc.h"
  44. #include "rtcheck.h"
  45. #include "rtdata.h"
  46. #include "iomode.h"
  47. #include "seterrno.h"
  48.  
  49. _WCRTLINK int setmode( int handle, int mode )
  50. {
  51.     unsigned        iomode_flags;
  52.     unsigned        old_mode;
  53.     __stream_link   *link;
  54.     FILE            *fp;
  55.  
  56.     __handle_check( handle, -1 );
  57.  
  58.     iomode_flags = __GetIOMode( handle );
  59.     if( iomode_flags == 0 ) {
  60.         __set_errno( EBADF );
  61.         return( -1 );
  62.     }
  63.     old_mode = (iomode_flags & _BINARY) ? O_BINARY : O_TEXT;
  64.     if( mode != old_mode ) {
  65.         if( mode == O_BINARY  ||  mode == O_TEXT ) {
  66.             iomode_flags &= ~ _BINARY;
  67.             if( mode == O_BINARY ) {
  68.                 iomode_flags |= _BINARY;
  69.             }
  70.             __SetIOMode( handle, iomode_flags );
  71.             _AccessFileH( handle );
  72.             for( link = _RWD_ostream; link != NULL; link = link->next ) {
  73.                 fp = link->stream;
  74.                 if( fp->_flag != 0 ) {      /* if file is open */
  75.                     if( fileno( fp ) == handle ) {
  76.                         fp->_flag &= ~ _BINARY;
  77.                         if( mode == O_BINARY ) {
  78.                             fp->_flag |= _BINARY;
  79.                         }
  80.                         break;
  81.                     }
  82.                 }
  83.             }
  84.             _ReleaseFileH( handle );
  85.         } else {
  86.             __set_errno( EINVAL );
  87.             old_mode = -1;
  88.         }
  89.     }
  90.     return( old_mode );
  91. }
  92.