@matheusbsbs / matheus-bagliano / commits / 7683b6e81f
Fork 0

Avoid a dup2(2) in apply_filter() - start_command() can do it for us.

When apply_filter() runs the external (clean or smudge) filter program, it needs to pass the writable end of a pipe as its stdout. For this purpose, it used to dup2(2) the file descriptor explicitly to stdout. Now we use the facilities of start_command() to do it for us. Furthermore, the path argument of a subordinate function, filter_buffer(), was not used, so here we replace it to pass the fd instead. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Johannes Sixt committed Oct 19, 2007 at 21:48 UTC 7683b6e81fa0f1f55d4974d69fb87c7c7b6b394e
1 file changed +3 -4
convert.c
+3 -4
@@ -192,7 +192,7 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
192 return 1;
193 }
194
195 -static int filter_buffer(const char *path, const char *src,
195 +static int filter_buffer(int fd, const char *src,
196 unsigned long size, const char *cmd)
197 {
198 /*
@@ -205,6 +205,7 @@ static int filter_buffer(const char *path, const char *src,
205 memset(&child_process, 0, sizeof(child_process));
206 child_process.argv = argv;
207 child_process.in = -1;
208 + child_process.out = fd;
209
210 if (start_command(&child_process))
211 return error("cannot fork to run external filter %s", cmd);
@@ -254,10 +255,8 @@ static int apply_filter(const char *path, const char *src, size_t len,
255 return 0;
256 }
257 if (!child_process.pid) {
257 - dup2(pipe_feed[1], 1);
258 close(pipe_feed[0]);
259 - close(pipe_feed[1]);
260 - exit(filter_buffer(path, src, len, cmd));
259 + exit(filter_buffer(pipe_feed[1], src, len, cmd));
260 }
261 close(pipe_feed[1]);
262