main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2020 Brendan Shanks for CodeWeavers
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #define SECURITY_WIN32
  19. #include <windows.h>
  20. #include <security.h>
  21. #include "wine/debug.h"
  22. WINE_DEFAULT_DEBUG_CHANNEL(whoami);
  23. static int output_write(const WCHAR* str, int len)
  24. {
  25. DWORD ret, count;
  26. ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL);
  27. if (!ret)
  28. {
  29. DWORD lenA;
  30. char* strA;
  31. /* On Windows WriteConsoleW() fails if the output is redirected. So fall
  32. * back to WriteFile(), assuming the console encoding is still the right
  33. * one in that case.
  34. */
  35. lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len,
  36. NULL, 0, NULL, NULL);
  37. strA = HeapAlloc(GetProcessHeap(), 0, lenA);
  38. if (!strA)
  39. return 0;
  40. WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA,
  41. NULL, NULL);
  42. WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
  43. HeapFree(GetProcessHeap(), 0, strA);
  44. }
  45. return count;
  46. }
  47. int __cdecl wmain(int argc, WCHAR *argv[])
  48. {
  49. WCHAR *buf = NULL;
  50. ULONG size = 0;
  51. BOOL result;
  52. if (argc > 1)
  53. {
  54. int i;
  55. WINE_FIXME("unsupported arguments:");
  56. for (i = 0; i < argc; i++)
  57. WINE_FIXME(" %s", wine_dbgstr_w(argv[i]));
  58. WINE_FIXME("\n");
  59. }
  60. result = GetUserNameExW(NameSamCompatible, NULL, &size);
  61. if (result || GetLastError() != ERROR_MORE_DATA)
  62. {
  63. WINE_ERR("GetUserNameExW failed, result %d, error %ld\n", result, GetLastError());
  64. return 1;
  65. }
  66. buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  67. if (!buf)
  68. {
  69. WINE_ERR("Memory allocation failed\n");
  70. return 1;
  71. }
  72. result = GetUserNameExW(NameSamCompatible, buf, &size);
  73. if (result)
  74. {
  75. output_write(buf, size);
  76. output_write(L"\r\n", 2);
  77. }
  78. else
  79. WINE_ERR("GetUserNameExW failed, error %ld\n", GetLastError());
  80. HeapFree(GetProcessHeap(), 0, buf);
  81. return 0;
  82. }