From ea8b252cb30c4b4463df43a5342af95931920f09 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 2 Jun 2010 12:57:26 +0200 Subject: *: better string sharing text data bss dec hex filename 849427 441 7556 857424 d1550 busybox_old 849355 441 7556 857352 d1508 busybox_unstripped Signed-off-by: Denys Vlasenko --- archival/bzip2.c | 4 ++-- archival/libunarchive/decompress_unxz.c | 2 +- archival/libunarchive/decompress_unzip.c | 2 +- coreutils/head.c | 2 +- coreutils/od_bloaty.c | 2 +- coreutils/tail.c | 8 ++++---- miscutils/less.c | 2 +- modutils/modutils-24.c | 6 ++---- shell/ash.c | 14 ++++++-------- util-linux/fdisk.c | 6 +++--- 10 files changed, 22 insertions(+), 26 deletions(-) diff --git a/archival/bzip2.c b/archival/bzip2.c index f1c84d681..fdb8b9306 100644 --- a/archival/bzip2.c +++ b/archival/bzip2.c @@ -88,7 +88,7 @@ IF_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, vo if (n2 != n) { if (n2 >= 0) errno = 0; /* prevent bogus error message */ - bb_perror_msg(n2 >= 0 ? "short write" : "write error"); + bb_perror_msg(n2 >= 0 ? "short write" : bb_msg_write_error); return -1; } } @@ -118,7 +118,7 @@ IF_DESKTOP(long long) int FAST_FUNC compressStream(unpack_info_t *info UNUSED_PA while (1) { count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE); if (count < 0) { - bb_perror_msg("read error"); + bb_perror_msg(bb_msg_read_error); total = -1; break; } diff --git a/archival/libunarchive/decompress_unxz.c b/archival/libunarchive/decompress_unxz.c index 3f9392984..1302e29fb 100644 --- a/archival/libunarchive/decompress_unxz.c +++ b/archival/libunarchive/decompress_unxz.c @@ -86,7 +86,7 @@ unpack_xz_stream(int src_fd, int dst_fd) if (rd) { rd = safe_read(src_fd, membuf + insz, rd); if (rd < 0) { - bb_error_msg("read error"); + bb_error_msg(bb_msg_read_error); total = -1; break; } diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index 33e877ec8..bccd0262e 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c @@ -1072,7 +1072,7 @@ static int top_up(STATE_PARAM unsigned n) bytebuffer_offset = 0; bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count); if ((int)bytebuffer_size < 0) { - bb_error_msg("read error"); + bb_error_msg(bb_msg_read_error); return 0; } bytebuffer_size += count; diff --git a/coreutils/head.c b/coreutils/head.c index 75189eda8..cc2837411 100644 --- a/coreutils/head.c +++ b/coreutils/head.c @@ -27,7 +27,7 @@ static const struct suffix_mult head_suffixes[] = { { "", 0 } }; -static const char header_fmt_str[] ALIGN1 = "\n==> %s <==\n"; +#define header_fmt_str "\n==> %s <==\n" int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int head_main(int argc, char **argv) diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index 6aba0f6bb..a9a45c8d3 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -502,7 +502,7 @@ check_and_close(void) } if (ferror(stdout)) { - bb_error_msg("write error"); + bb_error_msg(bb_msg_write_error); ioerror = 1; } } diff --git a/coreutils/tail.c b/coreutils/tail.c index 8fc22f562..6397702a0 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -66,7 +66,7 @@ static ssize_t tail_read(int fd, char *buf, size_t count) return r; } -static const char header_fmt[] ALIGN1 = "\n==> %s <==\n"; +#define header_fmt_str "\n==> %s <==\n" static unsigned eat_num(const char *p) { @@ -166,7 +166,7 @@ int tail_main(int argc, char **argv) tailbuf = xmalloc(tailbufsize); /* tail the files */ - fmt = header_fmt + 1; /* skip header leading newline on first output */ + fmt = header_fmt_str + 1; /* skip header leading newline on first output */ i = 0; do { char *buf; @@ -181,7 +181,7 @@ int tail_main(int argc, char **argv) if (nfiles > header_threshhold) { tail_xprint_header(fmt, argv[i]); - fmt = header_fmt; + fmt = header_fmt_str; } if (!from_top) { @@ -333,7 +333,7 @@ int tail_main(int argc, char **argv) if (ENABLE_FEATURE_FANCY_TAIL && fd < 0) continue; if (nfiles > header_threshhold) { - fmt = header_fmt; + fmt = header_fmt_str; } while ((nread = tail_read(fd, tailbuf, BUFSIZ)) > 0) { if (fmt) { diff --git a/miscutils/less.c b/miscutils/less.c index 848266212..da2cd07af 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -430,7 +430,7 @@ static void read_lines(void) * immediately */ eof_error = 1; } else { - print_statusline("read error"); + print_statusline(bb_msg_read_error); } } #if !ENABLE_FEATURE_LESS_REGEXP diff --git a/modutils/modutils-24.c b/modutils/modutils-24.c index 5c1611c29..7f39e253d 100644 --- a/modutils/modutils-24.c +++ b/modutils/modutils-24.c @@ -3529,20 +3529,18 @@ static void set_tainted(int fd, const char *m_name, /* Check if loading this module will taint the kernel. */ static void check_tainted_module(struct obj_file *f, const char *m_name) { - static const char tainted_file[] ALIGN1 = TAINT_FILENAME; - int fd, kernel_has_tainted; const char *ptr; kernel_has_tainted = 1; - fd = open(tainted_file, O_RDWR); + fd = open(TAINT_FILENAME, O_RDWR); if (fd < 0) { if (errno == ENOENT) kernel_has_tainted = 0; else if (errno == EACCES) kernel_has_tainted = 1; else { - perror(tainted_file); + perror(TAINT_FILENAME); kernel_has_tainted = 0; } } diff --git a/shell/ash.c b/shell/ash.c index d77d9d3c2..08ad0f451 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -119,9 +119,7 @@ enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; /* ============ Misc data */ -static const char homestr[] ALIGN1 = "HOME"; -static const char snlfmt[] ALIGN1 = "%s\n"; -static const char msg_illnum[] ALIGN1 = "Illegal number: %s"; +#define msg_illnum "Illegal number: %s" /* * We enclose jmp_buf in a structure so that we can declare pointers to @@ -2520,7 +2518,7 @@ cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) flags = cdopt(); dest = *argptr; if (!dest) - dest = bltinlookup(homestr); + dest = bltinlookup("HOME"); else if (LONE_DASH(dest)) { dest = bltinlookup("OLDPWD"); flags |= CD_PRINT; @@ -2567,7 +2565,7 @@ cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) /* NOTREACHED */ out: if (flags & CD_PRINT) - out1fmt(snlfmt, curdir); + out1fmt("%s\n", curdir); return 0; } @@ -2583,7 +2581,7 @@ pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) setpwd(dir, 0); dir = physdir; } - out1fmt(snlfmt, dir); + out1fmt("%s\n", dir); return 0; } @@ -5625,7 +5623,7 @@ exptilde(char *startp, char *p, int flags) done: *p = '\0'; if (*name == '\0') { - home = lookupvar(homestr); + home = lookupvar("HOME"); } else { pw = getpwnam(name); if (pw == NULL) @@ -9857,7 +9855,7 @@ chkmail(void) } if (!mail_var_path_changed && statb.st_mtime != *mtp) { fprintf( - stderr, snlfmt, + stderr, "%s\n", pathopt ? pathopt : "you have mail" ); } diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index 8e133178d..7227a829e 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c @@ -107,9 +107,9 @@ struct partition { unsigned char size4[4]; /* nr of sectors in partition */ } PACKED; -static const char unable_to_open[] ALIGN1 = "can't open '%s'"; -static const char unable_to_read[] ALIGN1 = "can't read from %s"; -static const char unable_to_seek[] ALIGN1 = "can't seek on %s"; +#define unable_to_open "can't open '%s'" +#define unable_to_read "can't read from %s" +#define unable_to_seek "can't seek on %s" enum label_type { LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF -- cgit v1.2.3-55-g6feb From abb154ba02f9a9a98465cb1f816f974cca536392 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 2 Jun 2010 13:28:17 +0200 Subject: document how to find non-shared string dups Signed-off-by: Denys Vlasenko --- docs/keep_data_small.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/keep_data_small.txt b/docs/keep_data_small.txt index 079bac3a6..01c0d3c7b 100644 --- a/docs/keep_data_small.txt +++ b/docs/keep_data_small.txt @@ -145,6 +145,11 @@ one of above methods is not worth the resulting code obfuscation. If you have less than ~300 bytes of global data - don't bother. + Finding non-shared duplicated strings + +strings busybox | sort | uniq -c | sort -nr + + gcc's data alignment problem The following attribute added in vi.c: -- cgit v1.2.3-55-g6feb From 26b6ccf340243ec3acd594c6928b18e95d3deea9 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 2 Jun 2010 14:14:48 +0200 Subject: *: simplify checks for gz/bz2/xz magic function old new delta send_tree 360 355 -5 rpm2cpio_main 249 220 -29 setup_unzip_on_fd 150 118 -32 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-66) Total: -66 bytes Signed-off-by: Denys Vlasenko --- archival/rpm2cpio.c | 19 ++++++++++++------- include/unarchive.h | 16 ++++++++++++++++ libbb/read.c | 21 +++++++++++++-------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/archival/rpm2cpio.c b/archival/rpm2cpio.c index 598ec8670..1c67dcc6e 100644 --- a/archival/rpm2cpio.c +++ b/archival/rpm2cpio.c @@ -68,26 +68,31 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv) #else /* BLOAT */ { - unsigned char magic[8]; + union { + uint8_t b[4]; + uint16_t b16[2]; + uint32_t b32[1]; + } magic; IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd); - xread(rpm_fd, &magic, 2); - if (magic[0] == 0x1f && magic[1] == 0x8b) { + xread(rpm_fd, magic.b16, sizeof(magic.b16)); + if (magic.b16[0] == GZIP_MAGIC) { unpack = unpack_gz_stream; } else if (ENABLE_FEATURE_SEAMLESS_BZ2 - && magic[0] == 'B' && magic[1] == 'Z' + && magic.b16[0] == BZIP2_MAGIC ) { unpack = unpack_bz2_stream; } else if (ENABLE_FEATURE_SEAMLESS_XZ - && magic[0] == 0xfd && magic[1] == '7' + && magic.b16[0] == XZ_MAGIC1 ) { /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */ /* More info at: http://tukaani.org/xz/xz-file-format.txt */ - xread(rpm_fd, magic + 2, 4); - if (strcmp((char*)magic + 2, "zXZ") != 0) + xread(rpm_fd, magic.b32, sizeof(magic.b32)); + if (magic.b32[0] != XZ_MAGIC2) goto no_magic; + /* unpack_xz_stream wants fd at position 0 */ xlseek(rpm_fd, -6, SEEK_CUR); unpack = unpack_xz_stream; } else { diff --git a/include/unarchive.h b/include/unarchive.h index aa7ecec55..8009de282 100644 --- a/include/unarchive.h +++ b/include/unarchive.h @@ -4,6 +4,22 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN +enum { +#if BB_BIG_ENDIAN + COMPRESS_MAGIC = 0x1f9d, + GZIP_MAGIC = 0x1f8b, + BZIP2_MAGIC = ('B'<<8) + 'Z', + XZ_MAGIC1 = (0xfd<<8) + '7', + XZ_MAGIC2 = ((((('z'<<8) + 'X')<<8) + 'Z')<<8) + 0, +#else + COMPRESS_MAGIC = 0x9d1f, + GZIP_MAGIC = 0x8b1f, + BZIP2_MAGIC = ('Z'<<8) + 'B', + XZ_MAGIC1 = ('7'<<8) + 0xfd, + XZ_MAGIC2 = (((((0<<8) + 'Z')<<8) + 'X')<<8) + 'z', +#endif +}; + typedef struct file_header_t { char *name; char *link_target; diff --git a/libbb/read.c b/libbb/read.c index cd6bbeb13..b1eb3f24b 100644 --- a/libbb/read.c +++ b/libbb/read.c @@ -312,7 +312,11 @@ void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) { const int fail_if_not_detected = 1; - unsigned char magic[8]; + union { + uint8_t b[4]; + uint16_t b16[2]; + uint32_t b32[1]; + } magic; int offset = -2; # if BB_MMU IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); @@ -324,9 +328,9 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) /* .gz and .bz2 both have 2-byte signature, and their * unpack_XXX_stream wants this header skipped. */ - xread(fd, magic, 2); + xread(fd, magic.b16, sizeof(magic.b16)); if (ENABLE_FEATURE_SEAMLESS_GZ - && magic[0] == 0x1f && magic[1] == 0x8b + && magic.b16[0] == GZIP_MAGIC ) { # if BB_MMU xformer = unpack_gz_stream; @@ -336,7 +340,7 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) goto found_magic; } if (ENABLE_FEATURE_SEAMLESS_BZ2 - && magic[0] == 'B' && magic[1] == 'Z' + && magic.b16[0] == BZIP2_MAGIC ) { # if BB_MMU xformer = unpack_bz2_stream; @@ -346,19 +350,20 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) goto found_magic; } if (ENABLE_FEATURE_SEAMLESS_XZ - && magic[0] == 0xfd && magic[1] == '7' + && magic.b16[0] == XZ_MAGIC1 ) { /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */ /* More info at: http://tukaani.org/xz/xz-file-format.txt */ offset = -6; - xread(fd, magic + 2, 4); - if (strcmp((char*)magic + 2, "zXZ") == 0) { + xread(fd, magic.b32, sizeof(magic.b32)); + if (magic.b32[0] == XZ_MAGIC2) { # if BB_MMU xformer = unpack_xz_stream; + /* unpack_xz_stream wants fd at position 0 */ + xlseek(fd, offset, SEEK_CUR); # else xformer_prog = "unxz"; # endif - xlseek(fd, offset, SEEK_CUR); goto found_magic; } } -- cgit v1.2.3-55-g6feb From 3e47cfec90fbe358692b3b960f7fa2303e465c2f Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 3 Jun 2010 01:47:04 +0200 Subject: test: "" is not a valid number. Closes bug 1915 Signed-off-by: Denys Vlasenko --- coreutils/test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coreutils/test.c b/coreutils/test.c index 5864c7f32..a1d164574 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -393,7 +393,7 @@ static number_t getn(const char *s) if (errno != 0) syntax(s, "out of range"); - if (*(skip_whitespace(p))) + if (p == s || *(skip_whitespace(p)) != '\0') syntax(s, "bad number"); return r; -- cgit v1.2.3-55-g6feb