xref: /freebsd-src/contrib/blocklist/bin/support.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1*5f4c09ddSEd Maste /*	$NetBSD: support.c,v 1.9 2018/09/18 22:12:19 christos Exp $	*/
2*5f4c09ddSEd Maste 
3*5f4c09ddSEd Maste /*-
4*5f4c09ddSEd Maste  * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*5f4c09ddSEd Maste  * All rights reserved.
6*5f4c09ddSEd Maste  *
7*5f4c09ddSEd Maste  * This code is derived from software contributed to The NetBSD Foundation
8*5f4c09ddSEd Maste  * by Christos Zoulas.
9*5f4c09ddSEd Maste  *
10*5f4c09ddSEd Maste  * Redistribution and use in source and binary forms, with or without
11*5f4c09ddSEd Maste  * modification, are permitted provided that the following conditions
12*5f4c09ddSEd Maste  * are met:
13*5f4c09ddSEd Maste  * 1. Redistributions of source code must retain the above copyright
14*5f4c09ddSEd Maste  *    notice, this list of conditions and the following disclaimer.
15*5f4c09ddSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
16*5f4c09ddSEd Maste  *    notice, this list of conditions and the following disclaimer in the
17*5f4c09ddSEd Maste  *    documentation and/or other materials provided with the distribution.
18*5f4c09ddSEd Maste  *
19*5f4c09ddSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*5f4c09ddSEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*5f4c09ddSEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*5f4c09ddSEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*5f4c09ddSEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*5f4c09ddSEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*5f4c09ddSEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*5f4c09ddSEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*5f4c09ddSEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*5f4c09ddSEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*5f4c09ddSEd Maste  * POSSIBILITY OF SUCH DAMAGE.
30*5f4c09ddSEd Maste  */
31*5f4c09ddSEd Maste #ifdef HAVE_CONFIG_H
32*5f4c09ddSEd Maste #include "config.h"
33*5f4c09ddSEd Maste #endif
34*5f4c09ddSEd Maste 
35*5f4c09ddSEd Maste #include <sys/cdefs.h>
36*5f4c09ddSEd Maste __RCSID("$NetBSD: support.c,v 1.9 2018/09/18 22:12:19 christos Exp $");
37*5f4c09ddSEd Maste 
38*5f4c09ddSEd Maste #include <time.h>
39*5f4c09ddSEd Maste #include <string.h>
40*5f4c09ddSEd Maste #include <stdio.h>
41*5f4c09ddSEd Maste #include <stdarg.h>
42*5f4c09ddSEd Maste #include <errno.h>
43*5f4c09ddSEd Maste #include <stdlib.h>
44*5f4c09ddSEd Maste #include <inttypes.h>
45*5f4c09ddSEd Maste 
46*5f4c09ddSEd Maste #include "support.h"
47*5f4c09ddSEd Maste 
48*5f4c09ddSEd Maste static __attribute__((__format_arg__(3))) const char *
expandm(char * buf,size_t len,const char * fmt)49*5f4c09ddSEd Maste expandm(char *buf, size_t len, const char *fmt)
50*5f4c09ddSEd Maste {
51*5f4c09ddSEd Maste 	char *p;
52*5f4c09ddSEd Maste 	size_t r;
53*5f4c09ddSEd Maste 
54*5f4c09ddSEd Maste 	if ((p = strstr(fmt, "%m")) == NULL)
55*5f4c09ddSEd Maste 		return fmt;
56*5f4c09ddSEd Maste 
57*5f4c09ddSEd Maste 	r = (size_t)(p - fmt);
58*5f4c09ddSEd Maste 	if (r >= len)
59*5f4c09ddSEd Maste 		return fmt;
60*5f4c09ddSEd Maste 
61*5f4c09ddSEd Maste 	strlcpy(buf, fmt, r + 1);
62*5f4c09ddSEd Maste 	strlcat(buf, strerror(errno), len);
63*5f4c09ddSEd Maste 	strlcat(buf, fmt + r + 2, len);
64*5f4c09ddSEd Maste 
65*5f4c09ddSEd Maste 	return buf;
66*5f4c09ddSEd Maste }
67*5f4c09ddSEd Maste 
68*5f4c09ddSEd Maste void
vdlog(int level __unused,const char * fmt,va_list ap)69*5f4c09ddSEd Maste vdlog(int level __unused, const char *fmt, va_list ap)
70*5f4c09ddSEd Maste {
71*5f4c09ddSEd Maste 	char buf[BUFSIZ];
72*5f4c09ddSEd Maste 
73*5f4c09ddSEd Maste //	fprintf(stderr, "%s: ", getprogname());
74*5f4c09ddSEd Maste 	vfprintf(stderr, expandm(buf, sizeof(buf), fmt), ap);
75*5f4c09ddSEd Maste 	fprintf(stderr, "\n");
76*5f4c09ddSEd Maste }
77*5f4c09ddSEd Maste 
78*5f4c09ddSEd Maste void
dlog(int level,const char * fmt,...)79*5f4c09ddSEd Maste dlog(int level, const char *fmt, ...)
80*5f4c09ddSEd Maste {
81*5f4c09ddSEd Maste 	va_list ap;
82*5f4c09ddSEd Maste 
83*5f4c09ddSEd Maste 	va_start(ap, fmt);
84*5f4c09ddSEd Maste 	vdlog(level, fmt, ap);
85*5f4c09ddSEd Maste 	va_end(ap);
86*5f4c09ddSEd Maste }
87*5f4c09ddSEd Maste 
88*5f4c09ddSEd Maste const char *
fmttime(char * b,size_t l,time_t t)89*5f4c09ddSEd Maste fmttime(char *b, size_t l, time_t t)
90*5f4c09ddSEd Maste {
91*5f4c09ddSEd Maste 	struct tm tm;
92*5f4c09ddSEd Maste 	if (localtime_r(&t, &tm) == NULL)
93*5f4c09ddSEd Maste 		snprintf(b, l, "*%jd*", (intmax_t)t);
94*5f4c09ddSEd Maste 	else
95*5f4c09ddSEd Maste 		strftime(b, l, "%Y/%m/%d %H:%M:%S", &tm);
96*5f4c09ddSEd Maste 	return b;
97*5f4c09ddSEd Maste }
98*5f4c09ddSEd Maste 
99*5f4c09ddSEd Maste const char *
fmtydhms(char * b,size_t l,time_t t)100*5f4c09ddSEd Maste fmtydhms(char *b, size_t l, time_t t)
101*5f4c09ddSEd Maste {
102*5f4c09ddSEd Maste 	time_t s, m, h, d, y;
103*5f4c09ddSEd Maste 	int z;
104*5f4c09ddSEd Maste 	size_t o;
105*5f4c09ddSEd Maste 
106*5f4c09ddSEd Maste 	s = t % 60;
107*5f4c09ddSEd Maste 	t /= 60;
108*5f4c09ddSEd Maste 
109*5f4c09ddSEd Maste 	m = t % 60;
110*5f4c09ddSEd Maste 	t /= 60;
111*5f4c09ddSEd Maste 
112*5f4c09ddSEd Maste 	h = t % 24;
113*5f4c09ddSEd Maste 	t /= 24;
114*5f4c09ddSEd Maste 
115*5f4c09ddSEd Maste 	d = t % 365;
116*5f4c09ddSEd Maste 	t /= 365;
117*5f4c09ddSEd Maste 
118*5f4c09ddSEd Maste 	y = t;
119*5f4c09ddSEd Maste 
120*5f4c09ddSEd Maste 	z = 0;
121*5f4c09ddSEd Maste 	o = 0;
122*5f4c09ddSEd Maste #define APPEND(a) \
123*5f4c09ddSEd Maste 	if (a) { \
124*5f4c09ddSEd Maste 		z = snprintf(b + o, l - o, "%jd%s", (intmax_t)a, __STRING(a)); \
125*5f4c09ddSEd Maste 		if (z == -1) \
126*5f4c09ddSEd Maste 			return b; \
127*5f4c09ddSEd Maste 		o += (size_t)z; \
128*5f4c09ddSEd Maste 		if (o >= l) \
129*5f4c09ddSEd Maste 			return b; \
130*5f4c09ddSEd Maste 	}
131*5f4c09ddSEd Maste 	APPEND(y)
132*5f4c09ddSEd Maste 	APPEND(d)
133*5f4c09ddSEd Maste 	APPEND(h)
134*5f4c09ddSEd Maste 	APPEND(m)
135*5f4c09ddSEd Maste 	APPEND(s)
136*5f4c09ddSEd Maste 	return b;
137*5f4c09ddSEd Maste }
138*5f4c09ddSEd Maste 
139*5f4c09ddSEd Maste ssize_t
blhexdump(char * buf,size_t len,const char * str,const void * b,size_t l)140*5f4c09ddSEd Maste blhexdump(char *buf, size_t len, const char *str, const void *b, size_t l)
141*5f4c09ddSEd Maste {
142*5f4c09ddSEd Maste 	size_t z, cz;
143*5f4c09ddSEd Maste 	int r;
144*5f4c09ddSEd Maste 	const unsigned char *p = b;
145*5f4c09ddSEd Maste 	const unsigned char *e = p + l;
146*5f4c09ddSEd Maste 
147*5f4c09ddSEd Maste 	r = snprintf(buf, len, "%s: ", str);
148*5f4c09ddSEd Maste 	if (r == -1)
149*5f4c09ddSEd Maste 		return -1;
150*5f4c09ddSEd Maste 	if ((cz = z = (size_t)r) >= len)
151*5f4c09ddSEd Maste 		cz = len;
152*5f4c09ddSEd Maste 
153*5f4c09ddSEd Maste 	while (p < e) {
154*5f4c09ddSEd Maste 		r = snprintf(buf + cz, len - cz, "%.2x", *p++);
155*5f4c09ddSEd Maste 		if (r == -1)
156*5f4c09ddSEd Maste 			return -1;
157*5f4c09ddSEd Maste 		if ((cz = (z += (size_t)r)) >= len)
158*5f4c09ddSEd Maste 			cz = len;
159*5f4c09ddSEd Maste 	}
160*5f4c09ddSEd Maste 	return (ssize_t)z;
161*5f4c09ddSEd Maste }
162