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