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.

149 lines
4.3 KiB

  1. /***
  2. *initcon.c - direct console I/O initialization and termination for Win32
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines __initconin() and _initconout() and __termcon() routines.
  8. * The first two are called on demand to initialize _coninpfh and
  9. * _confh, and the third is called indirectly by CRTL termination.
  10. *
  11. * NOTE: The __termcon() routine is called indirectly by the C/C++
  12. * Run-Time Library termination code.
  13. *
  14. *Revision History:
  15. * 07-26-91 GJF Module created. Based on the original code by Stevewo
  16. * (which was distributed across several sources).
  17. * 03-12-92 SKS Split out initializer
  18. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  19. * 10-28-93 GJF Define entries for initialization and termination
  20. * sections (used to be i386\cinitcon.asm).
  21. * 04-12-94 GJF Made _initcon() and _termcon() into empty functions
  22. * for the Win32s version of msvcrt*.dll.
  23. * 12-08-95 SKS Replaced __initcon() with __initconin()/__initconout().
  24. * _confh and _coninfh are no longer initialized during
  25. * CRTL start-up but rather on demand in _getch(),
  26. * _putch(), _cgets(), _cputs(), and _kbhit().
  27. * 07-08-96 GJF Removed Win32s support.Also, detab-ed.
  28. * 02-07-98 GJF Changes for Win64: _coninph and _confh are now an
  29. * intptr_t.
  30. * 04-28-99 PML Wrap __declspec(allocate()) in _CRTALLOC macro.
  31. *
  32. *******************************************************************************/
  33. #include <sect_attribs.h>
  34. #include <cruntime.h>
  35. #include <internal.h>
  36. #include <oscalls.h>
  37. void __cdecl __termcon(void);
  38. #ifdef _MSC_VER
  39. #pragma data_seg(".CRT$XPX")
  40. _CRTALLOC(".CRT$XPX") static _PVFV pterm = __termcon;
  41. #pragma data_seg()
  42. #endif /* _MSC_VER */
  43. /*
  44. * define console handles. these definitions cause this file to be linked
  45. * in if one of the direct console I/O functions is referenced.
  46. * The value (-2) is used to indicate the un-initialized state.
  47. */
  48. intptr_t _coninpfh = -2; /* console input */
  49. intptr_t _confh = -2; /* console output */
  50. /***
  51. *void __initconin(void) - open handles for console input
  52. *
  53. *Purpose:
  54. * Opens handle for console input.
  55. *
  56. *Entry:
  57. * None.
  58. *
  59. *Exit:
  60. * No return value. If successful, the handle value is copied into the
  61. * global variable _coninpfh. Otherwise _coninpfh is set to -1.
  62. *
  63. *Exceptions:
  64. *
  65. *******************************************************************************/
  66. void __cdecl __initconin (
  67. void
  68. )
  69. {
  70. _coninpfh = (intptr_t)CreateFile( "CONIN$",
  71. GENERIC_READ | GENERIC_WRITE,
  72. FILE_SHARE_READ | FILE_SHARE_WRITE,
  73. NULL,
  74. OPEN_EXISTING,
  75. 0,
  76. NULL );
  77. }
  78. /***
  79. *void __initconout(void) - open handles for console output
  80. *
  81. *Purpose:
  82. * Opens handle for console output.
  83. *
  84. *Entry:
  85. * None.
  86. *
  87. *Exit:
  88. * No return value. If successful, the handle value is copied into the
  89. * global variable _confh. Otherwise _confh is set to -1.
  90. *
  91. *Exceptions:
  92. *
  93. *******************************************************************************/
  94. void __cdecl __initconout (
  95. void
  96. )
  97. {
  98. _confh = (intptr_t)CreateFile( "CONOUT$",
  99. GENERIC_WRITE,
  100. FILE_SHARE_READ | FILE_SHARE_WRITE,
  101. NULL,
  102. OPEN_EXISTING,
  103. 0,
  104. NULL );
  105. }
  106. /***
  107. *void __termcon(void) - close console I/O handles
  108. *
  109. *Purpose:
  110. * Closes _coninpfh and _confh.
  111. *
  112. *Entry:
  113. * None.
  114. *
  115. *Exit:
  116. * No return value.
  117. *
  118. *Exceptions:
  119. *
  120. *******************************************************************************/
  121. void __cdecl __termcon (
  122. void
  123. )
  124. {
  125. if ( _confh != -1 && _confh != -2 ) {
  126. CloseHandle( (HANDLE)_confh );
  127. }
  128. if ( _coninpfh != -1 && _coninpfh != -2 ) {
  129. CloseHandle( (HANDLE)_coninpfh );
  130. }
  131. }