platform compatibility work (by Dan Fandrich)

Signed-off-by: Dan Fandrich <dan@coneharvesters.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/libbb/Kbuild b/libbb/Kbuild
index 8c7a189..c3c02b3 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -71,6 +71,7 @@
 lib-y += perror_nomsg.o
 lib-y += perror_nomsg_and_die.o
 lib-y += pidfile.o
+lib-y += platform.o
 lib-y += printable.o
 lib-y += print_flags.o
 lib-y += process_escape_sequence.o
diff --git a/libbb/platform.c b/libbb/platform.c
new file mode 100644
index 0000000..470185a
--- /dev/null
+++ b/libbb/platform.c
@@ -0,0 +1,57 @@
+/*
+ * Replacements for common but usually nonstandard functions that aren't
+ * supplied by all platforms.
+ *
+ * Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al.
+ *
+ * Licensed under the GPL version 2, see the file LICENSE in this tarball.
+ */
+
+#include "libbb.h"
+
+#ifndef HAVE_STRCHRNUL
+char * FAST_FUNC strchrnul(const char *s, int c)
+{
+	while (*s && *s != c) ++s;
+	return (char*)s;
+}
+#endif
+
+#ifndef HAVE_VASPRINTF
+int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
+{
+	int r;
+	va_list p2;
+
+	va_copy(p2, p);
+	r = vsnprintf(NULL, 0, format, p);
+	va_end(p);
+	*string_ptr = xmalloc(r+1);
+	if (!*string_ptr)
+		r = -1;
+	else
+		r = vsnprintf(*string_ptr, r+1, format, p2);
+	va_end(p2);
+
+	return r;
+}
+#endif
+
+#ifndef HAVE_FDPRINTF
+int fdprintf(int fd, const char *format, ...)
+{
+	va_list p;
+	int r;
+	char *string_ptr;
+
+	va_start(p, format);
+	r = vasprintf(&string_ptr, format, p);
+	va_end(p);
+	if (r >= 0) {
+		r = full_write(fd, string_ptr, r);
+		free(string_ptr);
+	}
+	return r;
+}
+#endif
+
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index aaf9989..345c842 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -283,60 +283,15 @@
 	int r;
 	char *string_ptr;
 
-#if 1
-	// GNU extension
 	va_start(p, format);
 	r = vasprintf(&string_ptr, format, p);
 	va_end(p);
-#else
-	// Bloat for systems that haven't got the GNU extension.
-	va_start(p, format);
-	r = vsnprintf(NULL, 0, format, p);
-	va_end(p);
-	string_ptr = xmalloc(r+1);
-	va_start(p, format);
-	r = vsnprintf(string_ptr, r+1, format, p);
-	va_end(p);
-#endif
 
 	if (r < 0)
 		bb_error_msg_and_die(bb_msg_memory_exhausted);
 	return string_ptr;
 }
 
-#if 0 /* If we will ever meet a libc which hasn't [f]dprintf... */
-int FAST_FUNC fdprintf(int fd, const char *format, ...)
-{
-	va_list p;
-	int r;
-	char *string_ptr;
-
-#if 1
-	// GNU extension
-	va_start(p, format);
-	r = vasprintf(&string_ptr, format, p);
-	va_end(p);
-#else
-	// Bloat for systems that haven't got the GNU extension.
-	va_start(p, format);
-	r = vsnprintf(NULL, 0, format, p) + 1;
-	va_end(p);
-	string_ptr = malloc(r);
-	if (string_ptr) {
-		va_start(p, format);
-		r = vsnprintf(string_ptr, r, format, p);
-		va_end(p);
-	}
-#endif
-
-	if (r >= 0) {
-		full_write(fd, string_ptr, r);
-		free(string_ptr);
-	}
-	return r;
-}
-#endif
-
 void FAST_FUNC xsetenv(const char *key, const char *value)
 {
 	if (setenv(key, value, 1))