xref: /openbsd-src/lib/libc/net/htons.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*
2  * Written by J.T. Conklin <jtc@netbsd.org>.
3  * Public domain.
4  */
5 
6 #if defined(LIBC_SCCS) && !defined(lint)
7 static char *rcsid = "$OpenBSD: htons.c,v 1.7 2002/02/19 19:39:36 millert Exp $";
8 #endif /* LIBC_SCCS and not lint */
9 
10 #include <sys/types.h>
11 #include <machine/endian.h>
12 
13 #undef htons
14 
15 u_int16_t
16 htons(u_int16_t x)
17 {
18 #if BYTE_ORDER == LITTLE_ENDIAN
19 	u_char *s = (u_char *) &x;
20 	return (u_int16_t)(s[0] << 8 | s[1]);
21 #else
22 	return x;
23 #endif
24 }
25