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>
This commit is contained in:
Denys Vlasenko 2015-03-12 17:48:34 +01:00
parent 936c8809ca
commit 8dff01d06a
51 changed files with 165 additions and 151 deletions

View File

@ -37,7 +37,7 @@ char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
} }
archive_handle->offset += 110; 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') || (cpio_header[5] != '1' && cpio_header[5] != '2')
) { ) {
bb_error_msg_and_die("unsupported cpio format, use newc or crc"); bb_error_msg_and_die("unsupported cpio format, use newc or crc");

View File

@ -105,7 +105,7 @@ static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int g
value = end + 1; value = end + 1;
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS #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; value += sizeof("path=") - 1;
free(archive_handle->tar__longname); free(archive_handle->tar__longname);
archive_handle->tar__longname = xstrdup(value); 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. * This is what Red Hat's patched version of tar uses.
*/ */
# define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux" # 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; value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
free(archive_handle->tar__sctx[global]); free(archive_handle->tar__sctx[global]);
archive_handle->tar__sctx[global] = xstrdup(value); 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, /* Check header has valid magic, "ustar" is for the proper tar,
* five NULs are for the old tar format */ * 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 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|| memcmp(tar.magic, "\0\0\0\0", 5) != 0) || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
) { ) {

View File

@ -12,7 +12,7 @@ void FAST_FUNC unpack_ar_archive(archive_handle_t *ar_archive)
char magic[7]; char magic[7];
xread(ar_archive->src_fd, magic, AR_MAGIC_LEN); 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"); bb_error_msg_and_die("invalid ar magic");
} }
ar_archive->offset += AR_MAGIC_LEN; ar_archive->offset += AR_MAGIC_LEN;

View File

@ -15,7 +15,7 @@ const char* FAST_FUNC strip_unsafe_prefix(const char *str)
cp++; cp++;
continue; continue;
} }
if (strncmp(cp, "/../"+1, 3) == 0) { if (is_prefixed_with(cp, "/../"+1)) {
cp += 3; cp += 3;
continue; continue;
} }

View File

@ -57,7 +57,7 @@ int loadkmap_main(int argc UNUSED_PARAM, char **argv)
*/ */
xread(STDIN_FILENO, flags, 7); 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"); bb_error_msg_and_die("not a valid binary keymap");
xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS); xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);

View File

@ -373,7 +373,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
date_buf[0] = '\0'; date_buf[0] = '\0';
} else { } else {
/* Handle special conversions */ /* 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"; fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
} }
/* Generate output string */ /* Generate output string */

View File

@ -110,10 +110,10 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
FILE *dst_stream; FILE *dst_stream;
int mode; int mode;
if (strncmp(line, "begin-base64 ", 13) == 0) { if (is_prefixed_with(line, "begin-base64 ")) {
line_ptr = line + 13; line_ptr = line + 13;
decode_fn_ptr = read_base64; decode_fn_ptr = read_base64;
} else if (strncmp(line, "begin ", 6) == 0) { } else if (is_prefixed_with(line, "begin ")) {
line_ptr = line + 6; line_ptr = line + 6;
decode_fn_ptr = read_stduu; decode_fn_ptr = read_stduu;
} else { } else {

View File

@ -199,7 +199,7 @@ static char *base_device(const char *device)
} }
/* Handle DAC 960 devices */ /* Handle DAC 960 devices */
if (strncmp(cp, "rd/", 3) == 0) { if (is_prefixed_with(cp, "rd/")) {
cp += 3; cp += 3;
if (cp[0] != 'c' || !isdigit(cp[1]) if (cp[0] != 'c' || !isdigit(cp[1])
|| cp[2] != 'd' || !isdigit(cp[3])) || cp[2] != 'd' || !isdigit(cp[3]))
@ -224,9 +224,9 @@ static char *base_device(const char *device)
#if ENABLE_FEATURE_DEVFS #if ENABLE_FEATURE_DEVFS
/* Now let's handle devfs (ugh) names */ /* Now let's handle devfs (ugh) names */
len = 0; len = 0;
if (strncmp(cp, "ide/", 4) == 0) if (is_prefixed_with(cp, "ide/"))
len = 4; len = 4;
if (strncmp(cp, "scsi/", 5) == 0) if (is_prefixed_with(cp, "scsi/"))
len = 5; len = 5;
if (len) { if (len) {
cp += len; cp += len;
@ -237,38 +237,38 @@ static char *base_device(const char *device)
* some number of digits at each level, abort. * some number of digits at each level, abort.
*/ */
for (hier = devfs_hier; *hier; hier++) { for (hier = devfs_hier; *hier; hier++) {
len = strlen(*hier); cp = is_prefixed_with(cp, *hier);
if (strncmp(cp, *hier, len) != 0) if (!cp)
goto errout; goto errout;
cp += len; while (*cp != '/' && *cp != '\0') {
while (*cp != '/' && *cp != 0) {
if (!isdigit(*cp)) if (!isdigit(*cp))
goto errout; goto errout;
cp++; cp++;
} }
//FIXME: what if *cp = '\0' now? cp++ moves past it!!!
cp++; cp++;
} }
cp[-1] = 0; cp[-1] = '\0';
return str; return str;
} }
/* Now handle devfs /dev/disc or /dev/disk names */ /* Now handle devfs /dev/disc or /dev/disk names */
disk = 0; disk = NULL;
if (strncmp(cp, "discs/", 6) == 0) if (is_prefixed_with(cp, "discs/"))
disk = "disc"; disk = "disc";
else if (strncmp(cp, "disks/", 6) == 0) else if (is_prefixed_with(cp, "disks/"))
disk = "disk"; disk = "disk";
if (disk) { if (disk) {
cp += 6; cp += 6;
if (strncmp(cp, disk, 4) != 0) cp = is_prefixed_with(cp, disk);
if (!cp)
goto errout; goto errout;
cp += 4; while (*cp != '/' && *cp != '\0') {
while (*cp != '/' && *cp != 0) {
if (!isdigit(*cp)) if (!isdigit(*cp))
goto errout; goto errout;
cp++; cp++;
} }
*cp = 0; *cp = '\0';
return str; return str;
} }
#endif #endif
@ -593,8 +593,8 @@ static void fsck_device(struct fs_info *fs /*, int interactive */)
type, "from fstab"); type, "from fstab");
} else if (fstype } else if (fstype
&& (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */ && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
&& strncmp(fstype, "opts=", 5) != 0 && !is_prefixed_with(fstype, "opts=")
&& strncmp(fstype, "loop", 4) != 0 && !is_prefixed_with(fstype, "loop")
&& !strchr(fstype, ',') && !strchr(fstype, ',')
) { ) {
type = fstype; type = fstype;
@ -627,8 +627,8 @@ static int device_already_active(char *device)
#ifdef BASE_MD #ifdef BASE_MD
/* Don't check a soft raid disk with any other disk */ /* Don't check a soft raid disk with any other disk */
if (instance_list if (instance_list
&& (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) && (is_prefixed_with(instance_list->device, BASE_MD)
|| !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)) || is_prefixed_with(device, BASE_MD))
) { ) {
return 1; return 1;
} }
@ -895,7 +895,7 @@ static void compile_fs_type(char *fs_type)
if (strcmp(s, "loop") == 0) if (strcmp(s, "loop") == 0)
/* loop is really short-hand for opts=loop */ /* loop is really short-hand for opts=loop */
goto loop_special_case; goto loop_special_case;
if (strncmp(s, "opts=", 5) == 0) { if (is_prefixed_with(s, "opts=")) {
s += 5; s += 5;
loop_special_case: loop_special_case:
fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT; fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;

View File

@ -414,7 +414,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
} }
// Open a new file? // 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; char *s, **name = reverse ? &newname : &oldname;
int i; int i;
@ -446,7 +446,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
// Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@ // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
// but a missing ,value means the value is 1. // 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; int i;
char *s = patchline+4; char *s = patchline+4;

View File

@ -389,6 +389,7 @@ const char *bb_basename(const char *name) FAST_FUNC;
/* NB: can violate const-ness (similarly to strchr) */ /* NB: can violate const-ness (similarly to strchr) */
char *last_char_is(const char *s, int c) FAST_FUNC; char *last_char_is(const char *s, int c) FAST_FUNC;
const char* endofname(const char *name) 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_on(int fd) FAST_FUNC;
int ndelay_off(int fd) FAST_FUNC; int ndelay_off(int fd) FAST_FUNC;

View File

@ -193,7 +193,7 @@ void lbb_prepare(const char *applet
if (argv[1] if (argv[1]
&& !argv[2] && !argv[2]
&& strcmp(argv[1], "--help") == 0 && strcmp(argv[1], "--help") == 0
&& strncmp(applet, "busybox", 7) != 0 && !is_prefixed_with(applet, "busybox")
) { ) {
/* Special case. POSIX says "test --help" /* Special case. POSIX says "test --help"
* should be no different from e.g. "test --foo". */ * should be no different from e.g. "test --foo". */
@ -673,7 +673,7 @@ static int busybox_main(char **argv)
return 0; return 0;
} }
if (strncmp(argv[1], "--list", 6) == 0) { if (is_prefixed_with(argv[1], "--list")) {
unsigned i = 0; unsigned i = 0;
const char *a = applet_names; const char *a = applet_names;
dup2(1, 2); 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); int applet = find_applet_by_name(name);
if (applet >= 0) if (applet >= 0)
run_applet_no_and_exit(applet, argv); run_applet_no_and_exit(applet, argv);
if (strncmp(name, "busybox", 7) == 0) if (is_prefixed_with(name, "busybox"))
exit(busybox_main(argv)); exit(busybox_main(argv));
} }
@ -817,7 +817,7 @@ int main(int argc UNUSED_PARAM, char **argv)
#if defined(SINGLE_APPLET_MAIN) #if defined(SINGLE_APPLET_MAIN)
/* Only one applet is selected in .config */ /* 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 */ /* "busybox <applet> <params>" should still work as expected */
argv++; argv++;
} }

View File

@ -5,6 +5,24 @@
#include "libbb.h" #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 */ /* returns the array index of the string */
/* (index of first match is returned, or -1) */ /* (index of first match is returned, or -1) */
int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) 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 FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
{ {
int i; int i;
int len = strlen(key); if (key[0]) {
if (len) {
for (i = 0; string_array[i] != 0; i++) { 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; return i;
} }
} }

View File

@ -681,7 +681,7 @@ static NOINLINE unsigned complete_username(const char *ud)
setpwent(); setpwent();
while ((pw = getpwent()) != NULL) { while ((pw = getpwent()) != NULL) {
/* Null usernames should result in all users as possible completions. */ /* 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)); 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)) if (!pfind[0] && DOT_OR_DOTDOT(name_found))
continue; continue;
/* match? */ /* match? */
if (strncmp(name_found, pfind, pf_len) != 0) if (!is_prefixed_with(name_found, pfind))
continue; /* no */ continue; /* no */
found = concat_path_file(paths[i], name_found); 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); cwd_buf = xrealloc_getcwd_or_warn(NULL);
if (!cwd_buf) if (!cwd_buf)
cwd_buf = (char *)bb_msg_unknown; cwd_buf = (char *)bb_msg_unknown;
else { else if (home_pwd_buf[0]) {
char *after_home_user;
/* /home/user[/something] -> ~[/something] */ /* /home/user[/something] -> ~[/something] */
l = strlen(home_pwd_buf); after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
if (l != 0 if (after_home_user
&& strncmp(home_pwd_buf, cwd_buf, l) == 0 && (*after_home_user == '/' || *after_home_user == '\0')
&& (cwd_buf[l] == '/' || cwd_buf[l] == '\0')
) { ) {
cwd_buf[0] = '~'; cwd_buf[0] = '~';
overlapping_strcpy(cwd_buf + 1, cwd_buf + l); overlapping_strcpy(cwd_buf + 1, after_home_user);
} }
} }
} }

View File

@ -17,7 +17,6 @@
int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype) int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
{ {
int match = 1; int match = 1;
int len;
if (!t_fstype) if (!t_fstype)
return match; return match;
@ -27,10 +26,10 @@ int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
t_fstype += 2; t_fstype += 2;
} }
len = strlen(mt->mnt_type);
while (1) { while (1) {
if (strncmp(mt->mnt_type, t_fstype, len) == 0 char *after_mnt_type = is_prefixed_with(t_fstype, mt->mnt_type);
&& (t_fstype[len] == '\0' || t_fstype[len] == ',') if (after_mnt_type
&& (*after_mnt_type == '\0' || *after_mnt_type == ',')
) { ) {
return match; return match;
} }

View File

@ -205,11 +205,11 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
// Rss: nnn kB // Rss: nnn kB
// ..... // .....
char *tp = buf, *p; char *tp, *p;
#define SCAN(S, X) \ #define SCAN(S, X) \
if (strncmp(tp, S, sizeof(S)-1) == 0) { \ if ((tp = is_prefixed_with(buf, S)) != NULL) { \
tp = skip_whitespace(tp + sizeof(S)-1); \ tp = skip_whitespace(tp); \
total->X += currec.X = fast_strtoul_10(&tp); \ total->X += currec.X = fast_strtoul_10(&tp); \
continue; \ continue; \
} }
@ -247,7 +247,7 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
// skipping "rw-s FILEOFS M:m INODE " // skipping "rw-s FILEOFS M:m INODE "
tp = skip_whitespace(skip_fields(tp, 4)); tp = skip_whitespace(skip_fields(tp, 4));
// filter out /dev/something (something != zero) // 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') { if (currec.smap_mode[1] == 'w') {
currec.mapped_rw = currec.smap_size; currec.mapped_rw = currec.smap_size;
total->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)) { while (fgets(buf, sizeof(buf), file)) {
char *tp; char *tp;
#define SCAN_TWO(str, name, statement) \ #define SCAN_TWO(str, name, statement) \
if (strncmp(buf, str, sizeof(str)-1) == 0) { \ if ((tp = is_prefixed_with(buf, str)) != NULL) { \
tp = skip_whitespace(buf + sizeof(str)-1); \ tp = skip_whitespace(tp); \
sscanf(tp, "%u", &sp->name); \ sscanf(tp, "%u", &sp->name); \
statement; \ statement; \
} }

View File

@ -22,7 +22,7 @@ int FAST_FUNC rtc_adjtime_is_utc(void)
char buffer[128]; char buffer[128];
while (fgets(buffer, sizeof(buffer), f)) { while (fgets(buffer, sizeof(buffer), f)) {
if (strncmp(buffer, "UTC", 3) == 0) { if (is_prefixed_with(buffer, "UTC")) {
utc = 1; utc = 1;
break; break;
} }

View File

@ -33,7 +33,7 @@ char* FAST_FUNC skip_non_whitespace(const char *s)
char* FAST_FUNC skip_dev_pfx(const char *tty_name) 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; tty_name += 5;
return (char*)tty_name; return (char*)tty_name;
} }

View File

@ -84,7 +84,6 @@ int FAST_FUNC update_passwd(const char *filename,
char *fnamesfx; char *fnamesfx;
char *sfx_char; char *sfx_char;
char *name_colon; char *name_colon;
unsigned user_len;
int old_fd; int old_fd;
int new_fd; int new_fd;
int i; int i;
@ -108,7 +107,6 @@ int FAST_FUNC update_passwd(const char *filename,
fnamesfx = xasprintf("%s+", filename); fnamesfx = xasprintf("%s+", filename);
sfx_char = &fnamesfx[strlen(fnamesfx)-1]; sfx_char = &fnamesfx[strlen(fnamesfx)-1];
name_colon = xasprintf("%s:", name ? name : ""); name_colon = xasprintf("%s:", name ? name : "");
user_len = strlen(name_colon);
if (shadow) if (shadow)
old_fp = fopen(filename, "r+"); old_fp = fopen(filename, "r+");
@ -179,7 +177,7 @@ int FAST_FUNC update_passwd(const char *filename,
while (list) { while (list) {
list++; list++;
next_list_element: next_list_element:
if (strncmp(list, member, member_len) == 0) { if (is_prefixed_with(list, member)) {
char c; char c;
changed_lines++; changed_lines++;
c = list[member_len]; c = list[member_len];
@ -201,13 +199,14 @@ int FAST_FUNC update_passwd(const char *filename,
goto next; 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); fprintf(new_fp, "%s\n", line);
goto next; goto next;
} }
/* We have a match with "name:"... */ /* 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 ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
if (member) { if (member) {

View File

@ -171,7 +171,7 @@ IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
const char *cp; const char *cp;
struct addrinfo hint; 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; struct sockaddr_un *sun;
r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un)); r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));

View File

@ -438,14 +438,14 @@ static void load_crontab(const char *fileName)
log5("user:%s entry:%s", fileName, parser->data); log5("user:%s entry:%s", fileName, parser->data);
/* check if line is setting MAILTO= */ /* 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 #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
free(mailTo); free(mailTo);
mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL; mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
#endif /* otherwise just ignore such lines */ #endif /* otherwise just ignore such lines */
continue; continue;
} }
if (0 == strncmp(tokens[0], "SHELL=", 6)) { if (is_prefixed_with(tokens[0], "SHELL=")) {
free(shell); free(shell);
shell = xstrdup(&tokens[0][6]); shell = xstrdup(&tokens[0][6]);
continue; continue;

View File

@ -244,9 +244,9 @@ static void stack_machine(const char *argument)
o = operators; o = operators;
do { do {
const size_t name_len = strlen(o->name); char *after_name = is_prefixed_with(argument, o->name);
if (strncmp(o->name, argument, name_len) == 0) { if (after_name) {
argument += name_len; argument = after_name;
o->function(); o->function();
goto next; goto next;
} }

View File

@ -1405,7 +1405,6 @@ const char *get_old_name(const char *devname, unsigned int namelen,
int indexx; int indexx;
const char *pty1; const char *pty1;
const char *pty2; const char *pty2;
size_t len;
/* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */ /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
static const char *const fmt[] = { static const char *const fmt[] = {
NULL , NULL ,
@ -1425,12 +1424,11 @@ const char *get_old_name(const char *devname, unsigned int namelen,
}; };
for (trans = translate_table; trans->match != NULL; ++trans) { for (trans = translate_table; trans->match != NULL; ++trans) {
len = strlen(trans->match); char *after_match = is_prefixed_with(devname, trans->match);
if (after_match) {
if (strncmp(devname, trans->match, len) == 0) {
if (trans->format == NULL) if (trans->format == NULL)
return devname + len; return after_match;
sprintf(buffer, trans->format, devname + len); sprintf(buffer, trans->format, after_match);
return buffer; return buffer;
} }
} }

View File

@ -516,7 +516,7 @@ int fbsplash_main(int argc UNUSED_PARAM, char **argv)
// handle a case when we have many buffered lines // handle a case when we have many buffered lines
// already in the pipe // already in the pipe
while ((num_buf = xmalloc_fgetline(fp)) != NULL) { while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
if (strncmp(num_buf, "exit", 4) == 0) { if (is_prefixed_with(num_buf, "exit")) {
DEBUG_MESSAGE("exit"); DEBUG_MESSAGE("exit");
break; break;
} }

View File

@ -1198,7 +1198,7 @@ static void NORETURN list_i2c_busses_and_exit(void)
if (subde->d_name[0] == '.') if (subde->d_name[0] == '.')
continue; continue;
if (strncmp(subde->d_name, "i2c-", 4) == 0) { if (is_prefixed_with(subde->d_name, "i2c-")) {
snprintf(path, NAME_MAX, snprintf(path, NAME_MAX,
"%s/%s/device/%s/name", "%s/%s/device/%s/name",
i2cdev_path, de->d_name, i2cdev_path, de->d_name,
@ -1229,7 +1229,7 @@ found:
if (rv != 1) if (rv != 1)
continue; continue;
if (strncmp(name, "ISA", 3) == 0) if (is_prefixed_with(name, "ISA"))
adt = ADT_ISA; adt = ADT_ISA;
else else
adt = i2cdetect_get_funcs(bus); adt = i2cdetect_get_funcs(bus);

View File

@ -87,11 +87,11 @@ int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
if (++n > 0) if (++n > 0)
ut.ut_type = n != 3 ? n : SHUTDOWN_TIME; ut.ut_type = n != 3 ? n : SHUTDOWN_TIME;
#else #else
if (strncmp(ut.ut_user, "shutdown", 8) == 0) if (is_prefixed_with(ut.ut_user, "shutdown"))
ut.ut_type = SHUTDOWN_TIME; 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; 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; ut.ut_type = RUN_LVL;
#endif #endif
} else { } else {

View File

@ -66,7 +66,7 @@ static int run_pipe(const char *pager, char *man_filename, int man, int level)
goto ordinary_manpage; goto ordinary_manpage;
line = xmalloc_open_zipped_read_close(man_filename, NULL); 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); free(line);
goto ordinary_manpage; goto ordinary_manpage;
} }
@ -228,7 +228,7 @@ int man_main(int argc UNUSED_PARAM, char **argv)
if (!token[1]) if (!token[1])
continue; continue;
if (strcmp("DEFINE", token[0]) == 0) { 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); pager = xstrdup(skip_whitespace(token[1]) + 5);
} }
} else } else

View File

@ -55,7 +55,7 @@ static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARA
NULL NULL
); );
for (ptr = image; ptr < image + len - 10; ptr++) { for (ptr = image; ptr < image + len - 10; ptr++) {
if (strncmp(ptr, "depends=", 8) == 0) { if (is_prefixed_with(ptr, "depends=")) {
char *u; char *u;
ptr += 8; ptr += 8;
@ -64,15 +64,15 @@ static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARA
*u = '_'; *u = '_';
ptr += string_to_llist(ptr, &info->dependencies, ","); ptr += string_to_llist(ptr, &info->dependencies, ",");
} else if (ENABLE_FEATURE_MODUTILS_ALIAS } else if (ENABLE_FEATURE_MODUTILS_ALIAS
&& strncmp(ptr, "alias=", 6) == 0 && is_prefixed_with(ptr, "alias=")
) { ) {
llist_add_to(&info->aliases, xstrdup(ptr + 6)); llist_add_to(&info->aliases, xstrdup(ptr + 6));
ptr += strlen(ptr); ptr += strlen(ptr);
} else if (ENABLE_FEATURE_MODUTILS_SYMBOLS } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
&& strncmp(ptr, "__ksymtab_", 10) == 0 && is_prefixed_with(ptr, "__ksymtab_")
) { ) {
ptr += 10; ptr += 10;
if (strncmp(ptr, "gpl", 3) == 0 if (is_prefixed_with(ptr, "gpl")
|| strcmp(ptr, "strings") == 0 || strcmp(ptr, "strings") == 0
) { ) {
continue; continue;

View File

@ -62,7 +62,7 @@ static void modinfo(const char *path, const char *version,
"firmware", "firmware",
}; };
size_t len; size_t len;
int j, length; int j;
char *ptr, *the_module; char *ptr, *the_module;
const char *field = env->field; const char *field = env->field;
int tags = env->tags; int tags = env->tags;
@ -94,16 +94,18 @@ static void modinfo(const char *path, const char *version,
pattern = field; pattern = field;
if ((1<<j) & OPT_TAGS) if ((1<<j) & OPT_TAGS)
pattern = shortcuts[j]; pattern = shortcuts[j];
length = strlen(pattern);
ptr = the_module; ptr = the_module;
while (1) { while (1) {
char *after_pattern;
ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module)); ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module));
if (ptr == NULL) /* no occurance left, done */ if (ptr == NULL) /* no occurance left, done */
break; 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 */ /* field prefixes are 0x80 or 0x00 */
if ((ptr[-1] & 0x7F) == '\0') { if ((ptr[-1] & 0x7F) == 0x00) {
ptr += length + 1; ptr = after_pattern + 1;
display(ptr, pattern, (1<<j) != tags); display(ptr, pattern, (1<<j) != tags);
ptr += strlen(ptr); ptr += strlen(ptr);
} }

View File

@ -116,21 +116,21 @@ static char* copy_stringbuf(void)
static char* find_keyword(char *ptr, size_t len, const char *word) 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 */ if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
return NULL; return NULL;
wlen = strlen(word); len -= strlen(word) - 1;
len -= wlen - 1;
while ((ssize_t)len > 0) { while ((ssize_t)len > 0) {
char *old = ptr; char *old = ptr;
char *after_word;
/* search for the first char in 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 */ if (ptr == NULL) /* no occurance left, done */
break; break;
if (strncmp(ptr, word, wlen) == 0) after_word = is_prefixed_with(ptr, word);
return ptr + wlen; /* found, return ptr past it */ if (after_word)
return after_word; /* found, return ptr past it */
++ptr; ++ptr;
len -= (ptr - old); len -= (ptr - old);
} }
@ -536,7 +536,7 @@ static module_info** find_alias(const char *alias)
// TODO: open only once, invent config_rewind() // TODO: open only once, invent config_rewind()
static int already_loaded(const char *name) static int already_loaded(const char *name)
{ {
int ret, namelen; int ret;
char *line; char *line;
FILE *fp; FILE *fp;
@ -545,15 +545,16 @@ static int already_loaded(const char *name)
fp = fopen_for_read("/proc/modules"); fp = fopen_for_read("/proc/modules");
if (!fp) if (!fp)
return 0; return 0;
namelen = strlen(name);
while ((line = xmalloc_fgetline(fp)) != NULL) { while ((line = xmalloc_fgetline(fp)) != NULL) {
char *live; char *live;
char *after_name;
// Examples from kernel 3.14.6: // Examples from kernel 3.14.6:
//pcspkr 12718 0 - Live 0xffffffffa017e000 //pcspkr 12718 0 - Live 0xffffffffa017e000
//snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000 //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000
//i915 801405 2 - Live 0xffffffffa0096000 //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); free(line);
continue; continue;
} }

View File

@ -260,7 +260,7 @@ static void add_probe(const char *name)
llist_add_to_end(&G.probes, m); llist_add_to_end(&G.probes, m);
G.num_unresolved_deps++; G.num_unresolved_deps++;
if (ENABLE_FEATURE_MODUTILS_SYMBOLS if (ENABLE_FEATURE_MODUTILS_SYMBOLS
&& strncmp(m->modname, "symbol:", 7) == 0 && is_prefixed_with(m->modname, "symbol:")
) { ) {
G.need_symbols = 1; 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_buf;
char *kcmdline; char *kcmdline;
char *kptr; char *kptr;
int len;
kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL); kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
if (!kcmdline_buf) if (!kcmdline_buf)
return options; return options;
kcmdline = kcmdline_buf; kcmdline = kcmdline_buf;
len = strlen(modulename);
while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) { while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
if (strncmp(modulename, kptr, len) != 0) char *after_modulename = is_prefixed_with(kptr, modulename);
continue; if (!after_modulename || *after_modulename != '.')
kptr += len;
if (*kptr != '.')
continue; continue;
/* It is "modulename.xxxx" */ /* It is "modulename.xxxx" */
kptr++; kptr = after_modulename + 1;
if (strchr(kptr, '=') != NULL) { if (strchr(kptr, '=') != NULL) {
/* It is "modulename.opt=[val]" */ /* It is "modulename.opt=[val]" */
options = gather_options_str(options, kptr); options = gather_options_str(options, kptr);

View File

@ -2255,7 +2255,7 @@ static int add_symbols_from(struct obj_file *f,
* symbols so they cannot fudge it by adding the prefix on * symbols so they cannot fudge it by adding the prefix on
* their references. * 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 ENABLE_FEATURE_CHECK_TAINTED_MODULE
if (gpl) if (gpl)
s->name += 8; s->name += 8;

View File

@ -194,7 +194,7 @@ static char *table_lookup(struct dns_entry *d,
if ((len != 1 || d->name[1] != '*') if ((len != 1 || d->name[1] != '*')
/* we assume (do not check) that query_string /* we assume (do not check) that query_string
* ends in ".in-addr.arpa" */ * ends in ".in-addr.arpa" */
&& strncmp(d->rip, query_string, strlen(d->rip)) == 0 && is_prefixed_with(query_string, d->rip)
) { ) {
#if DEBUG #if DEBUG
fprintf(stderr, "Found name:%s\n", d->name); fprintf(stderr, "Found name:%s\n", d->name);

View File

@ -697,7 +697,7 @@ static void parse_conf(const char *path, int flag)
goto config_error; goto config_error;
} }
*host_port++ = '\0'; *host_port++ = '\0';
if (strncmp(host_port, "http://", 7) == 0) if (is_prefixed_with(host_port, "http://"))
host_port += 7; host_port += 7;
if (*host_port == '\0') { if (*host_port == '\0') {
goto config_error; goto config_error;
@ -1894,7 +1894,7 @@ static Htaccess_Proxy *find_proxy_entry(const char *url)
{ {
Htaccess_Proxy *p; Htaccess_Proxy *p;
for (p = proxy; p; p = p->next) { 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 p;
} }
return NULL; return NULL;
@ -2183,7 +2183,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
if (STRNCASECMP(iobuf, "Range:") == 0) { if (STRNCASECMP(iobuf, "Range:") == 0) {
/* We know only bytes=NNN-[MMM] */ /* We know only bytes=NNN-[MMM] */
char *s = skip_whitespace(iobuf + sizeof("Range:")-1); 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; s += sizeof("bytes=")-1;
range_start = BB_STRTOOFF(s, &s, 10); range_start = BB_STRTOOFF(s, &s, 10);
if (s[0] != '-' || range_start < 0) { 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 '/' */ tptr = urlcopy + 1; /* skip first '/' */
#if ENABLE_FEATURE_HTTPD_CGI #if ENABLE_FEATURE_HTTPD_CGI
if (strncmp(tptr, "cgi-bin/", 8) == 0) { if (is_prefixed_with(tptr, "cgi-bin/")) {
if (tptr[8] == '\0') { if (tptr[8] == '\0') {
/* protect listing "cgi-bin/" */ /* protect listing "cgi-bin/" */
send_headers_and_exit(HTTP_FORBIDDEN); send_headers_and_exit(HTTP_FORBIDDEN);

View File

@ -289,7 +289,7 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
/* "hwaddress <class> <address>": /* "hwaddress <class> <address>":
* unlike ifconfig, ip doesnt want <class> * unlike ifconfig, ip doesnt want <class>
* (usually "ether" keyword). Skip it. */ * (usually "ether" keyword). Skip it. */
if (strncmp(command, "hwaddress", 9) == 0) { if (is_prefixed_with(command, "hwaddress")) {
varvalue = skip_whitespace(skip_non_whitespace(varvalue)); varvalue = skip_whitespace(skip_non_whitespace(varvalue));
} }
# endif # endif
@ -298,7 +298,7 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
# if ENABLE_FEATURE_IFUPDOWN_IP # if ENABLE_FEATURE_IFUPDOWN_IP
/* Sigh... Add a special case for 'ip' to convert from /* Sigh... Add a special case for 'ip' to convert from
* dotted quad to bit count style netmasks. */ * dotted quad to bit count style netmasks. */
if (strncmp(command, "bnmask", 6) == 0) { if (is_prefixed_with(command, "bnmask")) {
unsigned res; unsigned res;
varvalue = get_var("netmask", 7, ifd); varvalue = get_var("netmask", 7, ifd);
if (varvalue) { 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) static llist_t *find_iface_state(llist_t *state_list, const char *iface)
{ {
unsigned iface_len = strlen(iface);
llist_t *search = state_list; llist_t *search = state_list;
while (search) { while (search) {
if ((strncmp(search->data, iface, iface_len) == 0) char *after_iface = is_prefixed_with(search->data, iface);
&& (search->data[iface_len] == '=') if (after_iface
&& *after_iface == '='
) { ) {
return search; return search;
} }

View File

@ -727,7 +727,7 @@ static NOINLINE servtab_t *parse_one_line(void)
goto parse_err; goto parse_err;
#endif #endif
} }
if (strncmp(arg, "rpc/", 4) == 0) { if (is_prefixed_with(arg, "rpc/")) {
#if ENABLE_FEATURE_INETD_RPC #if ENABLE_FEATURE_INETD_RPC
unsigned n; unsigned n;
arg += 4; arg += 4;

View File

@ -701,7 +701,7 @@ static int ipaddr_modify(int cmd, char **argv)
/* There was no "dev IFACE", but we need that */ /* There was no "dev IFACE", but we need that */
bb_error_msg_and_die("need \"dev IFACE\""); 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); bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
} }

View File

@ -161,19 +161,19 @@ static void nameif_parse_selector(ethtable_t *ch, char *selector)
if (*next) if (*next)
*next++ = '\0'; *next++ = '\0';
/* Check for selectors, mac= is assumed */ /* Check for selectors, mac= is assumed */
if (strncmp(selector, "bus=", 4) == 0) { if (is_prefixed_with(selector, "bus=")) {
ch->bus_info = xstrdup(selector + 4); ch->bus_info = xstrdup(selector + 4);
found_selector++; found_selector++;
} else if (strncmp(selector, "driver=", 7) == 0) { } else if (is_prefixed_with(selector, "driver=")) {
ch->driver = xstrdup(selector + 7); ch->driver = xstrdup(selector + 7);
found_selector++; found_selector++;
} else if (strncmp(selector, "phyaddr=", 8) == 0) { } else if (is_prefixed_with(selector, "phyaddr=")) {
ch->phy_address = xatoi_positive(selector + 8); ch->phy_address = xatoi_positive(selector + 8);
found_selector++; found_selector++;
} else { } else {
#endif #endif
lmac = xmalloc(ETH_ALEN); 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) if (ch->mac == NULL)
bb_error_msg_and_die("can't parse %s", selector); bb_error_msg_and_die("can't parse %s", selector);
#if ENABLE_FEATURE_NAMEIF_EXTENDED #if ENABLE_FEATURE_NAMEIF_EXTENDED

View File

@ -228,12 +228,12 @@ static long extract_socket_inode(const char *lname)
{ {
long inode = -1; long inode = -1;
if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) { if (is_prefixed_with(lname, "socket:[")) {
/* "socket:[12345]", extract the "12345" as inode */ /* "socket:[12345]", extract the "12345" as inode */
inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0); inode = bb_strtoul(lname + sizeof("socket:[")-1, (char**)&lname, 0);
if (*lname != ']') if (*lname != ']')
inode = -1; 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 */ /* "[0000]:12345", extract the "12345" as inode */
inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0); inode = bb_strtoul(lname + sizeof("[0000]:")-1, NULL, 0);
if (errno) /* not NUL terminated? */ if (errno) /* not NUL terminated? */

View File

@ -526,7 +526,7 @@ static void get_irqs_from_stat(struct stats_irq *irq)
while (fgets(buf, sizeof(buf), fp)) { while (fgets(buf, sizeof(buf), fp)) {
//bb_error_msg("/proc/stat:'%s'", buf); //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 */ /* Read total number of IRQs since system boot */
sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr); sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
} }

View File

@ -458,9 +458,9 @@ static NOINLINE int process_timer_stats(void)
// func = "Load balancing tick"; // func = "Load balancing tick";
//} //}
if (strncmp(func, "tick_nohz_", 10) == 0) if (is_prefixed_with(func, "tick_nohz_"))
continue; continue;
if (strncmp(func, "tick_setup_sched_timer", 20) == 0) if (is_prefixed_with(func, "tick_setup_sched_timer"))
continue; continue;
//if (strcmp(process, "powertop") == 0) //if (strcmp(process, "powertop") == 0)
// continue; // continue;

View File

@ -41,7 +41,7 @@ int pwdx_main(int argc UNUSED_PARAM, char **argv)
// Allowed on the command line: // Allowed on the command line:
// /proc/NUM // /proc/NUM
// NUM // NUM
if (strncmp(arg, "/proc/", 6) == 0) if (is_prefixed_with(arg, "/proc/"))
arg += 6; arg += 6;
pid = bb_strtou(arg, NULL, 10); pid = bb_strtou(arg, NULL, 10);

View File

@ -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. * Our solution: ONLY bare $(trap) or `trap` is special.
*/ */
s = skip_whitespace(s); s = skip_whitespace(s);
if (strncmp(s, "trap", 4) == 0 if (is_prefixed_with(s, "trap")
&& skip_whitespace(s + 4)[0] == '\0' && skip_whitespace(s + 4)[0] == '\0'
) { ) {
static const char *const argv[] = { NULL, NULL }; static const char *const argv[] = { NULL, NULL };

View File

@ -151,7 +151,7 @@ static const char *find_action(struct input_event *ev, const char *buf)
} }
if (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; action = evt_tab[i].desc;
break; break;
} }

View File

@ -2781,14 +2781,14 @@ is_ide_cdrom_or_tape(const char *device)
the process hangs on the attempt to read a music CD. the process hangs on the attempt to read a music CD.
So try to be careful. This only works since 2.1.73. */ 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; return 0;
snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5); snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
procf = fopen_for_read(buf); procf = fopen_for_read(buf);
if (procf != NULL && fgets(buf, sizeof(buf), procf)) if (procf != NULL && fgets(buf, sizeof(buf), procf))
is_ide = (!strncmp(buf, "cdrom", 5) || is_ide = (is_prefixed_with(buf, "cdrom") ||
!strncmp(buf, "tape", 4)); is_prefixed_with(buf, "tape"));
else else
/* Now when this proc file does not exist, skip the /* Now when this proc file does not exist, skip the
device when it is read-only. */ device when it is read-only. */

View File

@ -854,7 +854,7 @@ xbsd_initlabel(struct partition *p)
d->d_magic = BSD_DISKMAGIC; 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; d->d_type = BSD_DTYPE_SCSI;
else else
d->d_type = BSD_DTYPE_ST506; d->d_type = BSD_DTYPE_ST506;

View File

@ -440,7 +440,7 @@ sgi_write_table(void)
(unsigned int*)sgilabel, sizeof(*sgilabel)) == 0); (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
write_sector(0, sgilabel); 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". * keep this habit of first writing the "sgilabel".
* I never tested whether it works without (AN 981002). * I never tested whether it works without (AN 981002).

View File

@ -27,7 +27,7 @@ int findfs_main(int argc UNUSED_PARAM, char **argv)
if (!dev) if (!dev)
bb_show_usage(); bb_show_usage();
if (strncmp(dev, "/dev/", 5) == 0) { if (is_prefixed_with(dev, "/dev/")) {
/* Just pass any /dev/xxx name right through. /* Just pass any /dev/xxx name right through.
* This might aid in some scripts being able * This might aid in some scripts being able
* to call this unconditionally */ * to call this unconditionally */

View File

@ -610,7 +610,7 @@ static void make_device(char *device_name, char *path, int operation)
* We use strstr("/block/") to forestall future surprises. * We use strstr("/block/") to forestall future surprises.
*/ */
type = S_IFCHR; 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; type = S_IFBLK;
#if ENABLE_FEATURE_MDEV_CONF #if ENABLE_FEATURE_MDEV_CONF

View File

@ -641,7 +641,7 @@ static llist_t *get_block_backed_filesystems(void)
if (!f) continue; if (!f) continue;
while ((buf = xmalloc_fgetline(f)) != NULL) { 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; goto next;
fs = skip_whitespace(buf); fs = skip_whitespace(buf);
if (*fs == '#' || *fs == '*' || !*fs) 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,")); strcspn(opteq, " \t\n\r,"));
continue; continue;
case 18: // "proto" case 18: // "proto"
if (!strncmp(opteq, "tcp", 3)) if (is_prefixed_with(opteq, "tcp"))
tcp = 1; tcp = 1;
else if (!strncmp(opteq, "udp", 3)) else if (is_prefixed_with(opteq, "udp"))
tcp = 0; tcp = 0;
else else
bb_error_msg("warning: unrecognized proto= option"); 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" "rdirplus\0"
"acl\0"; "acl\0";
int val = 1; int val = 1;
if (!strncmp(opt, "no", 2)) { if (is_prefixed_with(opt, "no")) {
val = 0; val = 0;
opt += 2; opt += 2;
} }
@ -1979,7 +1979,7 @@ static int singlemount(struct mntent *mp, int ignore_busy)
} }
// Might this be an NFS filesystem? // 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 && strchr(mp->mnt_fsname, ':') != NULL
) { ) {
if (!mp->mnt_type) if (!mp->mnt_type)

View File

@ -66,7 +66,7 @@ static NOINLINE bool may_wakeup(const char *rtcname)
return false; return false;
/* wakeup events could be disabled or not supported */ /* 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) static NOINLINE void setup_alarm(int fd, time_t *wakeup, time_t rtc_time)

View File

@ -302,9 +302,9 @@ int resolve_mount_spec(char **fsname)
{ {
char *tmp = *fsname; char *tmp = *fsname;
if (strncmp(*fsname, "UUID=", 5) == 0) if (is_prefixed_with(*fsname, "UUID="))
tmp = get_devname_from_uuid(*fsname + 5); 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); tmp = get_devname_from_label(*fsname + 6);
if (tmp == *fsname) if (tmp == *fsname)