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