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