Do not fork PAGER=cat
[git.git] / pager.c
1 #include "cache.h"
2
3 /*
4  * This is split up from the rest of git so that we might do
5  * something different on Windows, for example.
6  */
7
8 static void run_pager(const char *pager)
9 {
10         execlp(pager, pager, NULL);
11 }
12
13 void setup_pager(void)
14 {
15         pid_t pid;
16         int fd[2];
17         const char *pager = getenv("PAGER");
18
19         if (!isatty(1))
20                 return;
21         if (!pager)
22                 pager = "less";
23         else if (!*pager || !strcmp(pager, "cat"))
24                 return;
25
26         if (pipe(fd) < 0)
27                 return;
28         pid = fork();
29         if (pid < 0) {
30                 close(fd[0]);
31                 close(fd[1]);
32                 return;
33         }
34
35         /* return in the child */
36         if (!pid) {
37                 dup2(fd[1], 1);
38                 close(fd[0]);
39                 close(fd[1]);
40                 return;
41         }
42
43         /* The original process turns into the PAGER */
44         dup2(fd[0], 0);
45         close(fd[0]);
46         close(fd[1]);
47
48         setenv("LESS", "-S", 0);
49         run_pager(pager);
50         exit(255);
51 }