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*sprintf_s.c - print formatted to string
7*
8
9*
10*Purpose:
11* defines sprintf_s() and _snprintf_s() - print formatted data to string
12*
13*******************************************************************************/
14
15#include <string.h>
16#include <errno.h>
17#include <limits.h>
18#include "internal_securecrt.h"
19
20#include "mbusafecrt_internal.h"
21
22
23/***
24*ifndef _COUNT_
25*int sprintf_s(string, format, ...) - print formatted data to string
26*else
27*int _snprintf_s(string, cnt, format, ...) - print formatted data to string
28*endif
29*
30*Purpose:
31* Prints formatted data to the using the format string to
32* format data and getting as many arguments as called for
33* Sets up a FILE so file i/o operations can be used, make
34* string look like a huge buffer to it, but _flsbuf will
35* refuse to flush it if it fills up. Appends '\0' to make
36* it a true string. _output does the real work here
37*
38* Allocate the 'fake' _iob[] entry statically instead of on
39* the stack so that other routines can assume that _iob[]
40* entries are in are in DGROUP and, thus, are near.
41*
42*ifdef _COUNT_
43* The _snprintf_s() flavor takes a count argument that is
44* the max number of bytes that should be written to the
45* user's buffer.
46*endif
47*
48* Multi-thread: (1) Since there is no stream, this routine must
49* never try to get the stream lock (i.e., there is no stream
50* lock either). (2) Also, since there is only one statically
51* allocated 'fake' iob, we must lock/unlock to prevent collisions.
52*
53*Entry:
54* char *string - pointer to place to put output
55*ifdef _COUNT_
56* size_t count - max number of bytes to put in buffer
57*endif
58* char *format - format string to control data format/number
59* of arguments followed by list of arguments, number and type
60* controlled by format string
61*
62*Exit:
63* returns number of characters printed
64*
65*Exceptions:
66*
67*******************************************************************************/
68
69int sprintf_s (
70 char *string,
71 size_t sizeInBytes,
72 const char *format,
73 ...
74 )
75{
76 int ret;
77 va_list arglist;
78 va_start(arglist, format);
79 ret = vsprintf_s(string, sizeInBytes, format, arglist);
80 va_end(arglist);
81 return ret;
82}
83
84int _snprintf_s (
85 char *string,
86 size_t sizeInBytes,
87 size_t count,
88 const char *format,
89 ...
90 )
91{
92 int ret;
93 va_list arglist;
94 va_start(arglist, format);
95 ret = _vsnprintf_s(string, sizeInBytes, count, format, arglist);
96 va_end(arglist);
97 return ret;
98}
99