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.

125 lines
4.0 KiB

  1. /***
  2. *putch.c - contains the _putch() routine
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * The routine "_putch()" writes a single character to the console.
  8. *
  9. * NOTE: In real-mode MS-DOS the character is actually written to standard
  10. * output, and is therefore redirected when standard output is redirected.
  11. * However, under Win32 console mode, the character is ALWAYS written
  12. * to the console, even when standard output has been redirected.
  13. *
  14. *Revision History:
  15. * 06-08-89 PHG Module created, based on asm version
  16. * 03-13-90 GJF Made calling type _CALLTYPE1, added #include
  17. * <cruntime.h> and fixed copyright. Also, cleaned up
  18. * the formatting a bit.
  19. * 06-05-90 SBM Recoded as pure 32-bit, using new file handle state bits
  20. * 07-24-90 SBM Removed '32' from API names
  21. * 10-01-90 GJF New-style function declarators.
  22. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  23. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  24. * 01-16-91 GJF ANSI naming.
  25. * 02-19-91 SRW Adapt to OpenFile/CreateFile changes (_WIN32_)
  26. * 02-25-91 MHL Adapt to ReadFile/WriteFile changes (_WIN32_)
  27. * 07-26-91 GJF Took out init. stuff and cleaned up the error
  28. * handling [_WIN32_].
  29. * 03-20-93 GJF Use WriteConsole instead of WriteFile.
  30. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  31. * 09-06-94 CFW Remove Cruiser support.
  32. * 09-06-94 CFW Replace MTHREAD with _MT.
  33. * 12-03-94 SKS Clean up OS/2 references
  34. * 12-08-95 SKS _confh is now initialized on demand
  35. * 02-07-98 GJF Changes for Win64: type of _confh is now intptr_t.
  36. *
  37. *******************************************************************************/
  38. #include <cruntime.h>
  39. #include <oscalls.h>
  40. #include <conio.h>
  41. #include <internal.h>
  42. #include <mtdll.h>
  43. #include <stdio.h>
  44. /*
  45. * declaration for console handle
  46. */
  47. extern intptr_t _confh;
  48. /***
  49. *int _putch(c) - write a character to the console
  50. *
  51. *Purpose:
  52. * Calls WriteConsole to output the character
  53. * Note: in Win32 console mode always writes to console even
  54. * when stdout redirected
  55. *
  56. *Entry:
  57. * c - Character to be output
  58. *
  59. *Exit:
  60. * If an error is returned from WriteConsole
  61. * Then returns EOF
  62. * Otherwise
  63. * returns character that was output
  64. *
  65. *Exceptions:
  66. *
  67. *******************************************************************************/
  68. #ifdef _MT
  69. /* normal version lock and unlock the console, and then call the _lk version
  70. which directly accesses the console without locking. */
  71. int __cdecl _putch (
  72. int c
  73. )
  74. {
  75. int ch;
  76. _mlock(_CONIO_LOCK); /* secure the console lock */
  77. ch = _putch_lk(c); /* output the character */
  78. _munlock(_CONIO_LOCK); /* release the console lock */
  79. return ch;
  80. }
  81. #endif /* _MT */
  82. /* define version which accesses the console directly - normal version in
  83. non-_MT situations, special _lk version in _MT */
  84. #ifdef _MT
  85. int __cdecl _putch_lk (
  86. #else
  87. int __cdecl _putch (
  88. #endif
  89. int c
  90. )
  91. {
  92. /* can't use ch directly unless sure we have a big-endian machine */
  93. unsigned char ch = (unsigned char)c;
  94. ULONG num_written;
  95. /*
  96. * _confh, the handle to the console output, is created the
  97. * first time that either _putch() or _cputs() is called.
  98. */
  99. if (_confh == -2)
  100. __initconout();
  101. /* write character to console file handle */
  102. if ( (_confh == -1) || !WriteConsole( (HANDLE)_confh,
  103. (LPVOID)&ch,
  104. 1,
  105. &num_written,
  106. NULL )
  107. )
  108. /* return error indicator */
  109. return EOF;
  110. return ch;
  111. }