msvcrt: Import truncf implementation from musl.
[wine.git] / programs / whoami / main.c
blob297f12c9a911f78d3e60a70f2cc04c5e131bfc8a
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 ret, count;
30 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL);
31 if (!ret)
33 DWORD lenA;
34 char* strA;
36 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
37 * back to WriteFile(), assuming the console encoding is still the right
38 * one in that case.
40 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len,
41 NULL, 0, NULL, NULL);
42 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
43 if (!strA)
44 return 0;
46 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA,
47 NULL, NULL);
48 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
49 HeapFree(GetProcessHeap(), 0, strA);
51 return count;
54 int __cdecl wmain(int argc, WCHAR *argv[])
56 WCHAR *buf = NULL;
57 ULONG size = 0;
58 BOOL result;
60 if (argc > 1)
62 int i;
64 WINE_FIXME("unsupported arguments:");
65 for (i = 0; i < argc; i++)
66 WINE_FIXME(" %s", wine_dbgstr_w(argv[i]));
67 WINE_FIXME("\n");
70 result = GetUserNameExW(NameSamCompatible, NULL, &size);
71 if (result || GetLastError() != ERROR_MORE_DATA)
73 WINE_ERR("GetUserNameExW failed, result %d, error %d\n", result, GetLastError());
74 return 1;
77 buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
78 if (!buf)
80 WINE_ERR("Memory allocation failed\n");
81 return 1;
84 result = GetUserNameExW(NameSamCompatible, buf, &size);
85 if (result)
87 output_write(buf, size);
88 output_write(L"\r\n", 2);
90 else
91 WINE_ERR("GetUserNameExW failed, error %d\n", GetLastError());
93 HeapFree(GetProcessHeap(), 0, buf);
94 return 0;