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