BSD 4_3 release
[unix-history] / usr / src / lib / libc / ns / ns_ntoa.c
CommitLineData
8b0cde34
KS
1/*
2 * Copyright (c) 1986 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
95f51977 9static char sccsid[] = "@(#)ns_ntoa.c 6.3 (Berkeley) 3/9/86";
2ce81398 10#endif LIBC_SCCS and not lint
8b0cde34
KS
11
12#include <sys/types.h>
13#include <netns/ns.h>
14
15char *
16ns_ntoa(addr)
17struct ns_addr addr;
18{
19 static char obuf[40];
20 char *spectHex();
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;
27
489628ab
KS
28 net.net_e = addr.x_net;
29 sprintf(obuf, "%lx", ntohl(net.long_e));
8b0cde34
KS
30 cp = spectHex(obuf);
31 cp2 = cp + 1;
32 while (*up==0 && up < uplim) up++;
489628ab
KS
33 if (up == uplim) {
34 if (port) {
35 sprintf(cp, ".0");
36 cp += 2;
37 }
38 } else {
39 sprintf(cp, ".%x", *up++);
40 while (up < uplim) {
41 while (*cp) cp++;
42 sprintf(cp, "%02x", *up++);
43 }
44 cp = spectHex(cp2);
8b0cde34 45 }
8b0cde34
KS
46 if (port) {
47 sprintf(cp, ".%x", port);
48 spectHex(cp + 1);
49 }
50 return (obuf);
51}
52
53static char *
54spectHex(p0)
55char *p0;
56{
57 int ok = 0;
58 int nonzero = 0;
59 register char *p = p0;
60 for (; *p; p++) switch (*p) {
61
62 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
63 *p += ('A' - 'a');
64 /* fall into . . . */
65 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
66 ok = 1;
67 case '1': case '2': case '3': case '4': case '5':
68 case '6': case '7': case '8': case '9':
69 nonzero = 1;
70 }
71 if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
72 return (p);
73}