Replace index_in_[sub]str_array with index_in_[sub]strings,

which scans thru "abc\0def\0123\0\0" type strings. Saves 250 bytes.

   text    data     bss     dec     hex filename
 781266    1328   11844  794438   c1f46 busybox_old
 781010    1328   11844  794182   c1e46 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-07-24 15:54:42 +00:00
parent bcb66ec22e
commit 990d0f63ee
43 changed files with 352 additions and 340 deletions

View File

@ -582,10 +582,9 @@ static int read_package_field(const char *package_buffer, char **field_name, cha
static unsigned fill_package_struct(char *control_buffer) static unsigned fill_package_struct(char *control_buffer)
{ {
static const char *const field_names[] = { "Package", "Version", static const char field_names[] = "Package\0""Version\0"
"Pre-Depends", "Depends","Replaces", "Provides", "Pre-Depends\0""Depends\0""Replaces\0""Provides\0"
"Conflicts", "Suggests", "Recommends", "Enhances", NULL "Conflicts\0""Suggests\0""Recommends\0""Enhances\0";
};
common_node_t *new_node = xzalloc(sizeof(common_node_t)); common_node_t *new_node = xzalloc(sizeof(common_node_t));
char *field_name; char *field_name;
@ -602,10 +601,10 @@ static unsigned fill_package_struct(char *control_buffer)
&field_name, &field_value); &field_name, &field_value);
if (field_name == NULL) { if (field_name == NULL) {
goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement ! */ goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement! */
} }
field_num = index_in_str_array(field_names, field_name); field_num = index_in_strings(field_names, field_name);
switch (field_num) { switch (field_num) {
case 0: /* Package */ case 0: /* Package */
new_node->name = search_name_hashtable(field_value); new_node->name = search_name_hashtable(field_value);

View File

@ -752,7 +752,7 @@ static const char tar_longopts[] =
# if ENABLE_FEATURE_TAR_FROM # if ENABLE_FEATURE_TAR_FROM
"exclude\0" Required_argument "\xff" "exclude\0" Required_argument "\xff"
# endif # endif
"\0"; ;
#endif #endif
int tar_main(int argc, char **argv); int tar_main(int argc, char **argv);

View File

@ -13,7 +13,7 @@
#if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS #if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
static const char setconsole_longopts[] = static const char setconsole_longopts[] =
"reset\0" No_argument "r" "reset\0" No_argument "r"
"\0"; ;
#endif #endif
#define OPT_SETCONS_RESET 1 #define OPT_SETCONS_RESET 1

View File

@ -51,7 +51,7 @@ static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
} }
static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs, static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
const char * const filename) const char *filename)
{ {
ssize_t n = full_write_or_warn(fd, buf, len, filename); ssize_t n = full_write_or_warn(fd, buf, len, filename);
if (n < 0) if (n < 0)
@ -78,13 +78,12 @@ int dd_main(int argc, char **argv)
TRUNC_FLAG = 1 << 2, TRUNC_FLAG = 1 << 2,
TWOBUFS_FLAG = 1 << 3, TWOBUFS_FLAG = 1 << 3,
}; };
static const char * const keywords[] = { static const char keywords[] =
"bs=", "count=", "seek=", "skip=", "if=", "of=", "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
#if ENABLE_FEATURE_DD_IBS_OBS #if ENABLE_FEATURE_DD_IBS_OBS
"ibs=", "obs=", "conv=", "notrunc", "sync", "noerror", "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
#endif #endif
NULL ;
};
enum { enum {
OP_bs = 1, OP_bs = 1,
OP_count, OP_count,
@ -134,7 +133,7 @@ int dd_main(int argc, char **argv)
bb_show_usage(); bb_show_usage();
key_len = key - arg + 1; key_len = key - arg + 1;
key = xstrndup(arg, key_len); key = xstrndup(arg, key_len);
what = index_in_str_array(keywords, key) + 1; what = index_in_strings(keywords, key) + 1;
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
free(key); free(key);
if (what == 0) if (what == 0)
@ -153,13 +152,13 @@ int dd_main(int argc, char **argv)
if (what == OP_conv) { if (what == OP_conv) {
while (1) { while (1) {
/* find ',', replace them with nil so we can use arg for /* find ',', replace them with nil so we can use arg for
* index_in_str_array without copying. * index_in_strings() without copying.
* We rely on arg being non-null, else strchr would fault. * We rely on arg being non-null, else strchr would fault.
*/ */
key = strchr(arg, ','); key = strchr(arg, ',');
if (key) if (key)
*key = '\0'; *key = '\0';
what = index_in_str_array(keywords, arg) + 1; what = index_in_strings(keywords, arg) + 1;
if (what < OP_conv_notrunc) if (what < OP_conv_notrunc)
bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv"); bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
if (what == OP_conv_notrunc) if (what == OP_conv_notrunc)
@ -298,15 +297,15 @@ int dd_main(int argc, char **argv)
G.out_part++; G.out_part++;
} }
if (close(ifd) < 0) { if (close(ifd) < 0) {
die_infile: die_infile:
bb_perror_msg_and_die("%s", infile); bb_perror_msg_and_die("%s", infile);
} }
if (close(ofd) < 0) { if (close(ofd) < 0) {
die_outfile: die_outfile:
bb_perror_msg_and_die("%s", outfile); bb_perror_msg_and_die("%s", outfile);
} }
out_status: out_status:
dd_output_status(0); dd_output_status(0);
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -38,7 +38,7 @@ extern char **environ;
static const char env_longopts[] = static const char env_longopts[] =
"ignore-environment\0" No_argument "i" "ignore-environment\0" No_argument "i"
"unset\0" Required_argument "u" "unset\0" Required_argument "u"
"\0"; ;
#endif #endif
int env_main(int argc, char** argv); int env_main(int argc, char** argv);

View File

@ -277,14 +277,13 @@ static VALUE *eval7(void)
static VALUE *eval6(void) static VALUE *eval6(void)
{ {
static const char * const keywords[] = { static const char keywords[] =
"quote", "length", "match", "index", "substr", NULL "quote\0""length\0""match\0""index\0""substr\0";
};
VALUE *r, *i1, *i2; VALUE *r, *i1, *i2;
VALUE *l = l; /* silence gcc */ VALUE *l = l; /* silence gcc */
VALUE *v = v; /* silence gcc */ VALUE *v = v; /* silence gcc */
int key = *G.args ? index_in_str_array(keywords, *G.args) + 1 : 0; int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
if (key == 0) /* not a keyword */ if (key == 0) /* not a keyword */
return eval7(); return eval7();

View File

@ -28,7 +28,7 @@ static const char install_longopts[] =
"preserve_context\0" No_argument "\xff" "preserve_context\0" No_argument "\xff"
"preserve-context\0" No_argument "\xff" "preserve-context\0" No_argument "\xff"
#endif #endif
"\0"; ;
#endif #endif

View File

@ -122,7 +122,7 @@ static smallint show_color;
* equivalent */ * equivalent */
static const char ls_color_opt[] = static const char ls_color_opt[] =
"color\0" Optional_argument "\xff" /* no short equivalent */ "color\0" Optional_argument "\xff" /* no short equivalent */
"\0"; ;
#else #else
enum { show_color = 0 }; enum { show_color = 0 };
#endif #endif

View File

@ -31,7 +31,7 @@ static const char mkdir_longopts[] =
#if ENABLE_SELINUX #if ENABLE_SELINUX
"context\0" Required_argument "Z" "context\0" Required_argument "Z"
#endif #endif
"\0"; ;
#endif #endif
int mkdir_main(int argc, char **argv); int mkdir_main(int argc, char **argv);

View File

@ -24,7 +24,7 @@
static const char mv_longopts[] = static const char mv_longopts[] =
"interactive\0" No_argument "i" "interactive\0" No_argument "i"
"force\0" No_argument "f" "force\0" No_argument "f"
"\0"; ;
#endif #endif
#define OPT_FILEUTILS_FORCE 1 #define OPT_FILEUTILS_FORCE 1

View File

@ -1242,7 +1242,7 @@ int od_main(int argc, char **argv)
"strings\0" Optional_argument "S" "strings\0" Optional_argument "S"
"width\0" Optional_argument "w" "width\0" Optional_argument "w"
"traditional\0" No_argument "\xff" "traditional\0" No_argument "\xff"
"\0"; ;
#endif #endif
char *str_A, *str_N, *str_j, *str_S; char *str_A, *str_N, *str_j, *str_S;
char *str_w = NULL; char *str_w = NULL;

View File

@ -562,18 +562,16 @@ enum {
static int find_param(const char * const name) static int find_param(const char * const name)
{ {
static const char * const params[] = { static const char params[] =
"line", /* 1 */ "line\0" /* 1 */
"rows", /* 2 */ "rows\0" /* 2 */
"cols", /* 3 */ "cols\0" /* 3 */
"columns", /* 4 */ "columns\0" /* 4 */
"size", /* 5 */ "size\0" /* 5 */
"ispeed"+1, /* 6 */ "speed\0" /* 6 */
"ispeed", "ispeed\0"
"ospeed", "ospeed\0";
NULL int i = index_in_strings(params, name) + 1;
};
int i = index_in_str_array(params, name) + 1;
if (i == 0) if (i == 0)
return 0; return 0;
if (i != 5 && i != 6) if (i != 5 && i != 6)

View File

@ -51,11 +51,10 @@ static unsigned int expand(const char *arg, char *buffer)
char *buffer_start = buffer; char *buffer_start = buffer;
unsigned i; /* XXX: FIXME: use unsigned char? */ unsigned i; /* XXX: FIXME: use unsigned char? */
unsigned char ac; unsigned char ac;
#define CLO ":]" #define CLO ":]\0"
static const char * const classes[] = { static const char classes[] =
"alpha"CLO, "alnum"CLO, "digit"CLO, "lower"CLO, "upper"CLO, "space"CLO, "alpha"CLO "alnum"CLO "digit"CLO "lower"CLO "upper"CLO "space"CLO
"blank"CLO, "punct"CLO, "cntrl"CLO, NULL "blank"CLO "punct"CLO "cntrl"CLO;
};
#define CLASS_invalid 0 /* we increment the retval */ #define CLASS_invalid 0 /* we increment the retval */
#define CLASS_alpha 1 #define CLASS_alpha 1
#define CLASS_alnum 2 #define CLASS_alnum 2
@ -90,7 +89,7 @@ static unsigned int expand(const char *arg, char *buffer)
smalluint j; smalluint j;
{ /* not really pretty.. */ { /* not really pretty.. */
char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
j = index_in_str_array(classes, tmp) + 1; j = index_in_strings(classes, tmp) + 1;
free(tmp); free(tmp);
} }
if (j == CLASS_alnum || j == CLASS_digit) { if (j == CLASS_alnum || j == CLASS_digit) {

View File

@ -44,7 +44,7 @@ static const char runparts_longopts[] =
//TODO: "reverse\0" No_argument "r" //TODO: "reverse\0" No_argument "r"
//TODO: "verbose\0" No_argument "v" //TODO: "verbose\0" No_argument "v"
#endif #endif
"\0"; ;
#endif #endif
struct globals { struct globals {

View File

@ -215,7 +215,7 @@ static const char start_stop_daemon_longopts[] =
#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
"retry\0" Required_argument "R" "retry\0" Required_argument "R"
#endif #endif
"\0"; ;
#endif #endif
enum { enum {

View File

@ -79,29 +79,25 @@ struct fsck_instance {
char *base_device; /* /dev/hda for /dev/hdaN etc */ char *base_device; /* /dev/hda for /dev/hdaN etc */
}; };
static const char *const ignored_types[] = { static const char ignored_types[] =
"ignore", "ignore\0"
"iso9660", "iso9660\0"
"nfs", "nfs\0"
"proc", "proc\0"
"sw", "sw\0"
"swap", "swap\0"
"tmpfs", "tmpfs\0"
"devpts", "devpts\0";
NULL
};
#if 0 #if 0
static const char *const really_wanted[] = { static const char really_wanted[] =
"minix", "minix\0"
"ext2", "ext2\0"
"ext3", "ext3\0"
"jfs", "jfs\0"
"reiserfs", "reiserfs\0"
"xiafs", "xiafs\0"
"xfs", "xfs\0";
NULL
};
#endif #endif
#define BASE_MD "/dev/md" #define BASE_MD "/dev/md"
@ -847,7 +843,7 @@ static int ignore(struct fs_info *fs)
return 1; return 1;
/* Are we ignoring this type? */ /* Are we ignoring this type? */
if (index_in_str_array(ignored_types, fs->type) >= 0) if (index_in_strings(ignored_types, fs->type) >= 0)
return 1; return 1;
/* We can and want to check this file system type. */ /* We can and want to check this file system type. */

View File

@ -470,38 +470,37 @@ static action*** parse_params(char **argv)
USE_FEATURE_FIND_CONTEXT(PARM_context ,) USE_FEATURE_FIND_CONTEXT(PARM_context ,)
}; };
static const char *const params[] = { static const char params[] =
"-a" , "-a\0"
"-o" , "-o\0"
USE_FEATURE_FIND_NOT( "!" ,) USE_FEATURE_FIND_NOT( "!\0" )
#if ENABLE_DESKTOP #if ENABLE_DESKTOP
"-and" , "-and\0"
"-or" , "-or\0"
USE_FEATURE_FIND_NOT( "-not" ,) USE_FEATURE_FIND_NOT( "-not\0" )
#endif #endif
"-print" , "-print\0"
USE_FEATURE_FIND_PRINT0( "-print0" ,) USE_FEATURE_FIND_PRINT0( "-print0\0" )
USE_FEATURE_FIND_DEPTH( "-depth" ,) USE_FEATURE_FIND_DEPTH( "-depth\0" )
USE_FEATURE_FIND_PRUNE( "-prune" ,) USE_FEATURE_FIND_PRUNE( "-prune\0" )
USE_FEATURE_FIND_DELETE( "-delete" ,) USE_FEATURE_FIND_DELETE( "-delete\0" )
USE_FEATURE_FIND_EXEC( "-exec" ,) USE_FEATURE_FIND_EXEC( "-exec\0" )
USE_FEATURE_FIND_PAREN( "(" ,) USE_FEATURE_FIND_PAREN( "(\0" )
/* All options starting from here require argument */ /* All options starting from here require argument */
"-name" , "-name\0"
USE_FEATURE_FIND_PATH( "-path" ,) USE_FEATURE_FIND_PATH( "-path\0" )
USE_FEATURE_FIND_REGEX( "-regex" ,) USE_FEATURE_FIND_REGEX( "-regex\0" )
USE_FEATURE_FIND_TYPE( "-type" ,) USE_FEATURE_FIND_TYPE( "-type\0" )
USE_FEATURE_FIND_PERM( "-perm" ,) USE_FEATURE_FIND_PERM( "-perm\0" )
USE_FEATURE_FIND_MTIME( "-mtime" ,) USE_FEATURE_FIND_MTIME( "-mtime\0" )
USE_FEATURE_FIND_MMIN( "-mmin" ,) USE_FEATURE_FIND_MMIN( "-mmin\0" )
USE_FEATURE_FIND_NEWER( "-newer" ,) USE_FEATURE_FIND_NEWER( "-newer\0" )
USE_FEATURE_FIND_INUM( "-inum" ,) USE_FEATURE_FIND_INUM( "-inum\0" )
USE_FEATURE_FIND_USER( "-user" ,) USE_FEATURE_FIND_USER( "-user\0" )
USE_FEATURE_FIND_GROUP( "-group" ,) USE_FEATURE_FIND_GROUP( "-group\0" )
USE_FEATURE_FIND_SIZE( "-size" ,) USE_FEATURE_FIND_SIZE( "-size\0" )
USE_FEATURE_FIND_CONTEXT("-context",) USE_FEATURE_FIND_CONTEXT("-context\0")
NULL ;
};
action*** appp; action*** appp;
unsigned cur_group = 0; unsigned cur_group = 0;
@ -541,7 +540,7 @@ static action*** parse_params(char **argv)
*/ */
while (*argv) { while (*argv) {
const char *arg = argv[0]; const char *arg = argv[0];
int parm = index_in_str_array(params, arg); int parm = index_in_strings(params, arg);
const char *arg1 = argv[1]; const char *arg1 = argv[1];
if (parm >= PARM_name) { if (parm >= PARM_name) {
@ -795,14 +794,13 @@ static action*** parse_params(char **argv)
int find_main(int argc, char **argv); int find_main(int argc, char **argv);
int find_main(int argc, char **argv) int find_main(int argc, char **argv)
{ {
static const char *const options[] = { static const char options[] =
"-follow", "-follow\0"
USE_FEATURE_FIND_XDEV( "-xdev" ,) USE_FEATURE_FIND_XDEV( "-xdev\0" )
USE_FEATURE_FIND_MAXDEPTH("-maxdepth",) USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
NULL ;
};
enum { enum {
OPT_FOLLOW, OPT_FOLLOW,
USE_FEATURE_FIND_XDEV( OPT_XDEV ,) USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,) USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
}; };
@ -839,7 +837,7 @@ USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
/* (-a will be ignored by recursive parser later) */ /* (-a will be ignored by recursive parser later) */
argp = &argv[firstopt]; argp = &argv[firstopt];
while ((arg = argp[0])) { while ((arg = argp[0])) {
int opt = index_in_str_array(options, arg); int opt = index_in_strings(options, arg);
if (opt == OPT_FOLLOW) { if (opt == OPT_FOLLOW) {
recurse_flags |= ACTION_FOLLOWLINKS; recurse_flags |= ACTION_FOLLOWLINKS;
argp[0] = (char*)"-a"; argp[0] = (char*)"-a";

View File

@ -773,8 +773,10 @@ extern int correct_password(const struct passwd *pw);
/* Returns a ptr to static storage */ /* Returns a ptr to static storage */
extern char *pw_encrypt(const char *clear, const char *salt); extern char *pw_encrypt(const char *clear, const char *salt);
extern int obscure(const char *old, const char *newval, const struct passwd *pwdp); extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
extern int index_in_str_array(const char * const string_array[], const char *key); extern int index_in_str_array(const char *const string_array[], const char *key);
extern int index_in_substr_array(const char * const string_array[], const char *key); extern int index_in_strings(const char *strings, const char *key);
extern int index_in_substr_array(const char *const string_array[], const char *key);
extern int index_in_substrings(const char *strings, const char *key);
extern void print_login_issue(const char *issue_file, const char *tty); extern void print_login_issue(const char *issue_file, const char *tty);
extern void print_login_prompt(void); extern void print_login_prompt(void);

View File

@ -19,8 +19,23 @@ int index_in_str_array(const char * const string_array[], const char *key)
return -1; return -1;
} }
int index_in_strings(const char *strings, const char *key)
{
int idx = 0;
while (strings[0]) {
if (strcmp(strings, key) == 0) {
return idx;
}
strings += strlen(strings) + 1; /* skip NUL */
idx++;
}
return -1;
}
/* returns the array index of the string, even if it matches only a beginning */ /* returns the array index of the string, even if it matches only a beginning */
/* (index of first match is returned, or -1) */ /* (index of first match is returned, or -1) */
#ifdef UNUSED
int index_in_substr_array(const char * const string_array[], const char *key) int index_in_substr_array(const char * const string_array[], const char *key)
{ {
int i; int i;
@ -34,3 +49,21 @@ int index_in_substr_array(const char * const string_array[], const char *key)
} }
return -1; return -1;
} }
#endif
int index_in_substrings(const char *strings, const char *key)
{
int len = strlen(key);
if (len) {
int idx = 0;
while (strings[0]) {
if (strncmp(strings, key, len) == 0) {
return idx;
}
strings += strlen(strings) + 1; /* skip NUL */
idx++;
}
}
return -1;
}

View File

@ -449,7 +449,7 @@ static const char conv_str[] =
"\r\\r\0" "\r\\r\0"
"\t\\t\0" "\t\\t\0"
"\v\\v\0" "\v\\v\0"
"\0"; ;
static void conv_c(PR * pr, unsigned char * p) static void conv_c(PR * pr, unsigned char * p)

View File

@ -79,7 +79,7 @@ const char *applet_long_options
static const char applet_longopts[] = static const char applet_longopts[] =
//"name\0" has_arg val //"name\0" has_arg val
"verbose\0" No_argument "v" "verbose\0" No_argument "v"
"\0"; ;
applet_long_options = applet_longopts; applet_long_options = applet_longopts;
The last member of struct option (val) typically is set to The last member of struct option (val) typically is set to

View File

@ -14,7 +14,7 @@
static const char chpasswd_longopts[] = static const char chpasswd_longopts[] =
"encrypted\0" No_argument "e" "encrypted\0" No_argument "e"
"md5\0" No_argument "m" "md5\0" No_argument "m"
"\0"; ;
#endif #endif
#define OPT_ENC 1 #define OPT_ENC 1

View File

@ -509,12 +509,11 @@ static void process_config_line(const char *line, unsigned long *event_mask)
int i; int i;
/* !!!! Only Uppercase Keywords in devsfd.conf */ /* !!!! Only Uppercase Keywords in devsfd.conf */
static const char *const options[] = { static const char options[] =
"CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
"RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE", "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
"COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT", "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
"RMOLDCOMPAT", "RMNEWCOMPAT", 0 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
};
for (count = 0; count < MAX_ARGS; ++count) for (count = 0; count < MAX_ARGS; ++count)
p[count][0] = '\0'; p[count][0] = '\0';
@ -522,9 +521,9 @@ static void process_config_line(const char *line, unsigned long *event_mask)
when, name, what, when, name, what,
p[0], p[1], p[2], p[3], p[4], p[5], p[6]); p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
i = index_in_str_array(options, when); i = index_in_strings(options, when);
/*"CLEAR_CONFIG"*/ /* "CLEAR_CONFIG" */
if (i == 0) { if (i == 0) {
free_config(); free_config();
*event_mask = 0; *event_mask = 0;
@ -562,7 +561,7 @@ static void process_config_line(const char *line, unsigned long *event_mask)
goto process_config_line_err; goto process_config_line_err;
} }
i = index_in_str_array(options, what); i = index_in_strings(options, what);
switch (i) { switch (i) {
case 4: /* "PERMISSIONS" */ case 4: /* "PERMISSIONS" */
@ -1140,27 +1139,30 @@ static void signal_handler(int sig)
static const char *get_variable(const char *variable, void *info) static const char *get_variable(const char *variable, void *info)
{ {
static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
char hostname[STRING_LENGTH];
struct get_variable_info *gv_info = info; struct get_variable_info *gv_info = info;
static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH]; const char *field_names[] = {
const char *field_names[] = { "hostname", "mntpt", "devpath", "devname", "hostname", "mntpt", "devpath", "devname",
"uid", "gid", "mode", hostname, mount_point, "uid", "gid", "mode", hostname, mount_point,
gv_info->devpath, gv_info->devname, 0 }; gv_info->devpath, gv_info->devname, NULL
};
int i; int i;
if (gethostname(hostname, STRING_LENGTH - 1) != 0) if (gethostname(hostname, STRING_LENGTH - 1) != 0)
/* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
error_logger_and_die(LOG_ERR, "gethostname"); error_logger_and_die(LOG_ERR, "gethostname");
/* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ hostname[STRING_LENGTH - 1] = '\0';
hostname[STRING_LENGTH - 1] = '\0';
/* index_in_str_array returns i>=0 */ /* index_in_str_array returns i>=0 */
i = index_in_str_array(field_names, variable); i = index_in_str_array(field_names, variable);
if (i > 6 || i < 0 || (i > 1 && gv_info == NULL)) if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
return NULL; return NULL;
if (i >= 0 && i <= 3) { if (i >= 0 && i <= 3)
return field_names[i + 7]; return field_names[i + 7];
}
if (i == 4) if (i == 4)
sprintf(sbuf, "%u", gv_info->info->uid); sprintf(sbuf, "%u", gv_info->info->uid);

View File

@ -46,17 +46,15 @@ static int sockfd; /* active socket descriptor */
static smallint hw_set; /* flag if hw-type was set (-H) */ static smallint hw_set; /* flag if hw-type was set (-H) */
static const char *device = ""; /* current device */ static const char *device = ""; /* current device */
static const char *const options[] = { static const char options[] =
"pub", "pub\0"
"priv", "priv\0"
"temp", "temp\0"
"trail", "trail\0"
"dontpub", "dontpub\0"
"auto", "auto\0"
"dev", "dev\0"
"netmask", "netmask\0";
NULL
};
/* Delete an entry from the ARP cache. */ /* Delete an entry from the ARP cache. */
/* Called only from main, once */ /* Called only from main, once */
@ -85,7 +83,7 @@ static int arp_del(char **args)
req.arp_flags = ATF_PERM; req.arp_flags = ATF_PERM;
args++; args++;
while (*args != NULL) { while (*args != NULL) {
switch (index_in_str_array(options, *args)) { switch (index_in_strings(options, *args)) {
case 0: /* "pub" */ case 0: /* "pub" */
flags |= 1; flags |= 1;
args++; args++;
@ -239,7 +237,7 @@ static int arp_set(char **args)
/* Check out any modifiers. */ /* Check out any modifiers. */
flags = ATF_PERM | ATF_COM; flags = ATF_PERM | ATF_COM;
while (*args != NULL) { while (*args != NULL) {
switch (index_in_str_array(options, *args)) { switch (index_in_strings(options, *args)) {
case 0: /* "pub" */ case 0: /* "pub" */
flags |= ATF_PUBL; flags |= ATF_PUBL;
args++; args++;

View File

@ -293,7 +293,7 @@ static const char ftpgetput_longopts[] =
"username\0" Required_argument "u" "username\0" Required_argument "u"
"password\0" Required_argument "p" "password\0" Required_argument "p"
"port\0" Required_argument "P" "port\0" Required_argument "P"
"\0"; ;
#endif #endif
int ftpgetput_main(int argc, char **argv); int ftpgetput_main(int argc, char **argv);

View File

@ -82,14 +82,13 @@ int iptunnel_main(int argc, char **argv)
int ip_main(int argc, char **argv); int ip_main(int argc, char **argv);
int ip_main(int argc, char **argv) int ip_main(int argc, char **argv)
{ {
const char * const keywords[] = { static const char keywords[] =
USE_FEATURE_IP_ADDRESS("address",) USE_FEATURE_IP_ADDRESS("address\0")
USE_FEATURE_IP_ROUTE("route",) USE_FEATURE_IP_ROUTE("route\0")
USE_FEATURE_IP_LINK("link",) USE_FEATURE_IP_LINK("link\0")
USE_FEATURE_IP_TUNNEL("tunnel", "tunl",) USE_FEATURE_IP_TUNNEL("tunnel\0" "tunl\0")
USE_FEATURE_IP_RULE("rule",) USE_FEATURE_IP_RULE("rule\0")
NULL ;
};
enum { enum {
USE_FEATURE_IP_ADDRESS(IP_addr,) USE_FEATURE_IP_ADDRESS(IP_addr,)
USE_FEATURE_IP_ROUTE(IP_route,) USE_FEATURE_IP_ROUTE(IP_route,)
@ -101,7 +100,7 @@ int ip_main(int argc, char **argv)
ip_parse_common_args(&argc, &argv); ip_parse_common_args(&argc, &argv);
if (argc > 1) { if (argc > 1) {
int key = index_in_substr_array(keywords, argv[1]); int key = index_in_substrings(keywords, argv[1]);
argc -= 2; argc -= 2;
argv += 2; argv += 2;
#if ENABLE_FEATURE_IP_ADDRESS #if ENABLE_FEATURE_IP_ADDRESS
@ -125,7 +124,7 @@ int ip_main(int argc, char **argv)
ip_func = do_iprule; ip_func = do_iprule;
#endif #endif
} }
return (ip_func(argc, argv)); return ip_func(argc, argv);
} }
#endif /* any of ENABLE_FEATURE_IP_xxx is 1 */ #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */

View File

@ -72,7 +72,7 @@ int get_prefix(unsigned long netmask);
"hostname\0" No_argument "h" "hostname\0" No_argument "h"
"silent\0" No_argument "s" "silent\0" No_argument "s"
# endif # endif
"\0"; ;
#endif #endif
int ipcalc_main(int argc, char **argv); int ipcalc_main(int argc, char **argv);

View File

@ -26,9 +26,9 @@ void ip_parse_common_args(int *argcp, char ***argvp)
{ {
int argc = *argcp; int argc = *argcp;
char **argv = *argvp; char **argv = *argvp;
static const char * const ip_common_commands[] = static const char ip_common_commands[] =
{"-family", "inet", "inet6", "link", "-family\0""inet\0""inet6\0""link\0"
"-4", "-6", "-0", "-oneline", 0}; "-4\0""-6\0""-0\0""-oneline\0";
enum { enum {
ARG_family = 1, ARG_family = 1,
ARG_inet, ARG_inet,
@ -53,13 +53,13 @@ void ip_parse_common_args(int *argcp, char ***argvp)
break; break;
if (opt[1] == '-') if (opt[1] == '-')
opt++; opt++;
arg = index_in_str_array(ip_common_commands, opt) + 1; arg = index_in_strings(ip_common_commands, opt) + 1;
if (arg == ARG_family) { if (arg == ARG_family) {
argc--; argc--;
argv++; argv++;
if (!argv[1]) if (!argv[1])
bb_show_usage(); bb_show_usage();
arg = index_in_str_array(ip_common_commands, argv[1]) + 1; arg = index_in_strings(ip_common_commands, argv[1]) + 1;
if (arg == ARG_inet) if (arg == ARG_inet)
preferred_family = AF_INET; preferred_family = AF_INET;
else if (arg == ARG_inet6) else if (arg == ARG_inet6)

View File

@ -412,7 +412,7 @@ static void ipaddr_reset_filter(int _oneline)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
int ipaddr_list_or_flush(int argc, char **argv, int flush) int ipaddr_list_or_flush(int argc, char **argv, int flush)
{ {
static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 }; static const char option[] = "to\0""scope\0""up\0""label\0""dev\0";
struct nlmsg_list *linfo = NULL; struct nlmsg_list *linfo = NULL;
struct nlmsg_list *ainfo = NULL; struct nlmsg_list *ainfo = NULL;
@ -437,7 +437,7 @@ int ipaddr_list_or_flush(int argc, char **argv, int flush)
} }
while (argc > 0) { while (argc > 0) {
const int option_num = index_in_str_array(option, *argv); const int option_num = index_in_strings(option, *argv);
switch (option_num) { switch (option_num) {
case 0: /* to */ case 0: /* to */
NEXT_ARG(); NEXT_ARG();
@ -599,18 +599,17 @@ static int default_scope(inet_prefix *lcl)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
static int ipaddr_modify(int cmd, int argc, char **argv) static int ipaddr_modify(int cmd, int argc, char **argv)
{ {
static const char *const option[] = { static const char option[] =
"peer", "remote", "broadcast", "brd", "peer\0""remote\0""broadcast\0""brd\0"
"anycast", "scope", "dev", "label", "local", 0 "anycast\0""scope\0""dev\0""label\0""local\0";
};
struct rtnl_handle rth; struct rtnl_handle rth;
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
struct ifaddrmsg ifa; struct ifaddrmsg ifa;
char buf[256]; char buf[256];
} req; } req;
char *d = NULL; char *d = NULL;
char *l = NULL; char *l = NULL;
inet_prefix lcl; inet_prefix lcl;
inet_prefix peer; inet_prefix peer;
int local_len = 0; int local_len = 0;
@ -627,7 +626,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
req.ifa.ifa_family = preferred_family; req.ifa.ifa_family = preferred_family;
while (argc > 0) { while (argc > 0) {
const int option_num = index_in_str_array(option, *argv); const int option_num = index_in_strings(option, *argv);
switch (option_num) { switch (option_num) {
case 0: /* peer */ case 0: /* peer */
case 1: /* remote */ case 1: /* remote */
@ -769,14 +768,13 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
int do_ipaddr(int argc, char **argv) int do_ipaddr(int argc, char **argv)
{ {
static const char *const commands[] = { static const char commands[] =
"add", "delete", "list", "show", "lst", "flush", 0 "add\0""delete\0""list\0""show\0""lst\0""flush\0";
};
int command_num = 2; /* default command is list */ int command_num = 2; /* default command is list */
if (*argv) { if (*argv) {
command_num = index_in_substr_array(commands, *argv); command_num = index_in_substrings(commands, *argv);
} }
if (command_num < 0 || command_num > 5) if (command_num < 0 || command_num > 5)
bb_error_msg_and_die("unknown command %s", *argv); bb_error_msg_and_die("unknown command %s", *argv);

View File

@ -171,16 +171,15 @@ static int do_set(int argc, char **argv)
struct ifreq ifr0, ifr1; struct ifreq ifr0, ifr1;
char *newname = NULL; char *newname = NULL;
int htype, halen; int htype, halen;
static const char * const keywords[] = { static const char keywords[] =
"up", "down", "name", "mtu", "multicast", "arp", "addr", "dev", "up\0""down\0""name\0""mtu\0""multicast\0""arp\0""addr\0""dev\0"
"on", "off", NULL "on\0""off\0";
};
enum { ARG_up = 1, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp, enum { ARG_up = 1, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp,
ARG_addr, ARG_dev, PARM_on, PARM_off }; ARG_addr, ARG_dev, PARM_on, PARM_off };
smalluint key; smalluint key;
while (argc > 0) { while (argc > 0) {
key = index_in_str_array(keywords, *argv) + 1; key = index_in_strings(keywords, *argv) + 1;
if (key == ARG_up) { if (key == ARG_up) {
mask |= IFF_UP; mask |= IFF_UP;
flags |= IFF_UP; flags |= IFF_UP;
@ -199,7 +198,7 @@ static int do_set(int argc, char **argv)
} else if (key == ARG_multicast) { } else if (key == ARG_multicast) {
NEXT_ARG(); NEXT_ARG();
mask |= IFF_MULTICAST; mask |= IFF_MULTICAST;
key = index_in_str_array(keywords, *argv) + 1; key = index_in_strings(keywords, *argv) + 1;
if (key == PARM_on) { if (key == PARM_on) {
flags |= IFF_MULTICAST; flags |= IFF_MULTICAST;
} else if (key == PARM_off) { } else if (key == PARM_off) {
@ -209,7 +208,7 @@ static int do_set(int argc, char **argv)
} else if (key == ARG_arp) { } else if (key == ARG_arp) {
NEXT_ARG(); NEXT_ARG();
mask |= IFF_NOARP; mask |= IFF_NOARP;
key = index_in_str_array(keywords, *argv) + 1; key = index_in_strings(keywords, *argv) + 1;
if (key == PARM_on) { if (key == PARM_on) {
flags &= ~IFF_NOARP; flags &= ~IFF_NOARP;
} else if (key == PARM_off) { } else if (key == PARM_off) {
@ -276,13 +275,12 @@ static int ipaddr_list_link(int argc, char **argv)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
int do_iplink(int argc, char **argv) int do_iplink(int argc, char **argv)
{ {
static const char * const keywords[] = { static const char keywords[] =
"set", "show", "lst", "list", NULL "set\0""show\0""lst\0""list\0";
};
smalluint key; smalluint key;
if (argc <= 0) if (argc <= 0)
return ipaddr_list_link(0, NULL); return ipaddr_list_link(0, NULL);
key = index_in_substr_array(keywords, *argv) + 1; key = index_in_substrings(keywords, *argv) + 1;
if (key == 0) if (key == 0)
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
argc--; argv++; argc--; argv++;

View File

@ -294,22 +294,9 @@ static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
static int iproute_modify(int cmd, unsigned flags, int argc, char **argv) static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
{ {
struct rtnl_handle rth; static const char keywords[] =
struct { "src\0""via\0""mtu\0""lock\0""protocol\0"USE_FEATURE_IP_RULE("table\0")
struct nlmsghdr n; "dev\0""oif\0""to\0";
struct rtmsg r;
char buf[1024];
} req;
char mxbuf[256];
struct rtattr * mxrta = (void*)mxbuf;
unsigned mxlock = 0;
char *d = NULL;
enum { gw_ok = 1<<0, dst_ok = 1<<1, proto_ok = 1<<2, type_ok = 1<<3};
smalluint ok = 0;
static const char * const keywords[] = {
"src", "via", "mtu", "lock", "protocol", USE_FEATURE_IP_RULE("table",)
"dev", "oif", "to", NULL
};
enum { enum {
ARG_src, ARG_src,
ARG_via, ARG_via,
@ -320,6 +307,23 @@ USE_FEATURE_IP_RULE(ARG_table,)
ARG_oif, ARG_oif,
ARG_to ARG_to
}; };
enum {
gw_ok = 1 << 0,
dst_ok = 1 << 1,
proto_ok = 1 << 2,
type_ok = 1 << 3
};
struct rtnl_handle rth;
struct {
struct nlmsghdr n;
struct rtmsg r;
char buf[1024];
} req;
char mxbuf[256];
struct rtattr * mxrta = (void*)mxbuf;
unsigned mxlock = 0;
char *d = NULL;
smalluint ok = 0;
int arg; int arg;
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
@ -341,7 +345,7 @@ USE_FEATURE_IP_RULE(ARG_table,)
mxrta->rta_len = RTA_LENGTH(0); mxrta->rta_len = RTA_LENGTH(0);
while (argc > 0) { while (argc > 0) {
arg = index_in_substr_array(keywords, *argv); arg = index_in_substrings(keywords, *argv);
if (arg == ARG_src) { if (arg == ARG_src) {
inet_prefix addr; inet_prefix addr;
NEXT_ARG(); NEXT_ARG();
@ -361,7 +365,7 @@ USE_FEATURE_IP_RULE(ARG_table,)
} else if (arg == ARG_mtu) { } else if (arg == ARG_mtu) {
unsigned mtu; unsigned mtu;
NEXT_ARG(); NEXT_ARG();
if (index_in_str_array(keywords, *argv) == PARM_lock) { if (index_in_strings(keywords, *argv) == PARM_lock) {
mxlock |= (1<<RTAX_MTU); mxlock |= (1<<RTAX_MTU);
NEXT_ARG(); NEXT_ARG();
} }
@ -513,10 +517,9 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
struct rtnl_handle rth; struct rtnl_handle rth;
char *id = NULL; char *id = NULL;
char *od = NULL; char *od = NULL;
static const char * const keywords[] = { static const char keywords[] =
"protocol", "all", "dev", "oif", "iif", "via", "table", "cache",/*all,*/ "protocol\0""all\0""dev\0""oif\0""iif\0""via\0""table\0""cache\0" /*all*/
"from", "root", "match", "exact", "to", /*root,match,exact*/ NULL "from\0""root\0""match\0""exact\0""to\0"/*root match exact*/;
};
enum { enum {
ARG_proto, PARM_all, ARG_proto, PARM_all,
ARG_dev, ARG_dev,
@ -535,13 +538,13 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\""); bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
while (argc > 0) { while (argc > 0) {
arg = index_in_substr_array(keywords, *argv); arg = index_in_substrings(keywords, *argv);
if (arg == ARG_proto) { if (arg == ARG_proto) {
uint32_t prot = 0; uint32_t prot = 0;
NEXT_ARG(); NEXT_ARG();
filter.protocolmask = -1; filter.protocolmask = -1;
if (rtnl_rtprot_a2n(&prot, *argv)) { if (rtnl_rtprot_a2n(&prot, *argv)) {
if (index_in_str_array(keywords, *argv) != PARM_all) if (index_in_strings(keywords, *argv) != PARM_all)
invarg(*argv, "protocol"); invarg(*argv, "protocol");
prot = 0; prot = 0;
filter.protocolmask = 0; filter.protocolmask = 0;
@ -558,7 +561,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
get_prefix(&filter.rvia, *argv, do_ipv6); get_prefix(&filter.rvia, *argv, do_ipv6);
} else if (arg == ARG_table) { } else if (arg == ARG_table) {
NEXT_ARG(); NEXT_ARG();
parm = index_in_substr_array(keywords, *argv); parm = index_in_substrings(keywords, *argv);
if (parm == PARM_cache) if (parm == PARM_cache)
filter.tb = -1; filter.tb = -1;
else if (parm == PARM_all) else if (parm == PARM_all)
@ -567,7 +570,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
invarg(*argv, "table"); invarg(*argv, "table");
} else if (arg == ARG_from) { } else if (arg == ARG_from) {
NEXT_ARG(); NEXT_ARG();
parm = index_in_substr_array(keywords, *argv); parm = index_in_substrings(keywords, *argv);
if (parm == PARM_root) { if (parm == PARM_root) {
NEXT_ARG(); NEXT_ARG();
get_prefix(&filter.rsrc, *argv, do_ipv6); get_prefix(&filter.rsrc, *argv, do_ipv6);
@ -584,7 +587,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
/* parm = arg; // would be more plausible, we reuse arg here */ /* parm = arg; // would be more plausible, we reuse arg here */
if (arg == ARG_to) { if (arg == ARG_to) {
NEXT_ARG(); NEXT_ARG();
arg = index_in_substr_array(keywords, *argv); arg = index_in_substrings(keywords, *argv);
} }
if (arg == PARM_root) { if (arg == PARM_root) {
NEXT_ARG(); NEXT_ARG();
@ -645,9 +648,8 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE); xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
filter.flushed = 0; filter.flushed = 0;
xrtnl_dump_filter(&rth, print_route, stdout); xrtnl_dump_filter(&rth, print_route, stdout);
if (filter.flushed == 0) { if (filter.flushed == 0)
return 0; return 0;
}
if (flush_update()) if (flush_update())
return 1; return 1;
} }
@ -655,10 +657,8 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
if (filter.tb != -1) { if (filter.tb != -1) {
xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE); xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
} else { } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { bb_perror_msg_and_die("cannot send dump request");
bb_perror_msg_and_die("cannot send dump request");
}
} }
xrtnl_dump_filter(&rth, print_route, stdout); xrtnl_dump_filter(&rth, print_route, stdout);
@ -671,16 +671,16 @@ static int iproute_get(int argc, char **argv)
{ {
struct rtnl_handle rth; struct rtnl_handle rth;
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
struct rtmsg r; struct rtmsg r;
char buf[1024]; char buf[1024];
} req; } req;
char *idev = NULL; char *idev = NULL;
char *odev = NULL; char *odev = NULL;
bool connected = 0; bool connected = 0;
bool from_ok = 0; bool from_ok = 0;
static const char * const options[] = static const char options[] =
{ "from", "iif", "oif", "dev", "notify", "connected", "to", 0 }; "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
@ -699,7 +699,7 @@ static int iproute_get(int argc, char **argv)
req.r.rtm_tos = 0; req.r.rtm_tos = 0;
while (argc > 0) { while (argc > 0) {
switch (index_in_str_array(options, *argv)) { switch (index_in_strings(options, *argv)) {
case 0: /* from */ case 0: /* from */
{ {
inet_prefix addr; inet_prefix addr;
@ -824,19 +824,18 @@ static int iproute_get(int argc, char **argv)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
int do_iproute(int argc, char **argv) int do_iproute(int argc, char **argv)
{ {
static const char * const ip_route_commands[] = { static const char ip_route_commands[] =
/*0-3*/ "add", "append", "change", "chg", /*0-3*/ "add\0""append\0""change\0""chg\0"
/*4-7*/ "delete", "get", "list", "show", /*4-7*/ "delete\0""get\0""list\0""show\0"
/*8..*/ "prepend", "replace", "test", "flush", 0 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
};
int command_num = 6; int command_num = 6;
unsigned int flags = 0; unsigned flags = 0;
int cmd = RTM_NEWROUTE; int cmd = RTM_NEWROUTE;
/* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */ /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
/* It probably means that it is using "first match" rule */ /* It probably means that it is using "first match" rule */
if (*argv) { if (*argv) {
command_num = index_in_substr_array(ip_route_commands, *argv); command_num = index_in_substrings(ip_route_commands, *argv);
} }
switch (command_num) { switch (command_num) {
case 0: /* add */ case 0: /* add */

View File

@ -187,6 +187,15 @@ static int iprule_list(int argc, char **argv)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
static int iprule_modify(int cmd, int argc, char **argv) static int iprule_modify(int cmd, int argc, char **argv)
{ {
static const char keywords[] =
"from\0""to\0""preference\0""order\0""priority\0"
"tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
"iif\0""nat\0""map-to\0""type\0""help\0";
enum {
ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
};
bool table_ok = 0; bool table_ok = 0;
struct rtnl_handle rth; struct rtnl_handle rth;
struct { struct {
@ -194,13 +203,6 @@ static int iprule_modify(int cmd, int argc, char **argv)
struct rtmsg r; struct rtmsg r;
char buf[1024]; char buf[1024];
} req; } req;
static const char * const keywords[] =
{ "from", "to", "preference", "order", "priority", "tos", "fwmark",
"realms", "table", "lookup", "dev", "iif", "nat", "map-to", "type",
"help", NULL};
enum { ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help };
smalluint key; smalluint key;
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
@ -220,7 +222,7 @@ static int iprule_modify(int cmd, int argc, char **argv)
} }
while (argc > 0) { while (argc > 0) {
key = index_in_substr_array(keywords, *argv) + 1; key = index_in_substrings(keywords, *argv) + 1;
if (key == 0) /* no match found in keywords array, bail out. */ if (key == 0) /* no match found in keywords array, bail out. */
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
if (key == ARG_from) { if (key == ARG_from) {
@ -311,14 +313,14 @@ static int iprule_modify(int cmd, int argc, char **argv)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
int do_iprule(int argc, char **argv) int do_iprule(int argc, char **argv)
{ {
static const char * const ip_rule_commands[] = static const char ip_rule_commands[] =
{"add", "delete", "list", "show", 0}; "add\0""delete\0""list\0""show\0";
int cmd = 2; /* list */ int cmd = 2; /* list */
if (argc < 1) if (argc < 1)
return iprule_list(0, NULL); return iprule_list(0, NULL);
if (*argv) if (*argv)
cmd = index_in_substr_array(ip_rule_commands, *argv); cmd = index_in_substrings(ip_rule_commands, *argv);
switch (cmd) { switch (cmd) {
case 0: /* add */ case 0: /* add */

View File

@ -128,16 +128,13 @@ static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
/* Dies on error */ /* Dies on error */
static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p) static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
{ {
int count = 0; static const char keywords[] =
char medium[IFNAMSIZ]; "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
static const char * const keywords[] = { "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
"mode", "ipip", "ip/ip", "gre", "gre/ip", "sit", "ipv6/ip", "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
"key", "ikey", "okey", "seq", "iseq", "oseq", "remote\0""any\0""local\0""dev\0"
"csum", "icsum", "ocsum", "nopmtudisc", "pmtudisc", "ttl\0""inherit\0""tos\0""dsfield\0"
"remote", "any", "local", "dev", "name\0";
"ttl", "inherit", "tos", "dsfield",
"name", NULL
};
enum { enum {
ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip, ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq, ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
@ -146,22 +143,25 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield, ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
ARG_name ARG_name
}; };
int count = 0;
char medium[IFNAMSIZ];
int key; int key;
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
memset(&medium, 0, sizeof(medium)); memset(&medium, 0, sizeof(medium));
p->iph.version = 4; p->iph.version = 4;
p->iph.ihl = 5; p->iph.ihl = 5;
#ifndef IP_DF #ifndef IP_DF
#define IP_DF 0x4000 /* Flag: "Don't Fragment" */ #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
#endif #endif
p->iph.frag_off = htons(IP_DF); p->iph.frag_off = htons(IP_DF);
while (argc > 0) { while (argc > 0) {
key = index_in_str_array(keywords, *argv); key = index_in_strings(keywords, *argv);
if (key == ARG_mode) { if (key == ARG_mode) {
NEXT_ARG(); NEXT_ARG();
key = index_in_str_array(keywords, *argv); key = index_in_strings(keywords, *argv);
if (key == ARG_ipip || if (key == ARG_ipip ||
key == ARG_ip_ip) { key == ARG_ip_ip) {
if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
@ -240,12 +240,12 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
p->iph.frag_off = htons(IP_DF); p->iph.frag_off = htons(IP_DF);
} else if (key == ARG_remote) { } else if (key == ARG_remote) {
NEXT_ARG(); NEXT_ARG();
key = index_in_str_array(keywords, *argv); key = index_in_strings(keywords, *argv);
if (key == ARG_any) if (key == ARG_any)
p->iph.daddr = get_addr32(*argv); p->iph.daddr = get_addr32(*argv);
} else if (key == ARG_local) { } else if (key == ARG_local) {
NEXT_ARG(); NEXT_ARG();
key = index_in_str_array(keywords, *argv); key = index_in_strings(keywords, *argv);
if (key == ARG_any) if (key == ARG_any)
p->iph.saddr = get_addr32(*argv); p->iph.saddr = get_addr32(*argv);
} else if (key == ARG_dev) { } else if (key == ARG_dev) {
@ -254,7 +254,7 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
} else if (key == ARG_ttl) { } else if (key == ARG_ttl) {
unsigned uval; unsigned uval;
NEXT_ARG(); NEXT_ARG();
key = index_in_str_array(keywords, *argv); key = index_in_strings(keywords, *argv);
if (key != ARG_inherit) { if (key != ARG_inherit) {
if (get_unsigned(&uval, *argv, 0)) if (get_unsigned(&uval, *argv, 0))
invarg(*argv, "TTL"); invarg(*argv, "TTL");
@ -266,7 +266,7 @@ static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
key == ARG_dsfield) { key == ARG_dsfield) {
uint32_t uval; uint32_t uval;
NEXT_ARG(); NEXT_ARG();
key = index_in_str_array(keywords, *argv); key = index_in_strings(keywords, *argv);
if (key != ARG_inherit) { if (key != ARG_inherit) {
if (rtnl_dsfield_a2n(&uval, *argv)) if (rtnl_dsfield_a2n(&uval, *argv))
invarg(*argv, "TOS"); invarg(*argv, "TOS");
@ -519,14 +519,13 @@ static int do_show(int argc, char **argv)
/* Return value becomes exitcode. It's okay to not return at all */ /* Return value becomes exitcode. It's okay to not return at all */
int do_iptunnel(int argc, char **argv) int do_iptunnel(int argc, char **argv)
{ {
static const char *const keywords[] = { static const char keywords[] =
"add", "change", "delete", "show", "list", "lst", NULL "add\0""change\0""delete\0""show\0""list\0""lst\0";
};
enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst }; enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
int key; int key;
if (argc) { if (argc) {
key = index_in_substr_array(keywords, *argv); key = index_in_substrings(keywords, *argv);
if (key < 0) if (key < 0)
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
--argc; --argc;

View File

@ -51,16 +51,16 @@ const char *rtnl_rtntype_n2a(int id, char *buf, int len)
int rtnl_rtntype_a2n(int *id, char *arg) int rtnl_rtntype_a2n(int *id, char *arg)
{ {
static const char * const keywords[] = { static const char keywords[] =
"local", "nat", "broadcast", "brd", "anycast", "local\0""nat\0""broadcast\0""brd\0""anycast\0"
"multicast", "prohibit", "unreachable", "blackhole", "multicast\0""prohibit\0""unreachable\0""blackhole\0"
"xresolve", "unicast", "throw", NULL "xresolve\0""unicast\0""throw\0";
}; enum {
enum { ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast, ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole, ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
ARG_xresolve, ARG_unicast, ARG_throw ARG_xresolve, ARG_unicast, ARG_throw
}; };
const smalluint key = index_in_substr_array(keywords, arg) + 1; const smalluint key = index_in_substrings(keywords, arg) + 1;
char *end; char *end;
unsigned long res; unsigned long res;

View File

@ -16,16 +16,6 @@
#include "libbb.h" #include "libbb.h"
#include "libiproute/utils.h" /* invarg() */ #include "libiproute/utils.h" /* invarg() */
/* Line discipline code table */
static const char *const proto_names[] = {
"cslip"+1, /* 0 */
"cslip", /* 1 */
"cslip6"+1, /* 2 */
"cslip6", /* 3 */
"adaptive", /* 8 */
NULL
};
struct globals { struct globals {
int handle; int handle;
int saved_disc; int saved_disc;
@ -132,6 +122,15 @@ static void sig_handler(int signo)
int slattach_main(int argc, char **argv); int slattach_main(int argc, char **argv);
int slattach_main(int argc, char **argv) int slattach_main(int argc, char **argv)
{ {
/* Line discipline code table */
static const char proto_names[] =
"slip\0" /* 0 */
"cslip\0" /* 1 */
"slip6\0" /* 2 */
"cslip6\0" /* 3 */
"adaptive\0" /* 8 */
;
int i, encap, opt; int i, encap, opt;
struct termios state; struct termios state;
const char *proto = "cslip"; const char *proto = "cslip";
@ -160,7 +159,7 @@ int slattach_main(int argc, char **argv)
if (!*argv) if (!*argv)
bb_show_usage(); bb_show_usage();
encap = index_in_str_array(proto_names, proto); encap = index_in_strings(proto_names, proto);
if (encap < 0) if (encap < 0)
invarg(proto, "protocol"); invarg(proto, "protocol");

View File

@ -200,7 +200,7 @@ int udhcpc_main(int argc, char **argv)
"timeout\0" Required_argument "T" "timeout\0" Required_argument "T"
"version\0" No_argument "v" "version\0" No_argument "v"
"retries\0" Required_argument "t" "retries\0" Required_argument "t"
"\0"; ;
#endif #endif
/* Default options. */ /* Default options. */
client_config.interface = "eth0"; client_config.interface = "eth0";

View File

@ -28,7 +28,7 @@ int dumpleases_main(int argc, char **argv)
"absolute\0" No_argument "a" "absolute\0" No_argument "a"
"remaining\0" No_argument "r" "remaining\0" No_argument "r"
"file\0" Required_argument "f" "file\0" Required_argument "f"
"\0"; ;
applet_long_options = dumpleases_longopts; applet_long_options = dumpleases_longopts;
#endif #endif

View File

@ -114,9 +114,8 @@ int wget_main(int argc, char **argv)
bool use_proxy = 1; /* Use proxies if env vars are set */ bool use_proxy = 1; /* Use proxies if env vars are set */
const char *proxy_flag = "on"; /* Use proxies if env vars are set */ const char *proxy_flag = "on"; /* Use proxies if env vars are set */
const char *user_agent = "Wget";/* "User-Agent" header field */ const char *user_agent = "Wget";/* "User-Agent" header field */
static const char * const keywords[] = { static const char keywords[] =
"content-length", "transfer-encoding", "chunked", "location", NULL "content-length\0""transfer-encoding\0""chunked\0""location\0";
};
enum { enum {
KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
}; };
@ -143,7 +142,7 @@ int wget_main(int argc, char **argv)
"user-agent\0" Required_argument "U" "user-agent\0" Required_argument "U"
"passive-ftp\0" No_argument "\xff" "passive-ftp\0" No_argument "\xff"
"header\0" Required_argument "\xfe" "header\0" Required_argument "\xfe"
"\0"; ;
applet_long_options = wget_longopts; applet_long_options = wget_longopts;
#endif #endif
/* server.allocated = target.allocated = NULL; */ /* server.allocated = target.allocated = NULL; */
@ -327,7 +326,7 @@ int wget_main(int argc, char **argv)
*/ */
while ((str = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { while ((str = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
/* gethdr did already convert the "FOO:" string to lowercase */ /* gethdr did already convert the "FOO:" string to lowercase */
smalluint key = index_in_str_array(keywords, *&buf) + 1; smalluint key = index_in_strings(keywords, *&buf) + 1;
if (key == KEY_content_length) { if (key == KEY_content_length) {
content_len = BB_STRTOOFF(str, NULL, 10); content_len = BB_STRTOOFF(str, NULL, 10);
if (errno || content_len < 0) { if (errno || content_len < 0) {
@ -337,7 +336,7 @@ int wget_main(int argc, char **argv)
continue; continue;
} }
if (key == KEY_transfer_encoding) { if (key == KEY_transfer_encoding) {
if (index_in_str_array(keywords, str_tolower(str)) + 1 != KEY_chunked) if (index_in_strings(keywords, str_tolower(str)) + 1 != KEY_chunked)
bb_error_msg_and_die("server wants to do %s transfer encoding", str); bb_error_msg_and_die("server wants to do %s transfer encoding", str);
chunked = got_clen = 1; chunked = got_clen = 1;
} }

View File

@ -117,7 +117,7 @@ static const char chcon_longopts[] =
"range\0" Required_argument "l" "range\0" Required_argument "l"
"verbose\0" No_argument "v" "verbose\0" No_argument "v"
"reference\0" Required_argument "\xff" /* no short option */ "reference\0" Required_argument "\xff" /* no short option */
"\0"; ;
#endif #endif
int chcon_main(int argc, char **argv); int chcon_main(int argc, char **argv);

View File

@ -76,7 +76,7 @@ static const char runcon_options[] =
"range\0" Required_argument "l" "range\0" Required_argument "l"
"compute\0" No_argument "c" "compute\0" No_argument "c"
"help\0" No_argument "h" "help\0" No_argument "h"
"\0"; ;
#endif #endif
#define OPTS_ROLE (1<<0) /* r */ #define OPTS_ROLE (1<<0) /* r */

View File

@ -276,7 +276,7 @@ static const char getopt_longopts[] =
"unquoted\0" No_argument "u" "unquoted\0" No_argument "u"
"alternative\0" No_argument "a" "alternative\0" No_argument "a"
"name\0" Required_argument "n" "name\0" Required_argument "n"
"\0"; ;
#endif #endif
int getopt_main(int argc, char *argv[]); int getopt_main(int argc, char *argv[]);

View File

@ -185,7 +185,7 @@ int hwclock_main(int argc, char **argv)
"hctosys\0" No_argument "s" "hctosys\0" No_argument "s"
"systohc\0" No_argument "w" "systohc\0" No_argument "w"
"file\0" Required_argument "f" "file\0" Required_argument "f"
"\0"; ;
applet_long_options = hwclock_longopts; applet_long_options = hwclock_longopts;
#endif #endif
opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l"; opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";

View File

@ -888,33 +888,31 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) { if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
char *opteq = strchr(opt, '='); char *opteq = strchr(opt, '=');
if (opteq) { if (opteq) {
static const char *const options[] = { static const char options[] =
/* 0 */ "rsize", /* 0 */ "rsize\0"
/* 1 */ "wsize", /* 1 */ "wsize\0"
/* 2 */ "timeo", /* 2 */ "timeo\0"
/* 3 */ "retrans", /* 3 */ "retrans\0"
/* 4 */ "acregmin", /* 4 */ "acregmin\0"
/* 5 */ "acregmax", /* 5 */ "acregmax\0"
/* 6 */ "acdirmin", /* 6 */ "acdirmin\0"
/* 7 */ "acdirmax", /* 7 */ "acdirmax\0"
/* 8 */ "actimeo", /* 8 */ "actimeo\0"
/* 9 */ "retry", /* 9 */ "retry\0"
/* 10 */ "port", /* 10 */ "port\0"
/* 11 */ "mountport", /* 11 */ "mountport\0"
/* 12 */ "mounthost", /* 12 */ "mounthost\0"
/* 13 */ "mountprog", /* 13 */ "mountprog\0"
/* 14 */ "mountvers", /* 14 */ "mountvers\0"
/* 15 */ "nfsprog", /* 15 */ "nfsprog\0"
/* 16 */ "nfsvers", /* 16 */ "nfsvers\0"
/* 17 */ "vers", /* 17 */ "vers\0"
/* 18 */ "proto", /* 18 */ "proto\0"
/* 19 */ "namlen", /* 19 */ "namlen\0"
/* 20 */ "addr", /* 20 */ "addr\0";
NULL
};
int val = xatoi_u(opteq + 1); int val = xatoi_u(opteq + 1);
*opteq = '\0'; *opteq = '\0';
switch (index_in_str_array(options, opt)) { switch (index_in_strings(options, opt)) {
case 0: // "rsize" case 0: // "rsize"
data.rsize = val; data.rsize = val;
break; break;
@ -993,26 +991,24 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
} }
} }
else { else {
static const char *const options[] = { static const char options[] =
"bg", "bg\0"
"fg", "fg\0"
"soft", "soft\0"
"hard", "hard\0"
"intr", "intr\0"
"posix", "posix\0"
"cto", "cto\0"
"ac", "ac\0"
"tcp", "tcp\0"
"udp", "udp\0"
"lock", "lock\0";
NULL
};
int val = 1; int val = 1;
if (!strncmp(opt, "no", 2)) { if (!strncmp(opt, "no", 2)) {
val = 0; val = 0;
opt += 2; opt += 2;
} }
switch (index_in_str_array(options, opt)) { switch (index_in_strings(options, opt)) {
case 0: // "bg" case 0: // "bg"
bg = val; bg = val;
break; break;