Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
553 | serge | 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_s() - safe string fmt'd output. |
||
28 | * |
||
29 | ****************************************************************************/ |
||
30 | |||
31 | |||
32 | #include "variety.h" |
||
33 | #include "saferlib.h" |
||
34 | #include "widechar.h" |
||
35 | #include |
||
36 | #include |
||
37 | #include |
||
38 | #include "printf.h" |
||
39 | |||
40 | |||
41 | struct buf_limit { |
||
42 | CHAR_TYPE *bufptr; |
||
43 | rsize_t chars_output; |
||
44 | rsize_t max_chars; |
||
45 | }; |
||
46 | |||
47 | /* |
||
48 | * buf_putc -- append a character to a string in memory |
||
49 | */ |
||
50 | static slib_callback_t buf_putc; // setup calling convention |
||
51 | static void __SLIB_CALLBACK buf_putc( SPECS __SLIB *specs, int op_char ) |
||
52 | { |
||
53 | struct buf_limit *info; |
||
54 | |||
55 | info = (struct buf_limit *)specs->_dest; |
||
56 | if( specs->_output_count < info->max_chars ) { |
||
57 | *( info->bufptr++ ) = op_char; |
||
58 | info->chars_output++; |
||
59 | } |
||
60 | specs->_output_count++; |
||
61 | } |
||
62 | |||
63 | |||
64 | _WCRTLINK int __F_NAME(snprintf_s,snwprintf_s)( CHAR_TYPE * __restrict s, rsize_t n, |
||
65 | const CHAR_TYPE * __restrict format, ... ) |
||
66 | { |
||
67 | va_list arg; |
||
68 | struct buf_limit info; |
||
69 | const char *msg; |
||
70 | |||
71 | /* First check the critical conditions; if any of those |
||
72 | * is violated, return immediately and don't touch anything. |
||
73 | */ |
||
74 | if( __check_constraint_nullptr_msg( msg, s ) |
||
75 | && __check_constraint_maxsize_msg( msg, n ) |
||
76 | && __check_constraint_zero_msg( msg, n ) ) { |
||
77 | |||
78 | /* The buffer looks okay, try doing something useful */ |
||
79 | if( __check_constraint_nullptr_msg( msg, format ) ) { |
||
80 | int len; |
||
81 | |||
82 | info.bufptr = s; |
||
83 | info.chars_output = 0; |
||
84 | info.max_chars = n - 1; |
||
85 | msg = NULL; |
||
86 | va_start( arg, format ); |
||
87 | len = __F_NAME(__prtf_s,__wprtf_s)( &info, format, arg, &msg, buf_putc ); |
||
88 | va_end( arg ); |
||
89 | if( msg == NULL ) { |
||
90 | /* No rt-constraint violation while formatting */ |
||
91 | s[info.chars_output] = NULLCHAR; |
||
92 | return( len ); |
||
93 | } |
||
94 | } |
||
95 | /* Something went wrong, output buffer will contain empty string */ |
||
96 | *s = NULLCHAR; |
||
97 | } |
||
98 | __rtct_fail( __func__, msg, NULL ); |
||
99 | return( -1 ); |
||
100 | }> |