1 /* Safe version of strerror for MinGW, for GDB, the GNU debugger.
3 Copyright (C) 2006-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
24 /* Implementation of safe_strerror as defined in common-utils.h.
26 The Windows runtime implementation of strerror never returns NULL,
27 but does return a useless string for anything above sys_nerr;
28 unfortunately this includes all socket-related error codes.
29 This replacement tries to find a system-provided error message. */
32 safe_strerror (int errnum
)
37 if (errnum
>= 0 && errnum
< sys_nerr
)
38 return strerror (errnum
);
46 if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
47 | FORMAT_MESSAGE_FROM_SYSTEM
,
49 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
50 (LPTSTR
) &buffer
, 0, NULL
) == 0)
53 xsnprintf (buf
, sizeof buf
, "(undocumented errno %d)", errnum
);
57 /* Windows error messages end with a period and a CR-LF; strip that
59 len
= strlen (buffer
);
60 if (len
> 3 && strcmp (buffer
+ len
- 3, ".\r\n") == 0)
61 buffer
[len
- 3] = '\0';