source: trunk/src/kmk/w32/subproc/w32err.c

Last change on this file was 3625, checked in by bird, 13 months ago

kmk/w32err.c: Use safe snprintf instead of wsprintfA from USER32.DLL - we don't need that DLL.

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1/* Error handling for Windows
2Copyright (C) 1996-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include <stdlib.h>
18#include <windows.h>
19#include "makeint.h"
20#include "w32err.h"
21
22/*
23 * Description: the windows32 version of perror()
24 *
25 * Returns: a pointer to a static error
26 *
27 * Notes/Dependencies: I got this from
28 * comp.os.ms-windows.programmer.win32
29 */
30const char *
31map_windows32_error_to_string (DWORD ercode) {
32/*
33 * We used to have an MSVC-specific '__declspec (thread)' qualifier
34 * here, with the following comment:
35 *
36 * __declspec (thread) necessary if you will use multiple threads on MSVC
37 *
38 * However, Make was never multithreaded on Windows (except when
39 * Ctrl-C is hit, in which case the main thread is stopped
40 * immediately, so it doesn't matter in this context). The functions
41 * on sub_proc.c that started and stopped additional threads were
42 * never used, and are now #ifdef'ed away. Until we need more than
43 * one thread, we have no problems with the following buffer being
44 * static. (If and when we do need it to be in thread-local storage,
45 * the corresponding GCC qualifier is '__thread'.)
46 */
47 static char szMessageBuffer[128];
48 /* Fill message buffer with a default message in
49 * case FormatMessage fails
50 */
51#ifdef KMK /* Avoid unnecessary unsafe USER32.DLL sprintf. */
52 snprintf (szMessageBuffer, sizeof(szMessageBuffer), "Error %ld\n", ercode);
53#else
54 wsprintf (szMessageBuffer, "Error %ld\n", ercode);
55#endif
56
57 /*
58 * Special code for winsock error handling.
59 */
60 if (ercode > WSABASEERR) {
61#if 0
62 HMODULE hModule = GetModuleHandle("wsock32");
63 if (hModule != NULL) {
64 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,
65 hModule,
66 ercode,
67 LANG_NEUTRAL,
68 szMessageBuffer,
69 sizeof(szMessageBuffer),
70 NULL);
71 FreeLibrary(hModule);
72 }
73#else
74 O (fatal, NILF, szMessageBuffer);
75#endif
76 } else {
77 /*
78 * Default system message handling
79 */
80 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
81 NULL,
82 ercode,
83 LANG_NEUTRAL,
84 szMessageBuffer,
85 sizeof(szMessageBuffer),
86 NULL);
87 }
88 return szMessageBuffer;
89}
Note: See TracBrowser for help on using the repository browser.