xref: /netbsd-src/external/bsd/ntp/dist/libntp/numtoa.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: numtoa.c,v 1.5 2020/05/25 20:47:24 christos Exp $	*/
2 
3 /*
4  * numtoa - return asciized network numbers store in local array space
5  */
6 #include <config.h>
7 
8 #include <sys/types.h>
9 #ifdef HAVE_NETINET_IN_H
10 #include <netinet/in.h>		/* ntohl */
11 #endif
12 
13 #include <stdio.h>
14 
15 #include "ntp_fp.h"
16 #include "lib_strbuf.h"
17 #include "ntp_stdlib.h"
18 
19 char *
20 numtoa(
21 	u_int32 num
22 	)
23 {
24 	register u_int32 netnum;
25 	register char *buf;
26 
27 	netnum = ntohl(num);
28 	LIB_GETBUF(buf);
29 	snprintf(buf, LIB_BUFLENGTH, "%lu.%lu.%lu.%lu",
30 		 ((u_long)netnum >> 24) & 0xff,
31 		 ((u_long)netnum >> 16) & 0xff,
32 		 ((u_long)netnum >> 8) & 0xff,
33 		 (u_long)netnum & 0xff);
34 	return buf;
35 }
36 
37 
38 /* Convert a refid & stratum to a string */
39 const char *
40 refid_str(
41 	u_int32	refid,
42 	int	stratum
43 	)
44 {
45 	char *	text;
46 	size_t	tlen;
47 
48 	if (stratum > 1)
49 		return numtoa(refid);
50 
51 	LIB_GETBUF(text);
52 	text[0] = '.';
53 	memcpy(&text[1], &refid, sizeof(refid));
54 	text[1 + sizeof(refid)] = '\0';
55 	tlen = strlen(text);
56 	text[tlen] = '.';
57 	text[tlen + 1] = '\0';
58 
59 	return text;
60 }
61 
62