From fddd93edbdbf8c5afbfdb3c01d82e082a8a82d1a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 5 Feb 2026 13:36:27 +0100 Subject: libbb: introduce and use xasprintf_inplace() function old new delta xasprintf_and_free - 49 +49 watch_main 269 282 +13 singlemount 1313 1315 +2 append_mount_options 157 149 -8 ip_port_str 122 112 -10 lsblk_main 869 858 -11 add_cmd 1178 1167 -11 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 2/4 up/down: 64/-40) Total: 24 bytes Signed-off-by: Denys Vlasenko --- util-linux/mount.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'util-linux/mount.c') diff --git a/util-linux/mount.c b/util-linux/mount.c index d0f0ae1ad..d0d109c09 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -556,27 +556,33 @@ static int verbose_mount(const char *source, const char *target, #endif // Append mount options to string +// ("merge two comma-separated lists" is a good candidate for libbb!) static void append_mount_options(char **oldopts, const char *newopts) { if (*oldopts && **oldopts) { +//TODO: do this unconditionally? +//this way, newopts of "opt1,opt2,opt1" +//will be de-duped into "opt1,opt2" in _both_ cases +//(whether or now old opts are empty) +//the only modification needed is to not prepend extra comma +//when old opts is "". // Do not insert options which are already there - while (newopts[0]) { + while (*newopts) { char *p; int len; + //if (*newopts == ',') { newopts++; continue; } len = strchrnul(newopts, ',') - newopts; p = *oldopts; while (1) { - if (!strncmp(p, newopts, len) + if (strncmp(p, newopts, len) == 0 && (p[len] == ',' || p[len] == '\0')) goto skip; - p = strchr(p,','); + p = strchr(p, ','); if (!p) break; p++; } - p = xasprintf("%s,%.*s", *oldopts, len, newopts); - free(*oldopts); - *oldopts = p; + xasprintf_inplace(*oldopts, "%s,%.*s", *oldopts, len, newopts); skip: newopts += len; while (*newopts == ',') newopts++; @@ -2065,12 +2071,11 @@ static int singlemount(struct mntent *mp, int ignore_busy) if (!is_prefixed_with(filteropts, ",ip="+1) && !strstr(filteropts, ",ip=") ) { - char *dotted, *ip; + char *ip; // Insert "ip=..." option into options - dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); + ip = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); if (ENABLE_FEATURE_CLEAN_UP) free(lsa); - ip = xasprintf("ip=%s", dotted); - if (ENABLE_FEATURE_CLEAN_UP) free(dotted); + xasprintf_inplace(ip, "ip=%s", ip); // Note: IPv6 scoped addresses ("host%iface", see RFC 4007) should be // handled by libc in getnameinfo() (inside xmalloc_sockaddr2dotted_noport()). // Currently, glibc does not support that (has no NI_NUMERICSCOPE), -- cgit v1.2.3-55-g6feb