/* $NetBSD: support.c,v 1.9 2018/09/18 22:12:19 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#include <sys/cdefs.h>35__RCSID("$NetBSD: support.c,v 1.9 2018/09/18 22:12:19 christos Exp $");3637#include <time.h>38#include <string.h>39#include <stdio.h>40#include <stdarg.h>41#include <errno.h>42#include <stdlib.h>43#include <inttypes.h>4445#include "support.h"4647static __attribute__((__format_arg__(3))) const char *48expandm(char *buf, size_t len, const char *fmt)49{50char *p;51size_t r;5253if ((p = strstr(fmt, "%m")) == NULL)54return fmt;5556r = (size_t)(p - fmt);57if (r >= len)58return fmt;5960strlcpy(buf, fmt, r + 1);61strlcat(buf, strerror(errno), len);62strlcat(buf, fmt + r + 2, len);6364return buf;65}6667void68vdlog(int level __unused, const char *fmt, va_list ap)69{70char buf[BUFSIZ];7172// fprintf(stderr, "%s: ", getprogname());73vfprintf(stderr, expandm(buf, sizeof(buf), fmt), ap);74fprintf(stderr, "\n");75}7677void78dlog(int level, const char *fmt, ...)79{80va_list ap;8182va_start(ap, fmt);83vdlog(level, fmt, ap);84va_end(ap);85}8687const char *88fmttime(char *b, size_t l, time_t t)89{90struct tm tm;91if (localtime_r(&t, &tm) == NULL)92snprintf(b, l, "*%jd*", (intmax_t)t);93else94strftime(b, l, "%Y/%m/%d %H:%M:%S", &tm);95return b;96}9798const char *99fmtydhms(char *b, size_t l, time_t t)100{101time_t s, m, h, d, y;102int z;103size_t o;104105s = t % 60;106t /= 60;107108m = t % 60;109t /= 60;110111h = t % 24;112t /= 24;113114d = t % 365;115t /= 365;116117y = t;118119z = 0;120o = 0;121#define APPEND(a) \122if (a) { \123z = snprintf(b + o, l - o, "%jd%s", (intmax_t)a, __STRING(a)); \124if (z == -1) \125return b; \126o += (size_t)z; \127if (o >= l) \128return b; \129}130APPEND(y)131APPEND(d)132APPEND(h)133APPEND(m)134APPEND(s)135return b;136}137138ssize_t139blhexdump(char *buf, size_t len, const char *str, const void *b, size_t l)140{141size_t z, cz;142int r;143const unsigned char *p = b;144const unsigned char *e = p + l;145146r = snprintf(buf, len, "%s: ", str);147if (r == -1)148return -1;149if ((cz = z = (size_t)r) >= len)150cz = len;151152while (p < e) {153r = snprintf(buf + cz, len - cz, "%.2x", *p++);154if (r == -1)155return -1;156if ((cz = (z += (size_t)r)) >= len)157cz = len;158}159return (ssize_t)z;160}161162163