标签:
#include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<fcntl.h> int my_dup(int fd,int fd2) { if(fd==fd2) return fd2; int n= 0; int look= 0; int i_count= -1; int * fd_arr=NULL; fd_arr=(int *)malloc(1024*sizeof(int)); while((n=dup(fd))<1024 && ++i_count<1024){ look++; if(n<fd2) fd_arr[i_count]=n; else if (n==fd2) goto sucess; else close(fd2); } free(fd_arr); fd_arr=NULL; return -1; sucess: while(--i_count>0){ n=fd_arr[i_count]; close(n); } free(fd_arr); fd_arr=NULL; printf("look cycle:%d\n",look); return fd2; } void check_fd(fd) { int resu; int fd2; printf("please in put number >2 \n"); scanf("%d",&fd2); if(fd2<3){ printf("fuck you!"); return; } resu=my_dup(fd,fd2); if(resu<0) printf("eror:%d\n",resu); else printf("sucess!fd:%d\n",resu); } int main(int argc,char * argv[]) { int i=1; int fd; for(i;i<argc;i++){ fd=open(argv[i],O_RDONLY); printf("open fd:%d\n",fd); } if(fd<0) return -1; check_fd(fd); return 0; }
标签:
原文地址:http://my.oschina.net/u/1375613/blog/494808