From f0128dd11d08d79384a97a13e44476769b061f4f Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 10 Mar 2009 11:19:39 +0100 Subject: [PATCH] Error utils in separate file and _REENTRANT mode. The functions yaz_errno / yaz_set_errno / yaz_strerror is defined in errno.c rather than nmem.c. Also _REENTRANT is always set to ensure that access to errno is thread-aware. --- src/Makefile.am | 2 +- src/errno.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/nmem.c | 59 ------------------------------------- win/makefile | 1 + 4 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 src/errno.c diff --git a/src/Makefile.am b/src/Makefile.am index 49d5c56..dc07454 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -75,7 +75,7 @@ GEN_FILES = oid_std.c \ libyaz_la_SOURCES=version.c options.c log.c \ $(GEN_FILES) \ marcdisp.c marc_read_xml.c marc_read_iso2709.c marc_read_line.c \ - wrbuf.c oid_db.c \ + wrbuf.c oid_db.c errno.c \ nmemsdup.c xmalloc.c readconf.c tpath.c nmem.c matchstr.c atoin.c \ siconv.c iconv-p.h utf8.c ucs4.c iso5428.c advancegreek.c \ odr_bool.c ber_bool.c ber_len.c ber_tag.c odr_util.c \ diff --git a/src/errno.c b/src/errno.c new file mode 100644 index 0000000..6cadb04 --- /dev/null +++ b/src/errno.c @@ -0,0 +1,87 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2009 Index Data + * See the file LICENSE for details. + */ + +/** + * \file errno.c + * \brief errno utilities + */ +#if HAVE_CONFIG_H +#include +#endif + +/* prepare for threads.. even in non-threaded appliactions. + The yaz_errno/yaz_set_errno is part of core YAZ and shared */ +#ifndef _REENTRANT +#define _REENTRANT +#endif + +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include +#endif + +int yaz_errno(void) +{ + return errno; +} + +void yaz_set_errno(int v) +{ + errno = v; +} + +void yaz_strerror(char *buf, int max) +{ +#ifdef WIN32 + DWORD err; +#endif + char *cp; +#ifdef WIN32 + err = GetLastError(); + if (err) + { + FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */ + (LPTSTR) buf, + max-1, + NULL); + } + else + *buf = '\0'; +#else +/* UNIX */ +#if HAVE_STRERROR_R + *buf = '\0'; + strerror_r(errno, buf, max); + /* if buffer is unset - use strerror anyway (GLIBC bug) */ + if (*buf == '\0') + strcpy(buf, strerror(yaz_errno())); +#else + strcpy(buf, strerror(yaz_errno())); +#endif +/* UNIX */ +#endif + if ((cp = strrchr(buf, '\n'))) + *cp = '\0'; + if ((cp = strrchr(buf, '\r'))) + *cp = '\0'; +} +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/nmem.c b/src/nmem.c index ec4abea..93988b5 100644 --- a/src/nmem.c +++ b/src/nmem.c @@ -26,10 +26,6 @@ #include #include -#ifdef WIN32 -#include -#endif - #define NMEM_CHUNK (4*1024) struct nmem_block @@ -184,61 +180,6 @@ void nmem_transfer (NMEM dst, NMEM src) src->total = 0; } -int yaz_errno(void) -{ - return errno; -} - -void yaz_set_errno(int v) -{ - errno = v; -} - -void yaz_strerror(char *buf, int max) -{ -#ifdef WIN32 - DWORD err; -#endif - char *cp; - if (!log_level_initialized) - { - log_level = yaz_log_module_level("nmem"); - log_level_initialized = 1; - } - -#ifdef WIN32 - err = GetLastError(); - if (err) - { - FormatMessage( - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - err, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */ - (LPTSTR) buf, - max-1, - NULL); - } - else - *buf = '\0'; -#else -/* UNIX */ -#if HAVE_STRERROR_R - *buf = '\0'; - strerror_r(errno, buf, max); - /* if buffer is unset - use strerror anyway (GLIBC bug) */ - if (*buf == '\0') - strcpy(buf, strerror(yaz_errno())); -#else - strcpy(buf, strerror(yaz_errno())); -#endif -/* UNIX */ -#endif - if ((cp = strrchr(buf, '\n'))) - *cp = '\0'; - if ((cp = strrchr(buf, '\r'))) - *cp = '\0'; -} /* * Local variables: * c-basic-offset: 4 diff --git a/win/makefile b/win/makefile index 82d8771..8f45dd3 100644 --- a/win/makefile +++ b/win/makefile @@ -403,6 +403,7 @@ MISC_OBJS= \ $(OBJDIR)\ber_oid.obj \ $(OBJDIR)\ber_tag.obj \ $(OBJDIR)\dumpber.obj \ + $(OBJDIR)\errno.obj \ $(OBJDIR)\odr.obj \ $(OBJDIR)\odr_any.obj \ $(OBJDIR)\odr_bit.obj \ -- 1.7.10.4