80d796e954af50e4d7d4d59406f493bb2369b8eb
[platform/upstream/make.git] / w32 / subproc / w32err.c
1 /* Error handling for Windows
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GNU Make.
5
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <windows.h>
19 #include "w32err.h"
20
21 /*
22  * Description: the windows32 version of perror()
23  *
24  * Returns:  a pointer to a static error
25  *
26  * Notes/Dependencies:  I got this from
27  *      comp.os.ms-windows.programmer.win32
28  */
29 char *
30 map_windows32_error_to_string (DWORD ercode) {
31 /* __declspec (thread) necessary if you will use multiple threads on MSVC */
32 #ifdef _MSC_VER
33 __declspec (thread) static char szMessageBuffer[128];
34 #else
35 static char szMessageBuffer[128];
36 #endif
37         /* Fill message buffer with a default message in
38          * case FormatMessage fails
39          */
40     wsprintf (szMessageBuffer, "Error %ld\n", ercode);
41
42         /*
43          *  Special code for winsock error handling.
44          */
45         if (ercode > WSABASEERR) {
46                 HMODULE hModule = GetModuleHandle("wsock32");
47                 if (hModule != NULL) {
48                         FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,
49                                 hModule,
50                                 ercode,
51                                 LANG_NEUTRAL,
52                                 szMessageBuffer,
53                                 sizeof(szMessageBuffer),
54                                 NULL);
55                         FreeLibrary(hModule);
56                 }
57         } else {
58                 /*
59                  *  Default system message handling
60                  */
61         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
62                   NULL,
63                   ercode,
64                   LANG_NEUTRAL,
65                   szMessageBuffer,
66                   sizeof(szMessageBuffer),
67                   NULL);
68         }
69     return szMessageBuffer;
70 }