[Libguestfs] [PATCH] daemon/Win32: provide htonl, htons, ntohl, ntohs functions.

Richard W.M. Jones rjones at redhat.com
Thu Nov 26 11:54:13 UTC 2009


On Thu, Nov 26, 2009 at 12:41:42PM +0100, Jim Meyering wrote:
> Richard W.M. Jones wrote:
> > This is another candidate for gnulib ...
> 
> Yes, indeed.
> 
> ...
> > +static inline uint16_t
> > +htons (x)
> > +     uint16_t x;
> > +{
> 
> Using old-style function definitions like this is odd,
> but then I realized this was probably copied from glibc.

Yes, these are copied from glibc, but ...

> Did you have to make any changes?

This patch was a bit of a dog's dinner.  The functions also shouldn't
be static(!)

Try this version instead.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
-------------- next part --------------
>From fd84feb8229f792e22e3cbfa9bf7e8156947eb2f Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Thu, 26 Nov 2009 11:26:40 +0000
Subject: [PATCH] daemon/Win32: provide htonl, htons, ntohl, ntohs functions.

These functions are not available on Windows.
---
 daemon/.gitignore         |    1 +
 daemon/Makefile.am        |    1 +
 daemon/configure.ac       |    4 ++
 daemon/htonl.c            |   82 +++++++++++++++++++++++++++++++++++++++++++++
 daemon/m4/gnulib-cache.m4 |    3 +-
 5 files changed, 90 insertions(+), 1 deletions(-)
 create mode 100644 daemon/htonl.c

diff --git a/daemon/.gitignore b/daemon/.gitignore
index ac1b47b..0d9dac7 100644
--- a/daemon/.gitignore
+++ b/daemon/.gitignore
@@ -4,6 +4,7 @@ link-warning.h
 m4/00gnulib.m4
 m4/alloca.m4
 m4/arpa_inet_h.m4
+m4/byteswap.m4
 m4/canonicalize-lgpl.m4
 m4/chdir-long.m4
 m4/chown.m4
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 103e49d..2f43448 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -91,6 +91,7 @@ guestfsd_SOURCES = \
 	guestfsd.c \
 	headtail.c \
 	hexdump.c \
+	htonl.c \
 	initrd.c \
 	inotify.c \
 	link.c \
diff --git a/daemon/configure.ac b/daemon/configure.ac
index bd0bdbe..1d335b1 100644
--- a/daemon/configure.ac
+++ b/daemon/configure.ac
@@ -171,6 +171,8 @@ AC_CHECK_LIB([portablexdr],[xdrmem_create],[],[
 dnl Functions which may not be available in older distributions.
 AC_CHECK_FUNCS([\
         getxattr \
+        htonl \
+        htons \
         inotify_init1 \
         lgetxattr \
         listxattr \
@@ -178,6 +180,8 @@ AC_CHECK_FUNCS([\
         lsetxattr \
         lremovexattr \
         mknod \
+        ntohl \
+        ntohs \
         posix_fallocate \
         removexattr \
         setxattr \
diff --git a/daemon/htonl.c b/daemon/htonl.c
new file mode 100644
index 0000000..c9f1af0
--- /dev/null
+++ b/daemon/htonl.c
@@ -0,0 +1,82 @@
+/* Copyright (C) 1993,97,2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C 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.
+
+   The GNU C 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 the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+/* Windows lacks htonl, htons, ntohl, ntohs.  It defines them in
+ * header files, but doesn't have them in any library.  However
+ * because it defined them we can end up with conflicting
+ * definitions, which is why here we are careful to avoid
+ * including any Windows header file.
+ */
+
+#include <config.h>
+
+#include <stdint.h>
+#include <sys/param.h>
+#include <byteswap.h>
+
+#ifndef HAVE_NTOHL
+
+#undef	htonl
+#undef	ntohl
+
+uint32_t
+htonl (x)
+     uint32_t x;
+{
+#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN)
+#if BYTE_ORDER == BIG_ENDIAN
+  return x;
+#elif BYTE_ORDER == LITTLE_ENDIAN
+  return bswap_32 (x);
+#else
+# error "What kind of system is this?"
+#endif
+#else
+#error "BYTE_ORDER/BIG_ENDIAN/LITTLE_ENDIAN are not defined"
+#endif
+}
+
+uint32_t ntohl (uint32_t x) { return htonl (x); }
+
+#endif /* !HAVE_NTOHL */
+
+#ifndef HAVE_NTOHS
+
+#undef	htons
+#undef	ntohs
+
+uint16_t
+htons (x)
+     uint16_t x;
+{
+#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN)
+#if BYTE_ORDER == BIG_ENDIAN
+  return x;
+#elif BYTE_ORDER == LITTLE_ENDIAN
+  return bswap_16 (x);
+#else
+# error "What kind of system is this?"
+#endif
+#else
+#error "BYTE_ORDER/BIG_ENDIAN/LITTLE_ENDIAN are not defined"
+#endif
+}
+
+uint16_t ntohs (uint16_t x) { return htons (x); }
+
+#endif /* !HAVE_NTOHS */
diff --git a/daemon/m4/gnulib-cache.m4 b/daemon/m4/gnulib-cache.m4
index 01da897..e8b9edf 100644
--- a/daemon/m4/gnulib-cache.m4
+++ b/daemon/m4/gnulib-cache.m4
@@ -15,11 +15,12 @@
 
 
 # Specification in the form of a command-line invocation:
-#   gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --with-tests --no-libtool --macro-prefix=gl c-ctype fsusage futimens getaddrinfo getline glob hash ignore-value manywarnings mkdtemp netdb openat perror pread readlink select sleep strchrnul strndup sys_select sys_wait vasprintf warnings
+#   gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --with-tests --no-libtool --macro-prefix=gl byteswap c-ctype fsusage futimens getaddrinfo getline glob hash ignore-value manywarnings mkdtemp netdb openat perror pread readlink select sleep strchrnul strndup sys_select sys_wait vasprintf warnings
 
 # Specification in the form of a few gnulib-tool.m4 macro invocations:
 gl_LOCAL_DIR([])
 gl_MODULES([
+  byteswap
   c-ctype
   fsusage
   futimens
-- 
1.6.5.2



More information about the Libguestfs mailing list