Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/filterdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1828,15 +1828,10 @@ int main (int argc, char *argv[])

if (inplace_mode) {
/* Redirect stdout to temporary file for in-place processing */
FILE *temp_output = xtmpfile();
FILE *old_stdout = stdout;
stdout = temp_output;
FILE *temp_output = redirectfd(stdout);

filterdiff (f, argv[i]);

/* Restore stdout */
stdout = old_stdout;

/* Write temp file contents back to original file */
if (write_file_inplace(argv[i], temp_output) != 0) {
error (EXIT_FAILURE, errno, "failed to write %s", argv[i]);
Expand Down
24 changes: 24 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ FILE *xtmpfile (void)
return ret;
}

FILE *redirectfd (FILE* fd)
{
FILE *ret;
char *tmpfname;
char *tmpdir = getenv ("TMPDIR");
size_t tmpdirlen;

if (tmpdir == NULL) {
tmpdir = P_tmpdir;
}

tmpdirlen = strlen (tmpdir);
tmpfname = xmalloc (tmpdirlen + 8);
strcpy (tmpfname, tmpdir);
strcpy (tmpfname + tmpdirlen, "/XXXXXX");
close(mkstemp(tmpfname));
ret = freopen (tmpfname, "w+b", fd);
if (ret == NULL)
error (EXIT_FAILURE, errno, "freopen");
unlink (tmpfname);
free (tmpfname);
return ret;
}

/*
* Pattern operations.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ char *xstrndup (const char *s, const size_t n);
int xmkstemp (char *pattern);
/* safe tmpfile */
FILE *xtmpfile (void);
/* redirect fd to temp file */
FILE *redirectfd (FILE* fd);

FILE *xopen(const char *file, const char *mode);
FILE *xopen_seekable(const char *file, const char *mode);
Expand Down
Loading