summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/util.c b/util.c
index cee9cc3..fc73b1b 100644
--- a/util.c
+++ b/util.c
@@ -97,6 +97,19 @@ static char *str_dup(char const *src) {
#define strbuf_catf(str, ...) assert(sizeof str != 4 && sizeof str != 8), \
str_catf(str, sizeof str, __VA_ARGS__)
+#if __unix__
+static char *a_sprintf(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
+static char *a_sprintf(const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ char *str = NULL;
+ vasprintf(&str, fmt, args);
+ return str;
+}
+#else
+#error "@TODO"
+#endif
+
// on 16-bit systems, this is 16383. on 32/64-bit systems, this is 1073741823
// it is unusual to have a string that long.
#define STRLEN_SAFE_MAX (UINT_MAX >> 2)
@@ -134,9 +147,14 @@ static void str_cat(char *dst, size_t dst_sz, char const *src) {
}
// safer version of strncpy. dst_sz includes a null terminator.
-static void str_cpy(char *dst, size_t dst_sz, char const *src) {
- size_t srclen = strlen(src);
- size_t n = srclen; // number of bytes to copy
+static void strn_cpy(char *dst, size_t dst_sz, char const *src, size_t src_len) {
+ size_t n = src_len; // number of bytes to copy
+ for (size_t i = 0; i < n; ++i) {
+ if (src[i] == '\0') {
+ n = i;
+ break;
+ }
+ }
if (dst_sz == 0) {
assert(0);
@@ -149,6 +167,11 @@ static void str_cpy(char *dst, size_t dst_sz, char const *src) {
dst[n] = 0;
}
+// safer version of strcpy. dst_sz includes a null terminator.
+static void str_cpy(char *dst, size_t dst_sz, char const *src) {
+ strn_cpy(dst, dst_sz, src, SIZE_MAX);
+}
+
#define strbuf_cpy(dst, src) str_cpy(dst, sizeof dst, src)
#define strbuf_cat(dst, src) str_cat(dst, sizeof dst, src)