sed: fix "sed CMD -i nonexistent_file". Closes 7484
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 5 Oct 2014 00:44:34 +0000 (02:44 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 5 Oct 2014 00:44:34 +0000 (02:44 +0200)
function                                             old     new   delta
sed_main                                             643     676     +33

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/sed.c

index e1b8352fd033b315b9f1ea24c92bba246fa371e2..2c64ad500918473c8fa76d2d87ddf8d2032fbbca 100644 (file)
@@ -1522,12 +1522,16 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 
                        /* -i: process each FILE separately: */
 
+                       if (stat(*argv, &statbuf) != 0) {
+                               bb_simple_perror_msg(*argv);
+                               G.exitcode = EXIT_FAILURE;
+                               G.current_input_file++;
+                               continue;
+                       }
                        G.outname = xasprintf("%sXXXXXX", *argv);
                        nonstdoutfd = xmkstemp(G.outname);
                        G.nonstdout = xfdopen_for_write(nonstdoutfd);
-
                        /* Set permissions/owner of output file */
-                       stat(*argv, &statbuf);
                        /* chmod'ing AFTER chown would preserve suid/sgid bits,
                         * but GNU sed 4.2.1 does not preserve them either */
                        fchmod(nonstdoutfd, statbuf.st_mode);