debian: fix build-deps for focal
[amule.git] / src / libs / common / strerror_r.c
bloba692b32aeea6704417a27b78625f427e2b71852d
1 /*
2 * This file is part of the aMule Project.
4 * Copyright (c) 2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 * Any parts of this program derived from the xMule, lMule or eMule project,
7 * or contributed by third-party developers are copyrighted by their
8 * respective authors.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
27 #ifndef _XOPEN_SOURCE
28 # define _XOPEN_SOURCE 600
29 #endif
31 #include <string.h> /* Needed for strerror_r() and size_t */
33 #ifdef HAVE_ERRNO_H
34 # include <errno.h> /* Needed for errno */
35 #endif
37 #ifndef HAVE_STRERROR_R
38 # ifdef HAVE_STRERROR
40 /* Replacement strerror_r() function for systems that don't have any.
41 Note that this replacement function is NOT thread-safe! */
42 int mule_strerror_r(int errnum, char *buf, size_t buflen)
44 char *tmp;
45 if ((buf == NULL) || (buflen == 0)) {
46 errno = ERANGE;
47 return -1;
49 tmp = strerror(errnum);
50 if (tmp == NULL) {
51 errno = EINVAL;
52 return -1;
53 } else {
54 strncpy(buf, tmp, buflen - 1);
55 buf[buflen - 1] = '\0';
56 if (strlen(tmp) >= buflen) {
57 errno = ERANGE;
58 return -1;
61 return 0;
64 # else
66 /* No way to get error description */
67 int mule_strerror_r()
69 #ifdef HAVE_ERRNO_H
70 errno = ENOSYS; /* not implemented */
71 #endif
72 return -1;
75 # endif
76 #else
77 # ifdef STRERROR_R_CHAR_P
79 /* Replacement strerror_r() function for systems that return a char*. */
80 int mule_strerror_r(int errnum, char *buf, size_t buflen)
82 char *tmp = strerror_r(errnum, buf, buflen);
83 if (tmp == NULL) {
84 errno = EINVAL;
85 return -1;
86 } else if (tmp != buf) {
87 if ((buf == NULL) || (buflen == 0)) {
88 errno = ERANGE;
89 return -1;
90 } else {
91 strncpy(buf, tmp, buflen - 1);
92 buf[buflen - 1] = '\0';
93 if (strlen(tmp) >= buflen) {
94 errno = ERANGE;
95 return -1;
99 return 0;
102 # else
104 /* Nothing to do, library strerror_r() is XSI-compliant. */
105 int mule_strerror_r(int errnum, char *buf, size_t buflen)
107 return strerror_r(errnum, buf, buflen);
109 # endif
110 #endif