diff options
Diffstat (limited to 'src/lib/core')
| -rw-r--r-- | src/lib/core/include/str.h | 4 | ||||
| -rw-r--r-- | src/lib/core/str.c | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/lib/core/include/str.h b/src/lib/core/include/str.h index 5097148..3e7c3a4 100644 --- a/src/lib/core/include/str.h +++ b/src/lib/core/include/str.h @@ -18,6 +18,10 @@ #define STASIS_SORT_LEN_ASCENDING 1 << 2 #define STASIS_SORT_LEN_DESCENDING 1 << 3 + +char *strdup_maybe_entry(const char * restrict s, struct ExecPoint ep, int exit_code); +#define strdup_maybe(S) strdup_maybe_entry((S), EXECPOINT, 1) + /** * Determine how many times the character `ch` appears in `sptr` string * @param sptr string to scan diff --git a/src/lib/core/str.c b/src/lib/core/str.c index 7ba6c5b..5df7372 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -4,6 +4,22 @@ #include <unistd.h> #include "str.h" + + +char *strdup_maybe_entry(const char * restrict s, const struct ExecPoint ep, const int exit_code) { + // USE MACRO FROM str.h: strdup_maybe() + if (s != NULL) { + char *x = strdup(s); + if (!x) { + SYSERROR("unable to duplicate string"); + log_print_error(ep, "out of memory"); + exit(exit_code); + } + return x; + } + return NULL; +} + int num_chars(const char *sptr, int ch) { int result = 0; for (int i = 0; sptr[i] != '\0'; i++) { |
