| 1 | /* Host support routines for MinGW, for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 2006 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | Boston, MA 02110-1301, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | |
| 25 | #include "gdb_string.h" |
| 26 | |
| 27 | #include <windows.h> |
| 28 | |
| 29 | /* The strerror() function can return NULL for errno values that are |
| 30 | out of range. Provide a "safe" version that always returns a |
| 31 | printable string. |
| 32 | |
| 33 | The Windows runtime implementation of strerror never returns NULL, |
| 34 | but does return a useless string for anything above sys_nerr; |
| 35 | unfortunately this includes all socket-related error codes. |
| 36 | This replacement tries to find a system-provided error message. */ |
| 37 | |
| 38 | char * |
| 39 | safe_strerror (int errnum) |
| 40 | { |
| 41 | static char *buffer; |
| 42 | int len; |
| 43 | |
| 44 | if (errnum >= 0 && errnum < sys_nerr) |
| 45 | return strerror (errnum); |
| 46 | |
| 47 | if (buffer) |
| 48 | { |
| 49 | LocalFree (buffer); |
| 50 | buffer = NULL; |
| 51 | } |
| 52 | |
| 53 | if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 54 | | FORMAT_MESSAGE_FROM_SYSTEM, |
| 55 | NULL, errnum, |
| 56 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 57 | (LPTSTR) &buffer, 0, NULL) == 0) |
| 58 | { |
| 59 | static char buf[32]; |
| 60 | xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum); |
| 61 | return buf; |
| 62 | } |
| 63 | |
| 64 | /* Windows error messages end with a period and a CR-LF; strip that |
| 65 | out. */ |
| 66 | len = strlen (buffer); |
| 67 | if (len > 3 && strcmp (buffer + len - 3, ".\r\n") == 0) |
| 68 | buffer[len - 3] = '\0'; |
| 69 | |
| 70 | return buffer; |
| 71 | } |