|
|
0a6a4f |
commit 2a32bf09f5e9572da4be183bb0dbde8164351474
|
|
|
0a6a4f |
Author: Andreas Gruenbacher <agruen@gnu.org>
|
|
|
0a6a4f |
Date: Fri Apr 6 20:32:46 2018 +0200
|
|
|
0a6a4f |
|
|
|
0a6a4f |
Minor cleanups in do_ed_script
|
|
|
0a6a4f |
|
|
|
0a6a4f |
* src/pch.c (do_ed_script): Minor cleanups.
|
|
|
0a6a4f |
|
|
|
0a6a4f |
diff --git a/src/pch.c b/src/pch.c
|
|
|
0a6a4f |
index 1f14624..1055542 100644
|
|
|
0a6a4f |
--- a/src/pch.c
|
|
|
0a6a4f |
+++ b/src/pch.c
|
|
|
0a6a4f |
@@ -2396,6 +2396,10 @@ do_ed_script (char const *inname, char const *outname,
|
|
|
0a6a4f |
char const *tmpname;
|
|
|
0a6a4f |
int tmpfd;
|
|
|
0a6a4f |
pid_t pid;
|
|
|
0a6a4f |
+ int exclusive = *outname_needs_removal ? 0 : O_EXCL;
|
|
|
0a6a4f |
+ char const **ed_argv;
|
|
|
0a6a4f |
+ int stdin_dup, status;
|
|
|
0a6a4f |
+
|
|
|
0a6a4f |
|
|
|
0a6a4f |
if (! dry_run && ! skip_rest_of_patch)
|
|
|
0a6a4f |
{
|
|
|
0a6a4f |
@@ -2443,7 +2447,7 @@ do_ed_script (char const *inname, char const *outname,
|
|
|
0a6a4f |
break;
|
|
|
0a6a4f |
}
|
|
|
0a6a4f |
}
|
|
|
0a6a4f |
- if (!tmpfp)
|
|
|
0a6a4f |
+ if (dry_run || skip_rest_of_patch)
|
|
|
0a6a4f |
return;
|
|
|
0a6a4f |
if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
|
|
|
0a6a4f |
|| fflush (tmpfp) != 0)
|
|
|
0a6a4f |
@@ -2452,36 +2456,29 @@ do_ed_script (char const *inname, char const *outname,
|
|
|
0a6a4f |
if (lseek (tmpfd, 0, SEEK_SET) == -1)
|
|
|
0a6a4f |
pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
|
|
|
0a6a4f |
|
|
|
0a6a4f |
- if (! dry_run && ! skip_rest_of_patch) {
|
|
|
0a6a4f |
- int exclusive = *outname_needs_removal ? 0 : O_EXCL;
|
|
|
0a6a4f |
- char const **ed_argv;
|
|
|
0a6a4f |
- int stdin_dup, status;
|
|
|
0a6a4f |
-
|
|
|
0a6a4f |
+ if (inerrno != ENOENT)
|
|
|
0a6a4f |
+ {
|
|
|
0a6a4f |
*outname_needs_removal = true;
|
|
|
0a6a4f |
- if (inerrno != ENOENT)
|
|
|
0a6a4f |
- {
|
|
|
0a6a4f |
- *outname_needs_removal = true;
|
|
|
0a6a4f |
- copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
|
|
|
0a6a4f |
- }
|
|
|
0a6a4f |
- fflush (stdout);
|
|
|
0a6a4f |
-
|
|
|
0a6a4f |
- if ((stdin_dup = dup (0)) == -1
|
|
|
0a6a4f |
- || dup2 (tmpfd, 0) == -1)
|
|
|
0a6a4f |
- pfatal ("Failed to duplicate standard input");
|
|
|
0a6a4f |
- assert (outname[0] != '!' && outname[0] != '-');
|
|
|
0a6a4f |
- ed_argv = alloca (4 * sizeof * ed_argv);
|
|
|
0a6a4f |
- ed_argv[0] = editor_program;
|
|
|
0a6a4f |
- ed_argv[1] = "-";
|
|
|
0a6a4f |
- ed_argv[2] = outname;
|
|
|
0a6a4f |
- ed_argv[3] = (char *) NULL;
|
|
|
0a6a4f |
- status = execute (editor_program, editor_program, (char **)ed_argv,
|
|
|
0a6a4f |
- false, false, false, false, true, false, NULL);
|
|
|
0a6a4f |
- if (status)
|
|
|
0a6a4f |
- fatal ("%s FAILED", editor_program);
|
|
|
0a6a4f |
- if (dup2 (stdin_dup, 0) == -1
|
|
|
0a6a4f |
- || close (stdin_dup) == -1)
|
|
|
0a6a4f |
- pfatal ("Failed to duplicate standard input");
|
|
|
0a6a4f |
- }
|
|
|
0a6a4f |
+ copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
|
|
|
0a6a4f |
+ }
|
|
|
0a6a4f |
+ fflush (stdout);
|
|
|
0a6a4f |
+
|
|
|
0a6a4f |
+ if ((stdin_dup = dup (0)) == -1
|
|
|
0a6a4f |
+ || dup2 (tmpfd, 0) == -1)
|
|
|
0a6a4f |
+ pfatal ("Failed to duplicate standard input");
|
|
|
0a6a4f |
+ assert (outname[0] != '!' && outname[0] != '-');
|
|
|
0a6a4f |
+ ed_argv = alloca (4 * sizeof * ed_argv);
|
|
|
0a6a4f |
+ ed_argv[0] = editor_program;
|
|
|
0a6a4f |
+ ed_argv[1] = "-";
|
|
|
0a6a4f |
+ ed_argv[2] = outname;
|
|
|
0a6a4f |
+ ed_argv[3] = (char *) NULL;
|
|
|
0a6a4f |
+ status = execute (editor_program, editor_program, (char **)ed_argv,
|
|
|
0a6a4f |
+ false, false, false, false, true, false, NULL);
|
|
|
0a6a4f |
+ if (status)
|
|
|
0a6a4f |
+ fatal ("%s FAILED", editor_program);
|
|
|
0a6a4f |
+ if (dup2 (stdin_dup, 0) == -1
|
|
|
0a6a4f |
+ || close (stdin_dup) == -1)
|
|
|
0a6a4f |
+ pfatal ("Failed to duplicate standard input");
|
|
|
0a6a4f |
|
|
|
0a6a4f |
fclose (tmpfp);
|
|
|
0a6a4f |
safe_unlink (tmpname);
|