Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.6 KiB

  1. /***
  2. *sprintf.c - print formatted to string
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines sprintf() and _snprintf() - print formatted data to string
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 04-13-87 JCR added const to declaration
  12. * 06-24-87 JCR (1) Made declaration conform to ANSI prototype and use
  13. * the va_ macros; (2) removed SS_NE_DS conditionals.
  14. * 11-07-87 JCR Multi-thread support
  15. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  16. * 05-27-88 PHG Merged DLL and normal versions
  17. * 06-13-88 JCR Fake _iob entry is now static so that other routines
  18. * can assume _iob entries are in DGROUP.
  19. * 08-25-88 GJF Define MAXSTR to be INT_MAX (from LIMITS.H).
  20. * 06-06-89 JCR 386 mthread support
  21. * 08-18-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  22. * model). Also fixed copyright and indents.
  23. * 02-16-90 GJF Fixed copyright
  24. * 03-19-90 GJF Made calling type _CALLTYPE2, added #include
  25. * <cruntime.h> and removed #include <register.h>.
  26. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  27. * 10-03-90 GJF New-style function declarator.
  28. * 09-24-91 JCR Added _snprintf()
  29. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  30. * 09-05-94 SKS Change "#ifdef" inside comments to "*ifdef" to avoid
  31. * problems with CRTL source release process.
  32. * 02-06-94 CFW assert -> _ASSERTE.
  33. * 01-06-99 GJF Changes for 64-bit size_t.
  34. * 03-10-00 GB Added support for knowing the length of formatted
  35. * string by passing NULL for input string.
  36. * 03-16-00 GB Added _scprintf()
  37. *
  38. *******************************************************************************/
  39. #include <cruntime.h>
  40. #include <stdio.h>
  41. #include <dbgint.h>
  42. #include <stdarg.h>
  43. #include <internal.h>
  44. #include <limits.h>
  45. #include <mtdll.h>
  46. #define MAXSTR INT_MAX
  47. /***
  48. *ifndef _COUNT_
  49. *int sprintf(string, format, ...) - print formatted data to string
  50. *else
  51. *int _snprintf(string, cnt, format, ...) - print formatted data to string
  52. *endif
  53. *
  54. *Purpose:
  55. * Prints formatted data to the using the format string to
  56. * format data and getting as many arguments as called for
  57. * Sets up a FILE so file i/o operations can be used, make
  58. * string look like a huge buffer to it, but _flsbuf will
  59. * refuse to flush it if it fills up. Appends '\0' to make
  60. * it a true string. _output does the real work here
  61. *
  62. * Allocate the 'fake' _iob[] entry statically instead of on
  63. * the stack so that other routines can assume that _iob[]
  64. * entries are in are in DGROUP and, thus, are near.
  65. *
  66. *ifdef _COUNT_
  67. * The _snprintf() flavor takes a count argument that is
  68. * the max number of bytes that should be written to the
  69. * user's buffer.
  70. *endif
  71. *
  72. * Multi-thread: (1) Since there is no stream, this routine must
  73. * never try to get the stream lock (i.e., there is no stream
  74. * lock either). (2) Also, since there is only one statically
  75. * allocated 'fake' iob, we must lock/unlock to prevent collisions.
  76. *
  77. *Entry:
  78. * char *string - pointer to place to put output
  79. *ifdef _COUNT_
  80. * size_t count - max number of bytes to put in buffer
  81. *endif
  82. * char *format - format string to control data format/number
  83. * of arguments followed by list of arguments, number and type
  84. * controlled by format string
  85. *
  86. *Exit:
  87. * returns number of characters printed
  88. *
  89. *Exceptions:
  90. *
  91. *******************************************************************************/
  92. #ifndef _COUNT_
  93. int __cdecl sprintf (
  94. char *string,
  95. const char *format,
  96. ...
  97. )
  98. #else
  99. int __cdecl _snprintf (
  100. char *string,
  101. size_t count,
  102. const char *format,
  103. ...
  104. )
  105. #endif
  106. {
  107. FILE str;
  108. REG1 FILE *outfile = &str;
  109. va_list arglist;
  110. REG2 int retval;
  111. va_start(arglist, format);
  112. _ASSERTE(format != NULL);
  113. _ASSERTE(string != NULL);
  114. #ifndef _COUNT_
  115. outfile->_cnt = MAXSTR;
  116. #else
  117. outfile->_cnt = (int)count;
  118. #endif
  119. outfile->_flag = _IOWRT|_IOSTRG;
  120. outfile->_ptr = outfile->_base = string;
  121. retval = _output(outfile,format,arglist);
  122. if (string != NULL)
  123. _putc_lk('\0',outfile); /* no-lock version */
  124. return(retval);
  125. }
  126. /***
  127. * _scprintf() - counts the number of character needed to print the formatted
  128. * data
  129. *
  130. *Purpose:
  131. * Counts the number of characters in the fotmatted data.
  132. *
  133. *Entry:
  134. * char *format - format string to control data format/number
  135. * of arguments followed by list of arguments, number and type
  136. * controlled by format string
  137. *
  138. *Exit:
  139. * returns number of characters needed to print formatted data.
  140. *
  141. *Exceptions:
  142. *
  143. *******************************************************************************/
  144. #ifndef _COUNT_
  145. int __cdecl _scprintf (
  146. const char *format,
  147. ...
  148. )
  149. {
  150. FILE str;
  151. REG1 FILE *outfile = &str;
  152. va_list arglist;
  153. REG2 int retval;
  154. va_start(arglist, format);
  155. _ASSERTE(format != NULL);
  156. outfile->_cnt = MAXSTR;
  157. outfile->_flag = _IOWRT|_IOSTRG;
  158. outfile->_ptr = outfile->_base = NULL;
  159. retval = _output(outfile,format,arglist);
  160. return(retval);
  161. }
  162. #endif