ReactOS 0.4.15-dev-5672-gf73ac17
_sxprintf.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: GNU GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS crt library
4 * FILE: lib/sdk/crt/printf/_sxprintf.c
5 * PURPOSE: Implementation of swprintf
6 * PROGRAMMER: Timo Kreuzer
7 */
8
9#include <stdio.h>
10#include <stdarg.h>
11#include <limits.h>
12#include <tchar.h>
13#if IS_SECAPI
14#include <internal/safecrt.h>
15#endif
16
17#ifdef _UNICODE
18#define _tstreamout wstreamout
19#else
20#define _tstreamout streamout
21#endif
22
23#define min(a,b) (((a) < (b)) ? (a) : (b))
24
26
27int
28#if defined(USER32_WSPRINTF) && defined(_M_IX86)
30#else
32#endif
35#if IS_SECAPI
36 size_t sizeOfBuffer,
37#endif
38#if USE_COUNT
39 size_t count,
40#endif
41 const TCHAR *format,
43 va_list argptr)
44#else
45 ...)
46#endif
47{
48#if !USE_COUNT
49 const size_t count = INT_MAX;
50#endif
51#if !IS_SECAPI
52 const size_t sizeOfBuffer = count;
53#endif
54#if !USE_VARARGS
55 va_list argptr;
56#endif
57 int result;
59
60#if IS_SECAPI
61 /* Validate parameters */
62 if (MSVCRT_CHECK_PMT(((buffer == NULL) || (format == NULL) || (sizeOfBuffer <= 0))))
63 {
64 errno = EINVAL;
65 return -1;
66 }
67
68 /* Limit output to count + 1 characters */
69 if (count != -1)
71#endif
72
73 /* Setup the FILE structure */
74 stream._base = (char*)buffer;
75 stream._ptr = stream._base;
76 stream._charbuf = 0;
77 stream._cnt = (int)(sizeOfBuffer * sizeof(TCHAR));
78 stream._bufsiz = 0;
79 stream._flag = _IOSTRG | _IOWRT;
80 stream._tmpfname = 0;
81
82#if !USE_VARARGS
83 va_start(argptr, format);
84#endif
85 result = _tstreamout(&stream, format, argptr);
86#if !USE_VARARGS
87 va_end(argptr);
88#endif
89
90#if IS_SECAPI
91 /* Check for failure or unterminated string */
92 if ((result < 0) || (result == sizeOfBuffer))
93 {
94 /* Null-terminate the buffer at the end */
95 buffer[sizeOfBuffer-1] = _T('\0');
96
97 /* Check if we can truncate */
98 if (count != _TRUNCATE)
99 {
100 /* We can't, invoke invalid parameter handler */
101 MSVCRT_INVALID_PMT("Buffer is too small", ERANGE);
102
103 /* If we came back, set the buffer to an empty string */
104 *buffer = 0;
105 }
106
107 /* Return failure */
108 return -1;
109 }
110
111 /* Null-terminate the buffer after the string */
112 buffer[result] = _T('\0');
113#else
114 /* Only zero terminate if there is enough space left */
115 if ((stream._cnt >= sizeof(TCHAR)) && (stream._ptr))
116 *(TCHAR*)stream._ptr = _T('\0');
117#endif
118
119 return result;
120}
121
122
#define IS_SECAPI
Definition: _snprintf_s.c:11
#define _tstreamout
Definition: _sxprintf.c:20
#define min(a, b)
Definition: _sxprintf.c:23
#define EINVAL
Definition: acclib.h:90
#define ERANGE
Definition: acclib.h:92
#define __cdecl
Definition: accygwin.h:79
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define _TRUNCATE
Definition: crtdefs.h:262
#define NULL
Definition: types.h:112
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
GLuint buffer
Definition: glext.h:5915
GLuint64EXT * result
Definition: glext.h:11304
#define INT_MAX
Definition: limits.h:40
#define _IOWRT
Definition: stdio.h:125
#define _IOSTRG
Definition: stdio.h:134
#define MSVCRT_INVALID_PMT(x)
Definition: mbstowcs_s.c:25
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
static size_t sizeOfBuffer
Definition: printf.c:68
#define errno
Definition: errno.h:18
#define USE_COUNT
Definition: _snprintf.c:10
#define _sxprintf
Definition: _snprintf.c:9
#define USE_VARARGS
Definition: _vsnprintf.c:11
Definition: parse.h:23
#define __stdcall
Definition: typedefs.h:25
#define _T(x)
Definition: vfdio.h:22
char TCHAR
Definition: xmlstorage.h:189