lib: Provide MAX and MIN in system.h

This change also creates a new header file libeu.h to provide the
prototypes for the function of libeu. That hides the definition of function
crc32, which can conflict with zlib, from libelf. It also prevents mistakes
to refer those functions from a component which doesn't link with libeu,
such as libelf.

Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 76b5753..88c71c9 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,17 @@
+2015-10-11  Akihiko Odaki  <akihiko.odaki.4i@stu.hosei.ac.jp>
+
+	* Makefile.am (noinst_HEADERS): Add libeu.h.
+	* color.c: Remove system.h include, add libeu.h include.
+	* crc32_file.c: Likewise.
+	* fixedsizehash.h: Remove sys/param.h include.
+	* libeu.h: New file.
+	* system.h: Include sys/param.h.
+	(xmalloc, xcalloc, xrealloc, xstrdup, xstrndup, crc32, crc32_file,
+	color_argp, color_enum, color_*): Move definitions to libeu.h.
+	* xstrdup.c: Remove system.h include, add libeu.h include.
+	* xstrndup.c: Remove system.h include, add libeu.h and stdint.h
+	includes.
+
 2015-09-24  Jose E. Marchesi  <jose.marchesi@oracle.com>
 
 	* Makefile.am (AM_CFLAGS): Use -fPIC instead of -fpic to avoid relocation
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 2219eaa..7ca2bd4 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -37,8 +37,8 @@
 		  crc32.c crc32_file.c md5.c sha1.c \
 		  color.c
 
-noinst_HEADERS = fixedsizehash.h system.h dynamicsizehash.h list.h md5.h \
-		 sha1.h eu-config.h
+noinst_HEADERS = fixedsizehash.h libeu.h system.h dynamicsizehash.h list.h \
+		 md5.h sha1.h eu-config.h
 EXTRA_DIST = dynamicsizehash.c
 
 if !GPROF
diff --git a/lib/color.c b/lib/color.c
index d1309ed..fde2d9d 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -37,7 +37,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "system.h"
+#include "libeu.h"
 
 
 /* Prototype for option handler.  */
diff --git a/lib/crc32_file.c b/lib/crc32_file.c
index c0b18e9..a8434d4 100644
--- a/lib/crc32_file.c
+++ b/lib/crc32_file.c
@@ -30,7 +30,7 @@
 # include <config.h>
 #endif
 
-#include "system.h"
+#include "libeu.h"
 #include <errno.h>
 #include <unistd.h>
 #include <sys/stat.h>
diff --git a/lib/fixedsizehash.h b/lib/fixedsizehash.h
index 06ce6a2..18921a4 100644
--- a/lib/fixedsizehash.h
+++ b/lib/fixedsizehash.h
@@ -31,7 +31,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/cdefs.h>
-#include <sys/param.h>
 
 #include <system.h>
 
diff --git a/lib/libeu.h b/lib/libeu.h
new file mode 100644
index 0000000..69fe3d7
--- /dev/null
+++ b/lib/libeu.h
@@ -0,0 +1,78 @@
+/* Declarations for the common library.
+   Copyright (C) 2006-2011 Red Hat, Inc.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * 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
+
+   or both in parallel, as here.
+
+   elfutils 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 copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef LIBEU_H
+#define LIBEU_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+extern void *xmalloc (size_t) __attribute__ ((__malloc__));
+extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__));
+extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
+
+extern char *xstrdup (const char *) __attribute__ ((__malloc__));
+extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
+
+
+extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
+extern int crc32_file (int fd, uint32_t *resp);
+
+
+/* Color handling.  */
+
+/* Command line parser.  */
+extern const struct argp color_argp;
+
+/* Coloring mode.  */
+enum color_enum
+  {
+    color_never = 0,
+    color_always,
+    color_auto
+  } __attribute__ ((packed));
+extern enum color_enum color_mode;
+
+/* Colors to use for the various components.  */
+extern char *color_address;
+extern char *color_bytes;
+extern char *color_mnemonic;
+extern char *color_operand1;
+extern char *color_operand2;
+extern char *color_operand3;
+extern char *color_label;
+extern char *color_undef;
+extern char *color_undef_tls;
+extern char *color_undef_weak;
+extern char *color_symbol;
+extern char *color_tls;
+extern char *color_weak;
+
+extern const char color_off[];
+
+#endif
diff --git a/lib/system.h b/lib/system.h
index f31cfd0..ec387c3 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -32,6 +32,7 @@
 #include <argp.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <sys/param.h>
 #include <endian.h>
 #include <byteswap.h>
 #include <unistd.h>
@@ -50,17 +51,15 @@
 # error "Unknown byte order"
 #endif
 
-extern void *xmalloc (size_t) __attribute__ ((__malloc__));
-extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__));
-extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
+#ifndef MAX
+#define MAX(m, n) ((m) < (n) ? (n) : (m))
+#endif
 
-extern char *xstrdup (const char *) __attribute__ ((__malloc__));
-extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
+#ifndef MIN
+#define MIN(m, n) ((m) < (n) ? (m) : (n))
+#endif
 
 
-extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
-extern int crc32_file (int fd, uint32_t *resp);
-
 /* A special gettext function we use if the strings are too short.  */
 #define sgettext(Str) \
   ({ const char *__res = strrchr (gettext (Str), '|');			      \
@@ -142,39 +141,6 @@
 extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
 			     size_t *length, int *status);
 
-
-
-/* Color handling.  */
-
-/* Command line parser.  */
-extern const struct argp color_argp;
-
-/* Coloring mode.  */
-enum color_enum
-  {
-    color_never = 0,
-    color_always,
-    color_auto
-  } __attribute__ ((packed));
-extern enum color_enum color_mode;
-
-/* Colors to use for the various components.  */
-extern char *color_address;
-extern char *color_bytes;
-extern char *color_mnemonic;
-extern char *color_operand1;
-extern char *color_operand2;
-extern char *color_operand3;
-extern char *color_label;
-extern char *color_undef;
-extern char *color_undef_tls;
-extern char *color_undef_weak;
-extern char *color_symbol;
-extern char *color_tls;
-extern char *color_weak;
-
-extern const char color_off[];
-
 /* A static assertion.  This will cause a compile-time error if EXPR,
    which must be a compile-time constant, is false.  */
 
diff --git a/lib/xstrdup.c b/lib/xstrdup.c
index aa10352..ff1e3d4 100644
--- a/lib/xstrdup.c
+++ b/lib/xstrdup.c
@@ -31,7 +31,7 @@
 #endif
 
 #include <string.h>
-#include "system.h"
+#include "libeu.h"
 
 
 /* Return a newly allocated copy of STRING.  */
diff --git a/lib/xstrndup.c b/lib/xstrndup.c
index 92b79c1..d43e3b9 100644
--- a/lib/xstrndup.c
+++ b/lib/xstrndup.c
@@ -30,8 +30,9 @@
 # include <config.h>
 #endif
 
+#include <stdint.h>
 #include <string.h>
-#include "system.h"
+#include "libeu.h"
 
 
 /* Return a newly allocated copy of STRING.  */