1 | /* |
---|
2 | * fcntl() - POSIX 1003.1b 6.5.2 - File Control |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1998. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * Copyright assigned to U.S. Government, 1994. |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.OARcorp.com/rtems/license.html. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <unistd.h> |
---|
16 | #include <fcntl.h> |
---|
17 | #include <errno.h> |
---|
18 | |
---|
19 | #include <rtems.h> |
---|
20 | #include "libio_.h" |
---|
21 | |
---|
22 | int fcntl( |
---|
23 | int fd, |
---|
24 | int cmd, |
---|
25 | ... |
---|
26 | ) |
---|
27 | { |
---|
28 | va_list ap; |
---|
29 | rtems_libio_t *iop; |
---|
30 | rtems_libio_t *diop; |
---|
31 | int fd2; |
---|
32 | int flags; |
---|
33 | int ret = 0; |
---|
34 | |
---|
35 | va_start( ap, cmd ); |
---|
36 | |
---|
37 | rtems_libio_check_fd( fd ); |
---|
38 | iop = rtems_libio_iop( fd ); |
---|
39 | rtems_libio_check_is_open(iop); |
---|
40 | |
---|
41 | /* |
---|
42 | * Now process the fcntl(). |
---|
43 | */ |
---|
44 | |
---|
45 | /* |
---|
46 | * This switch should contain all the cases from POSIX. |
---|
47 | */ |
---|
48 | |
---|
49 | switch ( cmd ) { |
---|
50 | case F_DUPFD: /* dup */ |
---|
51 | fd2 = va_arg( ap, int ); |
---|
52 | if ( fd2 ) |
---|
53 | diop = rtems_libio_iop( fd2 ); |
---|
54 | else { |
---|
55 | /* allocate a file control block */ |
---|
56 | diop = rtems_libio_allocate(); |
---|
57 | if ( diop == 0 ) { |
---|
58 | ret = -1; |
---|
59 | break; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | diop->handlers = iop->handlers; |
---|
64 | diop->file_info = iop->file_info; |
---|
65 | diop->flags = iop->flags; |
---|
66 | diop->pathinfo = iop->pathinfo; |
---|
67 | |
---|
68 | break; |
---|
69 | |
---|
70 | case F_GETFD: /* get f_flags */ |
---|
71 | ret = ((iop->flags & LIBIO_FLAGS_CLOSE_ON_EXEC) != 0); |
---|
72 | break; |
---|
73 | |
---|
74 | case F_SETFD: /* set f_flags */ |
---|
75 | /* |
---|
76 | * Interpret the third argument as the "close on exec()" flag. |
---|
77 | * If this argument is 1, then the file descriptor is to be closed |
---|
78 | * if a new process is exec()'ed. Since RTEMS does not support |
---|
79 | * processes, then we can ignore this one except to make |
---|
80 | * F_GETFD work. |
---|
81 | */ |
---|
82 | |
---|
83 | if ( va_arg( ap, int ) ) |
---|
84 | iop->flags |= LIBIO_FLAGS_CLOSE_ON_EXEC; |
---|
85 | else |
---|
86 | iop->flags &= ~LIBIO_FLAGS_CLOSE_ON_EXEC; |
---|
87 | break; |
---|
88 | |
---|
89 | case F_GETFL: /* more flags (cloexec) */ |
---|
90 | ret = rtems_libio_to_fcntl_flags( iop->flags ); |
---|
91 | |
---|
92 | case F_SETFL: |
---|
93 | flags = rtems_libio_fcntl_flags( va_arg( ap, int ) ); |
---|
94 | |
---|
95 | /* |
---|
96 | * XXX If we are turning on append, should we seek to the end? |
---|
97 | */ |
---|
98 | |
---|
99 | iop->flags = (iop->flags & ~(O_APPEND | O_NONBLOCK)) | |
---|
100 | (flags & (O_APPEND | O_NONBLOCK)); |
---|
101 | break; |
---|
102 | |
---|
103 | case F_GETLK: |
---|
104 | errno = ENOTSUP; |
---|
105 | ret = -1; |
---|
106 | break; |
---|
107 | |
---|
108 | case F_SETLK: |
---|
109 | errno = ENOTSUP; |
---|
110 | ret = -1; |
---|
111 | break; |
---|
112 | |
---|
113 | case F_SETLKW: |
---|
114 | errno = ENOTSUP; |
---|
115 | ret = -1; |
---|
116 | break; |
---|
117 | |
---|
118 | case F_SETOWN: /* for sockets. */ |
---|
119 | errno = ENOTSUP; |
---|
120 | ret = -1; |
---|
121 | break; |
---|
122 | |
---|
123 | case F_GETOWN: /* for sockets. */ |
---|
124 | errno = ENOTSUP; |
---|
125 | ret = -1; |
---|
126 | break; |
---|
127 | |
---|
128 | default: |
---|
129 | errno = EINVAL; |
---|
130 | ret = -1; |
---|
131 | break; |
---|
132 | } |
---|
133 | if ((ret >= 0) && iop->handlers->fcntl) { |
---|
134 | int err = (*iop->handlers->fcntl)( cmd, iop ); |
---|
135 | if (err) { |
---|
136 | errno = err; |
---|
137 | ret = -1; |
---|
138 | } |
---|
139 | } |
---|
140 | return ret; |
---|
141 | } |
---|