Source code of Windows XP (NT5)
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.

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