/src/open5gs/lib/core/ogs-errno.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "core-config-private.h" |
21 | | |
22 | | #include "ogs-core.h" |
23 | | |
24 | | char *ogs_strerror(ogs_err_t err, char *buf, size_t size) |
25 | 74 | { |
26 | | #if defined(_WIN32) |
27 | | /* |
28 | | * The following code is stolen from APR Library |
29 | | * http://svn.apache.org/repos/asf/apr/apr/trunk/misc/unix/errorcodes.c |
30 | | */ |
31 | | size_t len = 0, i; |
32 | | LPTSTR msg = (LPTSTR)buf; |
33 | | len = FormatMessage( |
34 | | FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
35 | | NULL, |
36 | | err, |
37 | | MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), /* Default Language */ |
38 | | msg, |
39 | | (DWORD)(size/sizeof(TCHAR)), |
40 | | NULL); |
41 | | |
42 | | if (len) { |
43 | | /* FormatMessage put the message in the buffer, but it may |
44 | | * have embedded a newline (\r\n), and possible more than one. |
45 | | * Remove the newlines replacing them with a space. This is not |
46 | | * as visually perfect as moving all the remaining message over, |
47 | | * but more efficient. |
48 | | */ |
49 | | i = len; |
50 | | while (i) { |
51 | | i--; |
52 | | if ((buf[i] == '\r') || (buf[i] == '\n')) |
53 | | buf[i] = ' '; |
54 | | } |
55 | | } |
56 | | else { |
57 | | /* Windows didn't provide us with a message. Even stuff like */ |
58 | | ogs_snprintf(buf, size, "Unrecognized Win32 error code %d", (int)err); |
59 | | } |
60 | | |
61 | | return buf; |
62 | | #elif defined(HAVE_STRERROR_R) |
63 | | |
64 | 74 | #if defined(STRERROR_R_CHAR_P) |
65 | 74 | return strerror_r(err, buf, size); |
66 | | #else |
67 | | int ret = strerror_r(err, buf, size); |
68 | | |
69 | | if (ret == 0) |
70 | | return buf; |
71 | | else |
72 | | return NULL; |
73 | | #endif |
74 | | |
75 | | #else |
76 | | #error TODO |
77 | | #endif |
78 | 74 | } |