xref: /openbsd-src/lib/libc/net/htons.c (revision e9b2b68ca3a6cce2632b4a73cc801bbb6f19c001)
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.4 1996/08/19 08:29:05 tholo 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 unsigned short
16 #if __STDC__
17 htons(unsigned short x)
18 #else
19 htons(x)
20 	unsigned short x;
21 #endif
22 {
23 #if BYTE_ORDER == LITTLE_ENDIAN
24 	u_char *s = (u_char *) &x;
25 	return (u_int16_t)(s[0] << 8 | s[1]);
26 #else
27 	return x;
28 #endif
29 }
30