xref: /titanic_41/usr/src/lib/libresolv2/common/inet/inet_neta.c (revision 6185db853e024a486ff8837e6784dd290d866112)
1 /*
2  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
3  * All rights reserved.
4  */
5 
6 /*
7  * Copyright (c) 1996,1999 by Internet Software Consortium.
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 #if defined(LIBC_SCCS) && !defined(lint)
26 static const char rcsid[] = "$Id: inet_neta.c,v 1.6 1999/01/08 19:23:45 vixie Exp $";
27 #endif
28 
29 #include "port_before.h"
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "port_after.h"
41 
42 #ifdef SPRINTF_CHAR
43 # define SPRINTF(x) strlen(sprintf/**/x)
44 #else
45 # define SPRINTF(x) ((size_t)sprintf x)
46 #endif
47 
48 /*
49  * char *
50  * inet_neta(src, dst, size)
51  *	format a u_long network number into presentation format.
52  * return:
53  *	pointer to dst, or NULL if an error occurred (check errno).
54  * note:
55  *	format of ``src'' is as for inet_network().
56  * author:
57  *	Paul Vixie (ISC), July 1996
58  */
59 char *
60 inet_neta(src, dst, size)
61 	u_long src;
62 	char *dst;
63 	size_t size;
64 {
65 	char *odst = dst;
66 	char *tp;
67 
68 	while (src & 0xffffffff) {
69 		u_char b = (src & 0xff000000) >> 24;
70 
71 		src <<= 8;
72 		if (b) {
73 			if (size < sizeof "255.")
74 				goto emsgsize;
75 			tp = dst;
76 			dst += SPRINTF((dst, "%u", b));
77 			if (src != 0L) {
78 				*dst++ = '.';
79 				*dst = '\0';
80 			}
81 			size -= (size_t)(dst - tp);
82 		}
83 	}
84 	if (dst == odst) {
85 		if (size < sizeof "0.0.0.0")
86 			goto emsgsize;
87 		strcpy(dst, "0.0.0.0");
88 	}
89 	return (odst);
90 
91  emsgsize:
92 	errno = EMSGSIZE;
93 	return (NULL);
94 }
95