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.

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