1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
/*
 * This file is part of the aMule Project.
 *
 * Copyright (c) 2011 aMule Team ( admin@amule.org / http://www.amule.org )
 *
 * Any parts of this program derived from the xMule, lMule or eMule project,
 * or contributed by third-party developers are copyrighted by their
 * respective authors.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include "config.h"

#ifndef _XOPEN_SOURCE
#	define _XOPEN_SOURCE	600
#endif

#include <string.h>			/* Needed for strerror_r() and size_t */

#ifdef HAVE_ERRNO_H
#	include <errno.h>		/* Needed for errno */
#endif

#ifndef HAVE_STRERROR_R
#	ifdef HAVE_STRERROR

/* Replacement strerror_r() function for systems that don't have any.
   Note that this replacement function is NOT thread-safe! */
int mule_strerror_r(int errnum, char *buf, size_t buflen)
{
	char *tmp;<--- Variable 'tmp' can be declared as pointer to const
	if ((buf == NULL) || (buflen == 0)) {
		errno = ERANGE;
		return -1;
	}
	tmp = strerror(errnum);
	if (tmp == NULL) {
		errno = EINVAL;
		return -1;
	} else {
		strncpy(buf, tmp, buflen - 1);
		buf[buflen - 1] = '\0';
		if (strlen(tmp) >= buflen) {
			errno = ERANGE;
			return -1;
		}
	}
	return 0;
}

#	else

/* No way to get error description */
int mule_strerror_r()
{
#ifdef HAVE_ERRNO_H
	errno = ENOSYS;	/* not implemented */
#endif
	return -1;
}

#	endif
#else
#	ifdef STRERROR_R_CHAR_P

/* Replacement strerror_r() function for systems that return a char*. */
int mule_strerror_r(int errnum, char *buf, size_t buflen)
{
	char *tmp = strerror_r(errnum, buf, buflen);<--- Variable 'tmp' can be declared as pointer to const
	if (tmp == NULL) {
		errno = EINVAL;
		return -1;
	} else if (tmp != buf) {
		if ((buf == NULL) || (buflen == 0)) {
			errno = ERANGE;
			return -1;
		} else {
			strncpy(buf, tmp, buflen - 1);
			buf[buflen - 1] = '\0';
			if (strlen(tmp) >= buflen) {
				errno = ERANGE;
				return -1;
			}
		}
	}
	return 0;
}

#	else

/* Nothing to do, library strerror_r() is XSI-compliant. */
int mule_strerror_r(int errnum, char *buf, size_t buflen)
{
	return strerror_r(errnum, buf, buflen);
}
#	endif
#endif