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 snprintf().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "widechar.h"
  34. #include <stdio.h>
  35. #include <stdarg.h>
  36. #include "printf.h"
  37.  
  38. struct buf_limit {
  39.     CHAR_TYPE   *bufptr;
  40.     size_t      bufsize;
  41. };
  42.  
  43. /*
  44.  * buf_putc -- append a character to a string in memory
  45.  */
  46. static slib_callback_t buf_putc; // setup calling convention
  47. static void __SLIB_CALLBACK buf_putc( SPECS __SLIB *specs, int op_char )
  48. {
  49.     struct buf_limit    *bufinfo;
  50.  
  51.     bufinfo = (struct buf_limit *)specs->_dest;
  52.     if( specs->_output_count < bufinfo->bufsize ) {
  53.         *( bufinfo->bufptr++ ) = op_char;
  54.     }
  55.     specs->_output_count++;
  56. }
  57.  
  58. /*
  59.  * buf_count_putc -- only count characters to be output
  60.  */
  61. static slib_callback_t buf_count_putc; // setup calling convention
  62. static void __SLIB_CALLBACK buf_count_putc( SPECS __SLIB *specs, int op_char )
  63. {
  64.     specs->_output_count++;
  65. }
  66.  
  67.  
  68. _WCRTLINK int __F_NAME(vsnprintf,vsnwprintf) ( CHAR_TYPE *s, size_t bufsize,
  69.                 const CHAR_TYPE *format, va_list arg)
  70. {
  71.     register int            len;
  72.     auto struct buf_limit   bufinfo;
  73.  
  74.     bufinfo.bufptr  = s;
  75.     bufinfo.bufsize = bufsize - 1;
  76.     if( bufsize == 0 )
  77.         len = __F_NAME(__prtf,__wprtf)( &bufinfo, format, arg, buf_count_putc );
  78.     else {
  79.         len = __F_NAME(__prtf,__wprtf)( &bufinfo, format, arg, buf_putc );
  80.         s[(( len >= 0 ) && ( len < bufsize )) ? len : bufsize - 1] = '\0';
  81.     }
  82.     return( len );
  83. }
  84.  
  85.  
  86. _WCRTLINK int __F_NAME(snprintf,snwprintf) ( CHAR_TYPE *dest, size_t bufsize,
  87.                 const CHAR_TYPE *format, ... )
  88. {
  89.     auto va_list    args;
  90.  
  91.     va_start( args, format );
  92.     return( __F_NAME(vsnprintf,vsnwprintf)( dest, bufsize, format, args ) );
  93. }
  94.