1 | /** |
---|
2 | * @file rtems/libio_.h |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Libio Internal Information |
---|
7 | * |
---|
8 | * COPYRIGHT (c) 1989-1999. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_RTEMS_LIBIO__H |
---|
19 | #define _RTEMS_RTEMS_LIBIO__H |
---|
20 | |
---|
21 | #include <rtems.h> |
---|
22 | #include <rtems/libio.h> /* include before standard IO */ |
---|
23 | |
---|
24 | #include <sys/types.h> |
---|
25 | |
---|
26 | #include <errno.h> |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif |
---|
31 | |
---|
32 | /* |
---|
33 | * Semaphore to protect the io table |
---|
34 | */ |
---|
35 | |
---|
36 | #define RTEMS_LIBIO_SEM rtems_build_name('L', 'B', 'I', 'O') |
---|
37 | #define RTEMS_LIBIO_IOP_SEM(n) rtems_build_name('L', 'B', 'I', n) |
---|
38 | |
---|
39 | extern rtems_id rtems_libio_semaphore; |
---|
40 | extern const rtems_filesystem_file_handlers_r rtems_filesystem_null_handlers; |
---|
41 | |
---|
42 | /* |
---|
43 | * File descriptor Table Information |
---|
44 | */ |
---|
45 | |
---|
46 | extern uint32_t rtems_libio_number_iops; |
---|
47 | extern rtems_libio_t *rtems_libio_iops; |
---|
48 | extern rtems_libio_t *rtems_libio_last_iop; |
---|
49 | extern rtems_libio_t *rtems_libio_iop_freelist; |
---|
50 | |
---|
51 | /* |
---|
52 | * rtems_libio_iop |
---|
53 | * |
---|
54 | * Macro to return the file descriptor pointer. |
---|
55 | */ |
---|
56 | |
---|
57 | #define rtems_libio_iop(_fd) \ |
---|
58 | ((((uint32_t)(_fd)) < rtems_libio_number_iops) ? \ |
---|
59 | &rtems_libio_iops[_fd] : 0) |
---|
60 | |
---|
61 | /* |
---|
62 | * rtems_libio_iop_to_descriptor |
---|
63 | * |
---|
64 | * Macro to convert an internal file descriptor pointer (iop) into |
---|
65 | * the integer file descriptor used by the "section 2" system calls. |
---|
66 | */ |
---|
67 | |
---|
68 | #define rtems_libio_iop_to_descriptor(_iop) \ |
---|
69 | ((!(_iop)) ? -1 : (_iop - rtems_libio_iops)) |
---|
70 | |
---|
71 | /* |
---|
72 | * rtems_libio_check_is_open |
---|
73 | * |
---|
74 | * Macro to check if a file descriptor is actually open. |
---|
75 | */ |
---|
76 | |
---|
77 | #define rtems_libio_check_is_open(_iop) \ |
---|
78 | do { \ |
---|
79 | if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \ |
---|
80 | errno = EBADF; \ |
---|
81 | return -1; \ |
---|
82 | } \ |
---|
83 | } while (0) |
---|
84 | |
---|
85 | /* |
---|
86 | * rtems_libio_check_fd |
---|
87 | * |
---|
88 | * Macro to check if a file descriptor number is valid. |
---|
89 | */ |
---|
90 | |
---|
91 | #define rtems_libio_check_fd(_fd) \ |
---|
92 | do { \ |
---|
93 | if ((uint32_t) (_fd) >= rtems_libio_number_iops) { \ |
---|
94 | errno = EBADF; \ |
---|
95 | return -1; \ |
---|
96 | } \ |
---|
97 | } while (0) |
---|
98 | |
---|
99 | /* |
---|
100 | * rtems_libio_check_buffer |
---|
101 | * |
---|
102 | * Macro to check if a buffer pointer is valid. |
---|
103 | */ |
---|
104 | |
---|
105 | #define rtems_libio_check_buffer(_buffer) \ |
---|
106 | do { \ |
---|
107 | if ((_buffer) == 0) { \ |
---|
108 | errno = EINVAL; \ |
---|
109 | return -1; \ |
---|
110 | } \ |
---|
111 | } while (0) |
---|
112 | |
---|
113 | /* |
---|
114 | * rtems_libio_check_count |
---|
115 | * |
---|
116 | * Macro to check if a count or length is valid. |
---|
117 | */ |
---|
118 | |
---|
119 | #define rtems_libio_check_count(_count) \ |
---|
120 | do { \ |
---|
121 | if ((_count) == 0) { \ |
---|
122 | return 0; \ |
---|
123 | } \ |
---|
124 | } while (0) |
---|
125 | |
---|
126 | /* |
---|
127 | * rtems_libio_check_permissions |
---|
128 | * |
---|
129 | * Macro to check if a file descriptor is open for this operation. |
---|
130 | */ |
---|
131 | |
---|
132 | #define rtems_libio_check_permissions(_iop, _flag) \ |
---|
133 | do { \ |
---|
134 | if (((_iop)->flags & (_flag)) == 0) { \ |
---|
135 | rtems_set_errno_and_return_minus_one( EINVAL ); \ |
---|
136 | return -1; \ |
---|
137 | } \ |
---|
138 | } while (0) |
---|
139 | |
---|
140 | /* |
---|
141 | * rtems_filesystem_freenode |
---|
142 | * |
---|
143 | * Macro to free a node. |
---|
144 | */ |
---|
145 | |
---|
146 | #define rtems_filesystem_freenode( _node ) \ |
---|
147 | do { \ |
---|
148 | if ( (_node)->ops )\ |
---|
149 | if ( (_node)->ops->freenod_h ) \ |
---|
150 | (*(_node)->ops->freenod_h)( (_node) ); \ |
---|
151 | } while (0) |
---|
152 | |
---|
153 | /* |
---|
154 | * rtems_filesystem_is_separator |
---|
155 | * |
---|
156 | * Macro to determine if a character is a path name separator. |
---|
157 | * |
---|
158 | * NOTE: This macro handles MS-DOS and UNIX style names. |
---|
159 | */ |
---|
160 | |
---|
161 | #define rtems_filesystem_is_separator( _ch ) \ |
---|
162 | ( ((_ch) == '/') || ((_ch) == '\\') || ((_ch) == '\0')) |
---|
163 | |
---|
164 | /* |
---|
165 | * rtems_filesystem_get_start_loc |
---|
166 | * |
---|
167 | * Macro to determine if path is absolute or relative. |
---|
168 | */ |
---|
169 | |
---|
170 | #define rtems_filesystem_get_start_loc( _path, _index, _loc ) \ |
---|
171 | do { \ |
---|
172 | if ( rtems_filesystem_is_separator( (_path)[ 0 ] ) ) { \ |
---|
173 | *(_loc) = rtems_filesystem_root; \ |
---|
174 | *(_index) = 1; \ |
---|
175 | } else { \ |
---|
176 | *(_loc) = rtems_filesystem_current; \ |
---|
177 | *(_index) = 0; \ |
---|
178 | } \ |
---|
179 | } while (0) |
---|
180 | |
---|
181 | #define rtems_filesystem_get_sym_start_loc( _path, _index, _loc ) \ |
---|
182 | do { \ |
---|
183 | if ( rtems_filesystem_is_separator( (_path)[ 0 ] ) ) { \ |
---|
184 | *(_loc) = rtems_filesystem_root; \ |
---|
185 | *(_index) = 1; \ |
---|
186 | } else { \ |
---|
187 | *(_index) = 0; \ |
---|
188 | } \ |
---|
189 | } while (0) |
---|
190 | |
---|
191 | |
---|
192 | /* |
---|
193 | * External structures |
---|
194 | */ |
---|
195 | #include <rtems/userenv.h> |
---|
196 | |
---|
197 | extern rtems_user_env_t * rtems_current_user_env; |
---|
198 | extern rtems_user_env_t rtems_global_user_env; |
---|
199 | |
---|
200 | /* |
---|
201 | * Instantiate a private copy of the per user information for the calling task. |
---|
202 | */ |
---|
203 | |
---|
204 | rtems_status_code rtems_libio_set_private_env(void); |
---|
205 | rtems_status_code rtems_libio_share_private_env(rtems_id task_id) ; |
---|
206 | |
---|
207 | static inline void rtems_libio_lock( void ) |
---|
208 | { |
---|
209 | rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT ); |
---|
210 | } |
---|
211 | |
---|
212 | static inline void rtems_libio_unlock( void ) |
---|
213 | { |
---|
214 | rtems_semaphore_release( rtems_libio_semaphore ); |
---|
215 | } |
---|
216 | |
---|
217 | /* |
---|
218 | * File Descriptor Routine Prototypes |
---|
219 | */ |
---|
220 | |
---|
221 | rtems_libio_t *rtems_libio_allocate(void); |
---|
222 | |
---|
223 | uint32_t rtems_libio_fcntl_flags( |
---|
224 | uint32_t fcntl_flags |
---|
225 | ); |
---|
226 | |
---|
227 | uint32_t rtems_libio_to_fcntl_flags( |
---|
228 | uint32_t flags |
---|
229 | ); |
---|
230 | |
---|
231 | void rtems_libio_free( |
---|
232 | rtems_libio_t *iop |
---|
233 | ); |
---|
234 | |
---|
235 | int rtems_libio_is_open_files_in_fs( |
---|
236 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
237 | ); |
---|
238 | |
---|
239 | int rtems_libio_is_file_open( |
---|
240 | void *node_access |
---|
241 | ); |
---|
242 | |
---|
243 | /* |
---|
244 | * File System Routine Prototypes |
---|
245 | */ |
---|
246 | |
---|
247 | int rtems_filesystem_evaluate_relative_path( |
---|
248 | const char *pathname, |
---|
249 | size_t pathnamelen, |
---|
250 | int flags, |
---|
251 | rtems_filesystem_location_info_t *pathloc, |
---|
252 | int follow_link |
---|
253 | ); |
---|
254 | |
---|
255 | int rtems_filesystem_evaluate_path( |
---|
256 | const char *pathname, |
---|
257 | size_t pathnamelen, |
---|
258 | int flags, |
---|
259 | rtems_filesystem_location_info_t *pathloc, |
---|
260 | int follow_link |
---|
261 | ); |
---|
262 | |
---|
263 | int rtems_filesystem_dirname( |
---|
264 | const char *pathname |
---|
265 | ); |
---|
266 | |
---|
267 | int rtems_filesystem_prefix_separators( |
---|
268 | const char *pathname, |
---|
269 | int pathnamelen |
---|
270 | ); |
---|
271 | |
---|
272 | void rtems_filesystem_initialize(void); |
---|
273 | |
---|
274 | #ifdef __cplusplus |
---|
275 | } |
---|
276 | #endif |
---|
277 | |
---|
278 | #endif |
---|
279 | /* end of include file */ |
---|