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/lsblk.c | 11 +++++------ util-linux/mount.c | 25 +++++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) (limited to 'util-linux') diff --git a/util-linux/lsblk.c b/util-linux/lsblk.c index a482bfbbc..3bd40bcb4 100644 --- a/util-linux/lsblk.c +++ b/util-linux/lsblk.c @@ -101,10 +101,10 @@ static char *get_mountpoints(const char *majmin) mountpoints = NULL; len = strlen(majmin); - p = G.mountinfo; // "/proc/self/mountinfo" + p = G.mountinfo; // contents of "/proc/self/mountinfo" /* lines a-la "63 1 259:3 / /MNTPOINT per-mount_options - ext4 /dev/NAME per-superblock_options" */ while (*p) { - char *e, *f; + char *e; p = skip_non_whitespace(p); if (*p != ' ') break; @@ -127,12 +127,11 @@ static char *get_mountpoints(const char *majmin) // at " /MNTPOINT" e = skip_non_whitespace(p + 1); // e is at the end of " /MNTPOINT" - f = mountpoints; // NO. We return " /MNT1 /MNT2 /MNT3" _with_ leading space! -// if (!f) +// if (!mountpoints) // p++; - mountpoints = xasprintf("%s%.*s", f ? f : "", (int)(e - p), p); - free(f); + xasprintf_inplace(mountpoints, "%s%.*s", + mountpoints ? mountpoints : "", (int)(e - p), p); } return mountpoints; } 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