Skip to content

Commit 34bbd68

Browse files
lib/string/strcpy/: strtcat(), STRTCAT(): Add APIs
Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 76448ca commit 34bbd68

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lib/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ libshadow_la_SOURCES = \
223223
string/strcpy/strncat.h \
224224
string/strcpy/strncpy.c \
225225
string/strcpy/strncpy.h \
226+
string/strcpy/strtcat.c \
227+
string/strcpy/strtcat.h \
226228
string/strcpy/strtcpy.c \
227229
string/strcpy/strtcpy.h \
228230
string/strdup/strndupa.c \

lib/string/strcpy/strtcat.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
5+
#include "config.h"
6+
7+
#include "string/strcpy/strtcat.h"
8+
9+
#include <sys/types.h>
10+
11+
12+
extern inline ssize_t strtcat(char *restrict dst, const char *restrict src,
13+
ssize_t dsize);

lib/string/strcpy/strtcat.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
5+
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCAT_H_
6+
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCAT_H_
7+
8+
9+
#include "config.h"
10+
11+
#include <string.h>
12+
#include <sys/types.h>
13+
14+
#include "attr.h"
15+
#include "sizeof.h"
16+
#include "string/strcpy/strtcpy.h"
17+
18+
19+
#define STRTCAT(dst, src) strtcat(dst, src, countof(dst))
20+
21+
22+
ATTR_STRING(2)
23+
inline ssize_t strtcat(char *restrict dst, const char *restrict src,
24+
ssize_t dsize);
25+
26+
27+
// string truncate catenate
28+
// Returns new length, or -1 on truncation.
29+
inline ssize_t
30+
strtcat(char *restrict dst, const char *restrict src, ssize_t dsize)
31+
{
32+
ssize_t oldlen, n;
33+
34+
oldlen = strlen(dst);
35+
36+
n = strtcpy(dst + oldlen, src, dsize - oldlen);
37+
if (n == -1)
38+
return -1;
39+
40+
return oldlen + n;
41+
}
42+
43+
44+
#endif // include guard

0 commit comments

Comments
 (0)