getopt32: fix llist_t options ordering. llist_rev is not unused.

function                                             old     new   delta
tar_main                                             705     695     -10
sort_main                                            928     918     -10
decode_format_string                                 886     876     -10
run_parts_main                                       197     185     -12
ps_main                                              513     500     -13
wget_main                                           2764    2750     -14
awk_main                                            1014    1000     -14
od_main                                             2886    2866     -20
llist_rev                                             25       -     -25
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/8 up/down: 0/-128)           Total: -128 bytes
This commit is contained in:
Denis Vlasenko 2007-04-08 15:08:42 +00:00
parent b04b4357ff
commit 8d9f495d68
10 changed files with 53 additions and 55 deletions

View File

@ -864,10 +864,9 @@ int tar_main(int argc, char **argv)
char *cp = last_char_is(argv[optind], '/'); char *cp = last_char_is(argv[optind], '/');
if (cp > argv[optind]) if (cp > argv[optind])
*cp = '\0'; *cp = '\0';
llist_add_to(&tar_handle->accept, argv[optind]); llist_add_to_end(&tar_handle->accept, argv[optind]);
optind++; optind++;
} }
tar_handle->accept = llist_rev(tar_handle->accept);
if (tar_handle->accept || tar_handle->reject) if (tar_handle->accept || tar_handle->reject)
tar_handle->filter = filter_accept_reject_list; tar_handle->filter = filter_accept_reject_list;

View File

@ -1312,7 +1312,6 @@ int od_main(int argc, char **argv)
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");
//if (opt & OPT_t)... //if (opt & OPT_t)...
lst_t = llist_rev(lst_t);
while (lst_t) { while (lst_t) {
decode_format_string(lst_t->data); decode_format_string(lst_t->data);
lst_t = lst_t->link; lst_t = lst_t->link;

View File

@ -296,7 +296,6 @@ int sort_main(int argc, char **argv)
key_separator = str_t[0]; key_separator = str_t[0];
} }
/* parse sort key */ /* parse sort key */
lst_k = llist_rev(lst_k);
while (lst_k) { while (lst_k) {
enum { enum {
FLAG_allowed_for_k = FLAG_allowed_for_k =

View File

@ -131,9 +131,6 @@ int run_parts_main(int argc, char **argv)
*/ */
umask(xstrtoul_range(umask_p, 8, 0, 07777)); umask(xstrtoul_range(umask_p, 8, 0, 07777));
} }
//XXX: FIXME: reverse the list before handing it over to the user.
//XXX: FIXME: The common case seems to be to use the order given by the user
arg_list = llist_rev(arg_list); /* XXX: getopt32 appends them */
for (tmp = 1; arg_list; arg_list = arg_list->link, tmp++) for (tmp = 1; arg_list; arg_list = arg_list->link, tmp++)
G.cmd[tmp] = arg_list->data; G.cmd[tmp] = arg_list->data;
/* G.cmd[tmp] = NULL; - G is already zeroed out */ /* G.cmd[tmp] = NULL; - G is already zeroed out */

View File

@ -2707,7 +2707,6 @@ int awk_main(int argc, char **argv)
argv += optind; argv += optind;
argc -= optind; argc -= optind;
if (opt & 0x1) setvar_s(V[FS], opt_F); // -F if (opt & 0x1) setvar_s(V[FS], opt_F); // -F
opt_v = llist_rev(opt_v);
while (opt_v) { /* -v */ while (opt_v) { /* -v */
if (!is_assignment(llist_pop(&opt_v))) if (!is_assignment(llist_pop(&opt_v)))
bb_show_usage(); bb_show_usage();

View File

@ -294,7 +294,6 @@ extern void sig_pause(void);
#define wait_stopped(w) (((w) & 127) == 127) #define wait_stopped(w) (((w) & 127) == 127)
void xsetgid(gid_t gid); void xsetgid(gid_t gid);
void xsetuid(uid_t uid); void xsetuid(uid_t uid);
void xchdir(const char *path); void xchdir(const char *path);
@ -343,7 +342,7 @@ int xsocket_stream(len_and_sockaddr **lsap);
/* Create server socket bound to bindaddr:port. bindaddr can be NULL, /* Create server socket bound to bindaddr:port. bindaddr can be NULL,
* numeric IP ("N.N.N.N") or numeric IPv6 address, * numeric IP ("N.N.N.N") or numeric IPv6 address,
* and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT").
* If there is no suffix, port argument is used */ * Only if there is no suffix, port argument is used */
int create_and_bind_stream_or_die(const char *bindaddr, int port); int create_and_bind_stream_or_die(const char *bindaddr, int port);
int create_and_bind_dgram_or_die(const char *bindaddr, int port); int create_and_bind_dgram_or_die(const char *bindaddr, int port);
/* Create client TCP socket connected to peer:port. Peer cannot be NULL. /* Create client TCP socket connected to peer:port. Peer cannot be NULL.
@ -456,14 +455,17 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode);
extern FILE *fopen_or_warn_stdin(const char *filename); extern FILE *fopen_or_warn_stdin(const char *filename);
extern char *utoa(unsigned n); char *utoa(unsigned n);
extern char *itoa(int n); char *itoa(int n);
/* Returns a pointer past the formatted number, does NOT null-terminate */ /* Returns a pointer past the formatted number, does NOT null-terminate */
extern char *utoa_to_buf(unsigned n, char *buf, unsigned buflen); char *utoa_to_buf(unsigned n, char *buf, unsigned buflen);
extern char *itoa_to_buf(int n, char *buf, unsigned buflen); char *itoa_to_buf(int n, char *buf, unsigned buflen);
extern void smart_ulltoa5(unsigned long long ul, char buf[5]); void smart_ulltoa5(unsigned long long ul, char buf[5]);
/* Put a string of hex bytes (ala "1b"), return advanced pointer */ //TODO: provide pointer to buf (avoid statics)?
extern char *bin2hex(char *buf, const char *cp, int count); const char *make_human_readable_str(unsigned long long size,
unsigned long block_size, unsigned long display_unit);
/* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */
char *bin2hex(char *buf, const char *cp, int count);
struct suffix_mult { struct suffix_mult {
const char *suffix; const char *suffix;
@ -580,16 +582,20 @@ extern uint32_t option_mask32;
extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...); extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...);
typedef struct llist_s { typedef struct llist_t {
char *data; char *data;
struct llist_s *link; struct llist_t *link;
} llist_t; } llist_t;
extern void llist_add_to(llist_t **old_head, void *data); void llist_add_to(llist_t **old_head, void *data);
extern void llist_add_to_end(llist_t **list_head, void *data); void llist_add_to_end(llist_t **list_head, void *data);
extern void *llist_pop(llist_t **elm); void *llist_pop(llist_t **elm);
extern void llist_unlink(llist_t **head, llist_t *elm); void llist_unlink(llist_t **head, llist_t *elm);
extern void llist_free(llist_t *elm, void (*freeit)(void *data)); void llist_free(llist_t *elm, void (*freeit)(void *data));
extern llist_t* llist_rev(llist_t *list); llist_t *llist_rev(llist_t *list);
/* BTW, surprisingly, changing API to
* llist_t *llist_add_to(llist_t *old_head, void *data)
* etc does not result in smaller code... */
#if ENABLE_FEATURE_PIDFILE #if ENABLE_FEATURE_PIDFILE
int write_pidfile(const char *path); int write_pidfile(const char *path);
@ -674,10 +680,6 @@ extern int del_loop(const char *device);
extern int set_loop(char **device, const char *file, unsigned long long offset); extern int set_loop(char **device, const char *file, unsigned long long offset);
//TODO: provide pointer to buf (avoid statics)?
const char *make_human_readable_str(unsigned long long size,
unsigned long block_size, unsigned long display_unit);
//TODO: pass buf pointer or return allocated buf (avoid statics)? //TODO: pass buf pointer or return allocated buf (avoid statics)?
char *bb_askpass(int timeout, const char * prompt); char *bb_askpass(int timeout, const char * prompt);
int bb_ask_confirmation(void); int bb_ask_confirmation(void);
@ -725,14 +727,14 @@ extern void print_login_issue(const char *issue_file, const char *tty);
extern void print_login_prompt(void); extern void print_login_prompt(void);
extern int get_terminal_width_height(const int fd, int *width, int *height); int get_terminal_width_height(const int fd, int *width, int *height);
char *is_in_ino_dev_hashtable(const struct stat *statbuf); char *is_in_ino_dev_hashtable(const struct stat *statbuf);
void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
void reset_ino_dev_hashtable(void); void reset_ino_dev_hashtable(void);
#ifdef __GLIBC__ #ifdef __GLIBC__
/* At least glibc has horrendously large inline for this, so wrap it */ /* At least glibc has horrendously large inline for this, so wrap it */
extern unsigned long long bb_makedev(unsigned int major, unsigned int minor); unsigned long long bb_makedev(unsigned int major, unsigned int minor);
#undef makedev #undef makedev
#define makedev(a,b) bb_makedev(a,b) #define makedev(a,b) bb_makedev(a,b)
#endif #endif
@ -745,7 +747,7 @@ extern unsigned long long bb_makedev(unsigned int major, unsigned int minor);
#else #else
#define MAX_HISTORY 0 #define MAX_HISTORY 0
#endif #endif
struct line_input_t { typedef struct line_input_t {
int flags; int flags;
const char *path_lookup; const char *path_lookup;
#if MAX_HISTORY #if MAX_HISTORY
@ -754,7 +756,7 @@ struct line_input_t {
USE_FEATURE_EDITING_SAVEHISTORY(const char *hist_file;) USE_FEATURE_EDITING_SAVEHISTORY(const char *hist_file;)
char *history[MAX_HISTORY + 1]; char *history[MAX_HISTORY + 1];
#endif #endif
}; } line_input_t;
enum { enum {
DO_HISTORY = 1 * (MAX_HISTORY > 0), DO_HISTORY = 1 * (MAX_HISTORY > 0),
SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY, SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY,
@ -764,7 +766,6 @@ enum {
WITH_PATH_LOOKUP = 0x20, WITH_PATH_LOOKUP = 0x20,
FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION, FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
}; };
typedef struct line_input_t line_input_t;
line_input_t *new_line_input_t(int flags); line_input_t *new_line_input_t(int flags);
int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state); int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state);
#else #else
@ -798,7 +799,7 @@ typedef struct {
/* sizeof(task_struct.comm) in /usr/include/linux/sched.h */ /* sizeof(task_struct.comm) in /usr/include/linux/sched.h */
char state[4]; char state[4];
char comm[COMM_LEN]; char comm[COMM_LEN];
// user/group? - use passwd/group parsing functions /* user/group? - use passwd/group parsing functions */
} procps_status_t; } procps_status_t;
enum { enum {
PSSCAN_PID = 1 << 0, PSSCAN_PID = 1 << 0,
@ -902,18 +903,22 @@ extern const char bb_dev_null[];
extern const int const_int_0; extern const int const_int_0;
extern const int const_int_1; extern const int const_int_1;
#ifndef BUFSIZ #ifndef BUFSIZ
#define BUFSIZ 4096 #define BUFSIZ 4096
#endif #endif
// TODO: provide hard guarantees on minimum size of bb_common_bufsiz1
extern char bb_common_bufsiz1[BUFSIZ+1]; extern char bb_common_bufsiz1[BUFSIZ+1];
/* This struct is deliberately not defined. */ /* This struct is deliberately not defined. */
/* See docs/keep_data_small.txt */ /* See docs/keep_data_small.txt */
struct globals; struct globals;
/* Magic prevents this from going into rodata */ /* '*const' ptr makes gcc optimize code much better.
/* If you want to assign a value, use PTR_TO_GLOBALS = xxx */ * Magic prevents ptr_to_globals from going into rodata
* If you want to assign a value, use PTR_TO_GLOBALS = xxx */
extern struct globals *const ptr_to_globals; extern struct globals *const ptr_to_globals;
#define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals) #define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals)
/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it, /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
* use bb_default_login_shell and following defines. * use bb_default_login_shell and following defines.
* If you change LIBBB_DEFAULT_LOGIN_SHELL, * If you change LIBBB_DEFAULT_LOGIN_SHELL,

View File

@ -242,7 +242,7 @@ Special characters:
llist_t *patterns = NULL; llist_t *patterns = NULL;
(this pointer must be initializated to NULL if the list is empty (this pointer must be initializated to NULL if the list is empty
as required by *llist_add_to(llist_t *old_head, char *new_item).) as required by llist_add_to_end(llist_t **old_head, char *new_item).)
opt_complementary = "e::"; opt_complementary = "e::";
@ -487,7 +487,7 @@ getopt32(int argc, char **argv, const char *applet_opts, ...)
if (on_off->counter) if (on_off->counter)
(*(on_off->counter))++; (*(on_off->counter))++;
if (on_off->list_flg) { if (on_off->list_flg) {
llist_add_to((llist_t **)(on_off->optarg), optarg); llist_add_to_end((llist_t **)(on_off->optarg), optarg);
} else if (on_off->optarg) { } else if (on_off->optarg) {
*(char **)(on_off->optarg) = optarg; *(char **)(on_off->optarg) = optarg;
} }

View File

@ -91,18 +91,19 @@ void llist_free(llist_t * elm, void (*freeit) (void *data))
} }
} }
/* Reverse list order. Useful since getopt32 saves option params #ifdef UNUSED
* in reverse order */ /* Reverse list order. */
llist_t *llist_rev(llist_t *list) llist_t *llist_rev(llist_t *list)
{ {
llist_t *new = NULL; llist_t *rev = NULL;
while (list) { while (list) {
llist_t *next = list->link; llist_t *next = list->link;
list->link = new; list->link = rev;
new = list; rev = list;
list = next; list = next;
} }
return new; return rev;
} }
#endif

View File

@ -159,7 +159,7 @@ int wget_main(int argc, char **argv)
if (headers_llist) { if (headers_llist) {
int size = 1; int size = 1;
char *cp; char *cp;
llist_t *ll = headers_llist = llist_rev(headers_llist); llist_t *ll = headers_llist;
while (ll) { while (ll) {
size += strlen(ll->data) + 2; size += strlen(ll->data) + 2;
ll = ll->link; ll = ll->link;

View File

@ -253,7 +253,6 @@ int ps_main(int argc, char **argv)
opt_complementary = "o::"; opt_complementary = "o::";
getopt32(argc, argv, "o:aAdefl", &opt_o); getopt32(argc, argv, "o:aAdefl", &opt_o);
if (opt_o) { if (opt_o) {
opt_o = llist_rev(opt_o);
do { do {
parse_o(opt_o->data); parse_o(opt_o->data);
opt_o = opt_o->link; opt_o = opt_o->link;