-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydup2.c
77 lines (67 loc) · 1.78 KB
/
mydup2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#define NMAX 1024
int mydup2(const int fd, const int fd2){
int tmpfd[NMAX], i;
/*check fd2 is an illegal value*/
if((fd2 < 3)||(fd2 > sysconf(_SC_OPEN_MAX))){
return -1;
}
/*check fd is opened*/
if((tmpfd[0] = dup(fd)) == -1){
return -1;
}
/* special case which fd equals fd2 */
if(fd == fd2){
return fd2;
}
/*case which fd > fd2*/
else{
if(tmpfd[0] == fd2){
return fd2;
}
else if(tmpfd[0] < fd2){
i = 1;
while((tmpfd[i++] = dup(fd)) != -1){
if(tmpfd[i-1] >= fd2){
if(tmpfd[i-1] == fd2){
/*close the opened fds*/
for(i-=2; i>=0; i--){
close(tmpfd[i]);
}
break;
}
else{
close(fd2);
tmpfd[i] = dup(fd);
assert(fd2 == tmpfd[i]);
/*close the opened fds*/
close(tmpfd[i-1]);
for(i-=2; i>=0; i--){
close(tmpfd[i]);
}
break;
}
}
}
return fd2;
}
else{
close(fd2);
tmpfd[1] = dup(fd);
assert(fd2 == tmpfd[1]);
/*close the opened fds*/
close(tmpfd[0]);
return fd2;
}
}
}
int main(){
int fd;
fd = dup2(1, 9);
printf("The result by dup2 is %d\n", fd);
fd = mydup2(2, 5);
printf("The result by mydup2 is %d\n", fd);
return 0;
}