Commit | Line | Data |
---|---|---|
8b0cde34 KS |
1 | /* |
2 | * Copyright (c) 1986 Regents of the University of California. | |
8146a28e | 3 | * All rights reserved. |
8b0cde34 | 4 | * |
269a7923 | 5 | * %sccs.include.redist.c% |
8b0cde34 KS |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
24fac7d8 | 9 | static char sccsid[] = "@(#)ns_ntoa.c 6.6 (Berkeley) %G%"; |
8146a28e | 10 | #endif /* LIBC_SCCS and not lint */ |
8b0cde34 | 11 | |
24fac7d8 | 12 | #include <sys/param.h> |
8b0cde34 | 13 | #include <netns/ns.h> |
24fac7d8 | 14 | #include <stdio.h> |
8b0cde34 KS |
15 | |
16 | char * | |
17 | ns_ntoa(addr) | |
24fac7d8 | 18 | struct ns_addr addr; |
8b0cde34 KS |
19 | { |
20 | static char obuf[40]; | |
489628ab | 21 | union { union ns_net net_e; u_long long_e; } net; |
8b0cde34 KS |
22 | u_short port = htons(addr.x_port); |
23 | register char *cp; | |
24 | char *cp2; | |
25 | register u_char *up = addr.x_host.c_host; | |
26 | u_char *uplim = up + 6; | |
24fac7d8 | 27 | static char *spectHex(); |
8b0cde34 | 28 | |
489628ab KS |
29 | net.net_e = addr.x_net; |
30 | sprintf(obuf, "%lx", ntohl(net.long_e)); | |
8b0cde34 KS |
31 | cp = spectHex(obuf); |
32 | cp2 = cp + 1; | |
33 | while (*up==0 && up < uplim) up++; | |
489628ab KS |
34 | if (up == uplim) { |
35 | if (port) { | |
36 | sprintf(cp, ".0"); | |
37 | cp += 2; | |
38 | } | |
39 | } else { | |
40 | sprintf(cp, ".%x", *up++); | |
41 | while (up < uplim) { | |
42 | while (*cp) cp++; | |
43 | sprintf(cp, "%02x", *up++); | |
44 | } | |
45 | cp = spectHex(cp2); | |
8b0cde34 | 46 | } |
8b0cde34 KS |
47 | if (port) { |
48 | sprintf(cp, ".%x", port); | |
49 | spectHex(cp + 1); | |
50 | } | |
51 | return (obuf); | |
52 | } | |
53 | ||
54 | static char * | |
55 | spectHex(p0) | |
24fac7d8 | 56 | char *p0; |
8b0cde34 KS |
57 | { |
58 | int ok = 0; | |
59 | int nonzero = 0; | |
60 | register char *p = p0; | |
61 | for (; *p; p++) switch (*p) { | |
62 | ||
63 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
64 | *p += ('A' - 'a'); | |
65 | /* fall into . . . */ | |
66 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
67 | ok = 1; | |
68 | case '1': case '2': case '3': case '4': case '5': | |
69 | case '6': case '7': case '8': case '9': | |
70 | nonzero = 1; | |
71 | } | |
72 | if (nonzero && !ok) { *p++ = 'H'; *p = 0; } | |
73 | return (p); | |
74 | } |