diff options
Diffstat (limited to 'src/lib/libc/net/ns_ntoa.c')
| -rw-r--r-- | src/lib/libc/net/ns_ntoa.c | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/src/lib/libc/net/ns_ntoa.c b/src/lib/libc/net/ns_ntoa.c index ad3265399b..35d4f28aca 100644 --- a/src/lib/libc/net/ns_ntoa.c +++ b/src/lib/libc/net/ns_ntoa.c | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | /* $NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | 1 | /* |
| 4 | * Copyright (c) 1986, 1993 | 2 | * Copyright (c) 1986, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. | 3 | * The Regents of the University of California. All rights reserved. |
| @@ -34,73 +32,79 @@ | |||
| 34 | */ | 32 | */ |
| 35 | 33 | ||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
| 37 | #if 0 | 35 | static char rcsid[] = "$OpenBSD: ns_ntoa.c,v 1.11 2003/04/05 00:44:42 tdeval Exp $"; |
| 38 | static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
| 43 | 37 | ||
| 44 | #include <sys/param.h> | 38 | #include <sys/param.h> |
| 45 | #include <netns/ns.h> | 39 | #include <netns/ns.h> |
| 46 | #include <stdio.h> | 40 | #include <stdio.h> |
| 47 | 41 | ||
| 42 | static char *spectHex(char *); | ||
| 43 | |||
| 48 | char * | 44 | char * |
| 49 | ns_ntoa(addr) | 45 | ns_ntoa(struct ns_addr addr) |
| 50 | struct ns_addr addr; | ||
| 51 | { | 46 | { |
| 52 | static char obuf[40]; | 47 | static char obuf[40]; |
| 53 | union { union ns_net net_e; u_long long_e; } net; | 48 | union { union ns_net net_e; u_int32_t long_e; } net; |
| 54 | u_short port = htons(addr.x_port); | 49 | in_port_t port = htons(addr.x_port); |
| 55 | register char *cp; | 50 | char *cp, *cp2; |
| 56 | char *cp2; | 51 | u_char *up = addr.x_host.c_host; |
| 57 | register u_char *up = addr.x_host.c_host; | ||
| 58 | u_char *uplim = up + 6; | 52 | u_char *uplim = up + 6; |
| 59 | static char *spectHex(); | 53 | size_t rem; |
| 60 | 54 | ||
| 61 | net.net_e = addr.x_net; | 55 | net.net_e = addr.x_net; |
| 62 | sprintf(obuf, "%lx", ntohl(net.long_e)); | 56 | snprintf(obuf, sizeof obuf, "%x", ntohl(net.long_e)); |
| 63 | cp = spectHex(obuf); | 57 | cp = spectHex(obuf); |
| 58 | rem = sizeof(obuf) - (cp - obuf); | ||
| 64 | cp2 = cp + 1; | 59 | cp2 = cp + 1; |
| 65 | while (*up==0 && up < uplim) up++; | 60 | while (*up==0 && up < uplim) |
| 61 | up++; | ||
| 66 | if (up == uplim) { | 62 | if (up == uplim) { |
| 67 | if (port) { | 63 | if (port) { |
| 68 | sprintf(cp, ".0"); | 64 | snprintf(cp, rem, ".0"); |
| 69 | cp += 2; | 65 | cp += 2; |
| 66 | rem -= 2; | ||
| 70 | } | 67 | } |
| 71 | } else { | 68 | } else { |
| 72 | sprintf(cp, ".%x", *up++); | 69 | snprintf(cp, rem, ".%x", *up++); |
| 73 | while (up < uplim) { | 70 | while (up < uplim) { |
| 74 | while (*cp) cp++; | 71 | while (*cp) { |
| 75 | sprintf(cp, "%02x", *up++); | 72 | cp++; |
| 73 | rem--; | ||
| 74 | } | ||
| 75 | snprintf(cp, rem, "%02x", *up++); | ||
| 76 | } | 76 | } |
| 77 | cp = spectHex(cp2); | 77 | cp = spectHex(cp2); |
| 78 | rem = sizeof(obuf) - (cp - obuf); | ||
| 78 | } | 79 | } |
| 79 | if (port) { | 80 | if (port) { |
| 80 | sprintf(cp, ".%x", port); | 81 | snprintf(cp, rem, ".%x", port); |
| 81 | spectHex(cp + 1); | 82 | spectHex(cp + 1); |
| 82 | } | 83 | } |
| 83 | return (obuf); | 84 | return (obuf); |
| 84 | } | 85 | } |
| 85 | 86 | ||
| 86 | static char * | 87 | static char * |
| 87 | spectHex(p0) | 88 | spectHex(char *p0) |
| 88 | char *p0; | ||
| 89 | { | 89 | { |
| 90 | int ok = 0; | 90 | int ok = 0, nonzero = 0; |
| 91 | int nonzero = 0; | 91 | char *p = p0; |
| 92 | register char *p = p0; | ||
| 93 | for (; *p; p++) switch (*p) { | ||
| 94 | 92 | ||
| 95 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | 93 | for (; *p; p++) { |
| 96 | *p += ('A' - 'a'); | 94 | switch (*p) { |
| 97 | /* fall into . . . */ | 95 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
| 98 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | 96 | *p += ('A' - 'a'); |
| 99 | ok = 1; | 97 | /* fall into . . . */ |
| 100 | case '1': case '2': case '3': case '4': case '5': | 98 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
| 101 | case '6': case '7': case '8': case '9': | 99 | ok = 1; |
| 102 | nonzero = 1; | 100 | case '1': case '2': case '3': case '4': case '5': |
| 101 | case '6': case '7': case '8': case '9': | ||
| 102 | nonzero = 1; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | if (nonzero && !ok) { | ||
| 106 | *p++ = 'H'; | ||
| 107 | *p = 0; | ||
| 103 | } | 108 | } |
| 104 | if (nonzero && !ok) { *p++ = 'H'; *p = 0; } | ||
| 105 | return (p); | 109 | return (p); |
| 106 | } | 110 | } |
