伟大航路
pz0513
全部博文(33)
UNIX操作系统设计(1)
Linux程序设计((8)
The C Programmin(8)
Chapter5(5)
2009年(33)
zhanghei
Wins0n
stokeora
分类: LINUX
2009-07-07 09:52:58
#include <unistd.h>#include <stdio.h>#include <sys/types.h>int pzpopen(char *proc, char *mode) { int pipe_d[2]; pid_t pid; if(pipe(pipe_d) != 0) { fprintf(stderr, "pipe failed!\n"); return -1; } if(*mode == 'r' && *(mode + 1) == '\0'){ pid = fork(); switch(pid) { case -1: fprintf(stderr, "fork failed.\n"); return -1; case 0: dup2(pipe_d[1], 1); close(pipe_d[1]); close(pipe_d[0]); execl(proc, proc, NULL); fprintf(stderr, "execl failed.\n"); return -1; } close(pipe_d[1]); return pipe_d[0]; }else if(*mode == 'w' && *(mode + 1) == '\0') { pid = fork(); switch(pid) { case -1: fprintf(stderr, "fork failed.\n"); return -1; case 0: dup2(pipe_d[0], 0); close(pipe_d[0]); close(pipe_d[1]); execl(proc, proc, NULL); fprintf(stderr, "execl failed.\n"); return -1; } close(pipe_d[0]); return pipe_d[1]; } fprintf(stderr, "Unknown mode %s!\n", mode); return -1;}
上一篇:头文件和函数整理工具
下一篇:C13 - 管道 笔记
登录 注册