diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-11-28 12:08:51 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-11-28 12:08:51 +0100 |
commit | deb0769b26c65c8796af105c705119c938c45b12 (patch) | |
tree | 7b25b6495ed28790b06f26b5dd9f9dd570ac4c88 | |
parent | 259b3c047aea430c4aaecbdb9580a07e67691e8d (diff) | |
download | busybox-w32-deb0769b26c65c8796af105c705119c938c45b12.tar.gz busybox-w32-deb0769b26c65c8796af105c705119c938c45b12.tar.bz2 busybox-w32-deb0769b26c65c8796af105c705119c938c45b12.zip |
sed: code shrink
function old new delta
get_next_line 246 250 +4
sed_main 671 662 -9
add_input_file 47 - -47
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/1 up/down: 4/-56) Total: -52 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/sed.c | 49 |
1 files changed, 21 insertions, 28 deletions
diff --git a/editors/sed.c b/editors/sed.c index 87fa00291..31fb103ec 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -23,9 +23,6 @@ | |||
23 | * resulting sed_cmd_t structures are appended to a linked list | 23 | * resulting sed_cmd_t structures are appended to a linked list |
24 | * (G.sed_cmd_head/G.sed_cmd_tail). | 24 | * (G.sed_cmd_head/G.sed_cmd_tail). |
25 | * | 25 | * |
26 | * add_input_file() adds a char* to the list of input files. We need to | ||
27 | * know all input sources ahead of time to find the last line for the $ match. | ||
28 | * | ||
29 | * process_files() does actual sedding, reading data lines from each input FILE* | 26 | * process_files() does actual sedding, reading data lines from each input FILE* |
30 | * (which could be stdin) and applying the sed command list (sed_cmd_head) to | 27 | * (which could be stdin) and applying the sed command list (sed_cmd_head) to |
31 | * each of the resulting lines. | 28 | * each of the resulting lines. |
@@ -141,8 +138,8 @@ struct globals { | |||
141 | smallint exitcode; | 138 | smallint exitcode; |
142 | 139 | ||
143 | /* list of input files */ | 140 | /* list of input files */ |
144 | int input_file_count, current_input_file; | 141 | int current_input_file, last_input_file; |
145 | const char **input_file_list; | 142 | char **input_file_list; |
146 | FILE *current_fp; | 143 | FILE *current_fp; |
147 | 144 | ||
148 | regmatch_t regmatch[10]; | 145 | regmatch_t regmatch[10]; |
@@ -942,7 +939,7 @@ static char *get_next_line(char *gets_char, char *last_puts_char, char last_gets | |||
942 | /* will be returned if last line in the file | 939 | /* will be returned if last line in the file |
943 | * doesn't end with either '\n' or '\0' */ | 940 | * doesn't end with either '\n' or '\0' */ |
944 | gc = NO_EOL_CHAR; | 941 | gc = NO_EOL_CHAR; |
945 | for (; G.input_file_list[G.current_input_file]; G.current_input_file++) { | 942 | for (; G.current_input_file <= G.last_input_file; G.current_input_file++) { |
946 | FILE *fp = G.current_fp; | 943 | FILE *fp = G.current_fp; |
947 | if (!fp) { | 944 | if (!fp) { |
948 | const char *path = G.input_file_list[G.current_input_file]; | 945 | const char *path = G.input_file_list[G.current_input_file]; |
@@ -1414,12 +1411,6 @@ static void add_cmd_block(char *cmdstr) | |||
1414 | free(sv); | 1411 | free(sv); |
1415 | } | 1412 | } |
1416 | 1413 | ||
1417 | static void add_input_file(const char *file) | ||
1418 | { | ||
1419 | G.input_file_list = xrealloc_vector(G.input_file_list, 2, G.input_file_count); | ||
1420 | G.input_file_list[G.input_file_count++] = file; | ||
1421 | } | ||
1422 | |||
1423 | int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 1414 | int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
1424 | int sed_main(int argc UNUSED_PARAM, char **argv) | 1415 | int sed_main(int argc UNUSED_PARAM, char **argv) |
1425 | { | 1416 | { |
@@ -1501,36 +1492,38 @@ int sed_main(int argc UNUSED_PARAM, char **argv) | |||
1501 | /* argv[0..(argc-1)] should be names of file to process. If no | 1492 | /* argv[0..(argc-1)] should be names of file to process. If no |
1502 | * files were specified or '-' was specified, take input from stdin. | 1493 | * files were specified or '-' was specified, take input from stdin. |
1503 | * Otherwise, we process all the files specified. */ | 1494 | * Otherwise, we process all the files specified. */ |
1504 | if (argv[0] == NULL) { | 1495 | G.input_file_list = argv; |
1496 | if (!argv[0]) { | ||
1505 | if (opt & OPT_in_place) | 1497 | if (opt & OPT_in_place) |
1506 | bb_error_msg_and_die(bb_msg_requires_arg, "-i"); | 1498 | bb_error_msg_and_die(bb_msg_requires_arg, "-i"); |
1507 | add_input_file(bb_msg_standard_input); | 1499 | argv[0] = (char*)bb_msg_standard_input; |
1500 | /* G.last_input_file = 0; - already is */ | ||
1508 | } else { | 1501 | } else { |
1509 | int i; | 1502 | goto start; |
1510 | 1503 | ||
1511 | for (i = 0; argv[i]; i++) { | 1504 | for (; *argv; argv++) { |
1512 | struct stat statbuf; | 1505 | struct stat statbuf; |
1513 | int nonstdoutfd; | 1506 | int nonstdoutfd; |
1514 | sed_cmd_t *sed_cmd; | 1507 | sed_cmd_t *sed_cmd; |
1515 | 1508 | ||
1516 | if (LONE_DASH(argv[i]) && !(opt & OPT_in_place)) { | 1509 | G.last_input_file++; |
1517 | add_input_file(bb_msg_standard_input); | 1510 | start: |
1518 | process_files(); | ||
1519 | continue; | ||
1520 | } | ||
1521 | add_input_file(argv[i]); | ||
1522 | if (!(opt & OPT_in_place)) { | 1511 | if (!(opt & OPT_in_place)) { |
1512 | if (LONE_DASH(*argv)) { | ||
1513 | *argv = (char*)bb_msg_standard_input; | ||
1514 | process_files(); | ||
1515 | } | ||
1523 | continue; | 1516 | continue; |
1524 | } | 1517 | } |
1525 | 1518 | ||
1526 | /* -i: process each FILE separately: */ | 1519 | /* -i: process each FILE separately: */ |
1527 | 1520 | ||
1528 | G.outname = xasprintf("%sXXXXXX", argv[i]); | 1521 | G.outname = xasprintf("%sXXXXXX", *argv); |
1529 | nonstdoutfd = xmkstemp(G.outname); | 1522 | nonstdoutfd = xmkstemp(G.outname); |
1530 | G.nonstdout = xfdopen_for_write(nonstdoutfd); | 1523 | G.nonstdout = xfdopen_for_write(nonstdoutfd); |
1531 | 1524 | ||
1532 | /* Set permissions/owner of output file */ | 1525 | /* Set permissions/owner of output file */ |
1533 | stat(argv[i], &statbuf); | 1526 | stat(*argv, &statbuf); |
1534 | /* chmod'ing AFTER chown would preserve suid/sgid bits, | 1527 | /* chmod'ing AFTER chown would preserve suid/sgid bits, |
1535 | * but GNU sed 4.2.1 does not preserve them either */ | 1528 | * but GNU sed 4.2.1 does not preserve them either */ |
1536 | fchmod(nonstdoutfd, statbuf.st_mode); | 1529 | fchmod(nonstdoutfd, statbuf.st_mode); |
@@ -1541,12 +1534,12 @@ int sed_main(int argc UNUSED_PARAM, char **argv) | |||
1541 | G.nonstdout = stdout; | 1534 | G.nonstdout = stdout; |
1542 | 1535 | ||
1543 | if (opt_i) { | 1536 | if (opt_i) { |
1544 | char *backupname = xasprintf("%s%s", argv[i], opt_i); | 1537 | char *backupname = xasprintf("%s%s", *argv, opt_i); |
1545 | xrename(argv[i], backupname); | 1538 | xrename(*argv, backupname); |
1546 | free(backupname); | 1539 | free(backupname); |
1547 | } | 1540 | } |
1548 | /* else unlink(argv[i]); - rename below does this */ | 1541 | /* else unlink(*argv); - rename below does this */ |
1549 | xrename(G.outname, argv[i]); //TODO: rollback backup on error? | 1542 | xrename(G.outname, *argv); //TODO: rollback backup on error? |
1550 | free(G.outname); | 1543 | free(G.outname); |
1551 | G.outname = NULL; | 1544 | G.outname = NULL; |
1552 | 1545 | ||