aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Denys Vlasenko <vda.linux@googlemail.com>2015-03-12 17:48:34 +0100
committerGravatar Denys Vlasenko <vda.linux@googlemail.com>2015-03-12 17:48:34 +0100
commit8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb (patch)
tree41c9dfbd45676c9e8737a7d8aa4361bbdfb40868
parent936c8809caea5705e26e5d7e06ea3895c28fffd8 (diff)
downloadbusybox-8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb.tar.gz
busybox-8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb.tar.bz2
libbb: introduce and use is_prefixed_with()
function old new delta is_prefixed_with - 18 +18 complete_username 78 77 -1 man_main 737 735 -2 fsck_device 429 427 -2 unpack_ar_archive 80 76 -4 strip_unsafe_prefix 105 101 -4 singlemount 1054 1050 -4 rtc_adjtime_is_utc 90 86 -4 resolve_mount_spec 88 84 -4 parse_one_line 1029 1025 -4 parse_conf 1460 1456 -4 may_wakeup 83 79 -4 loadkmap_main 219 215 -4 get_irqs_from_stat 103 99 -4 get_header_cpio 913 909 -4 findfs_main 79 75 -4 fbsplash_main 1230 1226 -4 load_crontab 776 771 -5 expand_vars_to_list 1151 1146 -5 date_main 881 876 -5 skip_dev_pfx 30 24 -6 make_device 2199 2193 -6 complete_cmd_dir_file 773 767 -6 run_applet_and_exit 715 708 -7 uudecode_main 321 313 -8 pwdx_main 197 189 -8 execute 568 560 -8 i2cdetect_main 1186 1176 -10 procps_scan 1242 1230 -12 procps_read_smaps 1017 1005 -12 process_module 746 734 -12 patch_main 1903 1891 -12 nfsmount 3572 3560 -12 stack_machine 126 112 -14 process_timer_stats 449 435 -14 match_fstype 111 97 -14 do_ipaddr 1344 1330 -14 open_list_and_close 359 343 -16 get_header_tar 1795 1779 -16 prepend_new_eth_table 340 323 -17 fsck_main 1811 1794 -17 find_iface_state 56 38 -18 dnsd_main 1321 1303 -18 base_device 179 158 -21 find_keyword 104 82 -22 handle_incoming_and_exit 2785 2762 -23 parse_and_put_prompt 774 746 -28 modinfo 347 317 -30 find_action 204 171 -33 update_passwd 1470 1436 -34 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/49 up/down: 18/-540) Total: -522 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/libarchive/get_header_cpio.c2
-rw-r--r--archival/libarchive/get_header_tar.c6
-rw-r--r--archival/libarchive/unpack_ar_archive.c2
-rw-r--r--archival/libarchive/unsafe_prefix.c2
-rw-r--r--console-tools/loadkmap.c2
-rw-r--r--coreutils/date.c2
-rw-r--r--coreutils/uudecode.c4
-rw-r--r--e2fsprogs/fsck.c40
-rw-r--r--editors/patch.c4
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/appletlib.c8
-rw-r--r--libbb/compare_string_array.c23
-rw-r--r--libbb/lineedit.c17
-rw-r--r--libbb/match_fstype.c7
-rw-r--r--libbb/procps.c12
-rw-r--r--libbb/rtc.c2
-rw-r--r--libbb/skip_whitespace.c2
-rw-r--r--libbb/update_passwd.c9
-rw-r--r--libbb/xconnect.c2
-rw-r--r--miscutils/crond.c4
-rw-r--r--miscutils/dc.c6
-rw-r--r--miscutils/devfsd.c10
-rw-r--r--miscutils/fbsplash.c2
-rw-r--r--miscutils/i2c_tools.c4
-rw-r--r--miscutils/last.c6
-rw-r--r--miscutils/man.c4
-rw-r--r--modutils/depmod.c8
-rw-r--r--modutils/modinfo.c12
-rw-r--r--modutils/modprobe-small.c21
-rw-r--r--modutils/modprobe.c12
-rw-r--r--modutils/modutils-24.c2
-rw-r--r--networking/dnsd.c2
-rw-r--r--networking/httpd.c8
-rw-r--r--networking/ifupdown.c10
-rw-r--r--networking/inetd.c2
-rw-r--r--networking/libiproute/ipaddress.c2
-rw-r--r--networking/nameif.c8
-rw-r--r--networking/netstat.c4
-rw-r--r--procps/mpstat.c2
-rw-r--r--procps/powertop.c4
-rw-r--r--procps/pwdx.c2
-rw-r--r--shell/hush.c2
-rw-r--r--util-linux/acpid.c2
-rw-r--r--util-linux/fdisk.c6
-rw-r--r--util-linux/fdisk_osf.c2
-rw-r--r--util-linux/fdisk_sgi.c2
-rw-r--r--util-linux/findfs.c2
-rw-r--r--util-linux/mdev.c2
-rw-r--r--util-linux/mount.c10
-rw-r--r--util-linux/rtcwake.c2
-rw-r--r--util-linux/volume_id/get_devname.c4
51 files changed, 165 insertions, 151 deletions
diff --git a/archival/libarchive/get_header_cpio.c b/archival/libarchive/get_header_cpio.c
index 1a0058b63..7861d1f6f 100644
--- a/archival/libarchive/get_header_cpio.c
+++ b/archival/libarchive/get_header_cpio.c
@@ -37,7 +37,7 @@ char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
}
archive_handle->offset += 110;
- if (strncmp(&cpio_header[0], "07070", 5) != 0
+ if (!is_prefixed_with(&cpio_header[0], "07070")
|| (cpio_header[5] != '1' && cpio_header[5] != '2')
) {
bb_error_msg_and_die("unsupported cpio format, use newc or crc");
diff --git a/archival/libarchive/get_header_tar.c b/archival/libarchive/get_header_tar.c
index 0c663fbd7..2dbcdb50c 100644
--- a/archival/libarchive/get_header_tar.c
+++ b/archival/libarchive/get_header_tar.c
@@ -105,7 +105,7 @@ static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int g
value = end + 1;
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
- if (!global && strncmp(value, "path=", sizeof("path=") - 1) == 0) {
+ if (!global && is_prefixed_with(value, "path=")) {
value += sizeof("path=") - 1;
free(archive_handle->tar__longname);
archive_handle->tar__longname = xstrdup(value);
@@ -118,7 +118,7 @@ static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int g
* This is what Red Hat's patched version of tar uses.
*/
# define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux"
- if (strncmp(value, SELINUX_CONTEXT_KEYWORD"=", sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1) == 0) {
+ if (is_prefixed_with(value, SELINUX_CONTEXT_KEYWORD"=")) {
value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
free(archive_handle->tar__sctx[global]);
archive_handle->tar__sctx[global] = xstrdup(value);
@@ -202,7 +202,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
/* Check header has valid magic, "ustar" is for the proper tar,
* five NULs are for the old tar format */
- if (strncmp(tar.magic, "ustar", 5) != 0
+ if (!is_prefixed_with(tar.magic, "ustar")
&& (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|| memcmp(tar.magic, "\0\0\0\0", 5) != 0)
) {
diff --git a/archival/libarchive/unpack_ar_archive.c b/archival/libarchive/unpack_ar_archive.c
index 214d17e23..0bc030349 100644
--- a/archival/libarchive/unpack_ar_archive.c
+++ b/archival/libarchive/unpack_ar_archive.c
@@ -12,7 +12,7 @@ void FAST_FUNC unpack_ar_archive(archive_handle_t *ar_archive)
char magic[7];
xread(ar_archive->src_fd, magic, AR_MAGIC_LEN);
- if (strncmp(magic, AR_MAGIC, AR_MAGIC_LEN) != 0) {
+ if (!is_prefixed_with(magic, AR_MAGIC)) {
bb_error_msg_and_die("invalid ar magic");
}
ar_archive->offset += AR_MAGIC_LEN;
diff --git a/archival/libarchive/unsafe_prefix.c b/archival/libarchive/unsafe_prefix.c
index 826c673bf..9994f4d94 100644
--- a/archival/libarchive/unsafe_prefix.c
+++ b/archival/libarchive/unsafe_prefix.c
@@ -15,7 +15,7 @@ const char* FAST_FUNC strip_unsafe_prefix(const char *str)
cp++;
continue;
}
- if (strncmp(cp, "/../"+1, 3) == 0) {
+ if (is_prefixed_with(cp, "/../"+1)) {
cp += 3;
continue;
}
diff --git a/console-tools/loadkmap.c b/console-tools/loadkmap.c
index 6dcf8133f..f525ee5d1 100644
--- a/console-tools/loadkmap.c
+++ b/console-tools/loadkmap.c
@@ -57,7 +57,7 @@ int loadkmap_main(int argc UNUSED_PARAM, char **argv)
*/
xread(STDIN_FILENO, flags, 7);
- if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7))
+ if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC))
bb_error_msg_and_die("not a valid binary keymap");
xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
diff --git a/coreutils/date.c b/coreutils/date.c
index 767e0d4a2..7965775fe 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -373,7 +373,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
date_buf[0] = '\0';
} else {
/* Handle special conversions */
- if (strncmp(fmt_dt2str, "%f", 2) == 0) {
+ if (is_prefixed_with(fmt_dt2str, "%f")) {
fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
}
/* Generate output string */
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c
index b298fcb95..3f1227306 100644
--- a/coreutils/uudecode.c
+++ b/coreutils/uudecode.c
@@ -110,10 +110,10 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
FILE *dst_stream;
int mode;
- if (strncmp(line, "begin-base64 ", 13) == 0) {
+ if (is_prefixed_with(line, "begin-base64 ")) {
line_ptr = line + 13;
decode_fn_ptr = read_base64;
- } else if (strncmp(line, "begin ", 6) == 0) {
+ } else if (is_prefixed_with(line, "begin ")) {
line_ptr = line + 6;
decode_fn_ptr = read_stduu;
} else {
diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c
index d32f396e9..d2d312e5c 100644
--- a/e2fsprogs/fsck.c
+++ b/e2fsprogs/fsck.c
@@ -199,7 +199,7 @@ static char *base_device(const char *device)
}
/* Handle DAC 960 devices */
- if (strncmp(cp, "rd/", 3) == 0) {
+ if (is_prefixed_with(cp, "rd/")) {
cp += 3;
if (cp[0] != 'c' || !isdigit(cp[1])
|| cp[2] != 'd' || !isdigit(cp[3]))
@@ -224,9 +224,9 @@ static char *base_device(const char *device)
#if ENABLE_FEATURE_DEVFS
/* Now let's handle devfs (ugh) names */
len = 0;
- if (strncmp(cp, "ide/", 4) == 0)
+ if (is_prefixed_with(cp, "ide/"))
len = 4;
- if (strncmp(cp, "scsi/", 5) == 0)
+ if (is_prefixed_with(cp, "scsi/"))
len = 5;
if (len) {
cp += len;
@@ -237,38 +237,38 @@ static char *base_device(const char *device)
* some number of digits at each level, abort.
*/
for (hier = devfs_hier; *hier; hier++) {
- len = strlen(*hier);
- if (strncmp(cp, *hier, len) != 0)
+ cp = is_prefixed_with(cp, *hier);
+ if (!cp)
goto errout;
- cp += len;
- while (*cp != '/' && *cp != 0) {
+ while (*cp != '/' && *cp != '\0') {
if (!isdigit(*cp))
goto errout;
cp++;
}
+//FIXME: what if *cp = '\0' now? cp++ moves past it!!!
cp++;
}
- cp[-1] = 0;
+ cp[-1] = '\0';
return str;
}
/* Now handle devfs /dev/disc or /dev/disk names */
- disk = 0;
- if (strncmp(cp, "discs/", 6) == 0)
+ disk = NULL;
+ if (is_prefixed_with(cp, "discs/"))
disk = "disc";
- else if (strncmp(cp, "disks/", 6) == 0)
+ else if (is_prefixed_with(cp, "disks/"))
disk = "disk";
if (disk) {
cp += 6;
- if (strncmp(cp, disk, 4) != 0)
+ cp = is_prefixed_with(cp, disk);
+ if (!cp)
goto errout;
- cp += 4;
- while (*cp != '/' && *cp != 0) {
+ while (*cp != '/' && *cp != '\0') {
if (!isdigit(*cp))
goto errout;
cp++;
}
- *cp = 0;
+ *cp = '\0';
return str;
}
#endif
@@ -593,8 +593,8 @@ static void fsck_device(struct fs_info *fs /*, int interactive */)
type, "from fstab");
} else if (fstype
&& (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
- && strncmp(fstype, "opts=", 5) != 0
- && strncmp(fstype, "loop", 4) != 0
+ && !is_prefixed_with(fstype, "opts=")
+ && !is_prefixed_with(fstype, "loop")
&& !strchr(fstype, ',')
) {
type = fstype;
@@ -627,8 +627,8 @@ static int device_already_active(char *device)
#ifdef BASE_MD
/* Don't check a soft raid disk with any other disk */
if (instance_list
- && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
- || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
+ && (is_prefixed_with(instance_list->device, BASE_MD)
+ || is_prefixed_with(device, BASE_MD))
) {
return 1;
}
@@ -895,7 +895,7 @@ static void compile_fs_type(char *fs_type)
if (strcmp(s, "loop") == 0)
/* loop is really short-hand for opts=loop */
goto loop_special_case;
- if (strncmp(s, "opts=", 5) == 0) {
+ if (is_prefixed_with(s, "opts=")) {
s += 5;
loop_special_case:
fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
diff --git a/editors/patch.c b/editors/patch.c
index f86067544..cb25e4140 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -414,7 +414,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
}
// Open a new file?
- if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) {
+ if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) {
char *s, **name = reverse ? &newname : &oldname;
int i;
@@ -446,7 +446,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
// Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
// but a missing ,value means the value is 1.
- } else if (state == 1 && !strncmp("@@ -", patchline, 4)) {
+ } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) {
int i;
char *s = patchline+4;
diff --git a/include/libbb.h b/include/libbb.h
index be792d6b2..c97df6047 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -389,6 +389,7 @@ const char *bb_basename(const char *name) FAST_FUNC;
/* NB: can violate const-ness (similarly to strchr) */
char *last_char_is(const char *s, int c) FAST_FUNC;
const char* endofname(const char *name) FAST_FUNC;
+char *is_prefixed_with(const char *string, const char *key) FAST_FUNC;
int ndelay_on(int fd) FAST_FUNC;
int ndelay_off(int fd) FAST_FUNC;
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 54300bd87..8fd8fd525 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -193,7 +193,7 @@ void lbb_prepare(const char *applet
if (argv[1]
&& !argv[2]
&& strcmp(argv[1], "--help") == 0
- && strncmp(applet, "busybox", 7) != 0
+ && !is_prefixed_with(applet, "busybox")
) {
/* Special case. POSIX says "test --help"
* should be no different from e.g. "test --foo". */
@@ -673,7 +673,7 @@ static int busybox_main(char **argv)
return 0;
}
- if (strncmp(argv[1], "--list", 6) == 0) {
+ if (is_prefixed_with(argv[1], "--list")) {
unsigned i = 0;
const char *a = applet_names;
dup2(1, 2);
@@ -778,7 +778,7 @@ void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
int applet = find_applet_by_name(name);
if (applet >= 0)
run_applet_no_and_exit(applet, argv);
- if (strncmp(name, "busybox", 7) == 0)
+ if (is_prefixed_with(name, "busybox"))
exit(busybox_main(argv));
}
@@ -817,7 +817,7 @@ int main(int argc UNUSED_PARAM, char **argv)
#if defined(SINGLE_APPLET_MAIN)
/* Only one applet is selected in .config */
- if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
+ if (argv[1] && is_prefixed_with(argv[0], "busybox")) {
/* "busybox <applet> <params>" should still work as expected */
argv++;
}
diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
index 4b10cc138..e24815a03 100644
--- a/libbb/compare_string_array.c
+++ b/libbb/compare_string_array.c
@@ -5,6 +5,24 @@
#include "libbb.h"
+char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
+{
+#if 0 /* Two passes over key - probably slower */
+ int len = strlen(key);
+ if (strncmp(string, key, len) == 0)
+ return string + len;
+ return NULL;
+#else /* Open-coded */
+ while (*key != '\0') {
+ if (*key != *string)
+ return NULL;
+ key++;
+ string++;
+ }
+ return (char*)string;
+#endif
+}
+
/* returns the array index of the string */
/* (index of first match is returned, or -1) */
int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
@@ -39,10 +57,9 @@ int FAST_FUNC index_in_strings(const char *strings, const char *key)
int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
{
int i;
- int len = strlen(key);
- if (len) {
+ if (key[0]) {
for (i = 0; string_array[i] != 0; i++) {
- if (strncmp(string_array[i], key, len) == 0) {
+ if (is_prefixed_with(string_array[i], key)) {
return i;
}
}
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 249b401b4..a83e07c0c 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -681,7 +681,7 @@ static NOINLINE unsigned complete_username(const char *ud)
setpwent();
while ((pw = getpwent()) != NULL) {
/* Null usernames should result in all users as possible completions. */
- if (/*!userlen || */ strncmp(ud, pw->pw_name, userlen) == 0) {
+ if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
add_match(xasprintf("~%s/", pw->pw_name));
}
}
@@ -792,7 +792,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
if (!pfind[0] && DOT_OR_DOTDOT(name_found))
continue;
/* match? */
- if (strncmp(name_found, pfind, pf_len) != 0)
+ if (!is_prefixed_with(name_found, pfind))
continue; /* no */
found = concat_path_file(paths[i], name_found);
@@ -1879,15 +1879,16 @@ static void parse_and_put_prompt(const char *prmt_ptr)
cwd_buf = xrealloc_getcwd_or_warn(NULL);
if (!cwd_buf)
cwd_buf = (char *)bb_msg_unknown;
- else {
+ else if (home_pwd_buf[0]) {
+ char *after_home_user;
+
/* /home/user[/something] -> ~[/something] */
- l = strlen(home_pwd_buf);
- if (l != 0
- && strncmp(home_pwd_buf, cwd_buf, l) == 0
- && (cwd_buf[l] == '/' || cwd_buf[l] == '\0')
+ after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
+ if (after_home_user
+ && (*after_home_user == '/' || *after_home_user == '\0')
) {
cwd_buf[0] = '~';
- overlapping_strcpy(cwd_buf + 1, cwd_buf + l);
+ overlapping_strcpy(cwd_buf + 1, after_home_user);
}
}
}
diff --git a/libbb/match_fstype.c b/libbb/match_fstype.c
index 32c3d7f18..b066b4211 100644
--- a/libbb/match_fstype.c
+++ b/libbb/match_fstype.c
@@ -17,7 +17,6 @@
int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
{
int match = 1;
- int len;
if (!t_fstype)
return match;
@@ -27,10 +26,10 @@ int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
t_fstype += 2;
}
- len = strlen(mt->mnt_type);
while (1) {
- if (strncmp(mt->mnt_type, t_fstype, len) == 0
- && (t_fstype[len] == '\0' || t_fstype[len] == ',')
+ char *after_mnt_type = is_prefixed_with(t_fstype, mt->mnt_type);
+ if (after_mnt_type
+ && (*after_mnt_type == '\0' || *after_mnt_type == ',')
) {
return match;
}
diff --git a/libbb/procps.c b/libbb/procps.c
index 5b68d3431..948b91ee6 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -205,11 +205,11 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
// Rss: nnn kB
// .....
- char *tp = buf, *p;
+ char *tp, *p;
#define SCAN(S, X) \
- if (strncmp(tp, S, sizeof(S)-1) == 0) { \
- tp = skip_whitespace(tp + sizeof(S)-1); \
+ if ((tp = is_prefixed_with(buf, S)) != NULL) { \
+ tp = skip_whitespace(tp); \
total->X += currec.X = fast_strtoul_10(&tp); \
continue; \
}
@@ -247,7 +247,7 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
// skipping "rw-s FILEOFS M:m INODE "
tp = skip_whitespace(skip_fields(tp, 4));
// filter out /dev/something (something != zero)
- if (strncmp(tp, "/dev/", 5) != 0 || strcmp(tp, "/dev/zero\n") == 0) {
+ if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
if (currec.smap_mode[1] == 'w') {
currec.mapped_rw = currec.smap_size;
total->mapped_rw += currec.smap_size;
@@ -497,8 +497,8 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
while (fgets(buf, sizeof(buf), file)) {
char *tp;
#define SCAN_TWO(str, name, statement) \
- if (strncmp(buf, str, sizeof(str)-1) == 0) { \
- tp = skip_whitespace(buf + sizeof(str)-1); \
+ if ((tp = is_prefixed_with(buf, str)) != NULL) { \
+ tp = skip_whitespace(tp); \
sscanf(tp, "%u", &sp->name); \
statement; \
}
diff --git a/libbb/rtc.c b/libbb/rtc.c
index 6d06d57f9..c4117ba34 100644
--- a/libbb/rtc.c
+++ b/libbb/rtc.c
@@ -22,7 +22,7 @@ int FAST_FUNC rtc_adjtime_is_utc(void)
char buffer[128];
while (fgets(buffer, sizeof(buffer), f)) {
- if (strncmp(buffer, "UTC", 3) == 0) {
+ if (is_prefixed_with(buffer, "UTC")) {
utc = 1;
break;
}
diff --git a/libbb/skip_whitespace.c b/libbb/skip_whitespace.c
index 8c7b674c3..b6cfbba4d 100644
--- a/libbb/skip_whitespace.c
+++ b/libbb/skip_whitespace.c
@@ -33,7 +33,7 @@ char* FAST_FUNC skip_non_whitespace(const char *s)
char* FAST_FUNC skip_dev_pfx(const char *tty_name)
{
- if (strncmp(tty_name, "/dev/", 5) == 0)
+ if (is_prefixed_with(tty_name, "/dev/"))
tty_name += 5;
return (char*)tty_name;
}
diff --git a/libbb/update_passwd.c b/libbb/update_passwd.c
index dc26ebd1d..a2004f480 100644
--- a/libbb/update_passwd.c
+++ b/libbb/update_passwd.c
@@ -84,7 +84,6 @@ int FAST_FUNC update_passwd(const char *filename,
char *fnamesfx;
char *sfx_char;
char *name_colon;
- unsigned user_len;
int old_fd;
int new_fd;
int i;
@@ -108,7 +107,6 @@ int FAST_FUNC update_passwd(const char *filename,
fnamesfx = xasprintf("%s+", filename);
sfx_char = &fnamesfx[strlen(fnamesfx)-1];
name_colon = xasprintf("%s:", name ? name : "");
- user_len = strlen(name_colon);
if (shadow)
old_fp = fopen(filename, "r+");
@@ -179,7 +177,7 @@ int FAST_FUNC update_passwd(const char *filename,
while (list) {
list++;
next_list_element:
- if (strncmp(list, member, member_len) == 0) {
+ if (is_prefixed_with(list, member)) {
char c;
changed_lines++;
c = list[member_len];
@@ -201,13 +199,14 @@ int FAST_FUNC update_passwd(const char *filename,
goto next;
}
- if (strncmp(name_colon, line, user_len) != 0) {
+ cp = is_prefixed_with(line, name_colon);
+ if (!cp) {
fprintf(new_fp, "%s\n", line);
goto next;
}
/* We have a match with "name:"... */
- cp = line + user_len; /* move past name: */
+ /* cp points past "name:" */
#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
if (member) {
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 1c8bb2b73..2a96e03dc 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -171,7 +171,7 @@ IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
const char *cp;
struct addrinfo hint;
- if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
+ if (ENABLE_FEATURE_UNIX_LOCAL && is_prefixed_with(host, "local:")) {
struct sockaddr_un *sun;
r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
diff --git a/miscutils/crond.c b/miscutils/crond.c
index 3659b9a6f..eb327f855 100644
--- a/miscutils/crond.c
+++ b/miscutils/crond.c
@@ -438,14 +438,14 @@ static void load_crontab(const char *fileName)
log5("user:%s entry:%s", fileName, parser->data);
/* check if line is setting MAILTO= */
- if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
+ if (is_prefixed_with(tokens[0], "MAILTO=")) {
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
free(mailTo);
mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
#endif /* otherwise just ignore such lines */
continue;
}
- if (0 == strncmp(tokens[0], "SHELL=", 6)) {
+ if (is_prefixed_with(tokens[0], "SHELL=")) {
free(shell);
shell = xstrdup(&tokens[0][6]);
continue;
diff --git a/miscutils/dc.c b/miscutils/dc.c
index a7bd360d2..f94d6fa6b 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -244,9 +244,9 @@ static void stack_machine(const char *argument)
o = operators;
do {
- const size_t name_len = strlen(o->name);
- if (strncmp(o->name, argument, name_len) == 0) {
- argument += name_len;
+ char *after_name = is_prefixed_with(argument, o->name);
+ if (after_name) {
+ argument = after_name;
o->function();
goto next;
}
diff --git a/miscutils/devfsd.c b/miscutils/devfsd.c
index 96ffe0738..5a6aec6bd 100644
--- a/miscutils/devfsd.c
+++ b/miscutils/devfsd.c
@@ -1405,7 +1405,6 @@ const char *get_old_name(const char *devname, unsigned int namelen,
int indexx;
const char *pty1;
const char *pty2;
- size_t len;
/* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
static const char *const fmt[] = {
NULL ,
@@ -1425,12 +1424,11 @@ const char *get_old_name(const char *devname, unsigned int namelen,
};
for (trans = translate_table; trans->match != NULL; ++trans) {
- len = strlen(trans->match);
-
- if (strncmp(devname, trans->match, len) == 0) {
+ char *after_match = is_prefixed_with(devname, trans->match);
+ if (after_match) {
if (trans->format == NULL)
- return devname + len;
- sprintf(buffer, trans->format, devname + len);
+ return after_match;
+ sprintf(buffer, trans->format, after_match);
return buffer;
}
}
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 7b695b26f..77033c258 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -516,7 +516,7 @@ int fbsplash_main(int argc UNUSED_PARAM, char **argv)
// handle a case when we have many buffered lines
// already in the pipe
while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
- if (strncmp(num_buf, "exit", 4) == 0) {
+ if (is_prefixed_with(num_buf, "exit")) {
DEBUG_MESSAGE("exit");
break;
}
diff --git a/miscutils/i2c_tools.c b/miscutils/i2c_tools.c
index 90d1e1e14..03bb03974 100644
--- a/miscutils/i2c_tools.c
+++ b/miscutils/i2c_tools.c
@@ -1198,7 +1198,7 @@ static void NORETURN list_i2c_busses_and_exit(void)
if (subde->d_name[0] == '.')
continue;
- if (strncmp(subde->d_name, "i2c-", 4) == 0) {
+ if (is_prefixed_with(subde->d_name, "i2c-")) {
snprintf(path, NAME_MAX,
"%s/%s/device/%s/name",
i2cdev_path, de->d_name,
@@ -1229,7 +1229,7 @@ found:
if (rv != 1)
continue;
- if (strncmp(name, "ISA", 3) == 0)
+ if (is_prefixed_with(name, "ISA"))
adt = ADT_ISA;
else
adt = i2cdetect_get_funcs(bus);
diff --git a/miscutils/last.c b/miscutils/last.c
index 24f6e1c78..a144c7e47 100644
--- a/miscutils/last.c
+++ b/miscutils/last.c
@@ -87,11 +87,11 @@ int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
if (++n > 0)
ut.ut_type = n != 3 ? n : SHUTDOWN_TIME;
#else
- if (strncmp(ut.ut_user, "shutdown", 8) == 0)
+ if (is_prefixed_with(ut.ut_user, "shutdown"))
ut.ut_type = SHUTDOWN_TIME;
- else if (strncmp(ut.ut_user, "reboot", 6) == 0)
+ else if (is_prefixed_with(ut.ut_user, "reboot"))
ut.ut_type = BOOT_TIME;
- else if (strncmp(ut.ut_user, "runlevel", 8) == 0)
+ else if (is_prefixed_with(ut.ut_user, "runlevel"))
ut.ut_type = RUN_LVL;
#endif
} else {
diff --git a/miscutils/man.c b/miscutils/man.c
index c39870e67..58ed81955 100644
--- a/miscutils/man.c
+++ b/miscutils/man.c
@@ -66,7 +66,7 @@ static int run_pipe(const char *pager, char *man_filename, int man, int level)
goto ordinary_manpage;
line = xmalloc_open_zipped_read_close(man_filename, NULL);
- if (!line || strncmp(line, ".so ", 4) != 0) {
+ if (!line || !is_prefixed_with(line, ".so ")) {
free(line);
goto ordinary_manpage;
}
@@ -228,7 +228,7 @@ int man_main(int argc UNUSED_PARAM, char **argv)
if (!token[1])
continue;
if (strcmp("DEFINE", token[0]) == 0) {
- if (strncmp("pager", token[1], 5) == 0) {
+ if (is_prefixed_with("pager", token[1])) {
pager = xstrdup(skip_whitespace(token[1]) + 5);
}
} else
diff --git a/modutils/depmod.c b/modutils/depmod.c
index 37a8482d9..9713aef92 100644
--- a/modutils/depmod.c
+++ b/modutils/depmod.c
@@ -55,7 +55,7 @@ static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARA
NULL
);
for (ptr = image; ptr < image + len - 10; ptr++) {
- if (strncmp(ptr, "depends=", 8) == 0) {
+ if (is_prefixed_with(ptr, "depends=")) {
char *u;
ptr += 8;
@@ -64,15 +64,15 @@ static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARA
*u = '_';
ptr += string_to_llist(ptr, &info->dependencies, ",");
} else if (ENABLE_FEATURE_MODUTILS_ALIAS
- && strncmp(ptr, "alias=", 6) == 0
+ && is_prefixed_with(ptr, "alias=")
) {
llist_add_to(&info->aliases, xstrdup(ptr + 6));
ptr += strlen(ptr);
} else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
- && strncmp(ptr, "__ksymtab_", 10) == 0
+ && is_prefixed_with(ptr, "__ksymtab_")
) {
ptr += 10;
- if (strncmp(ptr, "gpl", 3) == 0
+ if (is_prefixed_with(ptr, "gpl")
|| strcmp(ptr, "strings") == 0
) {
continue;
diff --git a/modutils/modinfo.c b/modutils/modinfo.c
index ee379304c..8e74b6438 100644
--- a/modutils/modinfo.c
+++ b/modutils/modinfo.c
@@ -62,7 +62,7 @@ static void modinfo(const char *path, const char *version,
"firmware",
};
size_t len;
- int j, length;
+ int j;
char *ptr, *the_module;
const char *field = env->field;
int tags = env->tags;
@@ -94,16 +94,18 @@ static void modinfo(const char *path, const char *version,
pattern = field;
if ((1<<j) & OPT_TAGS)
pattern = shortcuts[j];
- length = strlen(pattern);
ptr = the_module;
while (1) {
+ char *after_pattern;
+
ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
if (ptr == NULL) /* no occurance left, done */
break;
- if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') {
+ after_pattern = is_prefixed_with(ptr, pattern);
+ if (after_pattern && *after_pattern == '=') {
/* field prefixes are 0x80 or 0x00 */
- if ((ptr[-1] & 0x7F) == '\0') {
- ptr += length + 1;
+ if ((ptr[-1] & 0x7F) == 0x00) {
+ ptr = after_pattern + 1;
display(ptr, pattern, (1<<j) != tags);
ptr += strlen(ptr);
}
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index ed177bb9b..9c941064b 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -116,21 +116,21 @@ static char* copy_stringbuf(void)
static char* find_keyword(char *ptr, size_t len, const char *word)
{
- int wlen;
-
if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
return NULL;
- wlen = strlen(word);
- len -= wlen - 1;
+ len -= strlen(word) - 1;
while ((ssize_t)len > 0) {
char *old = ptr;
+ char *after_word;
+
/* search for the first char in word */
- ptr = memchr(ptr, *word, len);
+ ptr = memchr(ptr, word[0], len);
if (ptr == NULL) /* no occurance left, done */
break;
- if (strncmp(ptr, word, wlen) == 0)
- return ptr + wlen; /* found, return ptr past it */
+ after_word = is_prefixed_with(ptr, word);
+ if (after_word)
+ return after_word; /* found, return ptr past it */
++ptr;
len -= (ptr - old);
}
@@ -536,7 +536,7 @@ static module_info** find_alias(const char *alias)
// TODO: open only once, invent config_rewind()
static int already_loaded(const char *name)
{
- int ret, namelen;
+ int ret;
char *line;
FILE *fp;
@@ -545,15 +545,16 @@ static int already_loaded(const char *name)
fp = fopen_for_read("/proc/modules");
if (!fp)
return 0;
- namelen = strlen(name);
while ((line = xmalloc_fgetline(fp)) != NULL) {
char *live;
+ char *after_name;
// Examples from kernel 3.14.6:
//pcspkr 12718 0 - Live 0xffffffffa017e000
//snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
//i915 801405 2 - Live 0xffffffffa0096000
- if (strncmp(line, name, namelen) != 0 || line[namelen] != ' ') {
+ after_name = is_prefixed_with(line, name);
+ if (!after_name || *after_name != ' ') {
free(line);
continue;
}
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index 0e8aa9e85..996de4074 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -260,7 +260,7 @@ static void add_probe(const char *name)
llist_add_to_end(&G.probes, m);
G.num_unresolved_deps++;
if (ENABLE_FEATURE_MODUTILS_SYMBOLS
- && strncmp(m->modname, "symbol:", 7) == 0
+ && is_prefixed_with(m->modname, "symbol:")
) {
G.need_symbols = 1;
}
@@ -353,22 +353,18 @@ static char *parse_and_add_kcmdline_module_options(char *options, const char *mo
char *kcmdline_buf;
char *kcmdline;
char *kptr;
- int len;
kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
if (!kcmdline_buf)
return options;
kcmdline = kcmdline_buf;
- len = strlen(modulename);
while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
- if (strncmp(modulename, kptr, len) != 0)
- continue;
- kptr += len;
- if (*kptr != '.')
+ char *after_modulename = is_prefixed_with(kptr, modulename);
+ if (!after_modulename || *after_modulename != '.')
continue;
/* It is "modulename.xxxx" */
- kptr++;
+ kptr = after_modulename + 1;
if (strchr(kptr, '=') != NULL) {
/* It is "modulename.opt=[val]" */
options = gather_options_str(options, kptr);
diff --git a/modutils/modutils-24.c b/modutils/modutils-24.c
index 12cb75c54..fe46fc3fd 100644
--- a/modutils/modutils-24.c
+++ b/modutils/modutils-24.c
@@ -2255,7 +2255,7 @@ static int add_symbols_from(struct obj_file *f,
* symbols so they cannot fudge it by adding the prefix on
* their references.
*/
- if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
+ if (is_prefixed_with((char *)s->name, "GPLONLY_")) {
#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
if (gpl)
s->name += 8;
diff --git a/networking/dnsd.c b/networking/dnsd.c
index fe98400f7..923ad6bc6 100644
--- a/networking/dnsd.c
+++ b/networking/dnsd.c
@@ -194,7 +194,7 @@ static char *table_lookup(struct dns_entry *d,
if ((len != 1 || d->name[1] != '*')
/* we assume (do not check) that query_string
* ends in ".in-addr.arpa" */
- && strncmp(d->rip, query_string, strlen(d->rip)) == 0
+ && is_prefixed_with(query_string, d->rip)
) {
#if DEBUG
fprintf(stderr, "Found name:%s\n", d->name);
diff --git a/networking/httpd.c b/networking/httpd.c
index 9cf080401..7a9065fcc 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -697,7 +697,7 @@ static void parse_conf(const char *path, int flag)
goto config_error;
}
*host_port++ = '\0';
- if (strncmp(host_port, "http://", 7) == 0)
+ if (is_prefixed_with(host_port, "http://"))
host_port += 7;
if (*host_port == '\0') {
goto config_error;
@@ -1894,7 +1894,7 @@ static Htaccess_Proxy *find_proxy_entry(const char *url)
{
Htaccess_Proxy *p;
for (p = proxy; p; p = p->next) {
- if (strncmp(url, p->url_from, strlen(p->url_from)) == 0)
+ if (is_prefixed_with(url, p->url_from))
return p;
}
return NULL;
@@ -2183,7 +2183,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
if (STRNCASECMP(iobuf, "Range:") == 0) {
/* We know only bytes=NNN-[MMM] */
char *s = skip_whitespace(iobuf + sizeof("Range:")-1);
- if (strncmp(s, "bytes=", 6) == 0) {
+ if (is_prefixed_with(s, "bytes=") == 0) {
s += sizeof("bytes=")-1;
range_start = BB_STRTOOFF(s, &s, 10);
if (s[0] != '-' || range_start < 0) {
@@ -2269,7 +2269,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
tptr = urlcopy + 1; /* skip first '/' */
#if ENABLE_FEATURE_HTTPD_CGI
- if (strncmp(tptr, "cgi-bin/", 8) == 0) {
+ if (is_prefixed_with(tptr, "cgi-bin/")) {
if (tptr[8] == '\0') {
/* protect listing "cgi-bin/" */
send_headers_and_exit(HTTP_FORBIDDEN);
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index c35d97a1a..daabeec0c 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -289,7 +289,7 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
/* "hwaddress <class> <address>":
* unlike ifconfig, ip doesnt want <class>
* (usually "ether" keyword). Skip it. */
- if (strncmp(command, "hwaddress", 9) == 0) {
+ if (is_prefixed_with(command, "hwaddress")) {
varvalue = skip_whitespace(skip_non_whitespace(varvalue));
}
# endif
@@ -298,7 +298,7 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
# if ENABLE_FEATURE_IFUPDOWN_IP
/* Sigh... Add a special case for 'ip' to convert from
* dotted quad to bit count style netmasks. */
- if (strncmp(command, "bnmask", 6) == 0) {
+ if (is_prefixed_with(command, "bnmask")) {
unsigned res;
varvalue = get_var("netmask", 7, ifd);
if (varvalue) {
@@ -1159,12 +1159,12 @@ static char *run_mapping(char *physical, struct mapping_defn_t *map)
static llist_t *find_iface_state(llist_t *state_list, const char *iface)
{
- unsigned iface_len = strlen(iface);
llist_t *search = state_list;
while (search) {
- if ((strncmp(search->data, iface, iface_len) == 0)
- && (search->data[iface_len] == '=')
+ char *after_iface = is_prefixed_with(search->data, iface);
+ if (after_iface
+ && *after_iface == '='
) {
return search;
}
diff --git a/networking/inetd.c b/networking/inetd.c
index 8148925ce..dce5a0885 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -727,7 +727,7 @@ static NOINLINE servtab_t *parse_one_line(void)
goto parse_err;
#endif
}
- if (strncmp(arg, "rpc/", 4) == 0) {
+ if (is_prefixed_with(arg, "rpc/")) {
#if ENABLE_FEATURE_INETD_RPC
unsigned n;
arg += 4;
diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c
index aa4779ad1..4072d0626 100644
--- a/networking/libiproute/ipaddress.c
+++ b/networking/libiproute/ipaddress.c
@@ -701,7 +701,7 @@ static int ipaddr_modify(int cmd, char **argv)
/* There was no "dev IFACE", but we need that */
bb_error_msg_and_die("need \"dev IFACE\"");
}
- if (l && strncmp(d, l, strlen(d)) != 0) {
+ if (l && !is_prefixed_with(l, d)) {
bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
}
diff --git a/networking/nameif.c b/networking/nameif.c
index 9a8846dc0..9b18a6d16 100644
--- a/networking/nameif.c
+++ b/networking/nameif.c
@@ -161,19 +161,19 @@ static void nameif_parse_selector(ethtable_t *ch, char *selector)
if (*next)
*next++ = '\0';
/* Check for selectors, mac= is assumed */
- if (strncmp(selector, "bus=", 4) == 0) {
+ if (is_prefixed_with(selector, "bus=")) {
ch->bus_info = xstrdup(selector + 4);
found_selector++;
- } else if (strncmp(selector, "driver=", 7) == 0) {
+ } else if (is_prefixed_with(selector, "driver=")) {
ch->driver = xstrdup(selector + 7);
found_selector++;
- } else if (strncmp(selector, "phyaddr=", 8) == 0) {
+ } else if (is_prefixed_with(selector, "phyaddr=")) {
ch->phy_address = xatoi_positive(selector + 8);
found_selector++;
} else {
#endif
lmac = xmalloc(ETH_ALEN);
- ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac);
+ ch->mac = ether_aton_r(selector + (is_prefixed_with(selector, "mac=") ? 4 : 0), lmac);
if (ch->mac == NULL)
bb_error_msg_and_die("can't parse %s", selector);
#if ENABLE_FEATURE_NAMEIF_EXTENDED
diff --git a/networking/netstat.c b/networking/netstat.c
index f80b845bc..02f4cc7cc 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -228,12 +228,12 @@ static long extract_socket_inode(const char *lname)
{
long inode = -1;
- if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
+ if (is_prefixed_with(lname, "socket:[")) {
/* "socket:[12345]", extract the "12345" as inode */
inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
if (*lname != ']')
inode = -1;
- } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
+ } else if (is_prefixed_with(lname, "[0000]:")) {
/* "[0000]:12345", extract the "12345" as inode */
inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
if (errno) /* not NUL terminated? */
diff --git a/procps/mpstat.c b/procps/mpstat.c
index af3263d67..6028903fa 100644
--- a/procps/mpstat.c
+++ b/procps/mpstat.c
@@ -526,7 +526,7 @@ static void get_irqs_from_stat(struct stats_irq *irq)
while (fgets(buf, sizeof(buf), fp)) {
//bb_error_msg("/proc/stat:'%s'", buf);
- if (strncmp(buf, "intr ", 5) == 0) {
+ if (is_prefixed_with(buf, "intr ")) {
/* Read total number of IRQs since system boot */
sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
}
diff --git a/procps/powertop.c b/procps/powertop.c
index ddda5bd93..18affacdd 100644
--- a/procps/powertop.c
+++ b/procps/powertop.c
@@ -458,9 +458,9 @@ static NOINLINE int process_timer_stats(void)
// func = "Load balancing tick";
//}
- if (strncmp(func, "tick_nohz_", 10) == 0)
+ if (is_prefixed_with(func, "tick_nohz_"))
continue;
- if (strncmp(func, "tick_setup_sched_timer", 20) == 0)
+ if (is_prefixed_with(func, "tick_setup_sched_timer"))
continue;
//if (strcmp(process, "powertop") == 0)
// continue;
diff --git a/procps/pwdx.c b/procps/pwdx.c
index 22b892275..4e34149ed 100644
--- a/procps/pwdx.c
+++ b/procps/pwdx.c
@@ -41,7 +41,7 @@ int pwdx_main(int argc UNUSED_PARAM, char **argv)
// Allowed on the command line:
// /proc/NUM
// NUM
- if (strncmp(arg, "/proc/", 6) == 0)
+ if (is_prefixed_with(arg, "/proc/"))
arg += 6;
pid = bb_strtou(arg, NULL, 10);
diff --git a/shell/hush.c b/shell/hush.c
index 92d790180..f2c0a70f2 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -5884,7 +5884,7 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
* Our solution: ONLY bare $(trap) or `trap` is special.
*/
s = skip_whitespace(s);
- if (strncmp(s, "trap", 4) == 0
+ if (is_prefixed_with(s, "trap")
&& skip_whitespace(s + 4)[0] == '\0'
) {
static const char *const argv[] = { NULL, NULL };
diff --git a/util-linux/acpid.c b/util-linux/acpid.c
index fc8151f6a..0f2cb6bdc 100644
--- a/util-linux/acpid.c
+++ b/util-linux/acpid.c
@@ -151,7 +151,7 @@ static const char *find_action(struct input_event *ev, const char *buf)
}
if (buf) {
- if (strncmp(buf, evt_tab[i].desc, strlen(buf)) == 0) {
+ if (is_prefixed_with(evt_tab[i].desc, buf)) {
action = evt_tab[i].desc;
break;
}
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 39eb27b47..7fe70fb72 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -2781,14 +2781,14 @@ is_ide_cdrom_or_tape(const char *device)
the process hangs on the attempt to read a music CD.
So try to be careful. This only works since 2.1.73. */
- if (strncmp("/dev/hd", device, 7))
+ if (!is_prefixed_with(device, "/dev/hd"))
return 0;
snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
procf = fopen_for_read(buf);
if (procf != NULL && fgets(buf, sizeof(buf), procf))
- is_ide = (!strncmp(buf, "cdrom", 5) ||
- !strncmp(buf, "tape", 4));
+ is_ide = (is_prefixed_with(buf, "cdrom") ||
+ is_prefixed_with(buf, "tape"));
else
/* Now when this proc file does not exist, skip the
device when it is read-only. */
diff --git a/util-linux/fdisk_osf.c b/util-linux/fdisk_osf.c
index ff16389bd..af04cfcc8 100644
--- a/util-linux/fdisk_osf.c
+++ b/util-linux/fdisk_osf.c
@@ -854,7 +854,7 @@ xbsd_initlabel(struct partition *p)
d->d_magic = BSD_DISKMAGIC;
- if (strncmp(disk_device, "/dev/sd", 7) == 0)
+ if (is_prefixed_with(disk_device, "/dev/sd"))
d->d_type = BSD_DTYPE_SCSI;
else
d->d_type = BSD_DTYPE_ST506;
diff --git a/util-linux/fdisk_sgi.c b/util-linux/fdisk_sgi.c
index 785fc661b..23ebc56ef 100644
--- a/util-linux/fdisk_sgi.c
+++ b/util-linux/fdisk_sgi.c
@@ -440,7 +440,7 @@ sgi_write_table(void)
(unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
write_sector(0, sgilabel);
- if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
+ if (is_prefixed_with((char*)sgilabel->directory[0].vol_file_name, "sgilabel")) {
/*
* keep this habit of first writing the "sgilabel".
* I never tested whether it works without (AN 981002).
diff --git a/util-linux/findfs.c b/util-linux/findfs.c
index 49e8979ac..07734f359 100644
--- a/util-linux/findfs.c
+++ b/util-linux/findfs.c
@@ -27,7 +27,7 @@ int findfs_main(int argc UNUSED_PARAM, char **argv)
if (!dev)
bb_show_usage();
- if (strncmp(dev, "/dev/", 5) == 0) {
+ if (is_prefixed_with(dev, "/dev/")) {
/* Just pass any /dev/xxx name right through.
* This might aid in some scripts being able
* to call this unconditionally */
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index b2d56575f..ccc00d365 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -610,7 +610,7 @@ static void make_device(char *device_name, char *path, int operation)
* We use strstr("/block/") to forestall future surprises.
*/
type = S_IFCHR;
- if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
+ if (strstr(path, "/block/") || (G.subsystem && is_prefixed_with(G.subsystem, "block")))
type = S_IFBLK;
#if ENABLE_FEATURE_MDEV_CONF
diff --git a/util-linux/mount.c b/util-linux/mount.c
index fbc89c862..cb40c802d 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -641,7 +641,7 @@ static llist_t *get_block_backed_filesystems(void)
if (!f) continue;
while ((buf = xmalloc_fgetline(f)) != NULL) {
- if (strncmp(buf, "nodev", 5) == 0 && isspace(buf[5]))
+ if (is_prefixed_with(buf, "nodev") && isspace(buf[5]))
goto next;
fs = skip_whitespace(buf);
if (*fs == '#' || *fs == '*' || !*fs)
@@ -1364,9 +1364,9 @@ static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *fi
strcspn(opteq, " \t\n\r,"));
continue;
case 18: // "proto"
- if (!strncmp(opteq, "tcp", 3))
+ if (is_prefixed_with(opteq, "tcp"))
tcp = 1;
- else if (!strncmp(opteq, "udp", 3))
+ else if (is_prefixed_with(opteq, "udp"))
tcp = 0;
else
bb_error_msg("warning: unrecognized proto= option");
@@ -1459,7 +1459,7 @@ static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *fi
"rdirplus\0"
"acl\0";
int val = 1;
- if (!strncmp(opt, "no", 2)) {
+ if (is_prefixed_with(opt, "no")) {
val = 0;
opt += 2;
}
@@ -1979,7 +1979,7 @@ static int singlemount(struct mntent *mp, int ignore_busy)
}
// Might this be an NFS filesystem?
- if ((!mp->mnt_type || strncmp(mp->mnt_type, "nfs", 3) == 0)
+ if ((!mp->mnt_type || is_prefixed_with(mp->mnt_type, "nfs"))
&& strchr(mp->mnt_fsname, ':') != NULL
) {
if (!mp->mnt_type)
diff --git a/util-linux/rtcwake.c b/util-linux/rtcwake.c
index 53d9384db..8aee0cfcb 100644
--- a/util-linux/rtcwake.c
+++ b/util-linux/rtcwake.c
@@ -66,7 +66,7 @@ static NOINLINE bool may_wakeup(const char *rtcname)
return false;
/* wakeup events could be disabled or not supported */
- return strncmp(buf, "enabled\n", 8) == 0;
+ return is_prefixed_with(buf, "enabled\n") != NULL;
}
static NOINLINE void setup_alarm(int fd, time_t *wakeup, time_t rtc_time)
diff --git a/util-linux/volume_id/get_devname.c b/util-linux/volume_id/get_devname.c
index 0c6bdfddf..53bdbdf09 100644
--- a/util-linux/volume_id/get_devname.c
+++ b/util-linux/volume_id/get_devname.c
@@ -302,9 +302,9 @@ int resolve_mount_spec(char **fsname)
{
char *tmp = *fsname;
- if (strncmp(*fsname, "UUID=", 5) == 0)
+ if (is_prefixed_with(*fsname, "UUID="))
tmp = get_devname_from_uuid(*fsname + 5);
- else if (strncmp(*fsname, "LABEL=", 6) == 0)
+ else if (is_prefixed_with(*fsname, "LABEL=") == 0)
tmp = get_devname_from_label(*fsname + 6);
if (tmp == *fsname)