lib/pager: don't use pager if command not available

for example:
 # PAGER=foo dmesg -H
 sh: foo: command not found

the same problem is we have with fdisk 'l' command:

 # PAGER=foo fdisk /dev/sda
 Welcome to fdisk (util-linux 2.30-rc2-33-41b71).
 ...
 Command (m for help): l
 sh: foo: command not found

It seems better to don't use pager at all if not available.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2017-06-01 14:20:20 +02:00
parent 8ea1651c38
commit 535a4090b4
1 changed files with 36 additions and 0 deletions

View File

@ -170,6 +170,39 @@ static void wait_for_pager_signal(int signo)
raise(signo);
}
static int has_command(const char *cmd)
{
const char *path;
char *p, *s;
int rc = 0;
if (!cmd)
goto done;
if (*cmd == '/') {
rc = access(cmd, X_OK) == 0;
goto done;
}
path = getenv("PATH");
if (!path)
goto done;
p = strdup(path);
if (!p)
goto done;
for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
int fd = open(s, O_RDONLY|O_CLOEXEC);
rc = faccessat(fd, cmd, X_OK, 0) == 0;
close(fd);
if (rc)
break;
}
free(p);
done:
/*fprintf(stderr, "has PAGER %s rc=%d\n", cmd, rc);*/
return rc;
}
static void __setup_pager(void)
{
const char *pager = getenv("PAGER");
@ -183,6 +216,9 @@ static void __setup_pager(void)
else if (!*pager || !strcmp(pager, "cat"))
return;
if (!has_command(pager))
return;
/* spawn the pager */
pager_argv[2] = pager;
pager_process.argv = pager_argv;