Leaked source code of windows server 2003
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.

123 lines
3.0 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 = (HANDLE)-1, OutHandle = (HANDLE)-1;
  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. NlsPutMsg(STDERR,LIBUEMUL_ERROR_GETTING_CI_HANDLE,GetLastError());
  49. goto exit_getusername;
  50. }
  51. OutHandle = CreateFile("CONOUT$",
  52. GENERIC_WRITE,
  53. FILE_SHARE_READ | FILE_SHARE_WRITE,
  54. NULL,
  55. OPEN_EXISTING,
  56. FILE_ATTRIBUTE_NORMAL,
  57. NULL
  58. );
  59. if (OutHandle == (HANDLE)-1) {
  60. NlsPutMsg(STDERR,LIBUEMUL_ERROR_GETTING_CO_HANDLE,GetLastError());
  61. goto exit_getusername;
  62. }
  63. NumBytes = strlen(prompt);
  64. CharToOemBuff(prompt, prompt, NumBytes);
  65. Result = WriteFile(OutHandle,
  66. prompt,
  67. NumBytes,
  68. &NumBytes,
  69. NULL);
  70. if (!Result) {
  71. NlsPutMsg(STDERR,LIBUEMUL_WRITE_TO_CONSOLEOUT_ERROR, GetLastError());
  72. goto exit_getusername;
  73. }
  74. Result = ReadFile(InHandle,
  75. ubuf,
  76. MAXUSERNAMELEN,
  77. &NumBytes,
  78. NULL);
  79. if (!Result)
  80. NlsPutMsg(STDERR,LIBUEMUL_READ_FROM_CONSOLEIN_ERROR, GetLastError());
  81. ubuf[MAXUSERNAMELEN] = '\0';
  82. OemToCharBuff (ubuf, ubuf, NumBytes);
  83. // peel off linefeed
  84. i = (int) NumBytes;
  85. while(--i >= 0)
  86. if ((ubuf[i] == '\n') || (ubuf[i] == '\r'))
  87. ubuf[i] = '\0';
  88. exit_getusername :
  89. if (InHandle != (HANDLE)-1)
  90. CloseHandle(InHandle);
  91. if (OutHandle != (HANDLE)-1)
  92. CloseHandle(OutHandle);
  93. return(ubuf);
  94. }