#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)

int main(void)
{
 pid_t pid;
 if((pid = fork()) == 0)
 {
  umask( S_IWGRP | S_IWOTH );
  int fd = open("testls",O_RDWR | O_CREAT,RWRWRW);
  dup2(fd,1);
  close(fd);
  execl("/bin/ls","ls",(char *)0);
 }
 else
 {
  waitpid(pid);
  return 0;
 }
}
转载请注明来源:Leoncom-《利用dup2实现exec函数的重定向》
Trackback

only 1 comment untill now

  1. I see no mention in the Barnes article that leads the writer to conclude that he is likely to be a “bust.” In fact, just the opposite emerges, that the writer thinks Barnes has a decent chance to perform in some All-Star games.

Add your comment now