Simplify usage of strerror_r.

- Define STRERROR_R_CHAR_P properly on CMake and use it on futils.cpp
- We use now _GNU_SOURCE to determine which version of strerror_r to use
- strError only throws if ret==ERANGE
- Remove STRERROR_R_CHAR_P. It has been never used
- sort defines in config.h.cmake
- Remove EXV_HAVE_DECL_STRERROR_R that is not used anymore
- Remove EXV_HAVE_STRERROR. C++98 always will have it
This commit is contained in:
Luis Díaz Más 2018-04-29 20:31:51 +02:00
parent b8b73fdcf6
commit e32323d7c7
6 changed files with 18 additions and 57 deletions

View File

@ -25,8 +25,8 @@
// Define if you want video support.
#cmakedefine EXV_ENABLE_VIDEO
// Define if you have correct declaration of strerror_r().
#cmakedefine EXV_HAVE_DECL_STRERROR_R
// Define if you have the strerror_r function.
#cmakedefine EXV_HAVE_STRERROR_R
// Define to enable the Windows unicode path support.
#cmakedefine EXV_UNICODE_PATH
@ -57,15 +57,6 @@
#endif
#endif
// Define if you have the strerror function.
#cmakedefine EXV_HAVE_STRERROR
// Define if you have the strerror_r function.
#cmakedefine EXV_HAVE_STRERROR_R
// Define if strerror_r returns char *.
#cmakedefine STRERROR_R_CHAR_P
// Define if you have the <strings.h> header file.
#cmakedefine EXV_HAVE_STRINGS_H

View File

@ -22,12 +22,9 @@ set(EXV_UNICODE_PATH ${EXIV2_ENABLE_WIN_UNICODE})
check_function_exists( gmtime_r EXV_HAVE_GMTIME_R )
check_function_exists( mmap EXV_HAVE_MMAP )
check_function_exists( munmap EXV_HAVE_MUNMAP )
check_function_exists( strerror EXV_HAVE_STRERROR )
check_function_exists( strerror_r EXV_HAVE_STRERROR_R )
check_function_exists( timegm EXV_HAVE_TIMEGM )
# TODO : Do something about EXV_STRERROR_R_CHAR_P
# TODO: This check should be removed and rely on the check done in findDependencies.cmake
check_include_file( "libintl.h" EXV_HAVE_LIBINTL_H )
check_include_file( "unistd.h" EXV_HAVE_UNISTD_H )
@ -46,20 +43,4 @@ if (NOT EXV_HAVE_LIBINTL_H)
set(EXV_ENABLE_NLS 0)
endif()
include(CheckCSourceCompiles)
#####################################################################################
# strerror_r returns char*
# NOTE : reverting commit #2041, which break compilation under linux and windows
CHECK_C_SOURCE_COMPILES( "#include <string.h>
int main() {
char * c;
c = strerror_r(0,c,0);
return 0;
}" EXV_HAVE_DECL_STRERROR_R )
#####################################################################################
configure_file( config/config.h.cmake ${CMAKE_BINARY_DIR}/exv_conf.h @ONLY)

View File

@ -144,11 +144,6 @@ typedef int pid_t;
///// End symbol visibility /////////
///// Start of platform marcos /////////
// Linux GCC 4.8 appears to be confused about strerror_r
#if !defined(EXV_STRERROR_R_CHAR_P) && defined( __gnu_linux__) && defined(__GLIBC__)
#define EXV_STRERROR_R_CHAR_P
#endif
#if defined(__MINGW32__) || defined(__MINGW64__)
# ifndef __MING__
# define __MING__ 1

View File

@ -14,10 +14,6 @@
/* Define to 1 if you have the `alarm' function. */
/* #undef EXV_HAVE_ALARM */
/* Define to 1 if you have the declaration of `strerror_r', and to 0 if you
don't. */
/* #undef EXV_HAVE_DECL_STRERROR_R */
/* Define to 1 if you have the `gmtime_r' function. */
/* #undef EXV_HAVE_GMTIME_R */
@ -82,8 +78,8 @@
/* Define to 1 if you have the <stdint.h> header file. */
/* #undef EXV_HAVE_STDINT_H */
/* Define to 1 if you have the `strerror' function. */
#define EXV_HAVE_STRERROR 1
/* Define to 1 if you have the <stdlib.h> header file. */
/* #undef EXV_HAVE_STDLIB_H */
/* Define to 1 if you have the `strerror_r' function. */
/* #undef EXV_HAVE_STRERROR_R */

View File

@ -29,6 +29,7 @@
#include "config.h"
#include "futils.hpp"
#include "enforce.hpp"
// + standard includes
#include <sys/types.h>
@ -46,12 +47,12 @@
#include <algorithm>
#include <stdexcept>
#if defined EXV_HAVE_STRERROR_R && !defined EXV_HAVE_DECL_STRERROR_R
# ifdef EXV_STRERROR_R_CHAR_P
#ifdef EXV_HAVE_STRERROR_R
#ifdef _GNU_SOURCE
extern char *strerror_r(int errnum, char *buf, size_t n);
# else
#else
extern int strerror_r(int errnum, char *buf, size_t n);
# endif
#endif
#endif
namespace Exiv2 {
@ -331,27 +332,30 @@ namespace Exiv2 {
else return path.substr(found);
}
#endif
std::string strError()
{
int error = errno;
std::ostringstream os;
#ifdef EXV_HAVE_STRERROR_R
const size_t n = 1024;
// _GNU_SOURCE: See Debian bug #485135
# if defined EXV_STRERROR_R_CHAR_P && defined _GNU_SOURCE
#ifdef _GNU_SOURCE
char *buf = 0;
char buf2[n];
std::memset(buf2, 0x0, n);
buf = strerror_r(error, buf2, n);
# else
#else
char buf[n];
std::memset(buf, 0x0, n);
strerror_r(error, buf, n);
# endif
const int ret = strerror_r(error, buf, n);
enforce(ret != ERANGE, Exiv2::kerCallFailed);
#endif
os << buf;
// Issue# 908.
// report strerror() if strerror_r() returns empty
if ( !buf[0] ) os << strerror(error);
if ( !buf[0] ) {
os << strerror(error);
}
#else
os << std::strerror(error);
#endif

View File

@ -259,7 +259,6 @@ void Exiv2::dumpLibraryInfo(std::ostream& os,const exv_grep_keys_t& keys)
int have_stdint =0;
int have_stdlib =0;
int have_strlib =0;
int have_strerror =0;
int have_strerror_r =0;
int have_strings_h =0;
int have_mmap =0;
@ -326,10 +325,6 @@ void Exiv2::dumpLibraryInfo(std::ostream& os,const exv_grep_keys_t& keys)
have_stdlib=1;
#endif
#ifdef EXV_HAVE_STRERROR
have_strerror=1;
#endif
#ifdef EXV_HAVE_STRERROR_R
have_strerror_r=1;
#endif
@ -500,7 +495,6 @@ void Exiv2::dumpLibraryInfo(std::ostream& os,const exv_grep_keys_t& keys)
output(os,keys,"have_stdint" ,have_stdint );
output(os,keys,"have_stdlib" ,have_stdlib );
output(os,keys,"have_strlib" ,have_strlib );
output(os,keys,"have_strerror" ,have_strerror );
output(os,keys,"have_strerror_r" ,have_strerror_r );
output(os,keys,"have_strings_h" ,have_strings_h );
output(os,keys,"have_mmap" ,have_mmap );