-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsg_pr2serr.c
110 lines (98 loc) · 3.04 KB
/
sg_pr2serr.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (c) 2022-2023 Douglas Gilbert.
* All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the BSD_LICENSE file.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "sg_pr2serr.h"
FILE * sg_warnings_strm = NULL; /* would like to default to stderr */
int
pr2serr(const char * fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = vfprintf(stderr, fmt, args);
va_end(args);
return n;
}
int
pr2ws(const char * fmt, ...)
{
va_list args;
int n;
va_start(args, fmt);
n = vfprintf(sg_warnings_strm ? sg_warnings_strm : stderr, fmt, args);
va_end(args);
return n;
}
/* Want safe, 'n += snprintf(b + n, blen - n, ...);' pattern that can
* be called repeatedly. However snprintf() takes an unsigned second argument
* (size_t) that explodes if 'blen - n' goes negative. This function instead
* uses signed integers (second argument and return value) and is safe if the
* second argument is negative. It returns number of chars actually
* placed in cp excluding the trailing null char. So for cp_max_len > 0 the
* return value is always < cp_max_len; for cp_max_len <= 1 the return value
* is 0 and no chars are written to cp. Note this means that when
* cp_max_len = 1, this function assumes that cp[0] is the null character
* and does nothing (and returns 0). Linux kernel has a similar function
* called scnprintf(). */
int
sg_scnpr(char * cp, int cp_max_len, const char * fmt, ...)
{
va_list args;
int n;
#ifdef DEBUG
if (cp_max_len < 2) {
/* stack backtrace would be good here ... */
pr2ws("%s: buffer would overrun, 'fmt' string: %s\n", __func__, fmt);
return 0;
}
#else
if (cp_max_len < 2)
return 0;
#endif
va_start(args, fmt);
n = vsnprintf(cp, cp_max_len, fmt, args);
va_end(args);
return (n < cp_max_len) ? n : (cp_max_len - 1);
}
/* This function is similar to sg_scnpr() but takes the "n" in that pattern
* as an extra, third argument where it is renamed 'off'. This function will
* start writing chars at 'fcp + off' for no more than 'fcp_len - off - 1'
* characters. The return value is the same as sg_scnpr(). */
int
sg_scn3pr(char * fcp, int fcp_len, int off, const char * fmt, ...)
{
va_list args;
const int cp_max_len = fcp_len - off;
int n;
#ifdef DEBUG
if (cp_max_len < 2) {
/* stack backtrace would be good here ... */
pr2ws("%s: buffer would overrun, 'fmt' string: %s\n", __func__, fmt);
return 0;
}
#else
if (cp_max_len < 2)
return 0;
#endif
va_start(args, fmt);
n = vsnprintf(fcp + off, fcp_len - off, fmt, args);
va_end(args);
#ifdef DEBUG
if (n >= cp_max_len) /* make noise when truncation */
pr2ws("%s: truncated [n=%d]: %s; 'fmt' string: %s\n", __func__, n,
fcp, fmt);
#endif
return (n < cp_max_len) ? n : (cp_max_len - 1);
}