summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-02-22 12:36:50 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-02-22 12:36:50 -0500
commit1631f38d4dba64577c8f064225599273148ad83d (patch)
tree740747615e12e0d45681875cdb2e2d4f9f681e77 /util.c
parent37ce64c167e12c0d652442b2ff3deb9327d1317d (diff)
go to definition menu
Diffstat (limited to 'util.c')
-rw-r--r--util.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/util.c b/util.c
index 81c4ce3..9ceb4cc 100644
--- a/util.c
+++ b/util.c
@@ -65,15 +65,24 @@ static bool streq(char const *a, char const *b) {
return strcmp(a, b) == 0;
}
-// duplicates a null-terminated string. the returned string should be passed to free()
-static char *str_dup(char const *src) {
+// duplicates at most n characters from src
+static char *strn_dup(char const *src, size_t n) {
size_t len = strlen(src);
- char *ret = malloc(len + 1);
- if (ret)
- memcpy(ret, src, len + 1);
+ if (n > len)
+ n = len;
+ char *ret = malloc(n + 1);
+ if (ret) {
+ memcpy(ret, src, n);
+ ret[n] = 0;
+ }
return ret;
}
+// duplicates a null-terminated string. the returned string should be passed to free()
+static char *str_dup(char const *src) {
+ return strn_dup(src, SIZE_MAX);
+}
+
// like snprintf, but not screwed up on windows
#define str_printf(str, size, ...) (str)[(size) - 1] = '\0', snprintf((str), (size) - 1, __VA_ARGS__)
// like snprintf, but the size is taken to be the length of the array str.