xref: /netbsd-src/sys/lib/libkern/intoa.c (revision 454af1c0e885cbba6b59706391f770d646303f8c)
1*454af1c0Sdsl /*	$NetBSD: intoa.c,v 1.3 2009/03/14 15:36:22 dsl Exp $	*/
2e6b49c78Sdrochner 
3e6b49c78Sdrochner /*
4e6b49c78Sdrochner  * Copyright (c) 1992 Regents of the University of California.
5e6b49c78Sdrochner  * All rights reserved.
6e6b49c78Sdrochner  *
7e6b49c78Sdrochner  * This software was developed by the Computer Systems Engineering group
8e6b49c78Sdrochner  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9e6b49c78Sdrochner  * contributed to Berkeley.
10e6b49c78Sdrochner  *
11e6b49c78Sdrochner  * Redistribution and use in source and binary forms, with or without
12e6b49c78Sdrochner  * modification, are permitted provided that the following conditions
13e6b49c78Sdrochner  * are met:
14e6b49c78Sdrochner  * 1. Redistributions of source code must retain the above copyright
15e6b49c78Sdrochner  *    notice, this list of conditions and the following disclaimer.
16e6b49c78Sdrochner  * 2. Redistributions in binary form must reproduce the above copyright
17e6b49c78Sdrochner  *    notice, this list of conditions and the following disclaimer in the
18e6b49c78Sdrochner  *    documentation and/or other materials provided with the distribution.
19e6b49c78Sdrochner  * 3. All advertising materials mentioning features or use of this software
20e6b49c78Sdrochner  *    must display the following acknowledgement:
21e6b49c78Sdrochner  *	This product includes software developed by the University of
22e6b49c78Sdrochner  *	California, Lawrence Berkeley Laboratory and its contributors.
23e6b49c78Sdrochner  * 4. Neither the name of the University nor the names of its contributors
24e6b49c78Sdrochner  *    may be used to endorse or promote products derived from this software
25e6b49c78Sdrochner  *    without specific prior written permission.
26e6b49c78Sdrochner  *
27e6b49c78Sdrochner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28e6b49c78Sdrochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29e6b49c78Sdrochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30e6b49c78Sdrochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31e6b49c78Sdrochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32e6b49c78Sdrochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33e6b49c78Sdrochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34e6b49c78Sdrochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35e6b49c78Sdrochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36e6b49c78Sdrochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37e6b49c78Sdrochner  * SUCH DAMAGE.
38e6b49c78Sdrochner  *
39e6b49c78Sdrochner  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
40e6b49c78Sdrochner  */
41e6b49c78Sdrochner 
42e6b49c78Sdrochner #include <sys/types.h>
43e6b49c78Sdrochner 
44e6b49c78Sdrochner #if defined(_KERNEL) || defined(_STANDALONE)
45e6b49c78Sdrochner #include <lib/libkern/libkern.h>
46e6b49c78Sdrochner #else
4702cdf4d2Sdsl char *intoa(u_int32_t); /* XXX */
48e6b49c78Sdrochner #endif
49e6b49c78Sdrochner 
50e6b49c78Sdrochner /* Similar to inet_ntoa() */
51e6b49c78Sdrochner char *
intoa(u_int32_t addr)52*454af1c0Sdsl intoa(u_int32_t addr)
53e6b49c78Sdrochner {
54e6b49c78Sdrochner 	char *cp;
55e6b49c78Sdrochner 	u_int byte;
56e6b49c78Sdrochner 	int n;
57e6b49c78Sdrochner 	static char buf[17];	/* strlen(".255.255.255.255") + 1 */
58e6b49c78Sdrochner 
59e6b49c78Sdrochner 	addr = ntohl(addr);
60e6b49c78Sdrochner 	cp = &buf[sizeof buf];
61e6b49c78Sdrochner 	*--cp = '\0';
62e6b49c78Sdrochner 
63e6b49c78Sdrochner 	n = 4;
64e6b49c78Sdrochner 	do {
65e6b49c78Sdrochner 		byte = addr & 0xff;
66e6b49c78Sdrochner 		*--cp = byte % 10 + '0';
67e6b49c78Sdrochner 		byte /= 10;
68e6b49c78Sdrochner 		if (byte > 0) {
69e6b49c78Sdrochner 			*--cp = byte % 10 + '0';
70e6b49c78Sdrochner 			byte /= 10;
71e6b49c78Sdrochner 			if (byte > 0)
72e6b49c78Sdrochner 				*--cp = byte + '0';
73e6b49c78Sdrochner 		}
74e6b49c78Sdrochner 		*--cp = '.';
75e6b49c78Sdrochner 		addr >>= 8;
76e6b49c78Sdrochner 	} while (--n > 0);
77e6b49c78Sdrochner 
78e6b49c78Sdrochner 	return (cp+1);
79e6b49c78Sdrochner }
80