home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / putch.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  103 lines

  1. /***
  2. *putch.c - contains the _putch() routine
  3. *
  4. *       Copyright (c) 1989-1997, 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. *******************************************************************************/
  15.  
  16. #include <cruntime.h>
  17. #include <oscalls.h>
  18. #include <conio.h>
  19. #include <internal.h>
  20. #include <mtdll.h>
  21. #include <stdio.h>
  22.  
  23. /*
  24.  * declaration for console handle
  25.  */
  26. extern int _confh;
  27.  
  28. /***
  29. *int _putch(c) - write a character to the console
  30. *
  31. *Purpose:
  32. *       Calls WriteConsole to output the character
  33. *       Note: in Win32 console mode always writes to console even
  34. *       when stdout redirected
  35. *
  36. *Entry:
  37. *       c - Character to be output
  38. *
  39. *Exit:
  40. *       If an error is returned from WriteConsole
  41. *           Then returns EOF
  42. *       Otherwise
  43. *           returns character that was output
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48.  
  49. #ifdef _MT
  50. /* normal version lock and unlock the console, and then call the _lk version
  51.    which directly accesses the console without locking. */
  52.  
  53. int __cdecl _putch (
  54.         int c
  55.         )
  56. {
  57.         int ch;
  58.  
  59.         _mlock(_CONIO_LOCK);            /* secure the console lock */
  60.         ch = _putch_lk(c);              /* output the character */
  61.         _munlock(_CONIO_LOCK);          /* release the console lock */
  62.  
  63.         return ch;
  64. }
  65. #endif  /* _MT */
  66.  
  67. /* define version which accesses the console directly - normal version in
  68.    non-_MT situations, special _lk version in _MT */
  69.  
  70. #ifdef _MT
  71. int __cdecl _putch_lk (
  72. #else  /* _MT */
  73. int __cdecl _putch (
  74. #endif  /* _MT */
  75.         int c
  76.         )
  77. {
  78.         /* can't use ch directly unless sure we have a big-endian machine */
  79.         unsigned char ch = (unsigned char)c;
  80.         ULONG num_written;
  81.  
  82.         /*
  83.          * _confh, the handle to the console output, is created the
  84.          * first time that either _putch() or _cputs() is called.
  85.          */
  86.  
  87.         if (_confh == -2)
  88.             __initconout();
  89.  
  90.         /* write character to console file handle */
  91.  
  92.         if ( (_confh == -1) || !WriteConsole( (HANDLE)_confh,
  93.                                               (LPVOID)&ch,
  94.                                               1,
  95.                                               &num_written,
  96.                                               NULL )
  97.            )
  98.                 /* return error indicator */
  99.                 return EOF;
  100.  
  101.         return ch;
  102. }
  103.