aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2015-03-12 17:48:34 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-03-12 17:48:34 +0100
commit8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb (patch)
tree41c9dfbd45676c9e8737a7d8aa4361bbdfb40868 /libbb
parent936c8809caea5705e26e5d7e06ea3895c28fffd8 (diff)
downloadbusybox-w32-8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb.tar.gz
busybox-w32-8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb.tar.bz2
busybox-w32-8dff01d06a7ebd7330e3a1dd1ba47b3c74ee7dfb.zip
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>
Diffstat (limited to 'libbb')
-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
9 files changed, 49 insertions, 33 deletions
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
193 if (argv[1] 193 if (argv[1]
194 && !argv[2] 194 && !argv[2]
195 && strcmp(argv[1], "--help") == 0 195 && strcmp(argv[1], "--help") == 0
196 && strncmp(applet, "busybox", 7) != 0 196 && !is_prefixed_with(applet, "busybox")
197 ) { 197 ) {
198 /* Special case. POSIX says "test --help" 198 /* Special case. POSIX says "test --help"
199 * should be no different from e.g. "test --foo". */ 199 * should be no different from e.g. "test --foo". */
@@ -673,7 +673,7 @@ static int busybox_main(char **argv)
673 return 0; 673 return 0;
674 } 674 }
675 675
676 if (strncmp(argv[1], "--list", 6) == 0) { 676 if (is_prefixed_with(argv[1], "--list")) {
677 unsigned i = 0; 677 unsigned i = 0;
678 const char *a = applet_names; 678 const char *a = applet_names;
679 dup2(1, 2); 679 dup2(1, 2);
@@ -778,7 +778,7 @@ void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
778 int applet = find_applet_by_name(name); 778 int applet = find_applet_by_name(name);
779 if (applet >= 0) 779 if (applet >= 0)
780 run_applet_no_and_exit(applet, argv); 780 run_applet_no_and_exit(applet, argv);
781 if (strncmp(name, "busybox", 7) == 0) 781 if (is_prefixed_with(name, "busybox"))
782 exit(busybox_main(argv)); 782 exit(busybox_main(argv));
783} 783}
784 784
@@ -817,7 +817,7 @@ int main(int argc UNUSED_PARAM, char **argv)
817 817
818#if defined(SINGLE_APPLET_MAIN) 818#if defined(SINGLE_APPLET_MAIN)
819 /* Only one applet is selected in .config */ 819 /* Only one applet is selected in .config */
820 if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) { 820 if (argv[1] && is_prefixed_with(argv[0], "busybox")) {
821 /* "busybox <applet> <params>" should still work as expected */ 821 /* "busybox <applet> <params>" should still work as expected */
822 argv++; 822 argv++;
823 } 823 }
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 @@
5 5
6#include "libbb.h" 6#include "libbb.h"
7 7
8char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
9{
10#if 0 /* Two passes over key - probably slower */
11 int len = strlen(key);
12 if (strncmp(string, key, len) == 0)
13 return string + len;
14 return NULL;
15#else /* Open-coded */
16 while (*key != '\0') {
17 if (*key != *string)
18 return NULL;
19 key++;
20 string++;
21 }
22 return (char*)string;
23#endif
24}
25
8/* returns the array index of the string */ 26/* returns the array index of the string */
9/* (index of first match is returned, or -1) */ 27/* (index of first match is returned, or -1) */
10int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) 28int 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)
39int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key) 57int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
40{ 58{
41 int i; 59 int i;
42 int len = strlen(key); 60 if (key[0]) {
43 if (len) {
44 for (i = 0; string_array[i] != 0; i++) { 61 for (i = 0; string_array[i] != 0; i++) {
45 if (strncmp(string_array[i], key, len) == 0) { 62 if (is_prefixed_with(string_array[i], key)) {
46 return i; 63 return i;
47 } 64 }
48 } 65 }
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)
681 setpwent(); 681 setpwent();
682 while ((pw = getpwent()) != NULL) { 682 while ((pw = getpwent()) != NULL) {
683 /* Null usernames should result in all users as possible completions. */ 683 /* Null usernames should result in all users as possible completions. */
684 if (/*!userlen || */ strncmp(ud, pw->pw_name, userlen) == 0) { 684 if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
685 add_match(xasprintf("~%s/", pw->pw_name)); 685 add_match(xasprintf("~%s/", pw->pw_name));
686 } 686 }
687 } 687 }
@@ -792,7 +792,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
792 if (!pfind[0] && DOT_OR_DOTDOT(name_found)) 792 if (!pfind[0] && DOT_OR_DOTDOT(name_found))
793 continue; 793 continue;
794 /* match? */ 794 /* match? */
795 if (strncmp(name_found, pfind, pf_len) != 0) 795 if (!is_prefixed_with(name_found, pfind))
796 continue; /* no */ 796 continue; /* no */
797 797
798 found = concat_path_file(paths[i], name_found); 798 found = concat_path_file(paths[i], name_found);
@@ -1879,15 +1879,16 @@ static void parse_and_put_prompt(const char *prmt_ptr)
1879 cwd_buf = xrealloc_getcwd_or_warn(NULL); 1879 cwd_buf = xrealloc_getcwd_or_warn(NULL);
1880 if (!cwd_buf) 1880 if (!cwd_buf)
1881 cwd_buf = (char *)bb_msg_unknown; 1881 cwd_buf = (char *)bb_msg_unknown;
1882 else { 1882 else if (home_pwd_buf[0]) {
1883 char *after_home_user;
1884
1883 /* /home/user[/something] -> ~[/something] */ 1885 /* /home/user[/something] -> ~[/something] */
1884 l = strlen(home_pwd_buf); 1886 after_home_user = is_prefixed_with(cwd_buf, home_pwd_buf);
1885 if (l != 0 1887 if (after_home_user
1886 && strncmp(home_pwd_buf, cwd_buf, l) == 0 1888 && (*after_home_user == '/' || *after_home_user == '\0')
1887 && (cwd_buf[l] == '/' || cwd_buf[l] == '\0')
1888 ) { 1889 ) {
1889 cwd_buf[0] = '~'; 1890 cwd_buf[0] = '~';
1890 overlapping_strcpy(cwd_buf + 1, cwd_buf + l); 1891 overlapping_strcpy(cwd_buf + 1, after_home_user);
1891 } 1892 }
1892 } 1893 }
1893 } 1894 }
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 @@
17int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype) 17int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
18{ 18{
19 int match = 1; 19 int match = 1;
20 int len;
21 20
22 if (!t_fstype) 21 if (!t_fstype)
23 return match; 22 return match;
@@ -27,10 +26,10 @@ int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
27 t_fstype += 2; 26 t_fstype += 2;
28 } 27 }
29 28
30 len = strlen(mt->mnt_type);
31 while (1) { 29 while (1) {
32 if (strncmp(mt->mnt_type, t_fstype, len) == 0 30 char *after_mnt_type = is_prefixed_with(t_fstype, mt->mnt_type);
33 && (t_fstype[len] == '\0' || t_fstype[len] == ',') 31 if (after_mnt_type
32 && (*after_mnt_type == '\0' || *after_mnt_type == ',')
34 ) { 33 ) {
35 return match; 34 return match;
36 } 35 }
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,
205 // Rss: nnn kB 205 // Rss: nnn kB
206 // ..... 206 // .....
207 207
208 char *tp = buf, *p; 208 char *tp, *p;
209 209
210#define SCAN(S, X) \ 210#define SCAN(S, X) \
211 if (strncmp(tp, S, sizeof(S)-1) == 0) { \ 211 if ((tp = is_prefixed_with(buf, S)) != NULL) { \
212 tp = skip_whitespace(tp + sizeof(S)-1); \ 212 tp = skip_whitespace(tp); \
213 total->X += currec.X = fast_strtoul_10(&tp); \ 213 total->X += currec.X = fast_strtoul_10(&tp); \
214 continue; \ 214 continue; \
215 } 215 }
@@ -247,7 +247,7 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
247 // skipping "rw-s FILEOFS M:m INODE " 247 // skipping "rw-s FILEOFS M:m INODE "
248 tp = skip_whitespace(skip_fields(tp, 4)); 248 tp = skip_whitespace(skip_fields(tp, 4));
249 // filter out /dev/something (something != zero) 249 // filter out /dev/something (something != zero)
250 if (strncmp(tp, "/dev/", 5) != 0 || strcmp(tp, "/dev/zero\n") == 0) { 250 if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
251 if (currec.smap_mode[1] == 'w') { 251 if (currec.smap_mode[1] == 'w') {
252 currec.mapped_rw = currec.smap_size; 252 currec.mapped_rw = currec.smap_size;
253 total->mapped_rw += currec.smap_size; 253 total->mapped_rw += currec.smap_size;
@@ -497,8 +497,8 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
497 while (fgets(buf, sizeof(buf), file)) { 497 while (fgets(buf, sizeof(buf), file)) {
498 char *tp; 498 char *tp;
499#define SCAN_TWO(str, name, statement) \ 499#define SCAN_TWO(str, name, statement) \
500 if (strncmp(buf, str, sizeof(str)-1) == 0) { \ 500 if ((tp = is_prefixed_with(buf, str)) != NULL) { \
501 tp = skip_whitespace(buf + sizeof(str)-1); \ 501 tp = skip_whitespace(tp); \
502 sscanf(tp, "%u", &sp->name); \ 502 sscanf(tp, "%u", &sp->name); \
503 statement; \ 503 statement; \
504 } 504 }
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)
22 char buffer[128]; 22 char buffer[128];
23 23
24 while (fgets(buffer, sizeof(buffer), f)) { 24 while (fgets(buffer, sizeof(buffer), f)) {
25 if (strncmp(buffer, "UTC", 3) == 0) { 25 if (is_prefixed_with(buffer, "UTC")) {
26 utc = 1; 26 utc = 1;
27 break; 27 break;
28 } 28 }
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)
33 33
34char* FAST_FUNC skip_dev_pfx(const char *tty_name) 34char* FAST_FUNC skip_dev_pfx(const char *tty_name)
35{ 35{
36 if (strncmp(tty_name, "/dev/", 5) == 0) 36 if (is_prefixed_with(tty_name, "/dev/"))
37 tty_name += 5; 37 tty_name += 5;
38 return (char*)tty_name; 38 return (char*)tty_name;
39} 39}
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,
84 char *fnamesfx; 84 char *fnamesfx;
85 char *sfx_char; 85 char *sfx_char;
86 char *name_colon; 86 char *name_colon;
87 unsigned user_len;
88 int old_fd; 87 int old_fd;
89 int new_fd; 88 int new_fd;
90 int i; 89 int i;
@@ -108,7 +107,6 @@ int FAST_FUNC update_passwd(const char *filename,
108 fnamesfx = xasprintf("%s+", filename); 107 fnamesfx = xasprintf("%s+", filename);
109 sfx_char = &fnamesfx[strlen(fnamesfx)-1]; 108 sfx_char = &fnamesfx[strlen(fnamesfx)-1];
110 name_colon = xasprintf("%s:", name ? name : ""); 109 name_colon = xasprintf("%s:", name ? name : "");
111 user_len = strlen(name_colon);
112 110
113 if (shadow) 111 if (shadow)
114 old_fp = fopen(filename, "r+"); 112 old_fp = fopen(filename, "r+");
@@ -179,7 +177,7 @@ int FAST_FUNC update_passwd(const char *filename,
179 while (list) { 177 while (list) {
180 list++; 178 list++;
181 next_list_element: 179 next_list_element:
182 if (strncmp(list, member, member_len) == 0) { 180 if (is_prefixed_with(list, member)) {
183 char c; 181 char c;
184 changed_lines++; 182 changed_lines++;
185 c = list[member_len]; 183 c = list[member_len];
@@ -201,13 +199,14 @@ int FAST_FUNC update_passwd(const char *filename,
201 goto next; 199 goto next;
202 } 200 }
203 201
204 if (strncmp(name_colon, line, user_len) != 0) { 202 cp = is_prefixed_with(line, name_colon);
203 if (!cp) {
205 fprintf(new_fp, "%s\n", line); 204 fprintf(new_fp, "%s\n", line);
206 goto next; 205 goto next;
207 } 206 }
208 207
209 /* We have a match with "name:"... */ 208 /* We have a match with "name:"... */
210 cp = line + user_len; /* move past name: */ 209 /* cp points past "name:" */
211 210
212#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP 211#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
213 if (member) { 212 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;)
171 const char *cp; 171 const char *cp;
172 struct addrinfo hint; 172 struct addrinfo hint;
173 173
174 if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) { 174 if (ENABLE_FEATURE_UNIX_LOCAL && is_prefixed_with(host, "local:")) {
175 struct sockaddr_un *sun; 175 struct sockaddr_un *sun;
176 176
177 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un)); 177 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));