Skip to content

Commit da9ef60

Browse files
rjustogitster
authored andcommitted
pager: do not close fd 2 unnecessarily
We send errors to the pager since 61b8050 (sending errors to stdout under $PAGER, 2008-02-16). In a833502 (pager: do not dup2 stderr if it is already redirected, 2008-12-15) an exception was introduced to avoid redirecting stderr if it is not connected to a terminal. In such exceptional cases, the close(STDERR_FILENO) we're doing in close_pager_fds, is unnecessary. Furthermore, in a subsequent commit we're going to introduce changes that will involve using close_pager_fds multiple times. With this in mind, controlling when we want to close stderr, become sensible. Let's close(STDERR_FILENO) only when necessary, and pave the way for the upcoming changes. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7309be1 commit da9ef60

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pager.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int pager_use_color = 1;
1414

1515
static struct child_process pager_process;
1616
static char *pager_program;
17+
static int close_fd2;
1718

1819
/* Is the value coming back from term_columns() just a guess? */
1920
static int term_columns_guessed;
@@ -23,7 +24,8 @@ static void close_pager_fds(void)
2324
{
2425
/* signal EOF to pager */
2526
close(1);
26-
close(2);
27+
if (close_fd2)
28+
close(2);
2729
}
2830

2931
static void wait_for_pager_atexit(void)
@@ -141,8 +143,10 @@ void setup_pager(void)
141143

142144
/* original process continues, but writes to the pipe */
143145
dup2(pager_process.in, 1);
144-
if (isatty(2))
146+
if (isatty(2)) {
147+
close_fd2 = 1;
145148
dup2(pager_process.in, 2);
149+
}
146150
close(pager_process.in);
147151

148152
/* this makes sure that the parent terminates after the pager */

0 commit comments

Comments
 (0)