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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Concatenates/copies strings. In any case, terminates in all cases
* with '\0' and moves the @dest pointer forward to the added '\0'.
* Returns the remaining size, and 0 if the string was truncated.
*
* Due to the intended usage, these helpers silently noop invocations
* having zero size. This is technically an exception to the above
* statement "terminates in all cases". It's unexpected for such calls to
* occur outside of a loop where this is the preferred behavior.
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "string-util.h"
#include "strxcpyx.h"
size_t strnpcpy_full(char **dest, size_t size, const char *src, size_t len, bool *ret_truncated) {
bool truncated = false;
assert(dest);
assert(src);
if (size == 0) {
if (ret_truncated)
*ret_truncated = len > 0;
return 0;
}
if (len >= size) {
if (size > 1)
*dest = mempcpy(*dest, src, size-1);
size = 0;
truncated = true;
} else if (len > 0) {
*dest = mempcpy(*dest, src, len);
size -= len;
}
if (ret_truncated)
*ret_truncated = truncated;
*dest[0] = '\0';
return size;
}
size_t strpcpy_full(char **dest, size_t size, const char *src, bool *ret_truncated) {
assert(dest);
assert(src);
return strnpcpy_full(dest, size, src, strlen(src), ret_truncated);
}
size_t strpcpyf_full(char **dest, size_t size, bool *ret_truncated, const char *src, ...) {
bool truncated = false;
va_list va;
int i;
assert(dest);
assert(src);
va_start(va, src);
i = vsnprintf(*dest, size, src, va);
va_end(va);
if (i < (int) size) {
*dest += i;
size -= i;
} else {
size = 0;
truncated = i > 0;
}
if (ret_truncated)
*ret_truncated = truncated;
return size;
}
size_t strpcpyl_full(char **dest, size_t size, bool *ret_truncated, const char *src, ...) {
bool truncated = false;
va_list va;
assert(dest);
assert(src);
va_start(va, src);
do {
bool t;
size = strpcpy_full(dest, size, src, &t);
truncated = truncated || t;
src = va_arg(va, char *);
} while (src);
va_end(va);
if (ret_truncated)
*ret_truncated = truncated;
return size;
}
size_t strnscpy_full(char *dest, size_t size, const char *src, size_t len, bool *ret_truncated) {
char *s;
assert(dest);
assert(src);
s = dest;
return strnpcpy_full(&s, size, src, len, ret_truncated);
}
size_t strscpy_full(char *dest, size_t size, const char *src, bool *ret_truncated) {
assert(dest);
assert(src);
return strnscpy_full(dest, size, src, strlen(src), ret_truncated);
}
size_t strscpyl_full(char *dest, size_t size, bool *ret_truncated, const char *src, ...) {
bool truncated = false;
va_list va;
char *s;
assert(dest);
assert(src);
va_start(va, src);
s = dest;
do {
bool t;
size = strpcpy_full(&s, size, src, &t);
truncated = truncated || t;
src = va_arg(va, char *);
} while (src);
va_end(va);
if (ret_truncated)
*ret_truncated = truncated;
return size;
}
|