from the ANSI standard
[unix-history] / usr / src / lib / libc / net / ns_ntoa.c
CommitLineData
8b0cde34
KS
1/*
2 * Copyright (c) 1986 Regents of the University of California.
8146a28e 3 * All rights reserved.
8b0cde34 4 *
8146a28e
KB
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
8b0cde34
KS
16 */
17
2ce81398 18#if defined(LIBC_SCCS) && !defined(lint)
8146a28e
KB
19static char sccsid[] = "@(#)ns_ntoa.c 6.4 (Berkeley) %G%";
20#endif /* LIBC_SCCS and not lint */
8b0cde34
KS
21
22#include <sys/types.h>
23#include <netns/ns.h>
24
25char *
26ns_ntoa(addr)
27struct ns_addr addr;
28{
29 static char obuf[40];
30 char *spectHex();
489628ab 31 union { union ns_net net_e; u_long long_e; } net;
8b0cde34
KS
32 u_short port = htons(addr.x_port);
33 register char *cp;
34 char *cp2;
35 register u_char *up = addr.x_host.c_host;
36 u_char *uplim = up + 6;
37
489628ab
KS
38 net.net_e = addr.x_net;
39 sprintf(obuf, "%lx", ntohl(net.long_e));
8b0cde34
KS
40 cp = spectHex(obuf);
41 cp2 = cp + 1;
42 while (*up==0 && up < uplim) up++;
489628ab
KS
43 if (up == uplim) {
44 if (port) {
45 sprintf(cp, ".0");
46 cp += 2;
47 }
48 } else {
49 sprintf(cp, ".%x", *up++);
50 while (up < uplim) {
51 while (*cp) cp++;
52 sprintf(cp, "%02x", *up++);
53 }
54 cp = spectHex(cp2);
8b0cde34 55 }
8b0cde34
KS
56 if (port) {
57 sprintf(cp, ".%x", port);
58 spectHex(cp + 1);
59 }
60 return (obuf);
61}
62
63static char *
64spectHex(p0)
65char *p0;
66{
67 int ok = 0;
68 int nonzero = 0;
69 register char *p = p0;
70 for (; *p; p++) switch (*p) {
71
72 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
73 *p += ('A' - 'a');
74 /* fall into . . . */
75 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
76 ok = 1;
77 case '1': case '2': case '3': case '4': case '5':
78 case '6': case '7': case '8': case '9':
79 nonzero = 1;
80 }
81 if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
82 return (p);
83}