All pastes #542244 Raw Edit

dup2

public c v1 · immutable
·published 2007-06-05 19:35 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <unistd.h>#include <sys/wait.h>int main(int argc, char **argv){  int file;  int count;  char * buf;  int pid;  int tube[2];  int status;  buf = malloc(1024);  if(argc != 2){    fprintf(stderr,"Bad arguments number\n Usage : %s [filename]\n",argv[0]);    exit(EXIT_FAILURE);  }      file = open(argv[1], O_RDONLY);  if(file <= 0){    fprintf(stderr,"File does not exists\n");    exit(EXIT_FAILURE);  }  /* create pipe */  pipe(tube);  switch(pid = fork()){  case -1 :    fprintf(stderr,"Error\n");  case 0 :    close(tube[1]);        while((count = read(tube[0], buf, 1024)) != 0){      write(STDOUT_FILENO, buf, count);    }/*     dup2(STDIN_FILENO,tube[0]); */    /*     execle("/usr/bin/mplayer","-"); */    close(tube[0]);      default :     /* Playing the file */    close(tube[0]);    while((count = read(file, buf, 1024)) != 0){      write(tube[1], buf, count);    }        close(tube[1]);    close(file);    waitpid(pid, &status, 0);    if(!WIFEXITED(status)){      printf("arfl\n");      exit(EXIT_FAILURE);          }    exit(EXIT_SUCCESS);  }}