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 sprintf_s() - safe formatted string 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 <wchar.h>
  38. #include "printf.h"
  39.  
  40.  
  41. /*
  42.  * mem_putc -- append a character to a string in memory, with overflow check
  43.  */
  44.  
  45. struct vsprtf_s_buf {
  46.     CHAR_TYPE   *bufptr;
  47.     rsize_t     chars_output;
  48.     rsize_t     max_chars;
  49. };
  50.  
  51. static slib_callback_t mem_putc; // set up calling convention
  52. static void __SLIB_CALLBACK mem_putc( SPECS __SLIB *specs, int op_char )
  53. {
  54.     struct vsprtf_s_buf     *info;
  55.  
  56.     info = (struct vsprtf_s_buf *)specs->_dest;
  57.     if( info->chars_output <= info->max_chars ) {
  58.         *( info->bufptr++ ) = op_char;
  59.         specs->_output_count++;
  60.         info->chars_output++;
  61.     }
  62. }
  63.  
  64. _WCRTLINK int __F_NAME(sprintf_s,swprintf_s)( CHAR_TYPE * __restrict s, rsize_t n,
  65.                                         const CHAR_TYPE * __restrict format, ... )
  66. {
  67.     va_list                 arg;
  68.     struct vsprtf_s_buf     info;
  69.     const char              *msg;
  70.     int                     rc = 0;
  71.  
  72.     /* First check the critical conditions; if any of those
  73.      * is violated, return immediately and don't touch anything.
  74.      */
  75.     if( __check_constraint_nullptr_msg( msg, s )
  76.      && __check_constraint_maxsize_msg( msg, n )
  77.      && __check_constraint_zero_msg( msg, n ) ) {
  78.  
  79.         /* The buffer looks okay, try doing something useful */
  80.         if( __check_constraint_nullptr_msg( msg, format ) ) {
  81.             info.bufptr       = s;
  82.             info.chars_output = 0;
  83.             info.max_chars    = n - 1;
  84.             msg = NULL;
  85.  
  86.             va_start( arg, format );
  87.             __F_NAME(__prtf_s,__wprtf_s)( &info, format, arg, &msg, mem_putc );
  88.             va_end( arg );
  89.  
  90.             if( msg == NULL ) {
  91.                 if( info.chars_output <= info.max_chars ) {
  92.                     s[info.chars_output] = NULLCHAR;
  93.                     return( info.chars_output );
  94.                 }
  95.                 /* If we got here, the output buffer was too small */
  96.                 msg = "n < chars_output";
  97. #ifdef __WIDECHAR__
  98.                 rc = -1;    /* Return value for swprintf_s is different! */
  99. #endif
  100.             }
  101.         }
  102.         /* Something went wrong, output buffer will contain empty string */
  103.         *s = NULLCHAR;
  104.     }
  105.     __rtct_fail( __func__, msg, NULL );
  106.     return( rc );
  107. }
  108.