Skip to content

Commit deb0769

Browse files
author
Denys Vlasenko
committed
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 <[email protected]>
1 parent 259b3c0 commit deb0769

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

editors/sed.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
* resulting sed_cmd_t structures are appended to a linked list
2424
* (G.sed_cmd_head/G.sed_cmd_tail).
2525
*
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-
*
2926
* process_files() does actual sedding, reading data lines from each input FILE*
3027
* (which could be stdin) and applying the sed command list (sed_cmd_head) to
3128
* each of the resulting lines.
@@ -141,8 +138,8 @@ struct globals {
141138
smallint exitcode;
142139

143140
/* list of input files */
144-
int input_file_count, current_input_file;
145-
const char **input_file_list;
141+
int current_input_file, last_input_file;
142+
char **input_file_list;
146143
FILE *current_fp;
147144

148145
regmatch_t regmatch[10];
@@ -942,7 +939,7 @@ static char *get_next_line(char *gets_char, char *last_puts_char, char last_gets
942939
/* will be returned if last line in the file
943940
* doesn't end with either '\n' or '\0' */
944941
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++) {
946943
FILE *fp = G.current_fp;
947944
if (!fp) {
948945
const char *path = G.input_file_list[G.current_input_file];
@@ -1414,12 +1411,6 @@ static void add_cmd_block(char *cmdstr)
14141411
free(sv);
14151412
}
14161413

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-
14231414
int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
14241415
int sed_main(int argc UNUSED_PARAM, char **argv)
14251416
{
@@ -1501,36 +1492,38 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
15011492
/* argv[0..(argc-1)] should be names of file to process. If no
15021493
* files were specified or '-' was specified, take input from stdin.
15031494
* Otherwise, we process all the files specified. */
1504-
if (argv[0] == NULL) {
1495+
G.input_file_list = argv;
1496+
if (!argv[0]) {
15051497
if (opt & OPT_in_place)
15061498
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 */
15081501
} else {
1509-
int i;
1502+
goto start;
15101503

1511-
for (i = 0; argv[i]; i++) {
1504+
for (; *argv; argv++) {
15121505
struct stat statbuf;
15131506
int nonstdoutfd;
15141507
sed_cmd_t *sed_cmd;
15151508

1516-
if (LONE_DASH(argv[i]) && !(opt & OPT_in_place)) {
1517-
add_input_file(bb_msg_standard_input);
1518-
process_files();
1519-
continue;
1520-
}
1521-
add_input_file(argv[i]);
1509+
G.last_input_file++;
1510+
start:
15221511
if (!(opt & OPT_in_place)) {
1512+
if (LONE_DASH(*argv)) {
1513+
*argv = (char*)bb_msg_standard_input;
1514+
process_files();
1515+
}
15231516
continue;
15241517
}
15251518

15261519
/* -i: process each FILE separately: */
15271520

1528-
G.outname = xasprintf("%sXXXXXX", argv[i]);
1521+
G.outname = xasprintf("%sXXXXXX", *argv);
15291522
nonstdoutfd = xmkstemp(G.outname);
15301523
G.nonstdout = xfdopen_for_write(nonstdoutfd);
15311524

15321525
/* Set permissions/owner of output file */
1533-
stat(argv[i], &statbuf);
1526+
stat(*argv, &statbuf);
15341527
/* chmod'ing AFTER chown would preserve suid/sgid bits,
15351528
* but GNU sed 4.2.1 does not preserve them either */
15361529
fchmod(nonstdoutfd, statbuf.st_mode);
@@ -1541,12 +1534,12 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
15411534
G.nonstdout = stdout;
15421535

15431536
if (opt_i) {
1544-
char *backupname = xasprintf("%s%s", argv[i], opt_i);
1545-
xrename(argv[i], backupname);
1537+
char *backupname = xasprintf("%s%s", *argv, opt_i);
1538+
xrename(*argv, backupname);
15461539
free(backupname);
15471540
}
1548-
/* else unlink(argv[i]); - rename below does this */
1549-
xrename(G.outname, argv[i]); //TODO: rollback backup on error?
1541+
/* else unlink(*argv); - rename below does this */
1542+
xrename(G.outname, *argv); //TODO: rollback backup on error?
15501543
free(G.outname);
15511544
G.outname = NULL;
15521545

0 commit comments

Comments
 (0)