Fix installation/uninstallation and distribution of Russian manpages
[amule.git] / src / libs / common / strerror_r.c
blobe3bebd5c87bd59a619d2da8ad96ffbffc334003f
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #ifndef _XOPEN_SOURCE
30 # define _XOPEN_SOURCE 600
31 #endif
33 #include <string.h> // Needed for strerror_r() and size_t
35 #ifdef HAVE_ERRNO_H
36 # include <errno.h> // Needed for errno
37 #endif
39 #ifndef HAVE_STRERROR_R
40 # ifdef HAVE_STRERROR
42 /* Replacement strerror_r() function for systems that don't have any.
43 Note that this replacement function is NOT thread-safe! */
44 int mule_strerror_r(int errnum, char *buf, size_t buflen)
46 char *tmp;
47 if ((buf == NULL) || (buflen == 0)) {
48 errno = ERANGE;
49 return -1;
51 tmp = strerror(errnum);
52 if (tmp == NULL) {
53 errno = EINVAL;
54 return -1;
55 } else {
56 strncpy(buf, tmp, buflen - 1);
57 buf[buflen - 1] = '\0';
58 if (strlen(tmp) >= buflen) {
59 errno = ERANGE;
60 return -1;
63 return 0;
66 # else
68 /* No way to get error description */
69 int mule_strerror_r()
71 #ifdef HAVE_ERRNO_H
72 errno = ENOSYS; /* not implemented */
73 #endif
74 return -1;
77 # endif
78 #else
79 # ifdef STRERROR_R_CHAR_P
81 /* Replacement strerror_r() function for systems that return a char*. */
82 int mule_strerror_r(int errnum, char *buf, size_t buflen)
84 char *tmp = strerror_r(errnum, buf, buflen);
85 if (tmp == NULL) {
86 errno = EINVAL;
87 return -1;
88 } else if (tmp != buf) {
89 if ((buf == NULL) || (buflen == 0)) {
90 errno = ERANGE;
91 return -1;
92 } else {
93 strncpy(buf, tmp, buflen - 1);
94 buf[buflen - 1] = '\0';
95 if (strlen(tmp) >= buflen) {
96 errno = ERANGE;
97 return -1;
101 return 0;
104 # else
106 /* Nothing to do, library strerror_r() is XSI-compliant. */
107 int mule_strerror_r(int errnum, char *buf, size_t buflen)
109 return strerror_r(errnum, buf, buflen);
111 # endif
112 #endif