浏览代码

Use non-blocking pipes

master
Arun Prakash Jana 8 年前
父节点
当前提交
acdc6dc0a3
找不到此签名对应的密钥 GPG 密钥 ID: A75979F35C080412
共有 1 个文件被更改,包括 18 次插入4 次删除
  1. +18
    -4
      nnn.c

+ 18
- 4
nnn.c 查看文件

@@ -1279,25 +1279,38 @@ get_output(char *buf, size_t bytes, char *file,
pid_t pid; pid_t pid;
int pipefd[2]; int pipefd[2];
FILE *pf; FILE *pf;
int status;
int tmp, flags;
char *ret = NULL; char *ret = NULL;


if (pipe(pipefd) == -1) if (pipe(pipefd) == -1)
printerr(1, "pipe(2)"); 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(); pid = fork();
if (pid == 0) { if (pid == 0) {
/* In child */ /* In child */
close(pipefd[0]); close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO); dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO); dup2(pipefd[1], STDERR_FILENO);
close(pipefd[1]);
execlp(file, file, arg1, arg2, NULL); execlp(file, file, arg1, arg2, NULL);
_exit(1); _exit(1);
} }


/* In parent */ /* In parent */
waitpid(pid, &status, 0);
waitpid(pid, &tmp, 0);
close(pipefd[1]); close(pipefd[1]);

if (!pager) { if (!pager) {
pf = fdopen(pipefd[0], "r"); pf = fdopen(pipefd[0], "r");
if (pf) { if (pf) {
@@ -1312,13 +1325,14 @@ get_output(char *buf, size_t bytes, char *file,
if (pid == 0) { if (pid == 0) {
/* Show in pager in child */ /* Show in pager in child */
dup2(pipefd[0], STDIN_FILENO); dup2(pipefd[0], STDIN_FILENO);
execlp("less", "less", NULL);
close(pipefd[0]); close(pipefd[0]);
execlp("less", "less", NULL);
_exit(1); _exit(1);
} }


/* In parent */ /* In parent */
waitpid(pid, &status, 0);
waitpid(pid, &tmp, 0);
close(pipefd[0]);


return NULL; return NULL;
} }


正在加载...
取消
保存