summaryrefslogtreecommitdiff
path: root/firmware/include
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/include')
-rw-r--r--firmware/include/string-extra.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/firmware/include/string-extra.h b/firmware/include/string-extra.h
index 6a9e0c77be..9ab53d8154 100644
--- a/firmware/include/string-extra.h
+++ b/firmware/include/string-extra.h
@@ -34,4 +34,38 @@
34#endif 34#endif
35#endif 35#endif
36 36
37/* copies a buffer of len bytes and null terminates it */
38static inline char * strmemcpy(char *dst, const char *src, size_t len)
39{
40 /* NOTE: for now, assumes valid parameters! */
41 *(char *)mempcpy(dst, src, len) = '\0';
42 return dst;
43}
44
45/* duplicate and null-terminate a memory block on the stack with alloca() */
46#define strmemdupa(s, l) \
47 ({ const char *___s = (s); \
48 size_t ___l = (l); \
49 char *___buf = alloca(___l + 1); \
50 strmemcpy(___buf, ___s, ___l); })
51
52/* strdupa and strndupa may already be provided by a system's string.h */
53
54#ifndef strdupa
55/* duplicate an entire string on the stack with alloca() */
56#define strdupa(s) \
57 ({ const char *__s = (s); \
58 strmemdupa((__s), strlen(__s)); })
59#endif /* strdupa */
60
61#ifndef strndupa
62/* duplicate a string on the stack with alloca(), truncating it if it is too
63 long */
64#define strndupa(s, n) \
65 ({ const char *__s = (s); \
66 size_t __n = (n); \
67 size_t __len = strlen(_s); \
68 strmemdupa(__s, MIN(__n, __len)); })
69#endif /* strndupa */
70
37#endif /* STRING_EXTRA_H */ 71#endif /* STRING_EXTRA_H */