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 fputc() implementation.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "widechar.h"
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include "fileacc.h"
  37. #include "rtdata.h"
  38. #include "seterrno.h"
  39. #include "orient.h"
  40. #ifdef __WIDECHAR__
  41.     #include <mbstring.h>
  42.     #include <wchar.h>
  43. #endif
  44. #include "flush.h"
  45. #include "streamio.h"
  46.  
  47.  
  48. #ifndef __WIDECHAR__
  49.  
  50. _WCRTLINK int fputc( int c, FILE *fp )
  51. {
  52.     int flags;
  53.  
  54.     _ValidFile( fp, EOF );
  55.     _AccessFile( fp );
  56.  
  57.     /*** Deal with stream orientation ***/
  58.     ORIENT_STREAM(fp,EOF);
  59.  
  60.     if( !(fp->_flag & _WRITE) ) {
  61.         __set_errno( EBADF );
  62.         fp->_flag |= _SFERR;
  63.         _ReleaseFile( fp );
  64.         return( EOF );
  65.     }
  66.     if( _FP_BASE(fp) == NULL ) {
  67.         __ioalloc( fp );
  68.     }
  69.     flags = _IONBF;
  70.     if( c == '\n' ) {
  71.         flags = _IONBF | _IOLBF;
  72. #ifndef __UNIX__
  73.         if( !(fp->_flag & _BINARY) ) {
  74.             fp->_flag |= _DIRTY;
  75.             *(char*)fp->_ptr = '\r';   /* '\n' -> '\r''\n' */
  76.             fp->_ptr++;
  77.             fp->_cnt++;
  78.             if( fp->_cnt == fp->_bufsize ) {
  79.                 if( __flush( fp ) ) {
  80.                     _ReleaseFile( fp );
  81.                     return( EOF );
  82.                 }
  83.             }
  84.         }
  85. #endif
  86.     }
  87.     fp->_flag |= _DIRTY;
  88.     *(char *)fp->_ptr = c;
  89.     fp->_ptr++;
  90.     fp->_cnt++;
  91.     if( (fp->_flag & flags) || (fp->_cnt == fp->_bufsize) ) {
  92.         if( __flush( fp ) ) {
  93.             _ReleaseFile( fp );
  94.             return( EOF );
  95.         }
  96.     }
  97.     _ReleaseFile( fp );
  98.     return( (UCHAR_TYPE)c );
  99. }
  100.  
  101.  
  102. #else
  103.  
  104.  
  105. static int __write_wide_char( FILE *fp, wchar_t wc )
  106. /**************************************************/
  107. {
  108.     if( fp->_flag & _BINARY ) {
  109.         /*** Dump the wide character ***/
  110.         return( fwrite( &wc, sizeof( wchar_t ), 1, fp ) );
  111.     } else {
  112.         char            mbc[MB_CUR_MAX];
  113.         int             rc;
  114.  
  115.         /*** Convert the wide character to multibyte form and write it ***/
  116.         rc = wctomb( mbc, wc );
  117.         if( rc > 0 ) {
  118.             return( fwrite( mbc, rc, 1, fp ) );
  119.         } else {
  120.             __set_errno( EILSEQ );
  121.             return( 0 );
  122.         }
  123.     }
  124. }
  125.  
  126.  
  127. _WCRTLINK wint_t fputwc( wint_t c, FILE *fp )
  128. {
  129.     _ValidFile( fp, WEOF );
  130.     _AccessFile( fp );
  131.  
  132.     /*** Deal with stream orientation ***/
  133.     ORIENT_STREAM(fp,WEOF);
  134.  
  135.     /*** Write the character ***/
  136.     if( !__write_wide_char( fp, c ) ) {
  137.         _ReleaseFile( fp );
  138.         return( WEOF );
  139.     } else {
  140.         _ReleaseFile( fp );
  141.         return( c );
  142.     }
  143. }
  144.  
  145. #endif
  146.