shared code (new strerror stuff)

This commit is contained in:
Jean-Francois Dockes 2020-01-13 10:23:53 +01:00
parent 1ffd54f834
commit a9ae4de43c
2 changed files with 102 additions and 111 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 J.F.Dockes /* Copyright (C) 2014-2019 J.F.Dockes
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or * the Free Software Foundation; either version 2.1 of the License, or
@ -14,23 +14,6 @@
* Free Software Foundation, Inc., * Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
/* Copyright (C) 2006-2016 J.F.Dockes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef _LOG_H_X_INCLUDED_ #ifndef _LOG_H_X_INCLUDED_
#define _LOG_H_X_INCLUDED_ #define _LOG_H_X_INCLUDED_
@ -198,20 +181,18 @@ private:
LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \ LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
": " << strerror(errno) << std::endl); \ ": " << strerror(errno) << std::endl); \
} }
#else // !WINDOWS-> #else // not WINDOWS or sun
#if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
#define LOGSYSERR(who, what, arg) { \ inline char *_log_check_strerror_r(int, char *errbuf) {return errbuf;}
char buf[200]; buf[0] = 0; strerror_r(errno, buf, 200); \ inline char *_log_check_strerror_r(char *cp, char *){return cp;}
LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
": " << buf << std::endl); \
}
#else
#define LOGSYSERR(who, what, arg) { \ #define LOGSYSERR(who, what, arg) { \
char buf[200]; buf[0] = 0; \ char buf[200]; buf[0] = 0; \
LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \ LOGERR(who << ": " << what << "(" << arg << "): errno " << errno << \
": " << strerror_r(errno, buf, 200) << std::endl); \ ": " << _log_check_strerror_r( \
strerror_r(errno, buf, 200), buf) << std::endl); \
} }
#endif
#endif // not windows #endif // not windows
#endif /* _LOG_H_X_INCLUDED_ */ #endif /* _LOG_H_X_INCLUDED_ */

View File

@ -1164,6 +1164,16 @@ secondelt:
return true; return true;
} }
// We'd like these static, but then, as only one is used, this
// triggers a 'defined but not used' warning.
char *_check_strerror_r(int, char *errbuf)
{
return errbuf;
}
char *_check_strerror_r(char *cp, char *)
{
return cp;
}
void catstrerror(string *reason, const char *what, int _errno) void catstrerror(string *reason, const char *what, int _errno)
{ {
@ -1186,8 +1196,6 @@ void catstrerror(string *reason, const char *what, int _errno)
// Note: sun strerror is noted mt-safe ?? // Note: sun strerror is noted mt-safe ??
reason->append(strerror(_errno)); reason->append(strerror(_errno));
#else #else
#define ERRBUFSZ 200
char errbuf[ERRBUFSZ];
// There are 2 versions of strerror_r. // There are 2 versions of strerror_r.
// - The GNU one returns a pointer to the message (maybe // - The GNU one returns a pointer to the message (maybe
// static storage or supplied buffer). // static storage or supplied buffer).
@ -1198,13 +1206,15 @@ void catstrerror(string *reason, const char *what, int _errno)
// were returned a pointer... // were returned a pointer...
// Also couldn't find an easy way to disable the gnu version without // Also couldn't find an easy way to disable the gnu version without
// changing the cxxflags globally, so forget it. Recent gnu lib versions // changing the cxxflags globally, so forget it. Recent gnu lib versions
// normally default to the posix version. // normally default to the posix version. (not !)
// At worse we get no message at all here. // The feature defines tests are too complicated and seem unreliable.
// In short it's a mess, but thanks to c++ function overloading and smart
// people, we have a solution:
// https://www.zverovich.net/2015/03/13/reliable-detection-of-strerror-variants.html
char errbuf[200];
errbuf[0] = 0; errbuf[0] = 0;
// We don't use ret, it's there to silence a cc warning reason->append(_check_strerror_r(
auto ret = strerror_r(_errno, errbuf, ERRBUFSZ); strerror_r(_errno, errbuf, sizeof(errbuf)), errbuf));
(void)ret;
reason->append(errbuf);
#endif #endif
} }
@ -1232,7 +1242,7 @@ static std::unordered_map<string, string> lang_to_code {
{"th", "iso-8859-11"}, {"th", "iso-8859-11"},
{"tr", "iso-8859-9"}, {"tr", "iso-8859-9"},
{"uk", "koi8-u"}, {"uk", "koi8-u"},
}; };
static const string cstr_cp1252("CP1252"); static const string cstr_cp1252("CP1252");
string langtocode(const string& lang) string langtocode(const string& lang)