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.

138 lines
2.7 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. getuname.c
  5. Abstract:
  6. Provides a function to prompt the user for a username similar to getpass
  7. for passwords.
  8. Author:
  9. Mike Massa (mikemas) Sept 20, 1991
  10. Revision History:
  11. Who When What
  12. -------- -------- ----------------------------------------------
  13. mikemas 03-25-92 created by cloning getpass.c
  14. Notes:
  15. Exports:
  16. getuname
  17. --*/
  18. #include <stdio.h>
  19. #include <windef.h>
  20. #include <winbase.h>
  21. #include <wincon.h>
  22. #include <winuser.h>
  23. #include <nls.h>
  24. #include "nlstxt.h"
  25. #define MAXUSERNAMELEN 32
  26. static char ubuf[MAXUSERNAMELEN+1];
  27. /******************************************************************/
  28. char *
  29. getusername(
  30. char *prompt
  31. )
  32. /******************************************************************/
  33. {
  34. HANDLE InHandle, OutHandle;
  35. BOOL Result;
  36. DWORD NumBytes;
  37. int i;
  38. ubuf[0] = '\0';
  39. InHandle = CreateFile("CONIN$",
  40. GENERIC_READ | GENERIC_WRITE,
  41. FILE_SHARE_READ | FILE_SHARE_WRITE,
  42. NULL,
  43. OPEN_EXISTING,
  44. FILE_ATTRIBUTE_NORMAL,
  45. NULL
  46. );
  47. if (InHandle == (HANDLE) -1)
  48. {
  49. NlsPutMsg(STDERR,LIBUEMUL_ERROR_GETTING_CI_HANDLE,GetLastError());
  50. CloseHandle(InHandle);
  51. return(ubuf);
  52. }
  53. OutHandle = CreateFile("CONOUT$",
  54. GENERIC_WRITE,
  55. FILE_SHARE_READ | FILE_SHARE_WRITE,
  56. NULL,
  57. OPEN_EXISTING,
  58. FILE_ATTRIBUTE_NORMAL,
  59. NULL
  60. );
  61. if (OutHandle == (HANDLE) -1)
  62. {
  63. NlsPutMsg(STDERR,LIBUEMUL_ERROR_GETTING_CO_HANDLE,GetLastError());
  64. CloseHandle(InHandle);
  65. CloseHandle(OutHandle);
  66. return(ubuf);
  67. }
  68. NumBytes = strlen (prompt);
  69. CharToOemBuff (prompt, prompt, NumBytes);
  70. Result =
  71. WriteFile (
  72. OutHandle,
  73. prompt,
  74. NumBytes,
  75. &NumBytes,
  76. NULL);
  77. if (!Result)
  78. {
  79. NlsPutMsg(STDERR,LIBUEMUL_WRITE_TO_CONSOLEOUT_ERROR, GetLastError());
  80. CloseHandle(InHandle);
  81. CloseHandle(OutHandle);
  82. return(ubuf);
  83. }
  84. Result =
  85. ReadFile(
  86. InHandle,
  87. ubuf,
  88. MAXUSERNAMELEN,
  89. &NumBytes,
  90. NULL);
  91. if (!Result)
  92. {
  93. NlsPutMsg(STDERR,LIBUEMUL_READ_FROM_CONSOLEIN_ERROR, GetLastError());
  94. }
  95. OemToCharBuff (ubuf, ubuf, NumBytes);
  96. // peel off linefeed
  97. i = (int) NumBytes;
  98. while(--i >= 0) {
  99. if ((ubuf[i] == '\n') || (ubuf[i] == '\r')) {
  100. ubuf[i] = '\0';
  101. }
  102. }
  103. CloseHandle(InHandle);
  104. CloseHandle(OutHandle);
  105. return(ubuf);
  106. }