chechunli@chechunli-PC:tmp $ cat redirect.c
#include
#include
#include
#include
#include
#define MAX_SIZE 1024
#define OPENFILE "1"
int main(int argc, char *argv[])
{
int n;
int fd[2];
pid_t pid;
char line[MAX_SIZE];
int fd_test;
if (pipe(fd) < 0)
perror("pipe error");
if ((pid = fork()) < 0) {
perror("fork error");
} else if (pid > 0) {
close(fd[0]);
close(STDOUT_FILENO);
dup2(fd[1], STDOUT_FILENO);
system("ls -l");
system("ls");
} else {
close(fd[1]);
fd_test = open(OPENFILE, O_RDWR | O_CREAT, 0777);
while((n = read(fd[0], line, MAX_SIZE)) > 0)
if(write(fd_test, line, n) != n)
perror("write error");
}
close(fd_test);
return 0;
}
chechunli@chechunli-PC:tmp $ gcc redirect.c -Wall -o redirect
阅读(1162) | 评论(0) | 转发(1) |