]> Sergey Matveev's repositories - nnn.git/commitdiff
Use non-blocking pipes
authorArun Prakash Jana <engineerarun@gmail.com>
Sun, 2 Jul 2017 15:02:38 +0000 (20:32 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sun, 2 Jul 2017 15:02:38 +0000 (20:32 +0530)
nnn.c

diff --git a/nnn.c b/nnn.c
index 93feadde637ac1e9cc967fef03320b1bba68bb82..b6df7744c8f0774da82b7554953df0d5272445c9 100644 (file)
--- a/nnn.c
+++ b/nnn.c
@@ -1279,25 +1279,38 @@ get_output(char *buf, size_t bytes, char *file,
        pid_t pid;
        int pipefd[2];
        FILE *pf;
-       int status;
+       int tmp, flags;
        char *ret = NULL;
 
        if (pipe(pipefd) == -1)
                printerr(1, "pipe(2)");
 
+       for (tmp = 0; tmp < 2; ++tmp) {
+               /* Get previous flags */
+               flags = fcntl(pipefd[tmp], F_GETFL, 0);
+
+               /* Set bit for non-blocking flag */
+               flags |= O_NONBLOCK;
+
+               /* Change flags on fd */
+               fcntl(pipefd[tmp], F_SETFL, flags);
+       }
+
        pid = fork();
        if (pid == 0) {
                /* In child */
                close(pipefd[0]);
                dup2(pipefd[1], STDOUT_FILENO);
                dup2(pipefd[1], STDERR_FILENO);
+               close(pipefd[1]);
                execlp(file, file, arg1, arg2, NULL);
                _exit(1);
        }
 
        /* In parent */
-       waitpid(pid, &status, 0);
+       waitpid(pid, &tmp, 0);
        close(pipefd[1]);
+
        if (!pager) {
                pf = fdopen(pipefd[0], "r");
                if (pf) {
@@ -1312,13 +1325,14 @@ get_output(char *buf, size_t bytes, char *file,
        if (pid == 0) {
                /* Show in pager in child */
                dup2(pipefd[0], STDIN_FILENO);
-               execlp("less", "less", NULL);
                close(pipefd[0]);
+               execlp("less", "less", NULL);
                _exit(1);
        }
 
        /* In parent */
-       waitpid(pid, &status, 0);
+       waitpid(pid, &tmp, 0);
+       close(pipefd[0]);
 
        return NULL;
 }