wineoss: Fix missing break statement.
[wine.git] / programs / whoami / main.c
blob7e93de998ef9daf6d28b19b20489f75b9310f77d
1 /*
2 * Copyright 2020 Brendan Shanks for CodeWeavers
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.
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.
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
19 #define SECURITY_WIN32
20 #include <windows.h>
21 #include <security.h>
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(whoami);
27 static int output_write(const WCHAR* str, int len)
29 DWORD count;
30 if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL))
32 DWORD lenA;
33 char* strA;
35 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
36 * back to WriteFile() with OEM code page.
38 lenA = WideCharToMultiByte(GetOEMCP(), 0, str, len,
39 NULL, 0, NULL, NULL);
40 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
41 if (!strA)
42 return 0;
44 WideCharToMultiByte(GetOEMCP(), 0, str, len, strA, lenA,
45 NULL, NULL);
46 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
47 HeapFree(GetProcessHeap(), 0, strA);
49 return count;
52 int __cdecl wmain(int argc, WCHAR *argv[])
54 WCHAR *buf = NULL;
55 ULONG size = 0;
56 BOOL result;
58 if (argc > 1)
60 int i;
62 WINE_FIXME("unsupported arguments:");
63 for (i = 0; i < argc; i++)
64 WINE_FIXME(" %s", wine_dbgstr_w(argv[i]));
65 WINE_FIXME("\n");
68 result = GetUserNameExW(NameSamCompatible, NULL, &size);
69 if (result || GetLastError() != ERROR_MORE_DATA)
71 WINE_ERR("GetUserNameExW failed, result %d, error %ld\n", result, GetLastError());
72 return 1;
75 buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
76 if (!buf)
78 WINE_ERR("Memory allocation failed\n");
79 return 1;
82 result = GetUserNameExW(NameSamCompatible, buf, &size);
83 if (result)
85 output_write(buf, size);
86 output_write(L"\r\n", 2);
88 else
89 WINE_ERR("GetUserNameExW failed, error %ld\n", GetLastError());
91 HeapFree(GetProcessHeap(), 0, buf);
92 return 0;