Skip to content

Commit a0f922d

Browse files
Explorer09BenBE
andcommitted
Add strnlen() function and configure check
Add configure check for strnlen() function and provide our own implementation for systems that doesn't support it. Co-Authored-By: Benny Baumann <[email protected]> Signed-off-by: Kang-Che Sung <[email protected]>
1 parent a29b7de commit a0f922d

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

XUtils.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ size_t String_safeStrncpy(char* restrict dest, const char* restrict src, size_t
224224
return i;
225225
}
226226

227+
#ifndef HAVE_STRNLEN
228+
size_t strnlen(const char* str, size_t maxLen) {
229+
for (size_t len = 0; len < maxLen; len++) {
230+
if (!str[len]) {
231+
return len;
232+
}
233+
}
234+
return maxLen;
235+
}
236+
#endif
237+
227238
int xAsprintf(char** strp, const char* fmt, ...) {
228239
va_list vl;
229240
va_start(vl, fmt);

XUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ static inline char* String_strchrnul(const char* s, int c) {
102102
ATTR_NONNULL ATTR_ACCESS3_W(1, 3) ATTR_ACCESS3_R(2, 3)
103103
size_t String_safeStrncpy(char* restrict dest, const char* restrict src, size_t size);
104104

105+
#ifndef HAVE_STRNLEN
106+
size_t strnlen(const char* str, size_t maxLen);
107+
#endif
108+
105109
ATTR_FORMAT(printf, 2, 3) ATTR_NONNULL_N(1, 2)
106110
int xAsprintf(char** strp, const char* fmt, ...);
107111

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ AC_CHECK_FUNCS([ \
372372
readlinkat \
373373
sched_getscheduler \
374374
sched_setscheduler \
375+
strnlen \
375376
])
376377

377378
# strchrnul is available in macOS since 15.4, but the user may specify an older

0 commit comments

Comments
 (0)