commit f133f7a
uint
·
2026-06-09 21:12:05 +0000 UTC
parent e734053
Add `utils`, implement strdup in `utils`
A
utils.c
+19,
-0
1@@ -0,0 +1,19 @@
2+#include <stdlib.h>
3+#include <string.h>
4+
5+#include "utils.h"
6+
7+char* strdup(const char* src)
8+{
9+ if (src == NULL)
10+ return NULL;
11+
12+ size_t len = strlen(src);
13+ char* dst = malloc(len + 1);
14+ if (dst == NULL)
15+ return NULL;
16+
17+ memcpy(dst, src, len + 1);
18+ return dst;
19+}
20+
A
utils.h
+11,
-0
1@@ -0,0 +1,11 @@
2+#ifndef UTIL_H
3+#define UTIL_H
4+
5+/* duplicate a string. returns pointer to address of
6+ newly allocated string buffer. if NULL, allocation
7+ failed or src is NULL
8+ note: you must free this newly allocated string */
9+char* strdup(const char* src);
10+
11+#endif /* UTIL_H */
12+