diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-06 00:46:09 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-06 00:54:12 +0100 |
| commit | 1ff1c7094403fa96acce46611e7bf9534f2d8404 (patch) | |
| tree | 8e561d89247628eaa0f0f19a2f3d460a2847c884 /miscutils | |
| parent | e873ff9660a2f02a4b2647b74212e74649726e7d (diff) | |
| download | busybox-w32-1ff1c7094403fa96acce46611e7bf9534f2d8404.tar.gz busybox-w32-1ff1c7094403fa96acce46611e7bf9534f2d8404.tar.bz2 busybox-w32-1ff1c7094403fa96acce46611e7bf9534f2d8404.zip | |
bc: make bc_vm_envArgs() NULL-terminate its argv, simplify bc_vm_envLen()
function old new delta
bc_num_ulong 92 103 +11
static.nullptr - 4 +4
bc_args 120 123 +3
static.bc_args_env_name 4 - -4
dc_main 49 41 -8
bc_main 49 41 -8
bc_vm_run 1917 1883 -34
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 2/3 up/down: 18/-54) Total: -36 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/bc.c | 87 |
1 files changed, 38 insertions, 49 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 5966953d2..e9cfecccc 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
| @@ -1284,14 +1284,14 @@ static char* bc_read_file(const char *path) | |||
| 1284 | return buf; | 1284 | return buf; |
| 1285 | } | 1285 | } |
| 1286 | 1286 | ||
| 1287 | static void bc_args(int argc, char **argv) | 1287 | static void bc_args(char **argv) |
| 1288 | { | 1288 | { |
| 1289 | unsigned opts; | 1289 | unsigned opts; |
| 1290 | int i; | 1290 | int i; |
| 1291 | 1291 | ||
| 1292 | GETOPT_RESET(); | 1292 | GETOPT_RESET(); |
| 1293 | #if ENABLE_FEATURE_BC_LONG_OPTIONS | 1293 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 1294 | opts = getopt32long(argv, "xwvsqli", | 1294 | opts = option_mask32 |= getopt32long(argv, "xwvsqli", |
| 1295 | "extended-register\0" No_argument "x" | 1295 | "extended-register\0" No_argument "x" |
| 1296 | "warn\0" No_argument "w" | 1296 | "warn\0" No_argument "w" |
| 1297 | "version\0" No_argument "v" | 1297 | "version\0" No_argument "v" |
| @@ -1301,16 +1301,15 @@ static void bc_args(int argc, char **argv) | |||
| 1301 | "interactive\0" No_argument "i" | 1301 | "interactive\0" No_argument "i" |
| 1302 | ); | 1302 | ); |
| 1303 | #else | 1303 | #else |
| 1304 | opts = getopt32(argv, "xwvsqli"); | 1304 | opts = option_mask32 |= getopt32(argv, "xwvsqli"); |
| 1305 | #endif | 1305 | #endif |
| 1306 | if (getenv("POSIXLY_CORRECT")) | 1306 | if (getenv("POSIXLY_CORRECT")) |
| 1307 | option_mask32 |= BC_FLAG_S; | 1307 | option_mask32 |= BC_FLAG_S; |
| 1308 | 1308 | ||
| 1309 | ///should be in bc_vm_run() instead?? | ||
| 1309 | if (opts & BC_FLAG_V) bc_vm_info(); | 1310 | if (opts & BC_FLAG_V) bc_vm_info(); |
| 1310 | // should not be necessary, getopt32() handles this?? | ||
| 1311 | //if (argv[optind] && !strcmp(argv[optind], "--")) ++optind; | ||
| 1312 | 1311 | ||
| 1313 | for (i = optind; i < argc; ++i) | 1312 | for (i = optind; argv[i]; ++i) |
| 1314 | bc_vec_push(&G.files, argv + i); | 1313 | bc_vec_push(&G.files, argv + i); |
| 1315 | } | 1314 | } |
| 1316 | 1315 | ||
| @@ -6870,10 +6869,11 @@ static BcStatus bc_program_exec(void) | |||
| 6870 | #if ENABLE_BC | 6869 | #if ENABLE_BC |
| 6871 | static void bc_vm_envArgs(void) | 6870 | static void bc_vm_envArgs(void) |
| 6872 | { | 6871 | { |
| 6873 | static const char* const bc_args_env_name = "BC_ENV_ARGS"; | 6872 | static char *const nullptr = NULL; |
| 6874 | 6873 | ||
| 6875 | BcVec v; | 6874 | BcVec v; |
| 6876 | char *env_args = getenv(bc_args_env_name), *buf; | 6875 | char *env_args = getenv("BC_ENV_ARGS"); |
| 6876 | char *buf; | ||
| 6877 | 6877 | ||
| 6878 | if (!env_args) return; | 6878 | if (!env_args) return; |
| 6879 | 6879 | ||
| @@ -6881,40 +6881,34 @@ static void bc_vm_envArgs(void) | |||
| 6881 | buf = G.env_args; | 6881 | buf = G.env_args; |
| 6882 | 6882 | ||
| 6883 | bc_vec_init(&v, sizeof(char *), NULL); | 6883 | bc_vec_init(&v, sizeof(char *), NULL); |
| 6884 | bc_vec_push(&v, &bc_args_env_name); | 6884 | bc_vec_push(&v, &nullptr); |
| 6885 | 6885 | ||
| 6886 | while (*buf != 0) { | 6886 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6887 | if (!isspace(*buf)) { | 6887 | bc_vec_push(&v, &buf); |
| 6888 | bc_vec_push(&v, &buf); | 6888 | buf = skip_non_whitespace(buf); |
| 6889 | while (*buf != 0 && !isspace(*buf)) ++buf; | 6889 | if (!*buf) |
| 6890 | if (*buf != 0) (*(buf++)) = '\0'; | 6890 | break; |
| 6891 | } | 6891 | *buf++ = '\0'; |
| 6892 | else | ||
| 6893 | ++buf; | ||
| 6894 | } | 6892 | } |
| 6895 | 6893 | ||
| 6896 | bc_args((int) v.len, (char **) v.v); | 6894 | bc_vec_push(&v, &nullptr); |
| 6895 | bc_args((char **) v.v); | ||
| 6897 | 6896 | ||
| 6898 | bc_vec_free(&v); | 6897 | bc_vec_free(&v); |
| 6899 | } | 6898 | } |
| 6900 | #endif // ENABLE_BC | 6899 | #endif // ENABLE_BC |
| 6901 | 6900 | ||
| 6902 | static size_t bc_vm_envLen(const char *var) | 6901 | static unsigned bc_vm_envLen(const char *var) |
| 6903 | { | 6902 | { |
| 6904 | char *lenv = getenv(var); | 6903 | char *lenv; |
| 6905 | size_t i, len = BC_NUM_PRINT_WIDTH; | 6904 | unsigned len; |
| 6906 | int num; | ||
| 6907 | 6905 | ||
| 6906 | lenv = getenv(var); | ||
| 6907 | len = BC_NUM_PRINT_WIDTH; | ||
| 6908 | if (!lenv) return len; | 6908 | if (!lenv) return len; |
| 6909 | 6909 | ||
| 6910 | len = strlen(lenv); | 6910 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6911 | 6911 | if (errno || len < 2 || len >= INT_MAX) | |
| 6912 | for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]); | ||
| 6913 | if (num) { | ||
| 6914 | len = (size_t) atoi(lenv) - 1; | ||
| 6915 | if (len < 2 || len >= INT32_MAX) len = BC_NUM_PRINT_WIDTH; | ||
| 6916 | } | ||
| 6917 | else | ||
| 6918 | len = BC_NUM_PRINT_WIDTH; | 6912 | len = BC_NUM_PRINT_WIDTH; |
| 6919 | 6913 | ||
| 6920 | return len; | 6914 | return len; |
| @@ -7314,7 +7308,7 @@ static void bc_vm_free(void) | |||
| 7314 | } | 7308 | } |
| 7315 | #endif | 7309 | #endif |
| 7316 | 7310 | ||
| 7317 | static void bc_program_init(size_t line_len) | 7311 | static void bc_program_init(void) |
| 7318 | { | 7312 | { |
| 7319 | size_t idx; | 7313 | size_t idx; |
| 7320 | BcInstPtr ip; | 7314 | BcInstPtr ip; |
| @@ -7323,7 +7317,6 @@ static void bc_program_init(size_t line_len) | |||
| 7323 | memset(&ip, 0, sizeof(BcInstPtr)); | 7317 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7324 | 7318 | ||
| 7325 | /* G.prog.nchars = G.prog.scale = 0; - already is */ | 7319 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
| 7326 | G.prog.len = line_len; | ||
| 7327 | 7320 | ||
| 7328 | bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE); | 7321 | bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE); |
| 7329 | bc_num_ten(&G.prog.ib); | 7322 | bc_num_ten(&G.prog.ib); |
| @@ -7370,17 +7363,12 @@ static void bc_program_init(size_t line_len) | |||
| 7370 | bc_vec_push(&G.prog.stack, &ip); | 7363 | bc_vec_push(&G.prog.stack, &ip); |
| 7371 | } | 7364 | } |
| 7372 | 7365 | ||
| 7373 | static void bc_vm_init(const char *env_len) | 7366 | static void bc_vm_init(void) |
| 7374 | { | 7367 | { |
| 7375 | size_t len = bc_vm_envLen(env_len); | ||
| 7376 | |||
| 7377 | bc_vec_init(&G.files, sizeof(char *), NULL); | 7368 | bc_vec_init(&G.files, sizeof(char *), NULL); |
| 7378 | 7369 | if (IS_BC) | |
| 7379 | if (IS_BC) { | ||
| 7380 | bc_vm_envArgs(); | 7370 | bc_vm_envArgs(); |
| 7381 | } | 7371 | bc_program_init(); |
| 7382 | |||
| 7383 | bc_program_init(len); | ||
| 7384 | if (IS_BC) { | 7372 | if (IS_BC) { |
| 7385 | bc_parse_init(&G.prs, BC_PROG_MAIN); | 7373 | bc_parse_init(&G.prs, BC_PROG_MAIN); |
| 7386 | } else { | 7374 | } else { |
| @@ -7388,16 +7376,16 @@ static void bc_vm_init(const char *env_len) | |||
| 7388 | } | 7376 | } |
| 7389 | } | 7377 | } |
| 7390 | 7378 | ||
| 7391 | static BcStatus bc_vm_run(int argc, char *argv[], | 7379 | static BcStatus bc_vm_run(char **argv, const char *env_len) |
| 7392 | const char *env_len) | ||
| 7393 | { | 7380 | { |
| 7394 | BcStatus st; | 7381 | BcStatus st; |
| 7395 | 7382 | ||
| 7396 | bc_vm_init(env_len); | 7383 | G.prog.len = bc_vm_envLen(env_len); |
| 7397 | bc_args(argc, argv); | ||
| 7398 | 7384 | ||
| 7399 | G.ttyin = isatty(0); | 7385 | bc_vm_init(); |
| 7386 | bc_args(argv); | ||
| 7400 | 7387 | ||
| 7388 | G.ttyin = isatty(0); | ||
| 7401 | if (G.ttyin) { | 7389 | if (G.ttyin) { |
| 7402 | #if ENABLE_FEATURE_BC_SIGNALS | 7390 | #if ENABLE_FEATURE_BC_SIGNALS |
| 7403 | // With SA_RESTART, most system calls will restart | 7391 | // With SA_RESTART, most system calls will restart |
| @@ -7421,6 +7409,7 @@ static BcStatus bc_vm_run(int argc, char *argv[], | |||
| 7421 | if (!(option_mask32 & BC_FLAG_Q)) | 7409 | if (!(option_mask32 & BC_FLAG_Q)) |
| 7422 | bc_vm_info(); | 7410 | bc_vm_info(); |
| 7423 | } | 7411 | } |
| 7412 | |||
| 7424 | st = bc_vm_exec(); | 7413 | st = bc_vm_exec(); |
| 7425 | 7414 | ||
| 7426 | #if ENABLE_FEATURE_CLEAN_UP | 7415 | #if ENABLE_FEATURE_CLEAN_UP |
| @@ -7432,23 +7421,23 @@ static BcStatus bc_vm_run(int argc, char *argv[], | |||
| 7432 | 7421 | ||
| 7433 | #if ENABLE_BC | 7422 | #if ENABLE_BC |
| 7434 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 7423 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7435 | int bc_main(int argc, char **argv) | 7424 | int bc_main(int argc UNUSED_PARAM, char **argv) |
| 7436 | { | 7425 | { |
| 7437 | INIT_G(); | 7426 | INIT_G(); |
| 7438 | G.sbgn = G.send = '"'; | 7427 | G.sbgn = G.send = '"'; |
| 7439 | 7428 | ||
| 7440 | return bc_vm_run(argc, argv, "BC_LINE_LENGTH"); | 7429 | return bc_vm_run(argv, "BC_LINE_LENGTH"); |
| 7441 | } | 7430 | } |
| 7442 | #endif | 7431 | #endif |
| 7443 | 7432 | ||
| 7444 | #if ENABLE_DC | 7433 | #if ENABLE_DC |
| 7445 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 7434 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7446 | int dc_main(int argc, char **argv) | 7435 | int dc_main(int argc UNUSED_PARAM, char **argv) |
| 7447 | { | 7436 | { |
| 7448 | INIT_G(); | 7437 | INIT_G(); |
| 7449 | G.sbgn = '['; | 7438 | G.sbgn = '['; |
| 7450 | G.send = ']'; | 7439 | G.send = ']'; |
| 7451 | 7440 | ||
| 7452 | return bc_vm_run(argc, argv, "DC_LINE_LENGTH"); | 7441 | return bc_vm_run(argv, "DC_LINE_LENGTH"); |
| 7453 | } | 7442 | } |
| 7454 | #endif | 7443 | #endif |
