2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ This program is free software; you can redistribute it and/or modify │
|
|
|
|
│ it under the terms of the GNU General Public License as published by │
|
|
|
|
│ the Free Software Foundation; version 2 of the License. │
|
|
|
|
│ │
|
|
|
|
│ This program is distributed in the hope that it will be useful, but │
|
|
|
|
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
|
|
|
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
|
|
|
│ General Public License for more details. │
|
|
|
|
│ │
|
|
|
|
│ You should have received a copy of the GNU General Public License │
|
|
|
|
│ along with this program; if not, write to the Free Software │
|
|
|
|
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
|
|
|
│ 02110-1301 USA │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/bits/safemacros.h"
|
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/fmt/fmt.h"
|
|
|
|
#include "libc/macros.h"
|
|
|
|
#include "libc/nt/enum/formatmessageflags.h"
|
|
|
|
#include "libc/nt/process.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
|
2020-08-25 11:23:25 +00:00
|
|
|
const char *geterrname(int code) {
|
2020-06-15 14:18:57 +00:00
|
|
|
extern const char kErrnoNames[];
|
|
|
|
const long *e;
|
2020-08-25 11:23:25 +00:00
|
|
|
size_t i, n;
|
|
|
|
e = &E2BIG;
|
|
|
|
n = &EXFULL + 1 - e;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (code == e[i]) {
|
2020-10-27 10:39:46 +00:00
|
|
|
return IndexDoubleNulString(kErrnoNames, i);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts errno value to string.
|
|
|
|
* @return 0 on success, or error code
|
|
|
|
*/
|
|
|
|
int strerror_r(int err, char *buf, size_t size) {
|
|
|
|
const char *s;
|
2020-08-25 11:23:25 +00:00
|
|
|
if (err == -1 || IsTiny()) {
|
|
|
|
s = "?";
|
|
|
|
} else {
|
|
|
|
s = firstnonnull(geterrname(err), "?");
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!SupportsWindows()) {
|
2020-08-25 11:23:25 +00:00
|
|
|
DebugBreak();
|
2020-06-15 14:18:57 +00:00
|
|
|
snprintf(buf, size, "E%s[%d]", s, err);
|
|
|
|
} else {
|
|
|
|
char16_t buf16[100];
|
|
|
|
int winstate, sysvstate;
|
|
|
|
winstate = GetLastError();
|
|
|
|
sysvstate = errno;
|
|
|
|
if (FormatMessage(
|
|
|
|
kNtFormatMessageFromSystem | kNtFormatMessageIgnoreInserts, NULL,
|
|
|
|
err, 0, buf16, ARRAYLEN(buf16) - 1, 0) > 0) {
|
2020-11-09 23:41:11 +00:00
|
|
|
chomp16(buf16);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
|
|
|
buf16[0] = u'\0';
|
|
|
|
}
|
|
|
|
snprintf(buf, size, "E%s/err=%d/errno:%d/GetLastError:%d%s%hs", s, err,
|
|
|
|
sysvstate, winstate, buf16[0] ? " " : "", buf16);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|