1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// SPDX-FileCopyrightText: 2024-2026 Markus Uhlin <maxxe@rpblc.net>
// SPDX-License-Identifier: ISC
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#if __linux__
#include <bsd/string.h>
#endif
#include "maxxes-utils.h"
void
fprintf_logerr(const char *file, const long int line,
FILE *fp, const char *format, ...)
{
int ret;
va_list ap;
if (fp == NULL) {
warnx("%s:%ld: error: invalid argument (null pointer)",
file, line);
return;
} else if (fp == stdin || fp == stdout || fp == stderr) {
warnx("%s:%ld: error: invalid stream (stdin, stdout or stderr)",
file, line);
return;
}
va_start(ap, format);
ret = vfprintf(fp, format, ap);
va_end(ap);
if (ret < 0)
warnx("%s:%ld: warning: vfprintf() error", file, line);
}
bool
is_too_long(const int p_ret, const size_t p_maxlen)
{
return (p_ret < 0 || (size_t)p_ret >= p_maxlen);
}
void
snprintf_trunc_chk(const char *file, const long int line,
char *str, size_t size, const char *format, ...)
{
int ret;
va_list ap;
va_start(ap, format);
ret = vsnprintf(str, size, format, ap);
va_end(ap);
if (ret < 0 || (size_t)ret >= size)
warnx("%s:%ld: warning: vsnprintf() truncated", file, line);
}
void
strlcpy_trunc_chk(char *dst, const char *src, size_t dstsize,
const char *file,
const long int line)
{
if (strlcpy(dst, src, dstsize) >= dstsize)
warnx("%s:%ld: warning: strlcpy() truncated", file, line);
}
void
strlcat_trunc_chk(char *dst, const char *src, size_t dstsize,
const char *file,
const long int line)
{
if (strlcat(dst, src, dstsize) >= dstsize)
warnx("%s:%ld: warning: strlcat() truncated", file, line);
}
|