23
23
* resulting sed_cmd_t structures are appended to a linked list
24
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
26
* process_files() does actual sedding, reading data lines from each input FILE*
30
27
* (which could be stdin) and applying the sed command list (sed_cmd_head) to
31
28
* each of the resulting lines.
@@ -141,8 +138,8 @@ struct globals {
141
138
smallint exitcode ;
142
139
143
140
/* 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 ;
146
143
FILE * current_fp ;
147
144
148
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
939
/* will be returned if last line in the file
943
940
* doesn't end with either '\n' or '\0' */
944
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
943
FILE * fp = G .current_fp ;
947
944
if (!fp ) {
948
945
const char * path = G .input_file_list [G .current_input_file ];
@@ -1414,12 +1411,6 @@ static void add_cmd_block(char *cmdstr)
1414
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
1414
int sed_main (int argc , char * * argv ) MAIN_EXTERNALLY_VISIBLE ;
1424
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
1492
/* argv[0..(argc-1)] should be names of file to process. If no
1502
1493
* files were specified or '-' was specified, take input from stdin.
1503
1494
* Otherwise, we process all the files specified. */
1504
- if (argv [0 ] == NULL ) {
1495
+ G .input_file_list = argv ;
1496
+ if (!argv [0 ]) {
1505
1497
if (opt & OPT_in_place )
1506
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
1501
} else {
1509
- int i ;
1502
+ goto start ;
1510
1503
1511
- for (i = 0 ; argv [ i ]; i ++ ) {
1504
+ for (; * argv ; argv ++ ) {
1512
1505
struct stat statbuf ;
1513
1506
int nonstdoutfd ;
1514
1507
sed_cmd_t * sed_cmd ;
1515
1508
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 :
1522
1511
if (!(opt & OPT_in_place )) {
1512
+ if (LONE_DASH (* argv )) {
1513
+ * argv = (char * )bb_msg_standard_input ;
1514
+ process_files ();
1515
+ }
1523
1516
continue ;
1524
1517
}
1525
1518
1526
1519
/* -i: process each FILE separately: */
1527
1520
1528
- G .outname = xasprintf ("%sXXXXXX" , argv [ i ] );
1521
+ G .outname = xasprintf ("%sXXXXXX" , * argv );
1529
1522
nonstdoutfd = xmkstemp (G .outname );
1530
1523
G .nonstdout = xfdopen_for_write (nonstdoutfd );
1531
1524
1532
1525
/* Set permissions/owner of output file */
1533
- stat (argv [ i ] , & statbuf );
1526
+ stat (* argv , & statbuf );
1534
1527
/* chmod'ing AFTER chown would preserve suid/sgid bits,
1535
1528
* but GNU sed 4.2.1 does not preserve them either */
1536
1529
fchmod (nonstdoutfd , statbuf .st_mode );
@@ -1541,12 +1534,12 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
1541
1534
G .nonstdout = stdout ;
1542
1535
1543
1536
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 );
1546
1539
free (backupname );
1547
1540
}
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?
1550
1543
free (G .outname );
1551
1544
G .outname = NULL ;
1552
1545
0 commit comments