diff options
author | Andreas Gruenbacher <agruen@suse.de> | 2010-09-16 16:50:21 +0200 |
---|---|---|
committer | Andreas Gruenbacher <agruen@suse.de> | 2010-09-17 02:03:26 +0200 |
commit | a1a66e6032c293c9ea8885d2ff303061331e3cd6 (patch) | |
tree | f702b5e946e10efd03a340d89c27630dad62a8ac | |
parent | 93129f45a1fc1f824b652f50898fb57440449f73 (diff) | |
download | patch-a1a66e6032c293c9ea8885d2ff303061331e3cd6.tar.gz |
Stop using mktemp() for TMPREJNAME
* src/patch.c (main): Compute TMPREJNAME with make_tempfile() only if
needed.
(abort_hunk, init_reject): Pass through the output file name, and base
TMPREJNAME on it.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/patch.c | 41 |
2 files changed, 31 insertions, 15 deletions
@@ -17,6 +17,11 @@ * src/patch.c (abort_hunk_unified): Replace unnecessary local variable. + * src/patch.c (main): Compute TMPREJNAME with make_tempfile() only if + needed. + (abort_hunk, init_reject): Pass through the output file name, and base + TMPREJNAME on it. + 2010-07-27 Tim Waugh <twaugh@redhat.com> * src/patch.c: Stops "patch --get 1" from segfaulting. diff --git a/src/patch.c b/src/patch.c index 05890d3..acc4f56 100644 --- a/src/patch.c +++ b/src/patch.c @@ -45,12 +45,12 @@ static void cleanup (void); static void get_some_switches (void); static void init_output (struct outstate *); static FILE *open_outfile (char const *); -static void init_reject (void); +static void init_reject (char const *); static void reinitialize_almost_everything (void); static void remove_if_needed (char const *, int *); static void usage (FILE *, int) __attribute__((noreturn)); -static void abort_hunk (bool, bool); +static void abort_hunk (char const *, bool, bool); static void abort_hunk_context (bool, bool); static void abort_hunk_unified (bool, bool); @@ -140,7 +140,6 @@ main (int argc, char **argv) /* Cons up the names of the global temporary files. Do this before `cleanup' can possibly be called (e.g. by `pfatal'). */ TMPOUTNAME = make_temp ('o'); - TMPREJNAME = make_temp ('r'); /* parse switches */ Argc = argc; @@ -184,6 +183,16 @@ main (int argc, char **argv) bool mismatch = false; char const *outname = NULL; + if (TMPREJNAME_needs_removal) + { + if (rejfp) + { + fclose (rejfp); + rejfp = NULL; + } + remove_if_needed (TMPREJNAME, &TMPREJNAME_needs_removal); + } + if (! skip_rest_of_patch && ! file_type) { say ("File %s: can't change file type from 0%o to 0%o.\n", @@ -255,9 +264,6 @@ main (int argc, char **argv) outstate.ofp = create_output_file (TMPOUTNAME, exclusive); } - /* initialize reject file */ - init_reject (); - /* find out where all the lines are */ if (!skip_rest_of_patch) scan_input (inname, file_type); @@ -345,7 +351,7 @@ main (int argc, char **argv) || ! where || ! apply_hunk (&outstate, where)))) { - abort_hunk (! failed, reverse); + abort_hunk (outname, ! failed, reverse); failed++; if (verbosity == VERBOSE || (! skip_rest_of_patch && verbosity != SILENT)) @@ -381,7 +387,6 @@ main (int argc, char **argv) fclose (outstate.ofp); outstate.ofp = 0; } - fclose (rejfp); continue; } @@ -485,10 +490,10 @@ main (int argc, char **argv) if (diff_type != ED_DIFF) { struct stat rejst; - if ((failed && fstat (fileno (rejfp), &rejst) != 0) - || fclose (rejfp) != 0) - write_fatal (); if (failed) { + if (fstat (fileno (rejfp), &rejst) != 0 || fclose (rejfp) != 0) + write_fatal (); + rejfp = NULL; somefailed = true; say ("%d out of %d hunk%s %s", failed, hunk, "s" + (hunk == 1), skip_rest_of_patch ? "ignored" : "FAILED"); @@ -1239,8 +1244,10 @@ abort_hunk_context (bool header, bool reverse) /* Output the rejected hunk. */ static void -abort_hunk (bool header, bool reverse) +abort_hunk (char const *outname, bool header, bool reverse) { + if (! TMPREJNAME_needs_removal) + init_reject (outname); if (reject_format == UNI_DIFF || (reject_format == NO_DIFF && diff_type == UNI_DIFF)) abort_hunk_unified (header, reverse); @@ -1445,11 +1452,15 @@ open_outfile (char const *name) /* Open a file to put hunks we can't locate. */ static void -init_reject (void) +init_reject (char const *outname) { - int exclusive = TMPREJNAME_needs_removal ? 0 : O_EXCL; + int fd; + fd = make_tempfile (&TMPREJNAME, 'r', outname, O_WRONLY | binary_transput, + 0666); TMPREJNAME_needs_removal = 1; - rejfp = create_output_file (TMPREJNAME, exclusive); + rejfp = fdopen (fd, binary_transput ? "wb" : "w"); + if (! rejfp) + pfatal ("Can't open stream for file %s", quotearg (TMPREJNAME)); } /* Copy input file to output, up to wherever hunk is to be applied. */ |