X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=fc6f668726c60ee7d00db1809b7e3f8a3f4de744;hb=2928b0a863091f8f291fddb168988711afd389ef;hp=a54e8799530d4e2e3b40161ef1c618c14b6109a7;hpb=2de1851fe3611c59abf77127c6b5bc1b91eb7cba;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index a54e87995..fc6f66872 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -7137,3 +7137,24 @@ int unquote_many_words(const char **p, ...) { return c; } + +int free_and_strdup(char **p, const char *s) { + char *t; + + assert(p); + + /* Replaces a string pointer with an strdup()ed new string, + * possibly freeing the old one. */ + + if (s) { + t = strdup(s); + if (!t) + return -ENOMEM; + } else + t = NULL; + + free(*p); + *p = t; + + return 0; +}