Home | History | Annotate | Line # | Download | only in net
htonl.c revision 1.2.2.1
      1  1.2.2.1      yamt /*	$NetBSD: htonl.c,v 1.2.2.1 2012/04/17 00:01:39 yamt Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Written by J.T. Conklin <jtc (at) NetBSD.org>.
      5      1.1  christos  * Public domain.
      6      1.1  christos  */
      7      1.1  christos 
      8      1.1  christos #include <sys/cdefs.h>
      9      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     10  1.2.2.1      yamt __RCSID("$NetBSD: htonl.c,v 1.2.2.1 2012/04/17 00:01:39 yamt Exp $");
     11      1.1  christos #endif
     12      1.1  christos 
     13      1.1  christos #include <sys/types.h>
     14      1.1  christos 
     15      1.1  christos #undef htonl
     16      1.1  christos 
     17      1.1  christos uint32_t
     18      1.2     joerg htonl(uint32_t x)
     19      1.1  christos {
     20      1.1  christos #if BYTE_ORDER == LITTLE_ENDIAN
     21  1.2.2.1      yamt 	u_char *s = (void *)&x;
     22      1.1  christos 	return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
     23      1.1  christos #else
     24      1.1  christos 	return x;
     25      1.1  christos #endif
     26      1.1  christos }
     27