summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/libio_.h
blob: e14f63c2876ec5e0cb30f4f3c4dbe3f26bb69282 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *  Libio Internal Information
 *
 *  COPYRIGHT (c) 1989-1998.
 *  On-Line Applications Research Corporation (OAR).
 *  Copyright assigned to U.S. Government, 1994.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#ifndef __LIBIO__h
#define __LIBIO__h

#ifdef __cplusplus
extern "C" {
#endif

#include <rtems.h>
#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
#include <rtems/libio.h>

#include <stdio.h>                      /* O_RDONLY, et.al. */
#include <fcntl.h>                      /* O_RDONLY, et.al. */
#include <assert.h>
#include <stdarg.h>
#include <errno.h>

#if ! defined(O_NDELAY)
# if defined(solaris2)
#  define O_NDELAY O_NONBLOCK
# elif defined(RTEMS_NEWLIB)
#  define O_NDELAY _FNBIO
# endif
#endif

#if !defined(ENOTSUP)
#define ENOTSUP EOPNOTSUPP
#endif

#include <errno.h>
#include <string.h>                     /* strcmp */
#include <unistd.h>
#include <stdlib.h>                     /* calloc() */

#include "libio.h"                      /* libio.h not pulled in by rtems */


/*
 *  Semaphore to protect the io table
 */

#define RTEMS_LIBIO_SEM         rtems_build_name('L', 'B', 'I', 'O')
#define RTEMS_LIBIO_IOP_SEM(n)  rtems_build_name('L', 'B', 'I', n)

extern rtems_id        rtems_libio_semaphore;

/*
 *  File descriptor Table Information
 */

extern unsigned32      rtems_libio_number_iops;
extern rtems_libio_t  *rtems_libio_iops;
extern rtems_libio_t  *rtems_libio_last_iop;
extern rtems_libio_t *rtems_libio_iop_freelist;

/*
 *  Default mode for all files.
 */

extern mode_t    rtems_filesystem_umask;

/*
 *  set_errno_and_return_minus_one
 *
 *  Macro to ease common way to return an error.
 */

#ifndef set_errno_and_return_minus_one
#define set_errno_and_return_minus_one( _error ) \
  do { errno = (_error); return -1; } while(0)
#endif

/*
 *  rtems_libio_iop
 *
 *  Macro to return the file descriptor pointer.
 */

#define rtems_libio_iop(_fd) \
  ((((unsigned32)(_fd)) < rtems_libio_number_iops) ? \
         &rtems_libio_iops[_fd] : 0)

/*  
 *  rtems_libio_check_is_open
 *  
 *  Macro to check if a file descriptor is actually open.
 */

#define rtems_libio_check_is_open(_iop) \
  do {                                               \
      if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \
          errno = EBADF;                             \
          return -1;                                 \
      }                                              \
  } while (0)

/*
 *  rtems_libio_check_fd
 *
 *  Macro to check if a file descriptor number is valid.
 */

#define rtems_libio_check_fd(_fd) \
  do {                                                     \
      if ((unsigned32) (_fd) >= rtems_libio_number_iops) { \
          errno = EBADF;                                   \
          return -1;                                       \
      }                                                    \
  } while (0)

/*
 *  rtems_libio_check_buffer
 *
 *  Macro to check if a buffer pointer is valid.
 */

#define rtems_libio_check_buffer(_buffer) \
  do {                                    \
      if ((_buffer) == 0) {               \
          errno = EINVAL;                 \
          return -1;                      \
      }                                   \
  } while (0)

/*
 *  rtems_libio_check_count
 *
 *  Macro to check if a count or length is valid.
 */

#define rtems_libio_check_count(_count) \
  do {                                  \
      if ((_count) == 0) {              \
          return 0;                     \
      }                                 \
  } while (0)

/*
 *  rtems_libio_check_permissions
 *
 *  Macro to check if a file descriptor is open for this operation.
 */

#define rtems_libio_check_permissions(_iop, _flag)    \
  do {                                                \
      if (((_iop)->flags & (_flag)) == 0) {           \
            set_errno_and_return_minus_one( EINVAL ); \
            return -1;                                \
      }                                               \
  } while (0)

/*
 *  rtems_filesystem_is_separator
 *
 *  Macro to determine if a character is a path name separator.
 *
 *  NOTE:  This macro handles MS-DOS and UNIX style names.
 */

#define rtems_filesystem_is_separator( _ch ) \
   ( ((_ch) == '/') || ((_ch) == '\\') || ((_ch) == '\0'))

/*
 *  rtems_filesystem_get_start_loc
 *
 *  Macro to determine if path is absolute or relative.
 */

#define rtems_filesystem_get_start_loc( _path, _index, _loc )  \
  do {                                                         \
    if ( rtems_filesystem_is_separator( (_path)[ 0 ] ) ) {     \
      *(_loc) = rtems_filesystem_root;                         \
      *(_index) = 1;                                           \
    } else {                                                   \
      *(_loc) = rtems_filesystem_current;                      \
      *(_index) = 0;                                           \
    }                                                          \
  } while (0)

#define rtems_filesystem_get_sym_start_loc( _path, _index, _loc )  \
  do {                                                         \
    if ( rtems_filesystem_is_separator( (_path)[ 0 ] ) ) {     \
      *(_loc) = rtems_filesystem_root;                         \
      *(_index) = 1;                                           \
    } else {                                                   \
      *(_index) = 0;                                           \
    }                                                          \
  } while (0)


/*
 *  External structures
 */

extern rtems_filesystem_location_info_t rtems_filesystem_current;
extern rtems_filesystem_location_info_t rtems_filesystem_root;
extern nlink_t                          rtems_filesystem_link_counts;


/*
 *  File Descriptor Routine Prototypes
 */

rtems_libio_t *rtems_libio_allocate(void);

unsigned32 rtems_libio_fcntl_flags(
  unsigned32 fcntl_flags
);

unsigned32 rtems_libio_to_fcntl_flags(
  unsigned32 flags
);

void rtems_libio_free(
  rtems_libio_t *iop
);

int rtems_libio_is_open_files_in_fs(
  rtems_filesystem_mount_table_entry_t *mt_entry
);

int rtems_libio_is_file_open(
  void  *node_access
);

/*
 *  File System Routine Prototypes
 */

int rtems_filesystem_evaluate_path(
  const char                        *pathname,
  int                                flags,
  rtems_filesystem_location_info_t  *pathloc,
  int                                follow_link
);

void rtems_filesystem_initialize();

int init_fs_mount_table();

#ifdef __cplusplus
}
#endif

#endif
/* end of include file */