-
Notifications
You must be signed in to change notification settings - Fork 46
/
dup.c
65 lines (52 loc) · 1.44 KB
/
dup.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
//
// Created by bruce on 27/04/20.
//
#include <fs/fs.h>
int sys_dup(struct proc* who, int oldfd){
int ret, open_slot;
struct filp* file1, *file2;
if(!is_fd_opened_and_valid(who, oldfd))
return -EBADF;
file2 = get_free_filp();
if(!file2)
return -ENFILE;
ret = get_fd(who, 0, &open_slot, file2);
if(ret)
return ret;
file1 = who->fp_filp[oldfd];
who->fp_filp[open_slot] = file1;
file1->filp_count += 1;
return open_slot;
}
int sys_dup2(struct proc* who, int oldfd, int newfd){
int ret, open_slot;
struct filp* file1, *file2;
if(!is_fd_opened_and_valid(who, oldfd))
return -EBADF;
if(newfd < 0 || newfd >= OPEN_MAX)
return -EBADF;
file2 = get_free_filp();
if (!file2)
return -ENFILE;
ret = get_fd(who, 0, &open_slot, file2);
if(ret)
return ret;
if(who->fp_filp[newfd]){
ret = sys_close(who, newfd);
if(ret)
return ret;
}
file1 = who->fp_filp[oldfd];
if(!file1)
return -EBADF;
who->fp_filp[newfd] = file1;
file1->filp_count += 1;
// kdebug("dup2: %d %d by %s %d, filp dev %s\n", oldfd, newfd, who->name, who->proc_nr, file1->filp_dev->init_name);
return newfd;
}
int do_dup(struct proc* who, struct message* msg){
return sys_dup(who, msg->m1_i1);
}
int do_dup2(struct proc* who, struct message* msg){
return sys_dup2(who, msg->m1_i1, msg->m1_i2);
}