/* $NetBSD: support.c,v 1.4 2026/02/07 14:29:58 christos Exp $ */12/*-3* Copyright (c) 2015 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code is derived from software contributed to The NetBSD Foundation7* by Christos Zoulas.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED20* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS22* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/30#ifdef HAVE_CONFIG_H31#include "config.h"32#endif3334#ifdef HAVE_SYS_CDEFS_H35#include <sys/cdefs.h>36#endif37__RCSID("$NetBSD: support.c,v 1.4 2026/02/07 14:29:58 christos Exp $");3839#include <time.h>40#include <string.h>41#include <stdio.h>42#include <stdarg.h>43#include <errno.h>44#include <stdlib.h>45#include <inttypes.h>4647#include "support.h"4849static __attribute__((__format_arg__(3))) const char *50expandm(char *buf, size_t len, const char *fmt)51{52char *p;53size_t r;5455if ((p = strstr(fmt, "%m")) == NULL)56return fmt;5758r = (size_t)(p - fmt);59if (r >= len)60return fmt;6162strlcpy(buf, fmt, r + 1);63strlcat(buf, strerror(errno), len);64strlcat(buf, fmt + r + 2, len);6566return buf;67}6869void70vdlog(int level __unused, struct syslog_data *sd __unused,71const char *fmt, va_list ap)72{73char buf[BUFSIZ];7475// fprintf(stderr, "%s: ", getprogname());76vfprintf(stderr, expandm(buf, sizeof(buf), fmt), ap);77fprintf(stderr, "\n");78}7980void81dlog(int level, const char *fmt, ...)82{83va_list ap;8485va_start(ap, fmt);86vdlog(level, NULL, fmt, ap);87va_end(ap);88}8990const char *91fmttime(char *b, size_t l, time_t t)92{93struct tm tm;94if (localtime_r(&t, &tm) == NULL)95snprintf(b, l, "*%jd*", (intmax_t)t);96else97strftime(b, l, "%Y/%m/%d %H:%M:%S", &tm);98return b;99}100101const char *102fmtydhms(char *b, size_t l, time_t t)103{104time_t s, m, h, d, y;105int z;106size_t o;107108s = t % 60;109t /= 60;110111m = t % 60;112t /= 60;113114h = t % 24;115t /= 24;116117d = t % 365;118t /= 365;119120y = t;121122o = 0;123#define APPEND(a) \124if (a) { \125z = snprintf(b + o, l - o, "%jd%s", (intmax_t)a, __STRING(a)); \126if (z == -1) \127return b; \128o += (size_t)z; \129if (o >= l) \130return b; \131}132APPEND(y)133APPEND(d)134APPEND(h)135APPEND(m)136APPEND(s)137return b;138}139140ssize_t141blhexdump(char *buf, size_t len, const char *str, const void *b, size_t l)142{143size_t z, cz;144int r;145const unsigned char *p = b;146const unsigned char *e = p + l;147148r = snprintf(buf, len, "%s: ", str);149if (r == -1)150return -1;151if ((cz = z = (size_t)r) >= len)152cz = len;153154while (p < e) {155r = snprintf(buf + cz, len - cz, "%.2x", *p++);156if (r == -1)157return -1;158if ((cz = (z += (size_t)r)) >= len)159cz = len;160}161return (ssize_t)z;162}163164165