xref: /netbsd-src/common/lib/libc/net/htons.c (revision 2fa47ecfcb23a9fa15f2448c2ad113946a4b0bf9)
1*2fa47ecfShe /*	$NetBSD: htons.c,v 1.3 2012/03/21 20:02:56 he Exp $	*/
237c9f0a6Schristos 
337c9f0a6Schristos /*
437c9f0a6Schristos  * Written by J.T. Conklin <jtc@NetBSD.org>.
537c9f0a6Schristos  * Public domain.
637c9f0a6Schristos  */
737c9f0a6Schristos 
837c9f0a6Schristos #include <sys/cdefs.h>
937c9f0a6Schristos #if defined(LIBC_SCCS) && !defined(lint)
10*2fa47ecfShe __RCSID("$NetBSD: htons.c,v 1.3 2012/03/21 20:02:56 he Exp $");
1137c9f0a6Schristos #endif
1237c9f0a6Schristos 
1337c9f0a6Schristos #include <sys/types.h>
1437c9f0a6Schristos 
1537c9f0a6Schristos #undef htons
1637c9f0a6Schristos 
1737c9f0a6Schristos uint16_t
htons(uint16_t x)18b45fa494Sjoerg htons(uint16_t x)
1937c9f0a6Schristos {
2037c9f0a6Schristos #if BYTE_ORDER == LITTLE_ENDIAN
21*2fa47ecfShe 	u_char *s = (void *) &x;
2237c9f0a6Schristos 	return (uint16_t)(s[0] << 8 | s[1]);
2337c9f0a6Schristos #else
2437c9f0a6Schristos 	return x;
2537c9f0a6Schristos #endif
2637c9f0a6Schristos }
27