string-util: add new strdupcspn()/strdupspn()

These combine strndup() + strspn()/strcspn() into one.

There are a bunch of strndupa() calls that could use similar treatment
(or should be converted to strdup[c]spn(), but this commit doesn't
bother with that.
This commit is contained in:
Lennart Poettering
2023-01-20 17:27:51 +01:00
parent 71c6f0ac52
commit e8bec6242b
8 changed files with 27 additions and 13 deletions
+1 -3
View File
@@ -175,15 +175,13 @@ static int image_new(
static int extract_pretty(const char *path, const char *suffix, char **ret) {
_cleanup_free_ char *name = NULL;
const char *p;
size_t n;
assert(path);
assert(ret);
p = last_path_component(path);
n = strcspn(p, "/");
name = strndup(p, n);
name = strdupcspn(p, "/");
if (!name)
return -ENOMEM;
+1 -1
View File
@@ -108,7 +108,7 @@ static int url_from_catalog(sd_journal *j, char **ret) {
weblink += strspn(weblink, " \t");
/* Cut out till next whitespace/newline */
url = strndup(weblink, strcspn(weblink, WHITESPACE));
url = strdupcspn(weblink, WHITESPACE);
if (!url)
return log_oom();