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