diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 6b6a5746fe..b6d8be3083 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -301,3 +301,24 @@ bool version_is_valid(const char *s); bool version_is_valid_versionspec(const char *s); ssize_t strlevenshtein(const char *x, const char *y); + +static inline int strdup_or_null(const char *s, char **ret) { + char *c; + + assert(ret); + + /* This is a lot like strdup(), but is happy with NULL strings, and does not treat that as error, but + * copies the NULL value. */ + + if (!s) { + *ret = NULL; + return 0; + } + + c = strdup(s); + if (!c) + return -ENOMEM; + + *ret = c; + return 1; +}