Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7.  
  8. # define    STDOUT  (1)
  9. # define    TRUE    (1)
  10. # define    FALSE   (0)
  11.  
  12. typedef struct
  13. {
  14.   int   fd_1;
  15.   int   fd_2;
  16. }   t_fd;
  17.  
  18. static int  _redirect(t_fd *s_fd)
  19. {
  20.   int   save;
  21.  
  22.   save = dup(1);
  23.   close(1);      
  24.   dup(s_fd->fd_1);
  25.   close(s_fd->fd_1);
  26.   printf("Hello, world!\n"); // Ceci doit etre dans le fichier test et pas s'afficher a l'écran
  27.   dup2(save, 1);
  28. }
  29.  
  30. static int  _run(t_fd *s_fd)
  31. {
  32.   _redirect(s_fd);
  33.   return (TRUE);
  34. }
  35.  
  36. int main(int ac, char **av)
  37. {
  38.   t_fd  s_fd;
  39.  
  40.   if ((s_fd.fd_1 = open("test",O_WRONLY | O_CREAT, S_IREAD | S_IWRITE )) < 0)
  41.     exit(1);
  42.   s_fd.fd_2 = STDOUT;
  43.   if (!(_run(&s_fd)))
  44.     return (FALSE);  
  45.   printf("AAAAAA\n"); // Ceci doit s'afficher a l'écran mais n'est pas dans le fichier "test"
  46.   return (TRUE);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement