1 /* $NetBSD: support.c,v 1.2 2024/08/02 17:11:55 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 #include <sys/cdefs.h> 36 __RCSID("$NetBSD: support.c,v 1.2 2024/08/02 17:11:55 christos Exp $"); 37 38 #include <time.h> 39 #include <string.h> 40 #include <stdio.h> 41 #include <stdarg.h> 42 #include <errno.h> 43 #include <stdlib.h> 44 #include <inttypes.h> 45 46 #include "support.h" 47 48 static __attribute__((__format_arg__(3))) const char * 49 expandm(char *buf, size_t len, const char *fmt) 50 { 51 char *p; 52 size_t r; 53 54 if ((p = strstr(fmt, "%m")) == NULL) 55 return fmt; 56 57 r = (size_t)(p - fmt); 58 if (r >= len) 59 return fmt; 60 61 strlcpy(buf, fmt, r + 1); 62 strlcat(buf, strerror(errno), len); 63 strlcat(buf, fmt + r + 2, len); 64 65 return buf; 66 } 67 68 void 69 vdlog(int level __unused, struct syslog_data *sd __unused, 70 const char *fmt, va_list ap) 71 { 72 char buf[BUFSIZ]; 73 74 // fprintf(stderr, "%s: ", getprogname()); 75 vfprintf(stderr, expandm(buf, sizeof(buf), fmt), ap); 76 fprintf(stderr, "\n"); 77 } 78 79 void 80 dlog(int level, const char *fmt, ...) 81 { 82 va_list ap; 83 84 va_start(ap, fmt); 85 vdlog(level, NULL, fmt, ap); 86 va_end(ap); 87 } 88 89 const char * 90 fmttime(char *b, size_t l, time_t t) 91 { 92 struct tm tm; 93 if (localtime_r(&t, &tm) == NULL) 94 snprintf(b, l, "*%jd*", (intmax_t)t); 95 else 96 strftime(b, l, "%Y/%m/%d %H:%M:%S", &tm); 97 return b; 98 } 99 100 const char * 101 fmtydhms(char *b, size_t l, time_t t) 102 { 103 time_t s, m, h, d, y; 104 int z; 105 size_t o; 106 107 s = t % 60; 108 t /= 60; 109 110 m = t % 60; 111 t /= 60; 112 113 h = t % 24; 114 t /= 24; 115 116 d = t % 365; 117 t /= 365; 118 119 y = t; 120 121 z = 0; 122 o = 0; 123 #define APPEND(a) \ 124 if (a) { \ 125 z = snprintf(b + o, l - o, "%jd%s", (intmax_t)a, __STRING(a)); \ 126 if (z == -1) \ 127 return b; \ 128 o += (size_t)z; \ 129 if (o >= l) \ 130 return b; \ 131 } 132 APPEND(y) 133 APPEND(d) 134 APPEND(h) 135 APPEND(m) 136 APPEND(s) 137 return b; 138 } 139 140 ssize_t 141 blhexdump(char *buf, size_t len, const char *str, const void *b, size_t l) 142 { 143 size_t z, cz; 144 int r; 145 const unsigned char *p = b; 146 const unsigned char *e = p + l; 147 148 r = snprintf(buf, len, "%s: ", str); 149 if (r == -1) 150 return -1; 151 if ((cz = z = (size_t)r) >= len) 152 cz = len; 153 154 while (p < e) { 155 r = snprintf(buf + cz, len - cz, "%.2x", *p++); 156 if (r == -1) 157 return -1; 158 if ((cz = (z += (size_t)r)) >= len) 159 cz = len; 160 } 161 return (ssize_t)z; 162 } 163