source: rtems/cpukit/libcsupport/src/libio.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief File Descriptor Routines
5 *  @ingroup LibIOInternal
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-1999.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <fcntl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27#include <rtems.h>
28#include <rtems/libio_.h>
29#include <rtems/assoc.h>
30
31/* define this to alias O_NDELAY to  O_NONBLOCK, i.e.,
32 * O_NDELAY is accepted on input but fcntl(F_GETFL) returns
33 * O_NONBLOCK. This is because rtems has no distinction
34 * between the two (but some systems have).
35 * Note that accepting this alias creates a problem:
36 * an application trying to clear the non-blocking flag
37 * using a
38 *
39 *    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NDELAY);
40 *
41 * does (silently) ignore the operation.
42 */
43#undef ACCEPT_O_NDELAY_ALIAS
44
45static const rtems_assoc_t access_modes_assoc[] = {
46  { "READ",       LIBIO_FLAGS_READ,  O_RDONLY },
47  { "WRITE",      LIBIO_FLAGS_WRITE, O_WRONLY },
48  { "READ/WRITE", LIBIO_FLAGS_READ_WRITE, O_RDWR },
49  { 0, 0, 0 },
50};
51
52static const rtems_assoc_t status_flags_assoc[] = {
53#ifdef ACCEPT_O_NDELAY_ALIAS
54  { "NO DELAY",  LIBIO_FLAGS_NO_DELAY,  O_NDELAY },
55#endif
56  { "NONBLOCK",  LIBIO_FLAGS_NO_DELAY,  O_NONBLOCK },
57  { "APPEND",    LIBIO_FLAGS_APPEND,    O_APPEND },
58  { "CREATE",    LIBIO_FLAGS_CREATE,    O_CREAT },
59  { 0, 0, 0 },
60};
61
62uint32_t rtems_libio_fcntl_flags( int fcntl_flags )
63{
64  uint32_t   flags = 0;
65  uint32_t   access_modes;
66
67  /*
68   * Access mode is a small integer
69   */
70
71  access_modes = (uint32_t) (fcntl_flags & O_ACCMODE);
72  fcntl_flags &= ~O_ACCMODE;
73  flags = rtems_assoc_local_by_remote( access_modes_assoc, access_modes );
74
75  /*
76   * Everything else is single bits
77   */
78
79  flags |= rtems_assoc_local_by_remote_bitfield(
80    status_flags_assoc,
81    (uint32_t) fcntl_flags
82  );
83
84  return flags;
85}
86
87int rtems_libio_to_fcntl_flags( uint32_t flags )
88{
89  int fcntl_flags = 0;
90
91  if ( (flags & LIBIO_FLAGS_READ_WRITE) == LIBIO_FLAGS_READ_WRITE ) {
92    fcntl_flags |= O_RDWR;
93  } else if ( (flags & LIBIO_FLAGS_READ) == LIBIO_FLAGS_READ) {
94    fcntl_flags |= O_RDONLY;
95  } else if ( (flags & LIBIO_FLAGS_WRITE) == LIBIO_FLAGS_WRITE) {
96    fcntl_flags |= O_WRONLY;
97  }
98
99  if ( (flags & LIBIO_FLAGS_NO_DELAY) == LIBIO_FLAGS_NO_DELAY ) {
100    fcntl_flags |= O_NONBLOCK;
101  }
102
103  if ( (flags & LIBIO_FLAGS_APPEND) == LIBIO_FLAGS_APPEND ) {
104    fcntl_flags |= O_APPEND;
105  }
106
107  if ( (flags & LIBIO_FLAGS_CREATE) == LIBIO_FLAGS_CREATE ) {
108    fcntl_flags |= O_CREAT;
109  }
110
111  return fcntl_flags;
112}
113
114rtems_libio_t *rtems_libio_allocate( void )
115{
116  rtems_libio_t *iop = NULL;
117
118  rtems_libio_lock();
119
120  if (rtems_libio_iop_freelist) {
121    iop = rtems_libio_iop_freelist;
122    rtems_libio_iop_freelist = iop->data1;
123    memset( iop, 0, sizeof(*iop) );
124    iop->flags = LIBIO_FLAGS_OPEN;
125  }
126
127  rtems_libio_unlock();
128
129  return iop;
130}
131
132void rtems_libio_free(
133  rtems_libio_t *iop
134)
135{
136  rtems_filesystem_location_free( &iop->pathinfo );
137
138  rtems_libio_lock();
139
140    iop->flags = 0;
141    iop->data1 = rtems_libio_iop_freelist;
142    rtems_libio_iop_freelist = iop;
143
144  rtems_libio_unlock();
145}
Note: See TracBrowser for help on using the repository browser.