# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../csprng/0002-utils-fix-portability-of-error.h-usage.patch
# Copyright (C) 2013 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# This patch file is dual-licensed. It is available under the license the
# patched project is licensed under, as long as it is an OpenSource license
# as defined at http://www.opensource.org/ (e.g. BSD, X11) or 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.
# --- SDE-COPYRIGHT-NOTE-END ---

From 752194d531bd7f8bdd49b51b32c11b95503d109f Mon Sep 17 00:00:00 2001
From: Christian Wiese <chris@opensde.org>
Date: Thu, 5 Sep 2013 09:57:46 +0200
Subject: [PATCH] utils: fix portability of error.h usage

error(3) is an non-portable gnu extension thus we need to handle the
case when it is not available, like in the case of musl libc which is
not providing this extension.

From what I have tested so far error.h is only available in glibc and uclibc,
but not in musl libc.

Signed-off-by: Christian Wiese <chris@opensde.org>
---
 configure.ac                    |    1 +
 include/internal/csprng_error.h |   19 +++++++++++++++++++
 test/Makefile.am                |    1 +
 test/havege_main.c              |    2 +-
 test/http_main.c                |    2 +-
 test/memt_main.c                |    2 +-
 test/openssl-rand_main.c        |    2 +-
 test/qrbg_main.c                |    2 +-
 test/sha1_main.c                |    2 +-
 utils/csprng-generate.c         |    2 +-
 utils/csprngd.c                 |    3 ++-
 utils/random_interface_linux.c  |    3 ++-
 12 files changed, 32 insertions(+), 9 deletions(-)
 create mode 100644 include/internal/csprng_error.h

diff --git a/configure.ac b/configure.ac
index 12fd277..bce1891 100755
--- a/configure.ac
+++ b/configure.ac
@@ -91,6 +91,7 @@ AC_CHECK_HEADERS([openssl/err.h openssl/rand.h openssl/sha.h openssl/bn.h],[],[A
 ## Checks for header files.
 AC_HEADER_STDC
 AC_HEADER_SYS_WAIT
+AC_CHECK_HEADERS([error.h])
 AC_CHECK_HEADERS([fcntl.h])
 AC_CHECK_HEADERS([stdlib.h])
 AC_CHECK_HEADERS([string.h])
diff --git a/include/internal/csprng_error.h b/include/internal/csprng_error.h
new file mode 100644
index 0000000..2c18414
--- /dev/null
+++ b/include/internal/csprng_error.h
@@ -0,0 +1,19 @@
+#ifndef _CSPRNG_ERROR_H
+#define _CSPRNG_ERROR_H
+
+#if defined(HAVE_ERROR_H)
+#include <error.h>
+#else
+#include <err.h>
+#include <string.h>
+#define _csprng_error(S, E, F, ...) do { \
+	if (E) \
+		err(S, F ": %s", ##__VA_ARGS__, strerror(E)); \
+	else \
+		err(S, F, ##__VA_ARGS__); \
+} while(0)
+
+#define error _csprng_error
+#endif
+
+#endif
diff --git a/test/Makefile.am b/test/Makefile.am
index 023ded7..409ab54 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,6 +12,7 @@ if HAVE_LIBTESTU01
   bin_PROGRAMS += TestU01_raw_stdin_input_with_log
 endif
 
+openssl_rand_main_CPPFLAGS = -I$(top_srcdir)/include
 openssl_rand_main_SOURCES = openssl-rand_main.c
 openssl_rand_main_LDADD = -lcrypto
 
diff --git a/test/havege_main.c b/test/havege_main.c
index 181a997..1a03c18 100644
--- a/test/havege_main.c
+++ b/test/havege_main.c
@@ -34,7 +34,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <assert.h>
 #include <string.h>
 #include <errno.h>
-#include <error.h>
+#include <internal/csprng_error.h>
 
 #include <csprng/havege.h>
 #include <openssl/sha.h>
diff --git a/test/http_main.c b/test/http_main.c
index 9b10034..d3c14a0 100644
--- a/test/http_main.c
+++ b/test/http_main.c
@@ -33,7 +33,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <csprng/http_rng.h>
 #include <time.h>
 #include <errno.h>
-#include <error.h>
+#include <internal/csprng_error.h>
 #include <string.h>
 
 
diff --git a/test/memt_main.c b/test/memt_main.c
index 29ddfca..6037596 100644
--- a/test/memt_main.c
+++ b/test/memt_main.c
@@ -38,7 +38,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <inttypes.h>
 #include <assert.h>
 #include <errno.h>
-#include <error.h>
+#include <internal/csprng_error.h>
 
 int main(void)
 {
diff --git a/test/openssl-rand_main.c b/test/openssl-rand_main.c
index 5a4e5b6..78ec057 100644
--- a/test/openssl-rand_main.c
+++ b/test/openssl-rand_main.c
@@ -31,7 +31,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <stdlib.h>
 #include <inttypes.h>
 #include <errno.h>
-#include <error.h>
+#include <internal/csprng_error.h>
 
 #include <openssl/rand.h>
 
diff --git a/test/qrbg_main.c b/test/qrbg_main.c
index 4cf3a35..5dd1492 100644
--- a/test/qrbg_main.c
+++ b/test/qrbg_main.c
@@ -32,7 +32,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <errno.h>
 #include <string.h>
 #include <errno.h>
-#include <error.h>
+#include <internal/csprng_error.h>
 
 #include <csprng/qrbg-c.h>
 
diff --git a/test/sha1_main.c b/test/sha1_main.c
index 9b5753b..0a2349d 100644
--- a/test/sha1_main.c
+++ b/test/sha1_main.c
@@ -31,7 +31,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <stdlib.h>
 #include <inttypes.h>
 #include <errno.h>
-#include <error.h>
+#include <internal/csprng_error.h>
 
 
 #include <csprng/sha1_rng.h>
diff --git a/utils/csprng-generate.c b/utils/csprng-generate.c
index 7a1928a..cffffcd 100644
--- a/utils/csprng-generate.c
+++ b/utils/csprng-generate.c
@@ -58,7 +58,6 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <csprng/helper_utils.h>
 
 
-#include <error.h>
 #include <argp.h>
 #include <string.h>
 #include <math.h>
@@ -72,6 +71,7 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <internal/csprng_error.h>
 
 #include "config.h"
 
diff --git a/utils/csprngd.c b/utils/csprngd.c
index 8fd63be..9ddbe27 100644
--- a/utils/csprngd.c
+++ b/utils/csprngd.c
@@ -67,7 +67,6 @@ mpstat -P ALL 2
 #include <csprng/helper_utils.h>
 
 
-#include <error.h>
 #include <argp.h>
 #include <string.h>
 #include <assert.h>
@@ -88,6 +87,8 @@ mpstat -P ALL 2
 #include <sys/mman.h>   //mlock
 #include <sys/file.h>   //flock
 
+#include <internal/csprng_error.h>
+
 #include "random_interface_linux.h"
 #include "config.h"
 
diff --git a/utils/random_interface_linux.c b/utils/random_interface_linux.c
index 77552eb..ebe79b2 100644
--- a/utils/random_interface_linux.c
+++ b/utils/random_interface_linux.c
@@ -30,7 +30,6 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 #include <sys/utsname.h>  //uname system call
 #include <stdarg.h>
 #include <errno.h>
-#include <error.h>
 #include <time.h>
 #include <signal.h>
 
@@ -43,6 +42,8 @@ along with CSRNG.  If not, see <http://www.gnu.org/licenses/>.
 //
 #include <linux/random.h>
 
+#include <internal/csprng_error.h>
+
 #include <csprng/helper_utils.h>
 #include "random_interface_linux.h"
 
-- 
1.7.2.3