Commonalize typical [b,]k,m suffix struct

function                                             old     new   delta
bkm_suffixes                                           -      32     +32
static.km_suffixes                                    24       -     -24
suffixes                                              32       -     -32
static.bkm                                            32       -     -32
head_tail_suffixes                                    32       -     -32
------------------------------------------------------------------------------
(add/remove: 2/6 grow/shrink: 0/0 up/down: 72/-160)           Total: -88 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-07-13 23:49:45 +02:00
parent 0dacb68e1a
commit c72b43c2f0
11 changed files with 22 additions and 58 deletions

5
TODO
View File

@ -132,11 +132,6 @@ stty / catv
stty's visible() function and catv's guts are identical. Merge them into stty's visible() function and catv's guts are identical. Merge them into
an appropriate libbb function. an appropriate libbb function.
--- ---
struct suffix_mult
Several duplicate users of: grep -r "1024\*1024" * -B2 -A1
Merge to a single size_suffixes[] in libbb.
Users: head tail od_bloaty hexdump and (partially as it wouldn't hurt) svlogd
---
tail tail
./busybox tail -f foo.c~ TODO ./busybox tail -f foo.c~ TODO
should not print fmt=header_fmt for subsequent date >> TODO; i.e. only should not print fmt=header_fmt for subsequent date >> TODO; i.e. only

View File

@ -12,7 +12,6 @@
/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */ /* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
//kbuild:lib-$(CONFIG_HEAD) += head.o //kbuild:lib-$(CONFIG_HEAD) += head.o
//kbuild:lib-$(CONFIG_HEAD) += head_tail.o
//usage:#define head_trivial_usage //usage:#define head_trivial_usage
//usage: "[OPTIONS] [FILE]..." //usage: "[OPTIONS] [FILE]..."
@ -35,7 +34,6 @@
//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n" //usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
#include "libbb.h" #include "libbb.h"
#include "head_tail.h"
/* This is a NOEXEC applet. Be very careful! */ /* This is a NOEXEC applet. Be very careful! */
@ -140,7 +138,7 @@ eat_num(bool *negative_N, const char *p)
p++; p++;
} }
#endif #endif
return xatoul_sfx(p, head_tail_suffixes); return xatoul_sfx(p, bkm_suffixes);
} }
static const char head_opts[] ALIGN1 = static const char head_opts[] ALIGN1 =

View File

@ -1,14 +0,0 @@
/*
* Copyright (C) 2013 Denys Vlasenko
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
#include "libbb.h"
#include "head_tail.h"
const struct suffix_mult head_tail_suffixes[] = {
{ "b", 512 },
{ "k", 1024 },
{ "m", 1024*1024 },
{ "", 0 }
};

View File

@ -1,6 +0,0 @@
/*
* Copyright (C) 2013 Denys Vlasenko
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
extern const struct suffix_mult head_tail_suffixes[];

View File

@ -1166,12 +1166,6 @@ parse_old_offset(const char *s, off_t *offset)
int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int od_main(int argc UNUSED_PARAM, char **argv) int od_main(int argc UNUSED_PARAM, char **argv)
{ {
static const struct suffix_mult bkm[] = {
{ "b", 512 },
{ "k", 1024 },
{ "m", 1024*1024 },
{ "", 0 }
};
#if ENABLE_LONG_OPTS #if ENABLE_LONG_OPTS
static const char od_longopts[] ALIGN1 = static const char od_longopts[] ALIGN1 =
"skip-bytes\0" Required_argument "j" "skip-bytes\0" Required_argument "j"
@ -1230,7 +1224,7 @@ int od_main(int argc UNUSED_PARAM, char **argv)
address_pad_len_char = doxn_address_pad_len_char[pos]; address_pad_len_char = doxn_address_pad_len_char[pos];
} }
if (opt & OPT_N) { if (opt & OPT_N) {
max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm); max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm_suffixes);
} }
if (opt & OPT_a) decode_format_string("a"); if (opt & OPT_a) decode_format_string("a");
if (opt & OPT_b) decode_format_string("oC"); if (opt & OPT_b) decode_format_string("oC");
@ -1239,7 +1233,7 @@ int od_main(int argc UNUSED_PARAM, char **argv)
if (opt & OPT_f) decode_format_string("fF"); if (opt & OPT_f) decode_format_string("fF");
if (opt & OPT_h) decode_format_string("x2"); if (opt & OPT_h) decode_format_string("x2");
if (opt & OPT_i) decode_format_string("d2"); if (opt & OPT_i) decode_format_string("d2");
if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm); if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm_suffixes);
if (opt & OPT_l) decode_format_string("d4"); if (opt & OPT_l) decode_format_string("d4");
if (opt & OPT_o) decode_format_string("o2"); if (opt & OPT_o) decode_format_string("o2");
while (lst_t) { while (lst_t) {
@ -1248,7 +1242,7 @@ int od_main(int argc UNUSED_PARAM, char **argv)
if (opt & OPT_x) decode_format_string("x2"); if (opt & OPT_x) decode_format_string("x2");
if (opt & OPT_s) decode_format_string("d2"); if (opt & OPT_s) decode_format_string("d2");
if (opt & OPT_S) { if (opt & OPT_S) {
string_min = xstrtou_sfx(str_S, 0, bkm); string_min = xstrtou_sfx(str_S, 0, bkm_suffixes);
} }
// Bloat: // Bloat:

View File

@ -23,17 +23,15 @@
#include "libbb.h" #include "libbb.h"
static const struct suffix_mult split_suffices[] = {
#if ENABLE_FEATURE_SPLIT_FANCY #if ENABLE_FEATURE_SPLIT_FANCY
static const struct suffix_mult split_suffixes[] = {
{ "b", 512 }, { "b", 512 },
#endif
{ "k", 1024 }, { "k", 1024 },
{ "m", 1024*1024 }, { "m", 1024*1024 },
#if ENABLE_FEATURE_SPLIT_FANCY
{ "g", 1024*1024*1024 }, { "g", 1024*1024*1024 },
#endif
{ "", 0 } { "", 0 }
}; };
#endif
/* Increment the suffix part of the filename. /* Increment the suffix part of the filename.
* Returns NULL if we are out of filenames. * Returns NULL if we are out of filenames.
@ -86,7 +84,10 @@ int split_main(int argc UNUSED_PARAM, char **argv)
if (opt & SPLIT_OPT_l) if (opt & SPLIT_OPT_l)
cnt = XATOOFF(count_p); cnt = XATOOFF(count_p);
if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
cnt = xatoull_sfx(count_p, split_suffices); cnt = xatoull_sfx(count_p,
IF_FEATURE_SPLIT_FANCY(split_suffixes)
IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
);
sfx = "x"; sfx = "x";
argv += optind; argv += optind;

View File

@ -25,7 +25,6 @@
*/ */
//kbuild:lib-$(CONFIG_TAIL) += tail.o //kbuild:lib-$(CONFIG_TAIL) += tail.o
//kbuild:lib-$(CONFIG_TAIL) += head_tail.o
//usage:#define tail_trivial_usage //usage:#define tail_trivial_usage
//usage: "[OPTIONS] [FILE]..." //usage: "[OPTIONS] [FILE]..."
@ -51,7 +50,6 @@
//usage: "nameserver 10.0.0.1\n" //usage: "nameserver 10.0.0.1\n"
#include "libbb.h" #include "libbb.h"
#include "head_tail.h"
struct globals { struct globals {
bool from_top; bool from_top;
@ -89,7 +87,7 @@ static unsigned eat_num(const char *p)
p++; p++;
G.from_top = 1; G.from_top = 1;
} }
return xatou_sfx(p, head_tail_suffixes); return xatou_sfx(p, bkm_suffixes);
} }
int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;

View File

@ -832,6 +832,9 @@ struct suffix_mult {
char suffix[4]; char suffix[4];
unsigned mult; unsigned mult;
}; };
extern const struct suffix_mult bkm_suffixes[];
#define km_suffixes (bkm_suffixes + 1)
#include "xatonum.h" #include "xatonum.h"
/* Specialized: */ /* Specialized: */

View File

@ -68,3 +68,10 @@ uint16_t FAST_FUNC xatou16(const char *numstr)
{ {
return xatou_range(numstr, 0, 0xffff); return xatou_range(numstr, 0, 0xffff);
} }
const struct suffix_mult bkm_suffixes[] = {
{ "b", 512 },
{ "k", 1024 },
{ "m", 1024*1024 },
{ "", 0 }
};

View File

@ -745,11 +745,6 @@ static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
ld->inst = new; ld->inst = new;
break; break;
case 's': { case 's': {
static const struct suffix_mult km_suffixes[] = {
{ "k", 1024 },
{ "m", 1024*1024 },
{ "", 0 }
};
ld->sizemax = xatou_sfx(&s[1], km_suffixes); ld->sizemax = xatou_sfx(&s[1], km_suffixes);
break; break;
} }

View File

@ -66,13 +66,6 @@ static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R"); static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
static const struct suffix_mult suffixes[] = {
{ "b", 512 },
{ "k", 1024 },
{ "m", 1024*1024 },
{ "", 0 }
};
int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int hexdump_main(int argc, char **argv) int hexdump_main(int argc, char **argv)
{ {
@ -120,7 +113,7 @@ int hexdump_main(int argc, char **argv)
optarg, optarg,
/*base:*/ 0, /*base:*/ 0,
/*lo:*/ 0, /*hi:*/ OFF_T_MAX, /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
suffixes bkm_suffixes
); );
} /* else */ } /* else */
if (ch == 'v') { if (ch == 'v') {