swprintf_s.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE file in the project root for full license 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. #include <string.h>
  14. #include <errno.h>
  15. #include <limits.h>
  16. #include "internal_securecrt.h"
  17. #include "mbusafecrt_internal.h"
  18. #include <stdarg.h>
  19. /***
  20. *ifndef _COUNT_
  21. *int _swprintf(string, format, ...) - print formatted data to string
  22. *else
  23. *ifndef _SWPRINTFS_ERROR_RETURN_FIX
  24. *int _snwprintf(string, cnt, format, ...) - print formatted data to string
  25. *else
  26. *int _swprintf_c(string, cnt, format, ...) - print formatted data to string
  27. *endif
  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. * We alias swprintf to _swprintf
  43. *
  44. *ifdef _COUNT_
  45. *ifndef _SWPRINTFS_ERROR_RETURN_FIX
  46. * The _snwprintf() flavor takes a count argument that is
  47. * the max number of wide characters that should be written to the
  48. * user's buffer.
  49. * We don't expose this function directly in the headers.
  50. *else
  51. * The _swprintf_c() flavor does the same thing as the _snwprintf
  52. * above, but, it also fixes a issue in the return value in the case
  53. * when there isn't enough space to write the null terminator
  54. * We don't fix this issue in _snwprintf because of backward
  55. * compatibility. In new code, however, _snwprintf is #defined to
  56. * _swprintf_c so users get the fix.
  57. *
  58. *endif
  59. *
  60. * Multi-thread: (1) Since there is no stream, this routine must
  61. * never try to get the stream lock (i.e., there is no stream
  62. * lock either). (2) Also, since there is only one statically
  63. * allocated 'fake' iob, we must lock/unlock to prevent collisions.
  64. *
  65. *Entry:
  66. * char16_t *string - pointer to place to put output
  67. *ifdef _COUNT_
  68. * size_t count - max number of wide characters to put in buffer
  69. *endif
  70. * char16_t *format - format string to control data format/number
  71. * of arguments followed by list of arguments, number and type
  72. * controlled by format string
  73. *
  74. *Exit:
  75. * returns number of wide characters printed
  76. *
  77. *Exceptions:
  78. *
  79. *******************************************************************************/
  80. int __cdecl swprintf_s (
  81. char16_t *string,
  82. size_t sizeInWords,
  83. const char16_t *format,
  84. ...
  85. )
  86. {
  87. int ret;
  88. va_list arglist;
  89. va_start(arglist, format);
  90. ret = _vswprintf_s(string, sizeInWords, format, arglist);
  91. va_end(arglist);
  92. return ret;
  93. }
  94. int __cdecl _snwprintf_s (
  95. char16_t *string,
  96. size_t sizeInWords,
  97. size_t count,
  98. const char16_t *format,
  99. ...
  100. )
  101. {
  102. int ret;
  103. va_list arglist;
  104. va_start(arglist, format);
  105. ret = _vsnwprintf_s(string, sizeInWords, count, format, arglist);
  106. va_end(arglist);
  107. return ret;
  108. }