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