From aa07db0ac1449627b3926669b052b07a1d3986ca Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 30 Jun 2025 11:15:30 +0200 Subject: [PATCH] lib/strutils: add ul_ prefix to startswith() and endswith() Addresses: https://github.com/util-linux/util-linux/issues/3626 Signed-off-by: Karel Zak --- disk-utils/partx.c | 2 +- disk-utils/sfdisk.c | 6 +++--- include/strutils.h | 6 +++--- lib/canonicalize.c | 2 +- lib/loopdev.c | 2 +- lib/sysfs.c | 2 +- lib/timeutils.c | 4 ++-- libfdisk/src/utils.c | 2 +- libmount/src/context_mount.c | 4 ++-- libmount/src/context_umount.c | 2 +- libmount/src/hook_idmap.c | 6 +++--- libmount/src/optmap.c | 2 +- libmount/src/optstr.c | 2 +- libmount/src/tab.c | 4 ++-- libmount/src/tab_parse.c | 2 +- libmount/src/tab_update.c | 2 +- libmount/src/utils.c | 4 ++-- misc-utils/lsblk-properties.c | 2 +- misc-utils/lsclocks.c | 2 +- pam_lastlog2/src/pam_lastlog2.c | 6 +++--- sys-utils/lscpu-cputype.c | 2 +- sys-utils/mount.c | 6 +++--- sys-utils/rtcwake.c | 4 ++-- sys-utils/setpriv-landlock.c | 4 ++-- text-utils/bits.c | 12 ++++++------ 25 files changed, 46 insertions(+), 46 deletions(-) diff --git a/disk-utils/partx.c b/disk-utils/partx.c index 4c8b1f09b..347d5daad 100644 --- a/disk-utils/partx.c +++ b/disk-utils/partx.c @@ -953,7 +953,7 @@ int main(int argc, char **argv) device = argv[optind]; wholedisk = xstrdup(argv[optind + 1]); - if (device && wholedisk && !startswith(device, wholedisk)) + if (device && wholedisk && !ul_startswith(device, wholedisk)) errx(EXIT_FAILURE, _("partition and disk name do not match")); } } else if (optind == argc - 1) { diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 7f2f3deea..ad9bbb3cf 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -1794,12 +1794,12 @@ static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname, if (!partname) err(EXIT_FAILURE, _("failed to allocate partition name")); - if (!sf->prompt || !startswith(sf->prompt, partname)) { + if (!sf->prompt || !ul_startswith(sf->prompt, partname)) { free(sf->prompt); xasprintf(&sf->prompt,"%s: ", partname); } free(partname); - } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) { + } else if (!sf->prompt || !ul_startswith(sf->prompt, SFDISK_PROMPT)) { free(sf->prompt); sf->prompt = xstrdup(SFDISK_PROMPT); } @@ -2024,7 +2024,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) if (!rc) { /* add partition */ if (!sf->interactive && !sf->quiet && - (!sf->prompt || startswith(sf->prompt, SFDISK_PROMPT))) { + (!sf->prompt || ul_startswith(sf->prompt, SFDISK_PROMPT))) { refresh_prompt_buffer(sf, devname, next_partno, created); fputs(sf->prompt, stdout); } diff --git a/include/strutils.h b/include/strutils.h index e462040c8..2cea6e6e8 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -269,7 +269,7 @@ extern int streq_paths(const char *a, const char *b); /* * Match string beginning. */ -static inline const char *startswith(const char *s, const char *prefix) +static inline const char *ul_startswith(const char *s, const char *prefix) { size_t sz = prefix ? strlen(prefix) : 0; @@ -295,7 +295,7 @@ static inline const char *startswith_no_case(const char *s, const char *prefix) */ static inline const char *startswithpath(const char *s, const char *prefix) { - const char *p = startswith(s, prefix); + const char *p = ul_startswith(s, prefix); if (p && (*p == '/' || *p == '\0')) return p; @@ -306,7 +306,7 @@ static inline const char *startswithpath(const char *s, const char *prefix) /* * Match string ending. */ -static inline const char *endswith(const char *s, const char *postfix) +static inline const char *ul_endswith(const char *s, const char *postfix) { size_t sl = s ? strlen(s) : 0; size_t pl = postfix ? strlen(postfix) : 0; diff --git a/lib/canonicalize.c b/lib/canonicalize.c index 0992a4747..6e70afe18 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -96,7 +96,7 @@ char *absolute_path(const char *path) return NULL; /* simple clean up */ - if (startswith(path, "./")) + if (ul_startswith(path, "./")) path += 2; else if (strcmp(path, ".") == 0) path = NULL; diff --git a/lib/loopdev.c b/lib/loopdev.c index 65efeb9ea..8c5beaeb7 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -695,7 +695,7 @@ int is_loopdev(const char *device) cn = canonicalize_path(name); if (cn) p = stripoff_last_component(cn); - rc = p && startswith(p, "loop"); + rc = p && ul_startswith(p, "loop"); free(cn); } diff --git a/lib/sysfs.c b/lib/sysfs.c index 951de6332..c888617b7 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -978,7 +978,7 @@ dev_t __sysfs_devname_to_devno(const char *prefix, const char *name, const char /* * Read from /sys/block///dev */ - if (!dev && parent && startswith(name, parent)) { + if (!dev && parent && ul_startswith(name, parent)) { len = snprintf(buf, sizeof(buf), "%s" _PATH_SYS_BLOCK "/%s/%s/dev", prefix, _parent, _name); diff --git a/lib/timeutils.c b/lib/timeutils.c index 2688f60d7..48e91d8de 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -118,7 +118,7 @@ static int parse_sec(const char *t, usec_t *usec) e += strspn(e, WHITESPACE); for (i = 0; i < ARRAY_SIZE(table); i++) - if (startswith(e, table[i].suffix)) { + if (ul_startswith(e, table[i].suffix)) { usec_t k = (usec_t) z * table[i].usec; for (; n > 0; n--) @@ -278,7 +278,7 @@ static int parse_timestamp_reference(time_t x, const char *t, usec_t *usec) goto finish; return -EINVAL; - } else if (endswith(t, " ago")) { + } else if (ul_endswith(t, " ago")) { char *z; z = strndup(t, strlen(t) - 4); diff --git a/libfdisk/src/utils.c b/libfdisk/src/utils.c index a7c8bfa2e..ef2663737 100644 --- a/libfdisk/src/utils.c +++ b/libfdisk/src/utils.c @@ -143,7 +143,7 @@ char *fdisk_partname(const char *dev, size_t partno) /* devfs kludge - note: fdisk partition names are not supposed to equal kernel names, so there is no reason to do this */ - if (endswith(dev, "disc")) { + if (ul_endswith(dev, "disc")) { w -= 4; p = "part"; } diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c index 24369857d..9a1832550 100644 --- a/libmount/src/context_mount.c +++ b/libmount/src/context_mount.c @@ -447,7 +447,7 @@ static int exec_helper(struct libmnt_context *cxt) } if (type && strchr(type, '.') - && !endswith(cxt->helper, type)) { + && !ul_endswith(cxt->helper, type)) { args[i++] = "-t"; /* 10 */ args[i++] = type; /* 11 */ } @@ -1457,7 +1457,7 @@ static void join_err_mesgs(struct libmnt_context *cxt, char *buf, size_t bufsz) if (!bufsz) break; - if (!startswith(*s, "e ")) + if (!ul_startswith(*s, "e ")) continue; if (n) { len = xstrncpy(buf, "; ", bufsz); diff --git a/libmount/src/context_umount.c b/libmount/src/context_umount.c index 64a1d884b..27319f77c 100644 --- a/libmount/src/context_umount.c +++ b/libmount/src/context_umount.c @@ -725,7 +725,7 @@ static int exec_helper(struct libmnt_context *cxt) args[i++] = "-r"; /* 7 */ if (type && strchr(type, '.') - && !endswith(cxt->helper, type)) { + && !ul_endswith(cxt->helper, type)) { args[i++] = "-t"; /* 8 */ args[i++] = type; /* 9 */ } diff --git a/libmount/src/hook_idmap.c b/libmount/src/hook_idmap.c index e8c4cab5a..5095de1f6 100644 --- a/libmount/src/hook_idmap.c +++ b/libmount/src/hook_idmap.c @@ -434,15 +434,15 @@ static int hook_prepare_options( idmap_type_t map_type; uint32_t nsid = UINT_MAX, hostid = UINT_MAX, range = UINT_MAX; - if (startswith(tok, "b:")) { + if (ul_startswith(tok, "b:")) { /* b:id-mount:id-host:id-range */ map_type = ID_TYPE_UIDGID; tok += 2; - } else if (startswith(tok, "g:")) { + } else if (ul_startswith(tok, "g:")) { /* g:id-mount:id-host:id-range */ map_type = ID_TYPE_GID; tok += 2; - } else if (startswith(tok, "u:")) { + } else if (ul_startswith(tok, "u:")) { /* u:id-mount:id-host:id-range */ map_type = ID_TYPE_UID; tok += 2; diff --git a/libmount/src/optmap.c b/libmount/src/optmap.c index d7569a0f0..4ea455d94 100644 --- a/libmount/src/optmap.c +++ b/libmount/src/optmap.c @@ -250,7 +250,7 @@ const struct libmnt_optmap *mnt_optmap_get_entry( for (ent = map; ent && ent->name; ent++) { if (ent->mask & MNT_PREFIX) { - if (startswith(name, ent->name)) { + if (ul_startswith(name, ent->name)) { if (mapent) *mapent = ent; return map; diff --git a/libmount/src/optstr.c b/libmount/src/optstr.c index 30cd1af61..ac20d8755 100644 --- a/libmount/src/optstr.c +++ b/libmount/src/optstr.c @@ -914,7 +914,7 @@ int mnt_match_options(const char *optstr, const char *pattern) if (*name == '+') name++, namesz--; - else if ((no = (startswith(name, "no") != NULL))) { + else if ((no = (ul_startswith(name, "no") != NULL))) { name += 2, namesz -= 2; if (!*name || *name == ',') { match = 0; diff --git a/libmount/src/tab.c b/libmount/src/tab.c index a5070f824..c8433674f 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -1818,7 +1818,7 @@ struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb, DBG(FS, ul_debugobj(fs, "source root: %s, source FS root: %s", root, src_root)); - if (src_root && root && !startswith(root, src_root)) { + if (src_root && root && !ul_startswith(root, src_root)) { if (strcmp(root, "/") == 0) { free(root); root = strdup(src_root); @@ -1948,7 +1948,7 @@ int __mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_f int flags = 0; if (!mnt_fs_get_srcpath(fs) || - !startswith(mnt_fs_get_srcpath(fs), "/dev/loop")) + !ul_startswith(mnt_fs_get_srcpath(fs), "/dev/loop")) continue; /* does not look like loopdev */ if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0) { diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index e5db3e385..3dd853880 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -395,7 +395,7 @@ static int mnt_parse_swaps_line(struct libmnt_fs *fs, const char *s) /* (1) source */ p = unmangle(s, &s); if (p) { - char *x = (char *) endswith(p, PATH_DELETED_SUFFIX); + char *x = (char *) ul_endswith(p, PATH_DELETED_SUFFIX); if (x && *x) *x = '\0'; } diff --git a/libmount/src/tab_update.c b/libmount/src/tab_update.c index a256edf85..3cb5b30f9 100644 --- a/libmount/src/tab_update.c +++ b/libmount/src/tab_update.c @@ -763,7 +763,7 @@ static int update_modify_target(struct libmnt_update *upd) char *p; const char *e; - e = startswith(mnt_fs_get_target(fs), upd_source); + e = ul_startswith(mnt_fs_get_target(fs), upd_source); if (!e || (*e && *e != '/')) continue; if (*e == '/') diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 8c3151b73..46050ff51 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -1393,7 +1393,7 @@ static int test_startswith(struct libmnt_test *ts __attribute__((unused)), char *optstr = argv[1]; char *pattern = argv[2]; - printf("%s\n", startswith(optstr, pattern) ? "YES" : "NOT"); + printf("%s\n", ul_startswith(optstr, pattern) ? "YES" : "NOT"); return 0; } @@ -1406,7 +1406,7 @@ static int test_endswith(struct libmnt_test *ts __attribute__((unused)), char *optstr = argv[1]; char *pattern = argv[2]; - printf("%s\n", endswith(optstr, pattern) ? "YES" : "NOT"); + printf("%s\n", ul_endswith(optstr, pattern) ? "YES" : "NOT"); return 0; } diff --git a/misc-utils/lsblk-properties.c b/misc-utils/lsblk-properties.c index 8c88025e2..1f80f3e18 100644 --- a/misc-utils/lsblk-properties.c +++ b/misc-utils/lsblk-properties.c @@ -157,7 +157,7 @@ static struct lsblk_devprop *get_properties_by_udev(struct lsblk_device *ld) const char *name = udev_list_entry_get_name(le); size_t sz; - if (!name || !startswith(name, LSBLK_UDEV_BYID_PREFIX)) + if (!name || !ul_startswith(name, LSBLK_UDEV_BYID_PREFIX)) continue; name += LSBLK_UDEV_BYID_PREFIXSZ; if (!*name) diff --git a/misc-utils/lsclocks.c b/misc-utils/lsclocks.c index 24761bbd3..435894182 100644 --- a/misc-utils/lsclocks.c +++ b/misc-utils/lsclocks.c @@ -258,7 +258,7 @@ static int64_t get_namespace_offset(const char *name) line = strtok_r(tokstr, "\n", &saveptr); if (!line) continue; - line = (char *) startswith(line, name); + line = (char *) ul_startswith(line, name); if (!line || line[0] != ' ') continue; diff --git a/pam_lastlog2/src/pam_lastlog2.c b/pam_lastlog2/src/pam_lastlog2.c index c3ca989c1..0e3b89719 100644 --- a/pam_lastlog2/src/pam_lastlog2.c +++ b/pam_lastlog2/src/pam_lastlog2.c @@ -98,9 +98,9 @@ _pam_parse_args (pam_handle_t *pamh, ctrl |= LASTLOG2_DEBUG; else if (strcmp (*argv, "silent") == 0) ctrl |= LASTLOG2_QUIET; - else if ((str = startswith (*argv, "database=")) != NULL) + else if ((str = ul_startswith (*argv, "database=")) != NULL) lastlog2_path = str; - else if ((str = startswith (*argv, "silent_if=")) != NULL) { + else if ((str = ul_startswith (*argv, "silent_if=")) != NULL) { const void *void_str = NULL; const char *service; if ((pam_get_item (pamh, PAM_SERVICE, &void_str) != PAM_SUCCESS) || @@ -143,7 +143,7 @@ write_login_data (pam_handle_t *pamh, int ctrl, const char *user) tty = void_str; /* strip leading "/dev/" from tty. */ - const char *str = startswith(tty, "/dev/"); + const char *str = ul_startswith(tty, "/dev/"); if (str != NULL) tty = str; diff --git a/sys-utils/lscpu-cputype.c b/sys-utils/lscpu-cputype.c index 50eec6a37..d741ce600 100644 --- a/sys-utils/lscpu-cputype.c +++ b/sys-utils/lscpu-cputype.c @@ -966,7 +966,7 @@ int lscpu_read_vulnerabilities(struct lscpu_cxt *cxt) /* Description */ vu->text = str; - p = (char *) startswith(vu->text, "Mitigation"); + p = (char *) ul_startswith(vu->text, "Mitigation"); if (p) { *p = ';'; strrem(vu->text, ':'); diff --git a/sys-utils/mount.c b/sys-utils/mount.c index 875d249b4..b4b8fb63a 100644 --- a/sys-utils/mount.c +++ b/sys-utils/mount.c @@ -393,17 +393,17 @@ static size_t libmount_mesgs(struct libmnt_context *cxt, char type) UL_STRV_FOREACH(s, mesgs) { switch (type) { case 'e': - if (!startswith(*s, "e ")) + if (!ul_startswith(*s, "e ")) break; fprintf(stderr, " * %s\n", (*s) + 2); break; case 'w': - if (!startswith(*s, "w ")) + if (!ul_startswith(*s, "w ")) break; fprintf(stdout, " * %s\n", (*s) + 2); break; case 'i': - if (!startswith(*s, "i ")) + if (!ul_startswith(*s, "i ")) break; fprintf(stdout, " * %s\n", (*s) + 2); break; diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c index d01f5ed34..45cbe9bc6 100644 --- a/sys-utils/rtcwake.c +++ b/sys-utils/rtcwake.c @@ -138,7 +138,7 @@ static int is_wakeup_enabled(const char *devname) FILE *f; size_t skip = 0; - if (startswith(devname, "/dev/")) + if (ul_startswith(devname, "/dev/")) skip = 5; snprintf(buf, sizeof buf, SYS_WAKEUP_PATH_TEMPLATE, devname + skip); f = fopen(buf, "r"); @@ -402,7 +402,7 @@ static int open_dev_rtc(const char *devname) int fd; char *devpath = NULL; - if (startswith(devname, "/dev")) + if (ul_startswith(devname, "/dev")) devpath = xstrdup(devname); else xasprintf(&devpath, "/dev/%s", devname); diff --git a/sys-utils/setpriv-landlock.c b/sys-utils/setpriv-landlock.c index efbacafd0..1adea773f 100644 --- a/sys-utils/setpriv-landlock.c +++ b/sys-utils/setpriv-landlock.c @@ -120,7 +120,7 @@ void parse_landlock_access(struct setpriv_landlock_opts *opts, const char *str) return; } - type = startswith(str, "fs:"); + type = ul_startswith(str, "fs:"); if (type) opts->access_fs |= parse_landlock_fs_access(type); } @@ -132,7 +132,7 @@ void parse_landlock_rule(struct setpriv_landlock_opts *opts, const char *str) char *accesses_part; int parent_fd; - accesses = startswith(str, "path-beneath:"); + accesses = ul_startswith(str, "path-beneath:"); if (!accesses) errx(EXIT_FAILURE, _("invalid landlock rule: %s"), str); path = strchr(accesses, ':'); diff --git a/text-utils/bits.c b/text-utils/bits.c index aca4306ee..97a77a82f 100644 --- a/text-utils/bits.c +++ b/text-utils/bits.c @@ -39,16 +39,16 @@ static void parse_mask_or_list(const char *cmdline_arg, arg = cmdline_arg; /* strip optional operator first */ - if (startswith(arg, "&")) { + if (ul_startswith(arg, "&")) { bitwise_op = '&'; arg++; - } else if (startswith(arg, "^")) { + } else if (ul_startswith(arg, "^")) { bitwise_op = '^'; arg++; - } else if (startswith(arg, "~")) { + } else if (ul_startswith(arg, "~")) { bitwise_op = '~'; arg++; - } else if (startswith(arg, "|")) { + } else if (ul_startswith(arg, "|")) { arg++; } @@ -56,8 +56,8 @@ static void parse_mask_or_list(const char *cmdline_arg, if (bits == NULL) errx(EXIT_FAILURE, _("error: cannot allocate bit mask")); - if (startswith(arg, ",") || startswith(arg, "0x")) { - if (startswith(arg, ",")) + if (ul_startswith(arg, ",") || ul_startswith(arg, "0x")) { + if (ul_startswith(arg, ",")) arg++; if (cpumask_parse(arg, bits, size) < 0) errx(EXIT_FAILURE, _("error: invalid bit mask: %s"), cmdline_arg); -- 2.47.2