diff: move diff.c to diff-lib.c to make room.
[git/debian.git] / pager.c
blobb063353d96eb6d526438c1fa22cacd28bc12b0c1
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);
13 void setup_pager(void)
15 pid_t pid;
16 int fd[2];
17 const char *pager = getenv("PAGER");
19 if (!isatty(1))
20 return;
21 if (!pager)
22 pager = "less";
23 else if (!*pager || !strcmp(pager, "cat"))
24 return;
26 if (pipe(fd) < 0)
27 return;
28 pid = fork();
29 if (pid < 0) {
30 close(fd[0]);
31 close(fd[1]);
32 return;
35 /* return in the child */
36 if (!pid) {
37 dup2(fd[1], 1);
38 close(fd[0]);
39 close(fd[1]);
40 return;
43 /* The original process turns into the PAGER */
44 dup2(fd[0], 0);
45 close(fd[0]);
46 close(fd[1]);
48 setenv("LESS", "-S", 0);
49 run_pager(pager);
50 exit(255);