#include <9pm/windows.h>
#include <9pm/u.h>
#include <9pm/libc.h>

DWORD WINAPI
FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId,
	DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, va_list *Arguments)
{
	char *buf;
	int n;

	assert(Arguments == 0);

	if(win_useunicode)
		return FormatMessageW(dwFlags, lpSource, dwMessageId,
			dwLanguageId, lpBuffer, nSize, Arguments);

	buf = win_malloc(UTFmax*nSize);
	n = FormatMessageA(dwFlags, lpSource, dwMessageId,
			dwLanguageId, buf, UTFmax*nSize, Arguments);
	if(n == 0) {
		if(nSize)
			lpBuffer[0] = 0;
		return 0;
	}
	n = win_utf2wstrn(lpBuffer, nSize, buf);
	win_free(buf);
	if(n >= (int)nSize)
		return 0;
	return n;
}
