mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
3396c97da9
Now that the new CMake build system is encouraging us to lay out the code like a set of libraries, it seems like a good idea to make them look more _like_ libraries, by putting things into separate modules as far as possible. This fixes several previous annoyances in which you had to link against some object in order to get a function you needed, but that object also contained other functions you didn't need which included link-time symbol references you didn't want to have to deal with. The usual offender was subsidiary supporting programs including misc.c for some innocuous function and then finding they had to deal with the requirements of buildinfo(). This big reorganisation introduces three new subdirectories called 'utils', one at the top level and one in each platform subdir. In each case, the directory contains basically the same files that were previously placed in the 'utils' build-time library, except that the ones that were extremely miscellaneous (misc.c, utils.c, uxmisc.c, winmisc.c, winmiscs.c, winutils.c) have been split up into much smaller pieces.
73 lines
2.1 KiB
C
73 lines
2.1 KiB
C
/*
|
|
* Wrapper around the Windows FormatMessage system for retrieving the
|
|
* text of a system error code, with a simple API similar to strerror.
|
|
*
|
|
* Works by keeping a tree234 containing mappings from system error
|
|
* codes to strings. Entries allocated in this tree are simply never
|
|
* freed.
|
|
*
|
|
* Also, the returned string has its trailing newline removed (so it
|
|
* can go in places like the Event Log that never want a newline), and
|
|
* is prefixed with the error number (so that if a user sends an error
|
|
* report containing a translated error message we can't read, we can
|
|
* still find out what the error actually was).
|
|
*/
|
|
|
|
#include "putty.h"
|
|
|
|
struct errstring {
|
|
int error;
|
|
char *text;
|
|
};
|
|
|
|
static int errstring_find(void *av, void *bv)
|
|
{
|
|
int *a = (int *)av;
|
|
struct errstring *b = (struct errstring *)bv;
|
|
if (*a < b->error)
|
|
return -1;
|
|
if (*a > b->error)
|
|
return +1;
|
|
return 0;
|
|
}
|
|
static int errstring_compare(void *av, void *bv)
|
|
{
|
|
struct errstring *a = (struct errstring *)av;
|
|
return errstring_find(&a->error, bv);
|
|
}
|
|
|
|
static tree234 *errstrings = NULL;
|
|
|
|
const char *win_strerror(int error)
|
|
{
|
|
struct errstring *es;
|
|
|
|
if (!errstrings)
|
|
errstrings = newtree234(errstring_compare);
|
|
|
|
es = find234(errstrings, &error, errstring_find);
|
|
|
|
if (!es) {
|
|
char msgtext[65536]; /* maximum size for FormatMessage is 64K */
|
|
|
|
es = snew(struct errstring);
|
|
es->error = error;
|
|
if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
msgtext, lenof(msgtext)-1, NULL)) {
|
|
sprintf(msgtext,
|
|
"(unable to format: FormatMessage returned %u)",
|
|
(unsigned int)GetLastError());
|
|
} else {
|
|
int len = strlen(msgtext);
|
|
if (len > 0 && msgtext[len-1] == '\n')
|
|
msgtext[len-1] = '\0';
|
|
}
|
|
es->text = dupprintf("Error %d: %s", error, msgtext);
|
|
add234(errstrings, es);
|
|
}
|
|
|
|
return es->text;
|
|
}
|