Builtin git-read-tree.
[debian-git.git] / pager.c
blob9a30939016620072f319b5fef81d0c913513a641
1 #include "cache.h"
3 /*
4 * This is split up from the rest of git so that we might do
5 * something different on Windows, for example.
6 */
8 static void run_pager(const char *pager)
10 execlp(pager, pager, NULL);
11 execl("/bin/sh", "sh", "-c", pager, NULL);
14 void setup_pager(void)
16 pid_t pid;
17 int fd[2];
18 const char *pager = getenv("PAGER");
20 if (!isatty(1))
21 return;
22 if (!pager)
23 pager = "less";
24 else if (!*pager || !strcmp(pager, "cat"))
25 return;
27 if (pipe(fd) < 0)
28 return;
29 pid = fork();
30 if (pid < 0) {
31 close(fd[0]);
32 close(fd[1]);
33 return;
36 /* return in the child */
37 if (!pid) {
38 dup2(fd[1], 1);
39 close(fd[0]);
40 close(fd[1]);
41 return;
44 /* The original process turns into the PAGER */
45 dup2(fd[0], 0);
46 close(fd[0]);
47 close(fd[1]);
49 setenv("LESS", "-S", 0);
50 run_pager(pager);
51 die("unable to execute pager '%s'", pager);
52 exit(255);