1 /* $NetBSD: support.c,v 1.7 2015/06/02 14:02:10 christos Exp $ */
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: support.c,v 1.7 2015/06/02 14:02:10 christos Exp $");
48 static __attribute__((__format_arg__(3))) const char *
49 expandm(char *buf
, size_t len
, const char *fmt
)
54 if ((p
= strstr(fmt
, "%m")) == NULL
)
57 r
= (size_t)(p
- fmt
);
61 strlcpy(buf
, fmt
, r
+ 1);
62 strlcat(buf
, strerror(errno
), len
);
63 strlcat(buf
, fmt
+ r
+ 2, len
);
69 vdlog(int level __unused
, const char *fmt
, va_list ap
)
73 // fprintf(stderr, "%s: ", getprogname());
74 vfprintf(stderr
, expandm(buf
, sizeof(buf
), fmt
), ap
);
75 fprintf(stderr
, "\n");
79 dlog(int level
, const char *fmt
, ...)
84 vdlog(level
, fmt
, ap
);
89 fmttime(char *b
, size_t l
, time_t t
)
92 if (localtime_r(&t
, &tm
) == NULL
)
93 snprintf(b
, l
, "*%jd*", (intmax_t)t
);
95 strftime(b
, l
, "%Y/%m/%d %H:%M:%S", &tm
);
100 fmtydhms(char *b
, size_t l
, time_t t
)
102 time_t s
, m
, h
, d
, y
;
120 z = snprintf(b + o, l - o, "%jd%s", (intmax_t)a, __STRING(a)); \
136 hexdump(char *buf
, size_t len
, const char *str
, const void *b
, size_t l
)
140 const unsigned char *p
= b
;
141 const unsigned char *e
= p
+ l
;
143 r
= snprintf(buf
, len
, "%s: ", str
);
146 if ((cz
= z
= (size_t)r
) >= len
)
150 r
= snprintf(buf
+ cz
, len
- cz
, "%.2x", *p
++);
153 if ((cz
= (z
+= (size_t)r
)) >= len
)