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.

178 lines
3.6 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. getpass.c
  5. Abstract:
  6. Emulates the Unix getpass routine. Used by libstcp and the tcpcmd
  7. utilities.
  8. Author:
  9. Mike Massa (mikemas) Sept 20, 1991
  10. Revision History:
  11. Who When What
  12. -------- -------- ----------------------------------------------
  13. mikemas 10-29-91 created
  14. sampa 10-31-91 modified getpass to not echo input
  15. Notes:
  16. Exports:
  17. getpass
  18. --*/
  19. #include <stdio.h>
  20. #include <windef.h>
  21. #include <winbase.h>
  22. #include <wincon.h>
  23. #include <nls.h>
  24. #include <winuser.h>
  25. #include "nlstxt.h"
  26. #define MAXPASSLEN 32
  27. static char pbuf[MAXPASSLEN+1];
  28. /******************************************************************/
  29. char *
  30. getpass(
  31. char *prompt
  32. )
  33. /******************************************************************/
  34. {
  35. HANDLE InHandle, OutHandle;
  36. unsigned long SaveMode, NewMode;
  37. BOOL Result;
  38. DWORD NumBytes;
  39. int i;
  40. pbuf[0] = 0;
  41. InHandle = CreateFile("CONIN$",
  42. GENERIC_READ | GENERIC_WRITE,
  43. FILE_SHARE_READ | FILE_SHARE_WRITE,
  44. NULL,
  45. OPEN_EXISTING,
  46. FILE_ATTRIBUTE_NORMAL,
  47. NULL
  48. );
  49. if (InHandle == (HANDLE) -1)
  50. {
  51. NlsPutMsg(STDOUT, LIBUEMUL_ERROR_GETTING_CI_HANDLE,GetLastError());
  52. CloseHandle(InHandle);
  53. return(pbuf);
  54. }
  55. OutHandle = CreateFile("CONOUT$",
  56. GENERIC_WRITE,
  57. FILE_SHARE_READ | FILE_SHARE_WRITE,
  58. NULL,
  59. OPEN_EXISTING,
  60. FILE_ATTRIBUTE_NORMAL,
  61. NULL
  62. );
  63. if (OutHandle == (HANDLE) -1)
  64. {
  65. NlsPutMsg(STDOUT, LIBUEMUL_ERROR_GETTING_CO_HANDLE,GetLastError());
  66. CloseHandle(InHandle);
  67. CloseHandle(OutHandle);
  68. return(pbuf);
  69. }
  70. Result =
  71. GetConsoleMode(InHandle, &SaveMode);
  72. if (!Result)
  73. {
  74. NlsPutMsg(STDOUT,LIBUEMUL_ERROR_GETTING_CON_MODE, GetLastError());
  75. CloseHandle(InHandle);
  76. CloseHandle(OutHandle);
  77. return(pbuf);
  78. }
  79. NewMode = SaveMode & ~ENABLE_ECHO_INPUT;
  80. Result = SetConsoleMode(InHandle, NewMode);
  81. if (!Result)
  82. {
  83. NlsPutMsg(STDOUT,LIBUEMUL_ERROR_SETTING_CON_MODE, GetLastError());
  84. CloseHandle(InHandle);
  85. CloseHandle(OutHandle);
  86. return(pbuf);
  87. }
  88. NumBytes = strlen (prompt);
  89. CharToOemBuff (prompt, prompt, NumBytes);
  90. Result =
  91. WriteFile (
  92. OutHandle,
  93. prompt,
  94. NumBytes,
  95. &NumBytes,
  96. NULL);
  97. if (!Result)
  98. {
  99. NlsPutMsg(STDOUT,LIBUEMUL_WRITE_TO_CONSOLEOUT_ERROR, GetLastError());
  100. Result = SetConsoleMode(InHandle, SaveMode);
  101. CloseHandle(InHandle);
  102. CloseHandle(OutHandle);
  103. return(pbuf);
  104. }
  105. Result =
  106. ReadFile(
  107. InHandle,
  108. pbuf,
  109. MAXPASSLEN,
  110. &NumBytes,
  111. NULL);
  112. if (!Result)
  113. {
  114. NlsPutMsg(STDOUT,LIBUEMUL_READ_FROM_CONSOLEIN_ERROR, GetLastError());
  115. }
  116. OemToCharBuff (pbuf, pbuf, NumBytes);
  117. // peel off linefeed
  118. i = (int) NumBytes;
  119. while(--i >= 0) {
  120. if ((pbuf[i] == '\n') || (pbuf[i] == '\r')) {
  121. pbuf[i] = '\0';
  122. }
  123. }
  124. Result = SetConsoleMode(InHandle, SaveMode);
  125. if (!Result)
  126. {
  127. NlsPutMsg(STDOUT, LIBUEMUL_ERROR_RESTORING_CONSOLE_MODE, GetLastError());
  128. }
  129. WriteFile(
  130. OutHandle,
  131. "\n",
  132. 1,
  133. &NumBytes,
  134. NULL);
  135. CloseHandle(InHandle);
  136. CloseHandle(OutHandle);
  137. return(pbuf);
  138. }