This source file includes following definitions.
- dupfd
- sys_dup2
- sys_dup
- sys_fcntl
1
2
3
4
5
6
7 #include <errno.h>
8 #include <fcntl.h>
9
10 #include <sys/stat.h>
11
12 #include <asm/segment.h>
13
14 #include <linux/string.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17
18 extern int sys_close(int fd);
19
20 static int dupfd(unsigned int fd, unsigned int arg)
21 {
22 if (fd >= NR_OPEN || !current->filp[fd])
23 return -EBADF;
24 if (arg >= NR_OPEN)
25 return -EINVAL;
26 while (arg < NR_OPEN)
27 if (current->filp[arg])
28 arg++;
29 else
30 break;
31 if (arg >= NR_OPEN)
32 return -EMFILE;
33 current->close_on_exec &= ~(1<<arg);
34 (current->filp[arg] = current->filp[fd])->f_count++;
35 return arg;
36 }
37
38 int sys_dup2(unsigned int oldfd, unsigned int newfd)
39 {
40 if (newfd == oldfd)
41 return newfd;
42 sys_close(newfd);
43 return dupfd(oldfd,newfd);
44 }
45
46 int sys_dup(unsigned int fildes)
47 {
48 return dupfd(fildes,0);
49 }
50
51 int sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
52 {
53 struct file * filp;
54
55 if (fd >= NR_OPEN || !(filp = current->filp[fd]))
56 return -EBADF;
57 switch (cmd) {
58 case F_DUPFD:
59 return dupfd(fd,arg);
60 case F_GETFD:
61 return (current->close_on_exec>>fd)&1;
62 case F_SETFD:
63 if (arg&1)
64 current->close_on_exec |= (1<<fd);
65 else
66 current->close_on_exec &= ~(1<<fd);
67 return 0;
68 case F_GETFL:
69 return filp->f_flags;
70 case F_SETFL:
71 filp->f_flags &= ~(O_APPEND | O_NONBLOCK);
72 filp->f_flags |= arg & (O_APPEND | O_NONBLOCK);
73 return 0;
74 case F_GETLK: case F_SETLK: case F_SETLKW:
75 return -ENOSYS;
76 default:
77 return -EINVAL;
78 }
79 }