- 论坛徽章:
- 0
|
3.2
Write your own function called dup2 that performs the same service as
the dup2 function we described in section 3.12,without calling the
fcntl function. Be sure to handle errors correctly.
- #include <stdio.h>
- #include <errno.h>
- #include <unistd.h>
- int
- my_dup2(int oldd,int newd)
- {
- int fd;
- if(oldd==newd){
- return (-1==(fd=dup(oldd)))?-1:close(fd),oldd;
- }else{
- if( (-1==lseek(newd,0,SEEK_CUR)) && (errno==EBADF) ){
- close(newd);
- }
- if(newd != (fd=dup(oldd))){
- if(fd == -1){
- return -1;
- }else{
- int fd1;
- if(newd != (fd1=my_dup2(oldd,newd))){
- close(fd1);
- }
- }
- }
- }
- return fd;
- }
- int
- main(void)
- {
- int fd;
- if(-1==(fd=my_dup2(1,5))){
- perror("5555555");
- exit(-1);
- }
- write(5,"hahahahahahahaha",17);
- exit(0);
- }
复制代码
请问这样写的my_dup2有问题吗? 
[ 本帖最后由 雨丝风片 于 2006-5-21 09:03 编辑 ] |
|