summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-08-28 13:59:24 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-08-28 13:59:24 -0400
commit1661532486d742462f834d2e57f1ad827d6e8916 (patch)
tree67ffb590c5a10bc85a85d16a4792e48342545524 /util
parent62dfeef42d6c2e279165b6dbe73ee3abf98db146 (diff)
cleaned up code; fixed some function stuff
Diffstat (limited to 'util')
-rw-r--r--util/str.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/util/str.c b/util/str.c
new file mode 100644
index 0000000..b05cc63
--- /dev/null
+++ b/util/str.c
@@ -0,0 +1,18 @@
+/*
+A better alternative to strncpy. dest is guaranteed to be a null-terminated string
+after this function is run.
+Returns the number of characters copied to dest, not including the null character.
+destsz must be greater than 0.
+*/
+size_t str_copy(char *dest, size_t destsz, const char *src) {
+ assert(destsz);
+ for (size_t i = 0; i < destsz-1; i++) {
+ if (!*src) {
+ *dest = 0;
+ return i;
+ }
+ *dest++ = *src++;
+ }
+ dest[destsz] = 0;
+ return destsz-1;
+}