summaryrefslogtreecommitdiff
path: root/lib9/errstr-Nt.c
blob: 53c35323f027425bcd686776a78080fa1ca8924e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "lib9.h"
#include <Windows.h>

static char	errstring[ERRMAX];

enum
{
	Magic = 0xffffff
};

static void
winerror(int e, char *buf, uint nerr)
{
	int r;
	char buf2[ERRMAX], *p, *q;
	
	r = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
		0, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		buf2, sizeof(buf2), 0);

	if(r == 0)
		snprint(buf2, ERRMAX, "windows error %d", e);

	q = buf2;
	for(p = buf2; *p; p++) {
		if(*p == '\r')
			continue;
		if(*p == '\n')
			*q++ = ' ';
		else
			*q++ = *p;
	}
	*q = '\0';
	utfecpy(buf, buf+nerr, buf2);
}

void
werrstr(char *fmt, ...)
{
	va_list arg;

	va_start(arg, fmt);
	vseprint(errstring, errstring+sizeof(errstring), fmt, arg);
	va_end(arg);
	SetLastError(Magic);
}

int
errstr(char *buf, uint nerr)
{
	DWORD le;

	le = GetLastError();
	if(le == Magic)
		utfecpy(buf, buf+nerr, errstring);
	else
		winerror(le, buf, nerr);
	return 1;
}

void
oserrstr(char *buf, uint nerr)
{
	winerror(GetLastError(), buf, nerr);
}