git-tar-tree: fix minor memory leak
[alt-git.git] / pager.c
blob280f57f796c84d1c1fd54e295b542c3b33490b3e
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 pager_in_use = 1; /* means we are emitting to terminal */
29 if (pipe(fd) < 0)
30 return;
31 pid = fork();
32 if (pid < 0) {
33 close(fd[0]);
34 close(fd[1]);
35 return;
38 /* return in the child */
39 if (!pid) {
40 dup2(fd[1], 1);
41 close(fd[0]);
42 close(fd[1]);
43 return;
46 /* The original process turns into the PAGER */
47 dup2(fd[0], 0);
48 close(fd[0]);
49 close(fd[1]);
51 setenv("LESS", "-RS", 0);
52 run_pager(pager);
53 die("unable to execute pager '%s'", pager);
54 exit(255);