]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/pager: don't use pager if command not available
authorKarel Zak <kzak@redhat.com>
Thu, 1 Jun 2017 12:20:20 +0000 (14:20 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 1 Jun 2017 12:20:20 +0000 (14:20 +0200)
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>
lib/pager.c

index 24284d60522e60f2bb7e4a13d05d6ef050ece09d..b90d6398ae31621c2f50c1179762884d6fdd6ca9 100644 (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;