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.

174 lines
5.4 KiB

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