sprintf.c 2.8 KB

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