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:  Implementation of __fprtf_s() - safe formatted output.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "saferlib.h"
  34. #include "widechar.h"
  35. #include <stdio.h>
  36. #include <stdarg.h>
  37. #include "rtdata.h"
  38. #include "fileacc.h"
  39. #include "printf.h"
  40. #include "fprtf_s.h"
  41. #include "orient.h"
  42.  
  43. extern  void    __ioalloc( FILE * );
  44. extern  int     __flush( FILE * );
  45.  
  46.  
  47. /*
  48.  * file_putc -- write a character to a file
  49.  */
  50. static slib_callback_t file_putc; // setup calling convention
  51. static void __SLIB_CALLBACK file_putc( SPECS __SLIB *specs, int op_char )
  52. {
  53.     __F_NAME(fputc,fputwc)( op_char, (FILE *)specs->_dest );
  54.     specs->_output_count++;
  55. }
  56.  
  57.  
  58. int __F_NAME(__fprtf_s,__fwprtf_s)( FILE * __restrict stream,
  59.                          const CHAR_TYPE * __restrict format, va_list arg )
  60. {
  61.     int             not_buffered;
  62.     int             amount_written;
  63.     unsigned        oflag;
  64.     const char      *msg = NULL;    /* doubles as error indicator */
  65.  
  66.     /* Check for runtime-constraints before grabbing file lock */
  67.     /* stream   not null */
  68.     /* format   not null */
  69.     if( __check_constraint_nullptr_msg( msg, stream )  &&
  70.         __check_constraint_nullptr_msg( msg, format ) ) {
  71.  
  72.         _ValidFile( stream, 0 );
  73.         _AccessFile( stream );
  74.  
  75.         /*** Deal with stream orientation ***/
  76.         ORIENT_STREAM(stream,0);
  77.  
  78.         oflag = stream->_flag & (_SFERR | _EOF);
  79.         stream->_flag &= ~(_SFERR | _EOF);
  80.  
  81.         if( _FP_BASE(stream) == NULL ) {
  82.             __ioalloc( stream );            /* allocate buffer */
  83.         }
  84.         not_buffered = 0;
  85.         if( stream->_flag & _IONBF ) {
  86.             not_buffered = 1;
  87.             stream->_flag &= ~_IONBF;
  88.             stream->_flag |= _IOFBF;
  89.         }
  90.         amount_written = __F_NAME(__prtf_s,__wprtf_s)( stream, format, arg, &msg, file_putc );
  91.         if( not_buffered ) {
  92.             stream->_flag &= ~_IOFBF;
  93.             stream->_flag |= _IONBF;
  94.             __flush( stream );
  95.         }
  96.         if( ferror( stream ) )
  97.             amount_written = -1;
  98.         stream->_flag |= oflag;
  99.  
  100.         _ReleaseFile( stream );
  101.     }
  102.     if( msg != NULL ) {
  103.         /* There was a constraint violation; call the handler */
  104.         __rtct_fail( __func__, msg, NULL );
  105.         amount_written = -1;
  106.     }
  107.     return( amount_written );
  108. }
  109.