1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*** |
6 | *swprintf.c - print formatted to string |
7 | * |
8 | *Purpose: |
9 | * defines _swprintf(), _swprintf_c and _snwprintf() - print formatted data |
10 | * to string |
11 | * |
12 | *******************************************************************************/ |
13 | |
14 | #include <string.h> |
15 | #include <errno.h> |
16 | #include <limits.h> |
17 | #include "internal_securecrt.h" |
18 | |
19 | #include "mbusafecrt_internal.h" |
20 | |
21 | /*** |
22 | *ifndef _COUNT_ |
23 | *int _swprintf(string, format, ...) - print formatted data to string |
24 | *else |
25 | *ifndef _SWPRINTFS_ERROR_RETURN_FIX |
26 | *int _snwprintf(string, cnt, format, ...) - print formatted data to string |
27 | *else |
28 | *int _swprintf_c(string, cnt, format, ...) - print formatted data to string |
29 | *endif |
30 | *endif |
31 | * |
32 | *Purpose: |
33 | * Prints formatted data to the using the format string to |
34 | * format data and getting as many arguments as called for |
35 | * Sets up a FILE so file i/o operations can be used, make |
36 | * string look like a huge buffer to it, but _flsbuf will |
37 | * refuse to flush it if it fills up. Appends '\0' to make |
38 | * it a true string. _output does the real work here |
39 | * |
40 | * Allocate the 'fake' _iob[] entry statically instead of on |
41 | * the stack so that other routines can assume that _iob[] |
42 | * entries are in are in DGROUP and, thus, are near. |
43 | * |
44 | * We alias swprintf to _swprintf |
45 | * |
46 | *ifdef _COUNT_ |
47 | *ifndef _SWPRINTFS_ERROR_RETURN_FIX |
48 | * The _snwprintf() flavor takes a count argument that is |
49 | * the max number of wide characters that should be written to the |
50 | * user's buffer. |
51 | * We don't expose this function directly in the headers. |
52 | *else |
53 | * The _swprintf_c() flavor does the same thing as the _snwprintf |
54 | * above, but, it also fixes a issue in the return value in the case |
55 | * when there isn't enough space to write the null terminator |
56 | * We don't fix this issue in _snwprintf because of backward |
57 | * compatibility. In new code, however, _snwprintf is #defined to |
58 | * _swprintf_c so users get the fix. |
59 | * |
60 | *endif |
61 | * |
62 | * Multi-thread: (1) Since there is no stream, this routine must |
63 | * never try to get the stream lock (i.e., there is no stream |
64 | * lock either). (2) Also, since there is only one statically |
65 | * allocated 'fake' iob, we must lock/unlock to prevent collisions. |
66 | * |
67 | *Entry: |
68 | * wchar_t *string - pointer to place to put output |
69 | *ifdef _COUNT_ |
70 | * size_t count - max number of wide characters to put in buffer |
71 | *endif |
72 | * wchar_t *format - format string to control data format/number |
73 | * of arguments followed by list of arguments, number and type |
74 | * controlled by format string |
75 | * |
76 | *Exit: |
77 | * returns number of wide characters printed |
78 | * |
79 | *Exceptions: |
80 | * |
81 | *******************************************************************************/ |
82 | |
83 | int __cdecl swprintf_s ( |
84 | wchar_t *string, |
85 | size_t sizeInWords, |
86 | const wchar_t *format, |
87 | ... |
88 | ) |
89 | { |
90 | int ret; |
91 | va_list arglist; |
92 | |
93 | va_start(arglist, format); |
94 | |
95 | ret = vswprintf_s(string, sizeInWords, format, arglist); |
96 | |
97 | va_end(arglist); |
98 | |
99 | return ret; |
100 | } |
101 | |
102 | int __cdecl _snwprintf_s ( |
103 | wchar_t *string, |
104 | size_t sizeInWords, |
105 | size_t count, |
106 | const wchar_t *format, |
107 | ... |
108 | ) |
109 | { |
110 | int ret; |
111 | va_list arglist; |
112 | |
113 | va_start(arglist, format); |
114 | |
115 | ret = _vsnwprintf_s(string, sizeInWords, count, format, arglist); |
116 | |
117 | va_end(arglist); |
118 | |
119 | return ret; |
120 | } |
121 | |