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:  Platform independent fdopen() implementation.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "widechar.h"
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #ifdef __WIDECHAR__
  37.     #include <wctype.h>
  38. #endif
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #ifdef __NT__
  42.     #include <windows.h>
  43. #endif
  44. #include "iomode.h"
  45. #include "rtdata.h"
  46. #include "seterrno.h"
  47. #include "streamio.h"
  48.  
  49.  
  50. extern int      __F_NAME(__open_flags,__wopen_flags)( const CHAR_TYPE *, int * );
  51.  
  52.  
  53. #ifndef __NETWARE__
  54.  
  55. static int __iomode( int handle, int amode )
  56. {
  57.     int flags;
  58.     int __errno;
  59.  
  60. #ifdef __UNIX__
  61.     if( (flags = fcntl( handle, F_GETFL )) == -1 ) {
  62.         return( -1 );
  63.     }
  64.  
  65.     __errno = EOK;
  66.     if( (flags & O_APPEND) && !(amode & _APPEND) ) {
  67.         __errno = EACCES;
  68.     }
  69.     if( (flags & O_ACCMODE) == O_RDONLY ) {
  70.         if( amode & _WRITE ) {
  71.             __errno = EACCES;
  72.         }
  73.     } else if( (flags & O_ACCMODE) == O_WRONLY ) {
  74.         if( amode & _READ ) {
  75.             __errno = EACCES;
  76.         }
  77.     }
  78. #else
  79.     /* make sure the handle has the same text/binary mode */
  80.     flags = __GetIOMode( handle );
  81.     __errno = 0;
  82.     if( (amode ^ flags) & (_BINARY | _APPEND) ) {
  83.         __errno = EACCES;
  84.     }
  85.     if( ( amode & _READ )  && !(flags & _READ) ) {
  86.         __errno = EACCES;
  87.     }
  88.     if( ( amode & _WRITE ) && !(flags & _WRITE) ) {
  89.         __errno = EACCES;
  90.     }
  91. #endif
  92.     if( __errno == EACCES ) {
  93.         __set_errno( __errno );
  94.         return( -1 );
  95.     }
  96.     return( 0 );
  97. }
  98.  
  99. #endif
  100.  
  101. _WCRTLINK FILE *__F_NAME(fdopen,_wfdopen)( int handle, const CHAR_TYPE *access_mode )
  102. {
  103.     unsigned        flags;
  104.     FILE            *fp;
  105.     int             extflags;
  106.  
  107.     if( handle == -1 ) {
  108.         __set_errno( EBADF );           /* 5-dec-90 */
  109.         return( NULL );                 /* 19-apr-90 */
  110.     }
  111.     flags = __F_NAME(__open_flags,__wopen_flags)( access_mode, &extflags );
  112.     if( flags == 0 ) return( NULL );
  113.  
  114. #ifndef __NETWARE__
  115.     /* make sure the handle has the same text/binary mode */
  116.     if( __iomode( handle, flags ) == -1 ) {
  117.         return( NULL );
  118.     }
  119. #endif
  120.     fp = __allocfp( handle );               /* JBS 30-aug-91 */
  121.     if( fp ) {
  122.         fp->_flag &= ~(_READ | _WRITE); /* 2-dec-90 */
  123.         fp->_flag |= flags;
  124.         fp->_cnt = 0;
  125.         _FP_BASE(fp) = NULL;
  126.         fp->_bufsize = 0;                   /* was BUFSIZ JBS 91/05/31 */
  127. #ifndef __NETWARE__
  128.         _FP_ORIENTATION(fp) = _NOT_ORIENTED; /* initial orientation */
  129.         _FP_EXTFLAGS(fp) = extflags;
  130. #endif
  131. #if defined( __NT__ ) || defined( __OS2__ )
  132.         _FP_PIPEDATA(fp).isPipe = 0;    /* not a pipe */
  133. #endif
  134.         fp->_handle = handle;               /* BJS 91-07-23 */
  135.         if( __F_NAME(tolower,towlower)( *access_mode ) == 'a' ) {
  136.             fseek( fp, 0, SEEK_END );
  137.         }
  138.         __chktty( fp );                     /* JBS 31-may-91 */
  139. #if !defined( __UNIX__ ) && !defined( __NETWARE__ )
  140.         __SetIOMode( handle, flags );
  141. #endif
  142.     }
  143.     return( fp );
  144. }
  145.