分类: 系统运维
2012-02-28 14:47:55
思路: 能用dup就一直dup到所要求的那个文件描述符。 int test_dup(int fd1,int fd2){ /* dup2(fd1,fd2) */ int fd=0; int array_fd[100]; int i=0; printf(" dup2(%d,%d) \n", fd1, fd2); if((fd1 < 0) || (fd2 < 0) || fd2 > 100){ err_sys(" Please check file_id. "); exit(1); } fd = dup(fd1); if(fd < 0){ err_sys(" the file id has no corresponding file "); exit(1); } while(fd < fd2){ i++; if(i == 100){ err_sys(" open to much file "); exit(1); } array_fd[i]=fd; fd = dup(fd1); if (fd < 0) { err_sys(" can't open it "); } printf("fd = %d \n", fd); } if(fd > fd2){ close(fd2); fd = dup(fd1); } for(;i>=0;i--){ close(array_fd[i]); } return fd; } |