+/*
+ * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
+ * The result needs to be freed with mem_free.
+ */
+INLINE_MAYBE
+char* mem_strdup(const char* str)
+{
+ size_t size = strlen(str) + 1;
+ return (char*)memcpy(mem_alloc(size), str, size);
+}
+
+/*
+ * Get an allocated string made up of two strings concatenated together.
+ * The result needs to be freed with mem_free.
+ */
+INLINE_MAYBE
+char* mem_strcat(const char* str1, const char* str2)
+{
+ size_t size1 = strlen(str1);
+ size_t size2 = strlen(str2);
+ char* str = (char*)mem_alloc(size1 + size2 + 1);
+ memcpy(str + size1, str2, size2 + 1);
+ return memcpy(str, str1, size1);
+}
+
+
+/*
+ * Cut a string short at the last matching character. The string will be
+ * modified if the character was matched. Either way, str is returned.
+ */
+INLINE_MAYBE
+char* strcut(char* str, int c)
+{
+ char* ptr = strrchr(str, c);
+ if (ptr != NULL) {
+ *ptr = '\0';
+ }
+ return str;
+}
+