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.

238 lines
6.7 KiB

  1. /***
  2. *printf.h - print formatted
  3. *
  4. * Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines w4*printf() - print formatted data
  8. * defines w4v*printf() - print formatted output, get data from an
  9. * argument ptr instead of explicit args.
  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. *
  26. *******************************************************************************/
  27. #if CIDBG == 1 || DBG == 1
  28. #include <wchar.h>
  29. #include <stdarg.h>
  30. #include <limits.h>
  31. #include <windows.h>
  32. #include "w4io.h"
  33. #if defined(_W4PRINTF_)
  34. static long fh;
  35. // extern long GetStdHandle(long);
  36. // extern void WriteFile(long fh, char *s, long cch, long * pcchret, long);
  37. # define _PRINTF_
  38. #elif defined(_W4DPRINTF_)
  39. # define _pwritechar _dwritechar
  40. # define _pflushbuf _dflushbuf
  41. # define w4printf w4dprintf
  42. # define w4vprintf w4vdprintf
  43. # define _PRINTF_
  44. #elif defined(_W4SPRINTF_)
  45. # define _pwritechar _swritechar
  46. # define w4printf w4sprintf
  47. # define w4vprintf w4vsprintf
  48. #elif defined(_W4WCSPRINTF_)
  49. # define _TCHAR_ wchar_t
  50. # define _PBUF_ pwcbuf
  51. # define _PSTART_ pwcstart
  52. # define w4printf w4wcsprintf
  53. # define w4vprintf w4vwcsprintf
  54. # define _pwritechar _wwritechar
  55. #else
  56. # error configuration problem
  57. #endif
  58. #ifndef _TCHAR_
  59. # define _TCHAR_ char
  60. # define _PBUF_ pchbuf
  61. # define _PSTART_ pchstart
  62. #endif
  63. #ifdef _PRINTF_
  64. # undef OutputDebugString
  65. # define OutputDebugString OutputDebugStringA
  66. int _cdecl _pflushbuf(struct w4io *f);
  67. # define SPR(a)
  68. # define MAXSTR ((MAX_PATH + 1) * sizeof(WCHAR))
  69. #else
  70. # define SPR(a) a,
  71. # define MAXSTR INT_MAX
  72. #endif
  73. void _cdecl _pwritechar(int ch, int num, struct w4io *f, int *pcchwritten);
  74. int _cdecl w4vprintf(SPR(_TCHAR_ *string) const char *format, va_list arglist);
  75. /***
  76. *int w4printf(format, ...) - print formatted data
  77. *
  78. *Purpose:
  79. * Prints formatted data using the format string to
  80. * format data and getting as many arguments as called for
  81. * Sets up a w4io so file i/o operations can be used.
  82. * w4iooutput does the real work here
  83. *
  84. *Entry:
  85. * char *format - format string to control data format/number
  86. * of arguments followed by list of arguments, number and type
  87. * controlled by format string
  88. *
  89. *Exit:
  90. * returns number of characters written
  91. *
  92. *Exceptions:
  93. *
  94. *******************************************************************************/
  95. int _cdecl
  96. w4printf(SPR(_TCHAR_ *string) const char *format, ...)
  97. /*
  98. * 'PRINT', 'F'ormatted
  99. */
  100. {
  101. va_list arglist;
  102. va_start(arglist, format);
  103. return(w4vprintf(SPR(string) format, arglist));
  104. }
  105. /***
  106. *int w4vprintf(format, arglist) - print formatted data from arg ptr
  107. *
  108. *Purpose:
  109. * Prints formatted data, but gets data from an argument pointer.
  110. * Sets up a w4io so file i/o operations can be used, make string look
  111. * like a huge buffer to it, but _flsbuf will refuse to flush it if it
  112. * fills up. Appends '\0' to make it a true string.
  113. *
  114. * Multi-thread: (1) Since there is no stream, this routine must never try
  115. * to get the stream lock (i.e., there is no stream lock either). (2)
  116. * Also, since there is only one staticly allocated 'fake' iob, we must
  117. * lock/unlock to prevent collisions.
  118. *
  119. *Entry:
  120. * char *format - format string, describes format of data
  121. * va_list arglist - varargs argument pointer
  122. *
  123. *Exit:
  124. * returns number of characters written
  125. *
  126. *Exceptions:
  127. *
  128. *******************************************************************************/
  129. int _cdecl
  130. w4vprintf(SPR(_TCHAR_ *string) const char *format, va_list arglist)
  131. /*
  132. * 'V'ariable argument 'PRINT', 'F'ormatted
  133. */
  134. {
  135. struct w4io outfile;
  136. register int retval;
  137. #ifdef _PRINTF_
  138. char string[MAXSTR + 1]; // leave room for null termination
  139. #else
  140. int dummy = 0;
  141. #endif
  142. #ifdef _W4PRINTF_
  143. long ldummy;
  144. if (fh == 0 || fh == -1)
  145. {
  146. ldummy = -11; // C7 bug workaround
  147. if ((fh = PtrToLong(GetStdHandle(ldummy))) == 0 || fh == -1)
  148. {
  149. OutputDebugString("GetStdHandle in " __FILE__ " failed\n");
  150. return(-1);
  151. }
  152. }
  153. #endif
  154. outfile._PBUF_ = outfile._PSTART_ = string;
  155. outfile.cchleft = MAXSTR;
  156. outfile.writechar = _pwritechar;
  157. retval = w4iooutput(&outfile, format, arglist);
  158. #ifdef _PRINTF_
  159. if (_pflushbuf(&outfile) == -1) {
  160. return(-1);
  161. }
  162. #else
  163. _pwritechar('\0', 1, &outfile, &dummy);
  164. #endif
  165. return(retval);
  166. }
  167. void _cdecl _pwritechar(int ch, int num, struct w4io *f, int *pcchwritten)
  168. {
  169. //printf(" char: ch=%c, cnt=%d, cch=%d\n", ch, num, *pcchwritten);
  170. while (num-- > 0) {
  171. #ifdef _PRINTF_
  172. if (f->cchleft < 2 && _pflushbuf(f) == -1) {
  173. *pcchwritten = -1;
  174. return;
  175. }
  176. #endif
  177. #ifdef _W4DPRINTF_
  178. #endif
  179. *f->_PBUF_++ = (char) ch;
  180. f->cchleft--;
  181. (*pcchwritten)++;
  182. }
  183. }
  184. #ifdef _PRINTF_
  185. int _cdecl _pflushbuf(struct w4io *f)
  186. {
  187. int cch;
  188. if (cch = (int)(f->pchbuf - f->pchstart))
  189. {
  190. #ifdef _W4DPRINTF_
  191. *f->pchbuf = '\0'; // null terminate
  192. OutputDebugString(f->pchstart);
  193. #else
  194. long cchret;
  195. //*f->pchbuf = '\0'; // null terminate
  196. //printf("%d chars: \"%s\"\n", cch, f->pchstart);
  197. WriteFile( LongToHandle( fh ), f->pchstart, cch, &cchret, 0);
  198. if (cch != cchret)
  199. {
  200. OutputDebugString("WriteFile in " __FILE__ " failed\n");
  201. return(-1);
  202. }
  203. #endif
  204. f->pchbuf -= cch; // reset pointer
  205. f->cchleft += cch; // reset count
  206. }
  207. return(0);
  208. }
  209. #endif // _PRINTF_
  210. #endif //CIDBG == 1 || DBG == 1